1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetExprEditor.h>
21 #include <ModuleBase_Tools.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_ResultParameter.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeDouble.h>
30 #include <Config_WidgetAPI.h>
32 #include <QVBoxLayout>
37 #include <QStringListModel>
42 #include <QFontMetrics>
45 #include <QAbstractItemView>
50 ExpressionEditor::ExpressionEditor(QWidget* theParent)
51 : QPlainTextEdit(theParent), myCompletedAndSelected(false)
53 myCompleter = new QCompleter(this);
54 myCompleter->setWidget(this);
55 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
57 myCompleterModel = new QStringListModel(this);
58 myCompleter->setModel(myCompleterModel);
59 // Use sorted model to accelerate completion (QCompleter will use binary search)
60 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
61 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
63 connect(myCompleter, SIGNAL(activated(const QString&)),
64 this, SLOT(insertCompletion(const QString&)));
65 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
66 this, SLOT(performCompletion()));
68 connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
70 setTabChangesFocus(true);
73 ExpressionEditor::~ExpressionEditor()
78 void ExpressionEditor::setCompletionList(QStringList& theList)
81 theList.removeDuplicates();
82 myCompleterModel->setStringList(theList);
85 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
87 QTextCursor aCursor = textCursor();
88 int numberOfCharsToComplete = theCompletion.length() -
89 myCompleter->completionPrefix().length();
90 int insertionPosition = aCursor.position();
91 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
93 aCursor.setPosition(insertionPosition);
94 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
95 myCompletedAndSelected = true;
97 setTextCursor(aCursor);
100 void ExpressionEditor::performCompletion()
102 QTextCursor aCursor = textCursor();
103 aCursor.select(QTextCursor::WordUnderCursor);
104 const QString aPrefix = aCursor.selectedText();
105 performCompletion(aPrefix);
108 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
111 if (theCompletionPrefix != myCompleter->completionPrefix()) {
112 myCompleter->setCompletionPrefix(theCompletionPrefix);
113 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
115 if (myCompleter->completionCount() == 1) {
116 insertCompletion(myCompleter->currentCompletion(), true);
118 QRect aRect = cursorRect();
119 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
120 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
121 myCompleter->complete(aRect);
125 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
127 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
129 myCompletedAndSelected = false;
130 if (myCompleter->popup()->isVisible()) {
131 switch (theEvent->key()) {
140 myCompleter->popup()->hide();
145 switch (theEvent->key()) {
148 emit keyReleased(this, theEvent);
149 // do not react to the Enter key, the property panel processes it
156 QPlainTextEdit::keyPressEvent(theEvent);
159 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
161 myCompletedAndSelected = false;
162 QTextCursor aCursor = textCursor();
163 switch (theEvent->key()) {
165 case Qt::Key_Return: aCursor.clearSelection(); break;
166 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
167 default: return false;
169 setTextCursor(aCursor);
174 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
176 myPlaceHolderText = thePlaceHolderText;
179 QString ExpressionEditor::placeHolderText() const
181 return myPlaceHolderText;
184 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
186 QPlainTextEdit::paintEvent( theEvent );
188 if( toPlainText().isEmpty() )
190 QPainter aPainter( viewport() );
191 QFontMetrics aFontMetrics = fontMetrics();
193 QPointF offset(contentOffset());
195 int m = (int)document()->documentMargin();
196 QRect lineRect( r.x() + m + offset.x(), offset.y(),
197 r.width() - 2*m, aFontMetrics.height() );
199 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
200 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
202 QColor aColor = palette().text().color();
203 aColor.setAlpha( 128 );
204 QPen anOldpen = aPainter.pen();
205 aPainter.setPen( aColor );
206 lineRect.adjust(minLB, 0, 0, 0);
208 aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
209 aPainter.drawText( lineRect, va, elidedText );
210 aPainter.setPen( anOldpen );
214 void ExpressionEditor::onTextChanged()
216 emit valueModified();
220 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
221 const Config_WidgetAPI* theData,
222 const std::string& thePlaceHolder )
223 : ModuleBase_ModelWidget(theParent, theData)
225 QVBoxLayout* aMainLay = new QVBoxLayout(this);
226 ModuleBase_Tools::adjustMargins(aMainLay);
228 myResultLabel = new QLabel(this);
229 myResultLabel->setWordWrap(true);
230 QFontMetrics fm(myResultLabel->font());
231 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
232 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
233 aMainLay->addWidget(myResultLabel);
234 myEditor = new ExpressionEditor(this);
235 myEditor->setMinimumHeight(20);
236 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
237 aMainLay->addWidget(myEditor);
238 this->setLayout(aMainLay);
240 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
241 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
242 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
245 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
249 void ModuleBase_WidgetExprEditor::activateCustom()
251 ModuleBase_ModelWidget::activateCustom();
253 QStringList aParameters;
254 ModuleBase_Tools::getParameters(aParameters);
255 myEditor->setCompletionList(aParameters);
258 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
262 bool ModuleBase_WidgetExprEditor::storeValueCustom()
264 // A rare case when plugin was not loaded.
267 DataPtr aData = myFeature->data();
268 AttributeStringPtr aStringAttr = aData->string(attributeID());
270 QString aWidgetValue = myEditor->toPlainText();
271 aStringAttr->setValue(aWidgetValue.toStdString());
272 updateObject(myFeature);
274 // Try to get the value
276 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
277 if (anErrorMessage.empty()) {
278 ResultParameterPtr aParam =
279 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
281 AttributeDoublePtr aValueAttr =
282 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
283 if (aValueAttr.get()) {
284 double aValue = aValueAttr->value();
285 aStateMsg = "Result: " + QString::number(aValue);
289 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
291 myResultLabel->setText(aStateMsg);
295 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
297 // A rare case when plugin was not loaded.
300 DataPtr aData = myFeature->data();
301 AttributeStringPtr aStringAttr = aData->string(attributeID());
303 bool isBlocked = myEditor->blockSignals(true);
304 QTextCursor aCursor = myEditor->textCursor();
305 int pos = aCursor.position();
306 std::string aRestoredStr = aStringAttr->value();
307 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
308 aCursor.setPosition(pos);
309 myEditor->setTextCursor(aCursor);
310 myEditor->blockSignals(isBlocked);
315 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
317 QList<QWidget*> result;
322 bool ModuleBase_WidgetExprEditor::processEnter()
324 bool isModified = getValueState() == ModifiedInPP;
326 emit valuesChanged();
327 myEditor->selectAll();
332 void ModuleBase_WidgetExprEditor::onTextChanged()
334 emit valuesChanged();