Salome HOME
Create help documents management system
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetExprEditor.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_WidgetExprEditor.h>
22 #include <ModuleBase_Tools.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_ResultParameter.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_AttributeDouble.h>
30
31 #include <Config_WidgetAPI.h>
32
33 #include <QVBoxLayout>
34 #include <QLabel>
35 #include <QLineEdit>
36 #include <QObject>
37 #include <QString>
38 #include <QStringListModel>
39 #include <QCompleter>
40 #include <QSize>
41 #include <QShortcut>
42 #include <QScrollBar>
43 #include <QFontMetrics>
44 #include <QPainter>
45 #include <QStyle>
46 #include <QAbstractItemView>
47
48 #include <memory>
49 #include <string>
50
51 ExpressionEditor::ExpressionEditor(QWidget* theParent)
52 : QPlainTextEdit(theParent), myCompletedAndSelected(false)
53 {
54   myCompleter = new QCompleter(this);
55   myCompleter->setWidget(this);
56   myCompleter->setCompletionMode(QCompleter::PopupCompletion);
57
58   myCompleterModel = new QStringListModel(this);
59   myCompleter->setModel(myCompleterModel);
60   // Use sorted model to accelerate completion (QCompleter will use binary search)
61   myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
62   myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
63
64   connect(myCompleter, SIGNAL(activated(const QString&)),
65           this,        SLOT(insertCompletion(const QString&)));
66   (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
67                        this, SLOT(performCompletion()));
68
69   connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
70
71   setTabChangesFocus(true);
72 }
73
74 ExpressionEditor::~ExpressionEditor()
75 {
76
77 }
78
79 void ExpressionEditor::setCompletionList(QStringList& theList)
80 {
81   theList.sort();
82   theList.removeDuplicates();
83   myCompleterModel->setStringList(theList);
84 }
85
86 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
87 {
88   QTextCursor aCursor = textCursor();
89   int numberOfCharsToComplete = theCompletion.length() -
90       myCompleter->completionPrefix().length();
91   int insertionPosition = aCursor.position();
92   aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
93   if (isSingleWord) {
94     aCursor.setPosition(insertionPosition);
95     aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
96     myCompletedAndSelected = true;
97   }
98   setTextCursor(aCursor);
99 }
100
101 void ExpressionEditor::performCompletion()
102 {
103   QTextCursor aCursor = textCursor();
104   aCursor.select(QTextCursor::WordUnderCursor);
105   const QString aPrefix = aCursor.selectedText();
106   performCompletion(aPrefix);
107 }
108
109 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
110 {
111   //populate model?
112   if (theCompletionPrefix != myCompleter->completionPrefix()) {
113     myCompleter->setCompletionPrefix(theCompletionPrefix);
114     myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
115   }
116   if (myCompleter->completionCount() == 1) {
117     insertCompletion(myCompleter->currentCompletion(), true);
118   } else {
119     QRect aRect = cursorRect();
120     aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
121                   + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
122     myCompleter->complete(aRect);
123   }
124 }
125
126 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
127 {
128   if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
129     return;
130   myCompletedAndSelected = false;
131   if (myCompleter->popup()->isVisible()) {
132     switch (theEvent->key()) {
133       case Qt::Key_Up:
134       case Qt::Key_Down:
135       case Qt::Key_Escape:
136       case Qt::Key_Enter:
137       case Qt::Key_Return:
138         theEvent->ignore();
139       return;
140       default:
141         myCompleter->popup()->hide();
142         break;
143     }
144   }
145   else {
146     switch (theEvent->key()) {
147       case Qt::Key_Enter:
148       case Qt::Key_Return:
149         emit keyReleased(this, theEvent);
150         // do not react to the Enter key, the property panel processes it
151         return;
152       break;
153       default:
154         break;
155     }
156   }
157   QPlainTextEdit::keyPressEvent(theEvent);
158 }
159
160 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
161 {
162   myCompletedAndSelected = false;
163   QTextCursor aCursor = textCursor();
164   switch (theEvent->key()) {
165     case Qt::Key_Enter:
166     case Qt::Key_Return: aCursor.clearSelection(); break;
167     case Qt::Key_Escape: aCursor.removeSelectedText(); break;
168     default: return false;
169   }
170   setTextCursor(aCursor);
171   theEvent->accept();
172   return true;
173 }
174
175 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
176 {
177   myPlaceHolderText = thePlaceHolderText;
178 }
179
180 QString ExpressionEditor::placeHolderText() const
181 {
182   return myPlaceHolderText;
183 }
184
185 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
186 {
187   QPlainTextEdit::paintEvent( theEvent );
188
189   if( toPlainText().isEmpty() )
190   {
191     QPainter aPainter( viewport() );
192     QFontMetrics aFontMetrics = fontMetrics();
193
194     QPointF offset(contentOffset());
195     QRect r = rect();
196     int m = (int)document()->documentMargin();
197     QRect lineRect( r.x() + m + offset.x(), offset.y(),
198                     r.width() - 2*m, aFontMetrics.height() );
199
200     Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
201     int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
202
203     QColor aColor = palette().text().color();
204     aColor.setAlpha( 128 );
205     QPen anOldpen = aPainter.pen();
206     aPainter.setPen( aColor );
207     lineRect.adjust(minLB, 0, 0, 0);
208     QString elidedText =
209       aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
210     aPainter.drawText( lineRect, va, elidedText );
211     aPainter.setPen( anOldpen );
212   }
213 }
214
215 void ExpressionEditor::onTextChanged()
216 {
217   emit valueModified();
218 }
219
220
221 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
222                                                           const Config_WidgetAPI* theData,
223                                                           const std::string& thePlaceHolder )
224 : ModuleBase_ModelWidget(theParent, theData)
225 {
226   QVBoxLayout* aMainLay = new QVBoxLayout(this);
227   ModuleBase_Tools::adjustMargins(aMainLay);
228
229   myResultLabel = new QLabel(this);
230   myResultLabel->setWordWrap(true);
231   QFontMetrics fm(myResultLabel->font());
232   myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
233   myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
234   aMainLay->addWidget(myResultLabel);
235   myEditor = new ExpressionEditor(this);
236   myEditor->setMinimumHeight(20);
237   myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
238   aMainLay->addWidget(myEditor);
239   this->setLayout(aMainLay);
240
241   connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
242   connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
243           this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
244 }
245
246 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
247 {
248 }
249
250 void ModuleBase_WidgetExprEditor::activateCustom()
251 {
252   ModuleBase_ModelWidget::activateCustom();
253
254   QStringList aParameters;
255   ModuleBase_Tools::getParameters(aParameters);
256   myEditor->setCompletionList(aParameters);
257 }
258
259 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
260 {
261 }
262
263 bool ModuleBase_WidgetExprEditor::storeValueCustom()
264 {
265   // A rare case when plugin was not loaded.
266   if(!myFeature)
267     return false;
268   DataPtr aData = myFeature->data();
269   AttributeStringPtr aStringAttr = aData->string(attributeID());
270
271   QString aWidgetValue = myEditor->toPlainText();
272   aStringAttr->setValue(aWidgetValue.toStdString());
273   updateObject(myFeature);
274
275   // Try to get the value
276   QString aStateMsg;
277   std::string anErrorMessage = myFeature->string("ExpressionError")->value();
278   if (anErrorMessage.empty()) {
279     ResultParameterPtr aParam =
280       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
281     if(aParam.get()) {
282       AttributeDoublePtr aValueAttr =
283         aParam->data()->real(ModelAPI_ResultParameter::VALUE());
284       if (aValueAttr.get()) {
285         double aValue = aValueAttr->value();
286         aStateMsg = "Result: " + QString::number(aValue);
287       }
288     }
289   } else {
290     aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
291   }
292   myResultLabel->setText(aStateMsg);
293   return true;
294 }
295
296 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
297 {
298   // A rare case when plugin was not loaded.
299   if(!myFeature)
300     return false;
301   DataPtr aData = myFeature->data();
302   AttributeStringPtr aStringAttr = aData->string(attributeID());
303
304   bool isBlocked = myEditor->blockSignals(true);
305   QTextCursor aCursor = myEditor->textCursor();
306   int pos = aCursor.position();
307   std::string aRestoredStr = aStringAttr->value();
308   myEditor->setPlainText(QString::fromStdString(aRestoredStr));
309   aCursor.setPosition(pos);
310   myEditor->setTextCursor(aCursor);
311   myEditor->blockSignals(isBlocked);
312
313   return true;
314 }
315
316 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
317 {
318   QList<QWidget*> result;
319   result << myEditor;
320   return result;
321 }
322
323 bool ModuleBase_WidgetExprEditor::processEnter()
324 {
325   bool isModified = getValueState() == ModifiedInPP;
326   if (isModified) {
327     emit valuesChanged();
328     myEditor->selectAll();
329   }
330   return isModified;
331 }
332
333 void ModuleBase_WidgetExprEditor::onTextChanged()
334 {
335   emit valuesChanged();
336 }