Salome HOME
Expression editor implementation
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetExprEditor.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetExprEditor.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetExprEditor.h>
11 #include <ModuleBase_Tools.h>
12
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Validator.h>
17
18 #include <Config_WidgetAPI.h>
19
20 #include <QVBoxLayout>
21 #include <QLabel>
22 #include <QLineEdit>
23 #include <QObject>
24 #include <QString>
25 #include <QStringListModel>
26 #include <QCompleter>
27 #include <QSize>
28 #include <QShortcut>
29 #include <QScrollBar>
30
31 #include <memory>
32 #include <string>
33
34 ExpressionEditor::ExpressionEditor(QWidget* theParent)
35 : QPlainTextEdit(theParent)
36 {
37   myCompleter = new QCompleter(this);
38   myCompleter->setWidget(this);
39   myCompleter->setCompletionMode(QCompleter::PopupCompletion);
40
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);
46
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()));
51 }
52
53 ExpressionEditor::~ExpressionEditor()
54 {
55
56 }
57
58 void ExpressionEditor::setCompletionList(QStringList& theList)
59 {
60   theList.sort();
61   theList.removeDuplicates();
62   myCompleterModel->setStringList(theList);
63 }
64
65 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
66 {
67   QTextCursor aCursor = textCursor();
68   int numberOfCharsToComplete = theCompletion.length() -
69       myCompleter->completionPrefix().length();
70   int insertionPosition = aCursor.position();
71   aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
72   if (isSingleWord) {
73     aCursor.setPosition(insertionPosition);
74     aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
75     myCompletedAndSelected = true;
76   }
77   setTextCursor(aCursor);
78 }
79
80 void ExpressionEditor::performCompletion()
81 {
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);
87   }
88 }
89
90 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
91 {
92   //populate model?
93   if (theCompletionPrefix != myCompleter->completionPrefix()) {
94     myCompleter->setCompletionPrefix(theCompletionPrefix);
95     myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
96   }
97   if (myCompleter->completionCount() == 1) {
98     insertCompletion(myCompleter->currentCompletion(), true);
99   } else {
100     QRect aRect = cursorRect();
101     aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
102                   + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
103     myCompleter->complete(aRect);
104   }
105 }
106
107 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
108 {
109   if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
110     return;
111   myCompletedAndSelected = false;
112   if (myCompleter->popup()->isVisible()) {
113     switch (theEvent->key()) {
114       case Qt::Key_Up:
115       case Qt::Key_Down:
116       case Qt::Key_Enter:
117       case Qt::Key_Return:
118       case Qt::Key_Escape:
119         theEvent->ignore();
120         return;
121       default:
122         myCompleter->popup()->hide();
123         break;
124     }
125   }
126   QPlainTextEdit::keyPressEvent(theEvent);
127 }
128
129 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
130 {
131   myCompletedAndSelected = false;
132   QTextCursor aCursor = textCursor();
133   switch (theEvent->key()) {
134     case Qt::Key_Enter:
135     case Qt::Key_Return: aCursor.clearSelection(); break;
136     case Qt::Key_Escape: aCursor.removeSelectedText(); break;
137     default: return false;
138   }
139   setTextCursor(aCursor);
140   theEvent->accept();
141   return true;
142 }
143
144 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
145                                                      const Config_WidgetAPI* theData,
146                                                      const std::string& theParentId)
147     : ModuleBase_ModelWidget(theParent, theData, theParentId)
148 {
149   QVBoxLayout* aMainLay = new QVBoxLayout(this);
150   ModuleBase_Tools::adjustMargins(aMainLay);
151
152   myEditor = new ExpressionEditor(this);
153   myEditor->setMinimumHeight(20);
154   aMainLay->addWidget(myEditor);
155   this->setLayout(aMainLay);
156
157   connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
158 }
159
160 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
161 {
162 }
163
164 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
165 {
166   // A rare case when plugin was not loaded. 
167   if(!myFeature)
168     return false;
169   DataPtr aData = myFeature->data();
170   AttributeStringPtr aStringAttr = aData->string(attributeID());
171   QString aWidgetValue = myEditor->toPlainText();
172   aStringAttr->setValue(aWidgetValue.toStdString());
173   updateObject(myFeature);
174   return true;
175 }
176
177 bool ModuleBase_WidgetExprEditor::restoreValue()
178 {
179   // A rare case when plugin was not loaded. 
180   if(!myFeature)
181     return false;
182   DataPtr aData = myFeature->data();
183   AttributeStringPtr aStringAttr = aData->string(attributeID());
184
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);
193
194   return true;
195 }
196
197 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
198 {
199   QList<QWidget*> result;
200   result << myEditor;
201   return result;
202 }
203
204 void ModuleBase_WidgetExprEditor::onTextChanged()
205 {
206   storeValue();
207 }