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_AttributeString.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Validator.h>
18 #include <Config_WidgetAPI.h>
20 #include <QVBoxLayout>
25 #include <QStringListModel>
34 ExpressionEditor::ExpressionEditor(QWidget* theParent)
35 : QPlainTextEdit(theParent)
37 myCompleter = new QCompleter(this);
38 myCompleter->setWidget(this);
39 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
41 myCompleterModel = new QStringListModel(this);
42 myCompleter->setModel(myCompleterModel);
43 // Use sorted model to accelerate completion (QCompleter will use binary search)
44 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
45 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
47 connect(myCompleter, SIGNAL(activated(const QString&)),
48 this, SLOT(insertCompletion(const QString&)));
49 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
50 this, SLOT(performCompletion()));
53 ExpressionEditor::~ExpressionEditor()
58 void ExpressionEditor::setCompletionList(QStringList& theList)
61 theList.removeDuplicates();
62 myCompleterModel->setStringList(theList);
65 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
67 QTextCursor aCursor = textCursor();
68 int numberOfCharsToComplete = theCompletion.length() -
69 myCompleter->completionPrefix().length();
70 int insertionPosition = aCursor.position();
71 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
73 aCursor.setPosition(insertionPosition);
74 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
75 myCompletedAndSelected = true;
77 setTextCursor(aCursor);
80 void ExpressionEditor::performCompletion()
82 QTextCursor aCursor = textCursor();
83 aCursor.select(QTextCursor::WordUnderCursor);
84 const QString aPrefix = aCursor.selectedText();
85 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
86 performCompletion(aPrefix);
90 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
93 if (theCompletionPrefix != myCompleter->completionPrefix()) {
94 myCompleter->setCompletionPrefix(theCompletionPrefix);
95 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
97 if (myCompleter->completionCount() == 1) {
98 insertCompletion(myCompleter->currentCompletion(), true);
100 QRect aRect = cursorRect();
101 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
102 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
103 myCompleter->complete(aRect);
107 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
109 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
111 myCompletedAndSelected = false;
112 if (myCompleter->popup()->isVisible()) {
113 switch (theEvent->key()) {
122 myCompleter->popup()->hide();
126 QPlainTextEdit::keyPressEvent(theEvent);
129 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
131 myCompletedAndSelected = false;
132 QTextCursor aCursor = textCursor();
133 switch (theEvent->key()) {
135 case Qt::Key_Return: aCursor.clearSelection(); break;
136 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
137 default: return false;
139 setTextCursor(aCursor);
144 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
145 const Config_WidgetAPI* theData,
146 const std::string& theParentId)
147 : ModuleBase_ModelWidget(theParent, theData, theParentId)
149 QVBoxLayout* aMainLay = new QVBoxLayout(this);
150 ModuleBase_Tools::adjustMargins(aMainLay);
152 myEditor = new ExpressionEditor(this);
153 myEditor->setMinimumHeight(20);
154 aMainLay->addWidget(myEditor);
155 this->setLayout(aMainLay);
157 connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
160 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
164 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
166 // A rare case when plugin was not loaded.
169 DataPtr aData = myFeature->data();
170 AttributeStringPtr aStringAttr = aData->string(attributeID());
171 QString aWidgetValue = myEditor->toPlainText();
172 aStringAttr->setValue(aWidgetValue.toStdString());
173 updateObject(myFeature);
177 bool ModuleBase_WidgetExprEditor::restoreValue()
179 // A rare case when plugin was not loaded.
182 DataPtr aData = myFeature->data();
183 AttributeStringPtr aStringAttr = aData->string(attributeID());
185 bool isBlocked = myEditor->blockSignals(true);
186 QTextCursor aCursor = myEditor->textCursor();
187 int pos = aCursor.position();
188 std::string aRestoredStr = aStringAttr->value();
189 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
190 aCursor.setPosition(pos);
191 myEditor->setTextCursor(aCursor);
192 myEditor->blockSignals(isBlocked);
197 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
199 QList<QWidget*> result;
204 void ModuleBase_WidgetExprEditor::onTextChanged()