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)
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()));
57 ExpressionEditor::~ExpressionEditor()
62 void ExpressionEditor::setCompletionList(QStringList& theList)
65 theList.removeDuplicates();
66 myCompleterModel->setStringList(theList);
69 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
71 QTextCursor aCursor = textCursor();
72 int numberOfCharsToComplete = theCompletion.length() -
73 myCompleter->completionPrefix().length();
74 int insertionPosition = aCursor.position();
75 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
77 aCursor.setPosition(insertionPosition);
78 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
79 myCompletedAndSelected = true;
81 setTextCursor(aCursor);
84 void ExpressionEditor::performCompletion()
86 QTextCursor aCursor = textCursor();
87 aCursor.select(QTextCursor::WordUnderCursor);
88 const QString aPrefix = aCursor.selectedText();
89 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
90 performCompletion(aPrefix);
94 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
97 if (theCompletionPrefix != myCompleter->completionPrefix()) {
98 myCompleter->setCompletionPrefix(theCompletionPrefix);
99 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
101 if (myCompleter->completionCount() == 1) {
102 insertCompletion(myCompleter->currentCompletion(), true);
104 QRect aRect = cursorRect();
105 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
106 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
107 myCompleter->complete(aRect);
111 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
113 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
115 myCompletedAndSelected = false;
116 if (myCompleter->popup()->isVisible()) {
117 switch (theEvent->key()) {
126 myCompleter->popup()->hide();
130 QPlainTextEdit::keyPressEvent(theEvent);
133 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
135 myCompletedAndSelected = false;
136 QTextCursor aCursor = textCursor();
137 switch (theEvent->key()) {
139 case Qt::Key_Return: aCursor.clearSelection(); break;
140 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
141 default: return false;
143 setTextCursor(aCursor);
148 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
150 myPlaceHolderText = thePlaceHolderText;
153 QString ExpressionEditor::placeHolderText() const
155 return myPlaceHolderText;
158 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
160 QPlainTextEdit::paintEvent( theEvent );
162 if( toPlainText().isEmpty() )
164 QPainter aPainter( viewport() );
165 QFontMetrics aFontMetrics = fontMetrics();
167 QPointF offset(contentOffset());
169 int m = (int)document()->documentMargin();
170 QRect lineRect( r.x() + m + offset.x(), offset.y(),
171 r.width() - 2*m, aFontMetrics.height() );
173 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
174 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
176 QColor aColor = palette().text().color();
177 aColor.setAlpha( 128 );
178 QPen anOldpen = aPainter.pen();
179 aPainter.setPen( aColor );
180 lineRect.adjust(minLB, 0, 0, 0);
181 QString elidedText = aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
182 aPainter.drawText( lineRect, va, elidedText );
183 aPainter.setPen( anOldpen );
189 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
190 const Config_WidgetAPI* theData,
191 const std::string& theParentId,
192 const std::string& thePlaceHolder )
193 : ModuleBase_ModelWidget(theParent, theData, theParentId)
195 QVBoxLayout* aMainLay = new QVBoxLayout(this);
196 ModuleBase_Tools::adjustMargins(aMainLay);
198 myResultLabel = new QLabel(this);
199 myResultLabel->setWordWrap(true);
200 QFontMetrics fm(myResultLabel->font());
201 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
202 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
203 aMainLay->addWidget(myResultLabel);
204 myEditor = new ExpressionEditor(this);
205 myEditor->setMinimumHeight(20);
206 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
207 aMainLay->addWidget(myEditor);
208 this->setLayout(aMainLay);
210 connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
213 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
217 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
219 // A rare case when plugin was not loaded.
222 DataPtr aData = myFeature->data();
223 AttributeStringPtr aStringAttr = aData->string(attributeID());
224 QString aWidgetValue = myEditor->toPlainText();
225 aStringAttr->setValue(aWidgetValue.toStdString());
226 updateObject(myFeature);
228 // Try to get the value
230 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
231 if (anErrorMessage.empty()) {
232 ResultParameterPtr aParam =
233 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
235 AttributeDoublePtr aValueAttr =
236 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
237 if (aValueAttr.get()) {
238 double aValue = aValueAttr->value();
239 aStateMsg = "Result: " + QString::number(aValue);
243 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
245 myResultLabel->setText(aStateMsg);
249 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
251 // A rare case when plugin was not loaded.
254 DataPtr aData = myFeature->data();
255 AttributeStringPtr aStringAttr = aData->string(attributeID());
257 bool isBlocked = myEditor->blockSignals(true);
258 QTextCursor aCursor = myEditor->textCursor();
259 int pos = aCursor.position();
260 std::string aRestoredStr = aStringAttr->value();
261 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
262 aCursor.setPosition(pos);
263 myEditor->setTextCursor(aCursor);
264 myEditor->blockSignals(isBlocked);
269 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
271 QList<QWidget*> result;
276 void ModuleBase_WidgetExprEditor::onTextChanged()