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()));
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 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
119 myCompletedAndSelected = false;
120 if (myCompleter->popup()->isVisible()) {
121 switch (theEvent->key()) {
130 myCompleter->popup()->hide();
135 switch (theEvent->key()) {
138 emit keyReleased(this, theEvent);
139 // do not react to the Enter key, the property panel processes it
146 QPlainTextEdit::keyPressEvent(theEvent);
149 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
151 myCompletedAndSelected = false;
152 QTextCursor aCursor = textCursor();
153 switch (theEvent->key()) {
155 case Qt::Key_Return: aCursor.clearSelection(); break;
156 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
157 default: return false;
159 setTextCursor(aCursor);
164 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
166 myPlaceHolderText = thePlaceHolderText;
169 QString ExpressionEditor::placeHolderText() const
171 return myPlaceHolderText;
174 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
176 QPlainTextEdit::paintEvent( theEvent );
178 if( toPlainText().isEmpty() )
180 QPainter aPainter( viewport() );
181 QFontMetrics aFontMetrics = fontMetrics();
183 QPointF offset(contentOffset());
185 int m = (int)document()->documentMargin();
186 QRect lineRect( r.x() + m + offset.x(), offset.y(),
187 r.width() - 2*m, aFontMetrics.height() );
189 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
190 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
192 QColor aColor = palette().text().color();
193 aColor.setAlpha( 128 );
194 QPen anOldpen = aPainter.pen();
195 aPainter.setPen( aColor );
196 lineRect.adjust(minLB, 0, 0, 0);
197 QString elidedText = aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
198 aPainter.drawText( lineRect, va, elidedText );
199 aPainter.setPen( anOldpen );
203 void ExpressionEditor::onTextChanged()
205 emit valueModified();
209 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
210 const Config_WidgetAPI* theData,
211 const std::string& theParentId,
212 const std::string& thePlaceHolder )
213 : ModuleBase_ModelWidget(theParent, theData, theParentId)
215 QVBoxLayout* aMainLay = new QVBoxLayout(this);
216 ModuleBase_Tools::adjustMargins(aMainLay);
218 myResultLabel = new QLabel(this);
219 myResultLabel->setWordWrap(true);
220 QFontMetrics fm(myResultLabel->font());
221 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
222 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
223 aMainLay->addWidget(myResultLabel);
224 myEditor = new ExpressionEditor(this);
225 myEditor->setMinimumHeight(20);
226 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
227 aMainLay->addWidget(myEditor);
228 this->setLayout(aMainLay);
230 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
231 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
232 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
235 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
239 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
243 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
245 // A rare case when plugin was not loaded.
248 DataPtr aData = myFeature->data();
249 AttributeStringPtr aStringAttr = aData->string(attributeID());
251 QString aWidgetValue = myEditor->toPlainText();
252 aStringAttr->setValue(aWidgetValue.toStdString());
253 updateObject(myFeature);
255 // Try to get the value
257 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
258 if (anErrorMessage.empty()) {
259 ResultParameterPtr aParam =
260 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
262 AttributeDoublePtr aValueAttr =
263 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
264 if (aValueAttr.get()) {
265 double aValue = aValueAttr->value();
266 aStateMsg = "Result: " + QString::number(aValue);
270 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
272 myResultLabel->setText(aStateMsg);
276 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
278 // A rare case when plugin was not loaded.
281 DataPtr aData = myFeature->data();
282 AttributeStringPtr aStringAttr = aData->string(attributeID());
284 bool isBlocked = myEditor->blockSignals(true);
285 QTextCursor aCursor = myEditor->textCursor();
286 int pos = aCursor.position();
287 std::string aRestoredStr = aStringAttr->value();
288 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
289 aCursor.setPosition(pos);
290 myEditor->setTextCursor(aCursor);
291 myEditor->blockSignals(isBlocked);
296 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
298 QList<QWidget*> result;
303 bool ModuleBase_WidgetExprEditor::processEnter()
305 bool isModified = getValueState() == ModifiedInPP;
307 emit valuesChanged();
308 myEditor->selectAll();
313 void ModuleBase_WidgetExprEditor::onTextChanged()
315 emit valuesChanged();