1 // Copyright (C) 2014-2020 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 <Locale_Convert.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_AttributeDouble.h>
32 #include <Config_WidgetAPI.h>
34 #include <QVBoxLayout>
39 #include <QStringListModel>
44 #include <QFontMetrics>
47 #include <QAbstractItemView>
52 ExpressionEditor::ExpressionEditor(QWidget* theParent)
53 : QPlainTextEdit(theParent), myCompletedAndSelected(false)
55 myCompleter = new QCompleter(this);
56 myCompleter->setWidget(this);
57 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
59 myCompleterModel = new QStringListModel(this);
60 myCompleter->setModel(myCompleterModel);
61 // Use sorted model to accelerate completion (QCompleter will use binary search)
62 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
63 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
65 connect(myCompleter, SIGNAL(activated(const QString&)),
66 this, SLOT(insertCompletion(const QString&)));
67 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
68 this, SLOT(performCompletion()));
70 connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
72 setTabChangesFocus(true);
75 ExpressionEditor::~ExpressionEditor()
80 void ExpressionEditor::setCompletionList(QStringList& theList)
83 theList.removeDuplicates();
84 myCompleterModel->setStringList(theList);
87 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
89 QTextCursor aCursor = textCursor();
90 int numberOfCharsToComplete = theCompletion.length() -
91 myCompleter->completionPrefix().length();
92 int insertionPosition = aCursor.position();
93 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
95 aCursor.setPosition(insertionPosition);
96 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
97 myCompletedAndSelected = true;
99 setTextCursor(aCursor);
102 void ExpressionEditor::performCompletion()
104 QTextCursor aCursor = textCursor();
105 aCursor.select(QTextCursor::WordUnderCursor);
106 const QString aPrefix = aCursor.selectedText();
107 performCompletion(aPrefix);
110 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
113 if (theCompletionPrefix != myCompleter->completionPrefix()) {
114 myCompleter->setCompletionPrefix(theCompletionPrefix);
115 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
117 if (myCompleter->completionCount() == 1) {
118 insertCompletion(myCompleter->currentCompletion(), true);
120 QRect aRect = cursorRect();
121 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
122 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
123 myCompleter->complete(aRect);
127 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
129 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
131 myCompletedAndSelected = false;
132 if (myCompleter->popup()->isVisible()) {
133 switch (theEvent->key()) {
142 myCompleter->popup()->hide();
147 switch (theEvent->key()) {
150 emit keyReleased(this, theEvent);
151 // do not react to the Enter key, the property panel processes it
158 QPlainTextEdit::keyPressEvent(theEvent);
161 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
163 myCompletedAndSelected = false;
164 QTextCursor aCursor = textCursor();
165 switch (theEvent->key()) {
167 case Qt::Key_Return: aCursor.clearSelection(); break;
168 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
169 default: return false;
171 setTextCursor(aCursor);
176 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
178 myPlaceHolderText = thePlaceHolderText;
181 QString ExpressionEditor::placeHolderText() const
183 return myPlaceHolderText;
186 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
188 QPlainTextEdit::paintEvent( theEvent );
190 if( toPlainText().isEmpty() )
192 QPainter aPainter( viewport() );
193 QFontMetrics aFontMetrics = fontMetrics();
195 QPointF offset(contentOffset());
197 int m = (int)document()->documentMargin();
198 QRect lineRect( r.x() + m + offset.x(), offset.y(),
199 r.width() - 2*m, aFontMetrics.height() );
201 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
202 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
204 QColor aColor = palette().text().color();
205 aColor.setAlpha( 128 );
206 QPen anOldpen = aPainter.pen();
207 aPainter.setPen( aColor );
208 lineRect.adjust(minLB, 0, 0, 0);
210 aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
211 aPainter.drawText( lineRect, va, elidedText );
212 aPainter.setPen( anOldpen );
216 void ExpressionEditor::onTextChanged()
218 emit valueModified();
222 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
223 const Config_WidgetAPI* theData,
224 const std::string& thePlaceHolder )
225 : ModuleBase_ModelWidget(theParent, theData)
227 QVBoxLayout* aMainLay = new QVBoxLayout(this);
228 ModuleBase_Tools::adjustMargins(aMainLay);
230 myResultLabel = new QLabel(this);
231 myResultLabel->setWordWrap(true);
232 QFontMetrics fm(myResultLabel->font());
233 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
234 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
235 aMainLay->addWidget(myResultLabel);
236 myEditor = new ExpressionEditor(this);
237 myEditor->setMinimumHeight(20);
238 myEditor->setPlaceHolderText( translate( thePlaceHolder ) );
239 aMainLay->addWidget(myEditor);
240 this->setLayout(aMainLay);
242 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
243 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
244 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
247 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
251 void ModuleBase_WidgetExprEditor::activateCustom()
253 ModuleBase_ModelWidget::activateCustom();
255 QStringList aParameters;
256 ModuleBase_Tools::getParameters(aParameters);
257 myEditor->setCompletionList(aParameters);
260 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
264 bool ModuleBase_WidgetExprEditor::storeValueCustom()
266 // A rare case when plugin was not loaded.
269 DataPtr aData = myFeature->data();
270 AttributeStringPtr aStringAttr = aData->string(attributeID());
272 QString aWidgetValue = myEditor->toPlainText();
273 aStringAttr->setValue(aWidgetValue.toStdWString());
274 updateObject(myFeature);
276 // Try to get the value
278 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
279 if (anErrorMessage.empty()) {
280 ResultParameterPtr aParam =
281 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
283 AttributeDoublePtr aValueAttr =
284 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
285 if (aValueAttr.get()) {
286 double aValue = aValueAttr->value();
287 aStateMsg = "Result: " + QString::number(aValue);
291 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
293 myResultLabel->setText(aStateMsg);
297 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
299 // A rare case when plugin was not loaded.
302 DataPtr aData = myFeature->data();
303 AttributeStringPtr aStringAttr = aData->string(attributeID());
305 bool isBlocked = myEditor->blockSignals(true);
306 QTextCursor aCursor = myEditor->textCursor();
307 int pos = aCursor.position();
308 QString aRestoredStr;
309 if (aStringAttr->isUValue())
310 aRestoredStr = QString::fromStdWString(Locale::Convert::toWString(aStringAttr->valueU()));
312 aRestoredStr = QString::fromStdString(aStringAttr->value());
313 myEditor->setPlainText(aRestoredStr);
314 aCursor.setPosition(pos);
315 myEditor->setTextCursor(aCursor);
316 myEditor->blockSignals(isBlocked);
321 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
323 QList<QWidget*> result;
328 bool ModuleBase_WidgetExprEditor::processEnter()
330 bool isModified = getValueState() == ModifiedInPP;
332 emit valuesChanged();
333 myEditor->selectAll();
338 void ModuleBase_WidgetExprEditor::onTextChanged()
340 emit valuesChanged();