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>
35 #include <QAbstractItemView>
40 ExpressionEditor::ExpressionEditor(QWidget* theParent)
41 : QPlainTextEdit(theParent), myCompletedAndSelected(false)
43 myCompleter = new QCompleter(this);
44 myCompleter->setWidget(this);
45 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
47 myCompleterModel = new QStringListModel(this);
48 myCompleter->setModel(myCompleterModel);
49 // Use sorted model to accelerate completion (QCompleter will use binary search)
50 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
51 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
53 connect(myCompleter, SIGNAL(activated(const QString&)),
54 this, SLOT(insertCompletion(const QString&)));
55 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
56 this, SLOT(performCompletion()));
58 connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
60 setTabChangesFocus(true);
63 ExpressionEditor::~ExpressionEditor()
68 void ExpressionEditor::setCompletionList(QStringList& theList)
71 theList.removeDuplicates();
72 myCompleterModel->setStringList(theList);
75 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
77 QTextCursor aCursor = textCursor();
78 int numberOfCharsToComplete = theCompletion.length() -
79 myCompleter->completionPrefix().length();
80 int insertionPosition = aCursor.position();
81 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
83 aCursor.setPosition(insertionPosition);
84 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
85 myCompletedAndSelected = true;
87 setTextCursor(aCursor);
90 void ExpressionEditor::performCompletion()
92 QTextCursor aCursor = textCursor();
93 aCursor.select(QTextCursor::WordUnderCursor);
94 const QString aPrefix = aCursor.selectedText();
95 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
96 performCompletion(aPrefix);
100 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
103 if (theCompletionPrefix != myCompleter->completionPrefix()) {
104 myCompleter->setCompletionPrefix(theCompletionPrefix);
105 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
107 if (myCompleter->completionCount() == 1) {
108 insertCompletion(myCompleter->currentCompletion(), true);
110 QRect aRect = cursorRect();
111 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
112 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
113 myCompleter->complete(aRect);
117 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
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(this, 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 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
178 QPlainTextEdit::paintEvent( theEvent );
180 if( toPlainText().isEmpty() )
182 QPainter aPainter( viewport() );
183 QFontMetrics aFontMetrics = fontMetrics();
185 QPointF offset(contentOffset());
187 int m = (int)document()->documentMargin();
188 QRect lineRect( r.x() + m + offset.x(), offset.y(),
189 r.width() - 2*m, aFontMetrics.height() );
191 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
192 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
194 QColor aColor = palette().text().color();
195 aColor.setAlpha( 128 );
196 QPen anOldpen = aPainter.pen();
197 aPainter.setPen( aColor );
198 lineRect.adjust(minLB, 0, 0, 0);
200 aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
201 aPainter.drawText( lineRect, va, elidedText );
202 aPainter.setPen( anOldpen );
206 void ExpressionEditor::onTextChanged()
208 emit valueModified();
212 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
213 const Config_WidgetAPI* theData,
214 const std::string& thePlaceHolder )
215 : ModuleBase_ModelWidget(theParent, theData)
217 QVBoxLayout* aMainLay = new QVBoxLayout(this);
218 ModuleBase_Tools::adjustMargins(aMainLay);
220 myResultLabel = new QLabel(this);
221 myResultLabel->setWordWrap(true);
222 QFontMetrics fm(myResultLabel->font());
223 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
224 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
225 aMainLay->addWidget(myResultLabel);
226 myEditor = new ExpressionEditor(this);
227 myEditor->setMinimumHeight(20);
228 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
229 aMainLay->addWidget(myEditor);
230 this->setLayout(aMainLay);
232 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
233 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
234 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
237 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
241 void ModuleBase_WidgetExprEditor::activateCustom()
243 ModuleBase_ModelWidget::activateCustom();
245 QStringList aParameters;
246 ModuleBase_Tools::getParameters(aParameters);
247 myEditor->setCompletionList(aParameters);
250 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
254 bool ModuleBase_WidgetExprEditor::storeValueCustom()
256 // A rare case when plugin was not loaded.
259 DataPtr aData = myFeature->data();
260 AttributeStringPtr aStringAttr = aData->string(attributeID());
262 QString aWidgetValue = myEditor->toPlainText();
263 aStringAttr->setValue(aWidgetValue.toStdString());
264 updateObject(myFeature);
266 // Try to get the value
268 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
269 if (anErrorMessage.empty()) {
270 ResultParameterPtr aParam =
271 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
273 AttributeDoublePtr aValueAttr =
274 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
275 if (aValueAttr.get()) {
276 double aValue = aValueAttr->value();
277 aStateMsg = "Result: " + QString::number(aValue);
281 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
283 myResultLabel->setText(aStateMsg);
287 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
289 // A rare case when plugin was not loaded.
292 DataPtr aData = myFeature->data();
293 AttributeStringPtr aStringAttr = aData->string(attributeID());
295 bool isBlocked = myEditor->blockSignals(true);
296 QTextCursor aCursor = myEditor->textCursor();
297 int pos = aCursor.position();
298 std::string aRestoredStr = aStringAttr->value();
299 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
300 aCursor.setPosition(pos);
301 myEditor->setTextCursor(aCursor);
302 myEditor->blockSignals(isBlocked);
307 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
309 QList<QWidget*> result;
314 bool ModuleBase_WidgetExprEditor::processEnter()
316 bool isModified = getValueState() == ModifiedInPP;
318 emit valuesChanged();
319 myEditor->selectAll();
324 void ModuleBase_WidgetExprEditor::onTextChanged()
326 emit valuesChanged();