1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetExprEditor.h>
22 #include <ModuleBase_Tools.h>
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>
31 #include <Config_WidgetAPI.h>
33 #include <QVBoxLayout>
38 #include <QStringListModel>
43 #include <QFontMetrics>
46 #include <QAbstractItemView>
51 ExpressionEditor::ExpressionEditor(QWidget* theParent)
52 : QPlainTextEdit(theParent), myCompletedAndSelected(false)
54 myCompleter = new QCompleter(this);
55 myCompleter->setWidget(this);
56 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
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);
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()));
69 connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
71 setTabChangesFocus(true);
74 ExpressionEditor::~ExpressionEditor()
79 void ExpressionEditor::setCompletionList(QStringList& theList)
82 theList.removeDuplicates();
83 myCompleterModel->setStringList(theList);
86 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
88 QTextCursor aCursor = textCursor();
89 int numberOfCharsToComplete = theCompletion.length() -
90 myCompleter->completionPrefix().length();
91 int insertionPosition = aCursor.position();
92 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
94 aCursor.setPosition(insertionPosition);
95 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
96 myCompletedAndSelected = true;
98 setTextCursor(aCursor);
101 void ExpressionEditor::performCompletion()
103 QTextCursor aCursor = textCursor();
104 aCursor.select(QTextCursor::WordUnderCursor);
105 const QString aPrefix = aCursor.selectedText();
106 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
107 performCompletion(aPrefix);
111 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
114 if (theCompletionPrefix != myCompleter->completionPrefix()) {
115 myCompleter->setCompletionPrefix(theCompletionPrefix);
116 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
118 if (myCompleter->completionCount() == 1) {
119 insertCompletion(myCompleter->currentCompletion(), true);
121 QRect aRect = cursorRect();
122 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
123 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
124 myCompleter->complete(aRect);
128 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
130 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
132 myCompletedAndSelected = false;
133 if (myCompleter->popup()->isVisible()) {
134 switch (theEvent->key()) {
143 myCompleter->popup()->hide();
148 switch (theEvent->key()) {
151 emit keyReleased(this, theEvent);
152 // do not react to the Enter key, the property panel processes it
159 QPlainTextEdit::keyPressEvent(theEvent);
162 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
164 myCompletedAndSelected = false;
165 QTextCursor aCursor = textCursor();
166 switch (theEvent->key()) {
168 case Qt::Key_Return: aCursor.clearSelection(); break;
169 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
170 default: return false;
172 setTextCursor(aCursor);
177 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
179 myPlaceHolderText = thePlaceHolderText;
182 QString ExpressionEditor::placeHolderText() const
184 return myPlaceHolderText;
187 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
189 QPlainTextEdit::paintEvent( theEvent );
191 if( toPlainText().isEmpty() )
193 QPainter aPainter( viewport() );
194 QFontMetrics aFontMetrics = fontMetrics();
196 QPointF offset(contentOffset());
198 int m = (int)document()->documentMargin();
199 QRect lineRect( r.x() + m + offset.x(), offset.y(),
200 r.width() - 2*m, aFontMetrics.height() );
202 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
203 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
205 QColor aColor = palette().text().color();
206 aColor.setAlpha( 128 );
207 QPen anOldpen = aPainter.pen();
208 aPainter.setPen( aColor );
209 lineRect.adjust(minLB, 0, 0, 0);
211 aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
212 aPainter.drawText( lineRect, va, elidedText );
213 aPainter.setPen( anOldpen );
217 void ExpressionEditor::onTextChanged()
219 emit valueModified();
223 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
224 const Config_WidgetAPI* theData,
225 const std::string& thePlaceHolder )
226 : ModuleBase_ModelWidget(theParent, theData)
228 QVBoxLayout* aMainLay = new QVBoxLayout(this);
229 ModuleBase_Tools::adjustMargins(aMainLay);
231 myResultLabel = new QLabel(this);
232 myResultLabel->setWordWrap(true);
233 QFontMetrics fm(myResultLabel->font());
234 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
235 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
236 aMainLay->addWidget(myResultLabel);
237 myEditor = new ExpressionEditor(this);
238 myEditor->setMinimumHeight(20);
239 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
240 aMainLay->addWidget(myEditor);
241 this->setLayout(aMainLay);
243 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
244 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
245 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
248 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
252 void ModuleBase_WidgetExprEditor::activateCustom()
254 ModuleBase_ModelWidget::activateCustom();
256 QStringList aParameters;
257 ModuleBase_Tools::getParameters(aParameters);
258 myEditor->setCompletionList(aParameters);
261 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
265 bool ModuleBase_WidgetExprEditor::storeValueCustom()
267 // A rare case when plugin was not loaded.
270 DataPtr aData = myFeature->data();
271 AttributeStringPtr aStringAttr = aData->string(attributeID());
273 QString aWidgetValue = myEditor->toPlainText();
274 aStringAttr->setValue(aWidgetValue.toStdString());
275 updateObject(myFeature);
277 // Try to get the value
279 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
280 if (anErrorMessage.empty()) {
281 ResultParameterPtr aParam =
282 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
284 AttributeDoublePtr aValueAttr =
285 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
286 if (aValueAttr.get()) {
287 double aValue = aValueAttr->value();
288 aStateMsg = "Result: " + QString::number(aValue);
292 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
294 myResultLabel->setText(aStateMsg);
298 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
300 // A rare case when plugin was not loaded.
303 DataPtr aData = myFeature->data();
304 AttributeStringPtr aStringAttr = aData->string(attributeID());
306 bool isBlocked = myEditor->blockSignals(true);
307 QTextCursor aCursor = myEditor->textCursor();
308 int pos = aCursor.position();
309 std::string aRestoredStr = aStringAttr->value();
310 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
311 aCursor.setPosition(pos);
312 myEditor->setTextCursor(aCursor);
313 myEditor->blockSignals(isBlocked);
318 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
320 QList<QWidget*> result;
325 bool ModuleBase_WidgetExprEditor::processEnter()
327 bool isModified = getValueState() == ModifiedInPP;
329 emit valuesChanged();
330 myEditor->selectAll();
335 void ModuleBase_WidgetExprEditor::onTextChanged()
337 emit valuesChanged();