1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetExprEditor.cpp
6 * Created on: Aug 28, 2014
10 #include <ModuleBase_WidgetExprEditor.h>
11 #include <ModuleBase_Tools.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_ResultParameter.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_AttributeDouble.h>
20 #include <Config_WidgetAPI.h>
22 #include <QVBoxLayout>
27 #include <QStringListModel>
32 #include <QFontMetrics>
38 ExpressionEditor::ExpressionEditor(QWidget* theParent)
39 : QPlainTextEdit(theParent), myCompletedAndSelected(false), myIsModified(false)
41 myCompleter = new QCompleter(this);
42 myCompleter->setWidget(this);
43 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
45 myCompleterModel = new QStringListModel(this);
46 myCompleter->setModel(myCompleterModel);
47 // Use sorted model to accelerate completion (QCompleter will use binary search)
48 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
49 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
51 connect(myCompleter, SIGNAL(activated(const QString&)),
52 this, SLOT(insertCompletion(const QString&)));
53 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
54 this, SLOT(performCompletion()));
56 connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
58 setTabChangesFocus(true);
61 ExpressionEditor::~ExpressionEditor()
66 void ExpressionEditor::setCompletionList(QStringList& theList)
69 theList.removeDuplicates();
70 myCompleterModel->setStringList(theList);
73 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
75 QTextCursor aCursor = textCursor();
76 int numberOfCharsToComplete = theCompletion.length() -
77 myCompleter->completionPrefix().length();
78 int insertionPosition = aCursor.position();
79 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
81 aCursor.setPosition(insertionPosition);
82 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
83 myCompletedAndSelected = true;
85 setTextCursor(aCursor);
88 void ExpressionEditor::performCompletion()
90 QTextCursor aCursor = textCursor();
91 aCursor.select(QTextCursor::WordUnderCursor);
92 const QString aPrefix = aCursor.selectedText();
93 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
94 performCompletion(aPrefix);
98 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
101 if (theCompletionPrefix != myCompleter->completionPrefix()) {
102 myCompleter->setCompletionPrefix(theCompletionPrefix);
103 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
105 if (myCompleter->completionCount() == 1) {
106 insertCompletion(myCompleter->currentCompletion(), true);
108 QRect aRect = cursorRect();
109 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
110 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
111 myCompleter->complete(aRect);
115 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
117 bool anIsModified = myIsModified;
119 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
121 myCompletedAndSelected = false;
122 if (myCompleter->popup()->isVisible()) {
123 switch (theEvent->key()) {
132 myCompleter->popup()->hide();
137 switch (theEvent->key()) {
140 emit keyReleased(theEvent);
141 // do not react to the Enter key, the property panel processes it
148 QPlainTextEdit::keyPressEvent(theEvent);
151 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
153 myCompletedAndSelected = false;
154 QTextCursor aCursor = textCursor();
155 switch (theEvent->key()) {
157 case Qt::Key_Return: aCursor.clearSelection(); break;
158 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
159 default: return false;
161 setTextCursor(aCursor);
166 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
168 myPlaceHolderText = thePlaceHolderText;
171 QString ExpressionEditor::placeHolderText() const
173 return myPlaceHolderText;
176 bool ExpressionEditor::isModified() const
181 void ExpressionEditor::clearModified()
183 myIsModified = false;
186 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
188 QPlainTextEdit::paintEvent( theEvent );
190 if( toPlainText().isEmpty() )
192 QPainter aPainter( viewport() );
193 QFontMetrics aFontMetrics = fontMetrics();
195 QPointF offset(contentOffset());
197 int m = (int)document()->documentMargin();
198 QRect lineRect( r.x() + m + offset.x(), offset.y(),
199 r.width() - 2*m, aFontMetrics.height() );
201 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
202 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
204 QColor aColor = palette().text().color();
205 aColor.setAlpha( 128 );
206 QPen anOldpen = aPainter.pen();
207 aPainter.setPen( aColor );
208 lineRect.adjust(minLB, 0, 0, 0);
209 QString elidedText = aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
210 aPainter.drawText( lineRect, va, elidedText );
211 aPainter.setPen( anOldpen );
215 bool ExpressionEditor::focusNextPrevChild(bool theIsNext)
217 //emit valueStored();
218 //emit focusNextPrev();
219 return QPlainTextEdit::focusNextPrevChild(theIsNext);
222 void ExpressionEditor::onTextChanged()
225 emit valueModified();
229 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
230 const Config_WidgetAPI* theData,
231 const std::string& theParentId,
232 const std::string& thePlaceHolder )
233 : ModuleBase_ModelWidget(theParent, theData, theParentId)
235 QVBoxLayout* aMainLay = new QVBoxLayout(this);
236 ModuleBase_Tools::adjustMargins(aMainLay);
238 myResultLabel = new QLabel(this);
239 myResultLabel->setWordWrap(true);
240 QFontMetrics fm(myResultLabel->font());
241 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
242 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
243 aMainLay->addWidget(myResultLabel);
244 myEditor = new ExpressionEditor(this);
245 myEditor->setMinimumHeight(20);
246 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
247 aMainLay->addWidget(myEditor);
248 this->setLayout(aMainLay);
250 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
251 //connect(myEditor, SIGNAL(valueStored()), this, SLOT(onTextChanged()));
252 //connect(myEditor, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
254 connect(myEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
255 /// The signal about key release on the control, that corresponds to the attribute
256 /// \param theEvent key release event
259 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
263 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
267 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
269 // A rare case when plugin was not loaded.
272 DataPtr aData = myFeature->data();
273 AttributeStringPtr aStringAttr = aData->string(attributeID());
275 QString aWidgetValue = myEditor->toPlainText();
276 aStringAttr->setValue(aWidgetValue.toStdString());
277 updateObject(myFeature);
279 // Try to get the value
281 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
282 if (anErrorMessage.empty()) {
283 ResultParameterPtr aParam =
284 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
286 AttributeDoublePtr aValueAttr =
287 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
288 if (aValueAttr.get()) {
289 double aValue = aValueAttr->value();
290 aStateMsg = "Result: " + QString::number(aValue);
294 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
296 myResultLabel->setText(aStateMsg);
300 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
302 // A rare case when plugin was not loaded.
305 DataPtr aData = myFeature->data();
306 AttributeStringPtr aStringAttr = aData->string(attributeID());
308 bool isBlocked = myEditor->blockSignals(true);
309 QTextCursor aCursor = myEditor->textCursor();
310 int pos = aCursor.position();
311 std::string aRestoredStr = aStringAttr->value();
312 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
313 aCursor.setPosition(pos);
314 myEditor->setTextCursor(aCursor);
315 myEditor->blockSignals(isBlocked);
320 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
322 QList<QWidget*> result;
327 bool ModuleBase_WidgetExprEditor::processEnter()
329 bool isModified = myEditor->isModified();
331 emit valuesChanged();
332 myEditor->clearModified();
333 myEditor->selectAll();
338 void ModuleBase_WidgetExprEditor::onTextChanged()
340 emit valuesChanged();