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>
37 ExpressionEditor::ExpressionEditor(QWidget* theParent)
38 : QPlainTextEdit(theParent)
40 myCompleter = new QCompleter(this);
41 myCompleter->setWidget(this);
42 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
44 myCompleterModel = new QStringListModel(this);
45 myCompleter->setModel(myCompleterModel);
46 // Use sorted model to accelerate completion (QCompleter will use binary search)
47 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
48 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
50 connect(myCompleter, SIGNAL(activated(const QString&)),
51 this, SLOT(insertCompletion(const QString&)));
52 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
53 this, SLOT(performCompletion()));
56 ExpressionEditor::~ExpressionEditor()
61 void ExpressionEditor::setCompletionList(QStringList& theList)
64 theList.removeDuplicates();
65 myCompleterModel->setStringList(theList);
68 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
70 QTextCursor aCursor = textCursor();
71 int numberOfCharsToComplete = theCompletion.length() -
72 myCompleter->completionPrefix().length();
73 int insertionPosition = aCursor.position();
74 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
76 aCursor.setPosition(insertionPosition);
77 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
78 myCompletedAndSelected = true;
80 setTextCursor(aCursor);
83 void ExpressionEditor::performCompletion()
85 QTextCursor aCursor = textCursor();
86 aCursor.select(QTextCursor::WordUnderCursor);
87 const QString aPrefix = aCursor.selectedText();
88 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
89 performCompletion(aPrefix);
93 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
96 if (theCompletionPrefix != myCompleter->completionPrefix()) {
97 myCompleter->setCompletionPrefix(theCompletionPrefix);
98 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
100 if (myCompleter->completionCount() == 1) {
101 insertCompletion(myCompleter->currentCompletion(), true);
103 QRect aRect = cursorRect();
104 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
105 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
106 myCompleter->complete(aRect);
110 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
112 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
114 myCompletedAndSelected = false;
115 if (myCompleter->popup()->isVisible()) {
116 switch (theEvent->key()) {
125 myCompleter->popup()->hide();
129 QPlainTextEdit::keyPressEvent(theEvent);
132 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
134 myCompletedAndSelected = false;
135 QTextCursor aCursor = textCursor();
136 switch (theEvent->key()) {
138 case Qt::Key_Return: aCursor.clearSelection(); break;
139 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
140 default: return false;
142 setTextCursor(aCursor);
147 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
148 const Config_WidgetAPI* theData,
149 const std::string& theParentId)
150 : ModuleBase_ModelWidget(theParent, theData, theParentId)
152 QVBoxLayout* aMainLay = new QVBoxLayout(this);
153 ModuleBase_Tools::adjustMargins(aMainLay);
155 myResultLabel = new QLabel(this);
156 myResultLabel->setWordWrap(true);
157 QFontMetrics fm(myResultLabel->font());
158 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
159 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
160 aMainLay->addWidget(myResultLabel);
161 myEditor = new ExpressionEditor(this);
162 myEditor->setMinimumHeight(20);
163 aMainLay->addWidget(myEditor);
164 this->setLayout(aMainLay);
166 connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
169 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
173 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
175 // A rare case when plugin was not loaded.
178 DataPtr aData = myFeature->data();
179 AttributeStringPtr aStringAttr = aData->string(attributeID());
180 QString aWidgetValue = myEditor->toPlainText();
181 aStringAttr->setValue(aWidgetValue.toStdString());
182 updateObject(myFeature);
184 // Try to get the value
186 std::string anErrorMessage = myFeature->error();
187 if (anErrorMessage.empty()) {
188 ResultParameterPtr aParam =
189 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
191 AttributeDoublePtr aValueAttr =
192 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
193 if (aValueAttr.get()) {
194 double aValue = aValueAttr->value();
195 aStateMsg = "Result: " + QString::number(aValue);
199 aStateMsg = QString::fromStdString(anErrorMessage);
201 myResultLabel->setText(aStateMsg);
205 bool ModuleBase_WidgetExprEditor::restoreValue()
207 // A rare case when plugin was not loaded.
210 DataPtr aData = myFeature->data();
211 AttributeStringPtr aStringAttr = aData->string(attributeID());
213 bool isBlocked = myEditor->blockSignals(true);
214 QTextCursor aCursor = myEditor->textCursor();
215 int pos = aCursor.position();
216 std::string aRestoredStr = aStringAttr->value();
217 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
218 aCursor.setPosition(pos);
219 myEditor->setTextCursor(aCursor);
220 myEditor->blockSignals(isBlocked);
225 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
227 QList<QWidget*> result;
232 void ModuleBase_WidgetExprEditor::onTextChanged()