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>
36 ExpressionEditor::ExpressionEditor(QWidget* theParent)
37 : QPlainTextEdit(theParent)
39 myCompleter = new QCompleter(this);
40 myCompleter->setWidget(this);
41 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
43 myCompleterModel = new QStringListModel(this);
44 myCompleter->setModel(myCompleterModel);
45 // Use sorted model to accelerate completion (QCompleter will use binary search)
46 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
47 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
49 connect(myCompleter, SIGNAL(activated(const QString&)),
50 this, SLOT(insertCompletion(const QString&)));
51 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
52 this, SLOT(performCompletion()));
55 ExpressionEditor::~ExpressionEditor()
60 void ExpressionEditor::setCompletionList(QStringList& theList)
63 theList.removeDuplicates();
64 myCompleterModel->setStringList(theList);
67 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
69 QTextCursor aCursor = textCursor();
70 int numberOfCharsToComplete = theCompletion.length() -
71 myCompleter->completionPrefix().length();
72 int insertionPosition = aCursor.position();
73 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
75 aCursor.setPosition(insertionPosition);
76 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
77 myCompletedAndSelected = true;
79 setTextCursor(aCursor);
82 void ExpressionEditor::performCompletion()
84 QTextCursor aCursor = textCursor();
85 aCursor.select(QTextCursor::WordUnderCursor);
86 const QString aPrefix = aCursor.selectedText();
87 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
88 performCompletion(aPrefix);
92 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
95 if (theCompletionPrefix != myCompleter->completionPrefix()) {
96 myCompleter->setCompletionPrefix(theCompletionPrefix);
97 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
99 if (myCompleter->completionCount() == 1) {
100 insertCompletion(myCompleter->currentCompletion(), true);
102 QRect aRect = cursorRect();
103 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
104 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
105 myCompleter->complete(aRect);
109 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
111 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
113 myCompletedAndSelected = false;
114 if (myCompleter->popup()->isVisible()) {
115 switch (theEvent->key()) {
124 myCompleter->popup()->hide();
128 QPlainTextEdit::keyPressEvent(theEvent);
131 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
133 myCompletedAndSelected = false;
134 QTextCursor aCursor = textCursor();
135 switch (theEvent->key()) {
137 case Qt::Key_Return: aCursor.clearSelection(); break;
138 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
139 default: return false;
141 setTextCursor(aCursor);
146 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
147 const Config_WidgetAPI* theData,
148 const std::string& theParentId)
149 : ModuleBase_ModelWidget(theParent, theData, theParentId)
151 QVBoxLayout* aMainLay = new QVBoxLayout(this);
152 ModuleBase_Tools::adjustMargins(aMainLay);
154 myResultLabel = new QLabel(this);
155 myResultLabel->setWordWrap(true);
156 aMainLay->addWidget(myResultLabel);
157 myEditor = new ExpressionEditor(this);
158 myEditor->setMinimumHeight(20);
159 aMainLay->addWidget(myEditor);
160 this->setLayout(aMainLay);
162 connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
165 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
169 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
171 // A rare case when plugin was not loaded.
174 DataPtr aData = myFeature->data();
175 AttributeStringPtr aStringAttr = aData->string(attributeID());
176 QString aWidgetValue = myEditor->toPlainText();
177 aStringAttr->setValue(aWidgetValue.toStdString());
178 updateObject(myFeature);
180 // Try to get the value
182 std::string anErrorMessage = myFeature->error();
183 if (anErrorMessage.empty()) {
184 ResultParameterPtr aParam =
185 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
187 AttributeDoublePtr aValueAttr =
188 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
189 if (aValueAttr.get()) {
190 double aValue = aValueAttr->value();
191 aStateMsg = "Result: " + QString::number(aValue);
195 aStateMsg = QString::fromStdString(anErrorMessage);
197 myResultLabel->setText(aStateMsg);
201 bool ModuleBase_WidgetExprEditor::restoreValue()
203 // A rare case when plugin was not loaded.
206 DataPtr aData = myFeature->data();
207 AttributeStringPtr aStringAttr = aData->string(attributeID());
209 bool isBlocked = myEditor->blockSignals(true);
210 QTextCursor aCursor = myEditor->textCursor();
211 int pos = aCursor.position();
212 std::string aRestoredStr = aStringAttr->value();
213 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
214 aCursor.setPosition(pos);
215 myEditor->setTextCursor(aCursor);
216 myEditor->blockSignals(isBlocked);
221 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
223 QList<QWidget*> result;
228 void ModuleBase_WidgetExprEditor::onTextChanged()