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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 * ModuleBase_WidgetExprEditor.cpp
23 * Created on: Aug 28, 2014
27 #include <ModuleBase_WidgetExprEditor.h>
28 #include <ModuleBase_Tools.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Object.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_ResultParameter.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_AttributeDouble.h>
37 #include <Config_WidgetAPI.h>
39 #include <QVBoxLayout>
44 #include <QStringListModel>
49 #include <QFontMetrics>
52 #include <QAbstractItemView>
57 ExpressionEditor::ExpressionEditor(QWidget* theParent)
58 : QPlainTextEdit(theParent), myCompletedAndSelected(false)
60 myCompleter = new QCompleter(this);
61 myCompleter->setWidget(this);
62 myCompleter->setCompletionMode(QCompleter::PopupCompletion);
64 myCompleterModel = new QStringListModel(this);
65 myCompleter->setModel(myCompleterModel);
66 // Use sorted model to accelerate completion (QCompleter will use binary search)
67 myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
68 myCompleter->setCaseSensitivity(Qt::CaseInsensitive);
70 connect(myCompleter, SIGNAL(activated(const QString&)),
71 this, SLOT(insertCompletion(const QString&)));
72 (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
73 this, SLOT(performCompletion()));
75 connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
77 setTabChangesFocus(true);
80 ExpressionEditor::~ExpressionEditor()
85 void ExpressionEditor::setCompletionList(QStringList& theList)
88 theList.removeDuplicates();
89 myCompleterModel->setStringList(theList);
92 void ExpressionEditor::insertCompletion(const QString& theCompletion, bool isSingleWord)
94 QTextCursor aCursor = textCursor();
95 int numberOfCharsToComplete = theCompletion.length() -
96 myCompleter->completionPrefix().length();
97 int insertionPosition = aCursor.position();
98 aCursor.insertText(theCompletion.right(numberOfCharsToComplete));
100 aCursor.setPosition(insertionPosition);
101 aCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
102 myCompletedAndSelected = true;
104 setTextCursor(aCursor);
107 void ExpressionEditor::performCompletion()
109 QTextCursor aCursor = textCursor();
110 aCursor.select(QTextCursor::WordUnderCursor);
111 const QString aPrefix = aCursor.selectedText();
112 if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) {
113 performCompletion(aPrefix);
117 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
120 if (theCompletionPrefix != myCompleter->completionPrefix()) {
121 myCompleter->setCompletionPrefix(theCompletionPrefix);
122 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
124 if (myCompleter->completionCount() == 1) {
125 insertCompletion(myCompleter->currentCompletion(), true);
127 QRect aRect = cursorRect();
128 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
129 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
130 myCompleter->complete(aRect);
134 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
136 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
138 myCompletedAndSelected = false;
139 if (myCompleter->popup()->isVisible()) {
140 switch (theEvent->key()) {
149 myCompleter->popup()->hide();
154 switch (theEvent->key()) {
157 emit keyReleased(this, theEvent);
158 // do not react to the Enter key, the property panel processes it
165 QPlainTextEdit::keyPressEvent(theEvent);
168 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
170 myCompletedAndSelected = false;
171 QTextCursor aCursor = textCursor();
172 switch (theEvent->key()) {
174 case Qt::Key_Return: aCursor.clearSelection(); break;
175 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
176 default: return false;
178 setTextCursor(aCursor);
183 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
185 myPlaceHolderText = thePlaceHolderText;
188 QString ExpressionEditor::placeHolderText() const
190 return myPlaceHolderText;
193 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
195 QPlainTextEdit::paintEvent( theEvent );
197 if( toPlainText().isEmpty() )
199 QPainter aPainter( viewport() );
200 QFontMetrics aFontMetrics = fontMetrics();
202 QPointF offset(contentOffset());
204 int m = (int)document()->documentMargin();
205 QRect lineRect( r.x() + m + offset.x(), offset.y(),
206 r.width() - 2*m, aFontMetrics.height() );
208 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
209 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
211 QColor aColor = palette().text().color();
212 aColor.setAlpha( 128 );
213 QPen anOldpen = aPainter.pen();
214 aPainter.setPen( aColor );
215 lineRect.adjust(minLB, 0, 0, 0);
217 aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
218 aPainter.drawText( lineRect, va, elidedText );
219 aPainter.setPen( anOldpen );
223 void ExpressionEditor::onTextChanged()
225 emit valueModified();
229 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
230 const Config_WidgetAPI* theData,
231 const std::string& thePlaceHolder )
232 : ModuleBase_ModelWidget(theParent, theData)
234 QVBoxLayout* aMainLay = new QVBoxLayout(this);
235 ModuleBase_Tools::adjustMargins(aMainLay);
237 myResultLabel = new QLabel(this);
238 myResultLabel->setWordWrap(true);
239 QFontMetrics fm(myResultLabel->font());
240 myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
241 myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
242 aMainLay->addWidget(myResultLabel);
243 myEditor = new ExpressionEditor(this);
244 myEditor->setMinimumHeight(20);
245 myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
246 aMainLay->addWidget(myEditor);
247 this->setLayout(aMainLay);
249 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
250 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
251 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
254 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
258 void ModuleBase_WidgetExprEditor::activateCustom()
260 ModuleBase_ModelWidget::activateCustom();
262 QStringList aParameters;
263 ModuleBase_Tools::getParameters(aParameters);
264 myEditor->setCompletionList(aParameters);
267 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
271 bool ModuleBase_WidgetExprEditor::storeValueCustom()
273 // A rare case when plugin was not loaded.
276 DataPtr aData = myFeature->data();
277 AttributeStringPtr aStringAttr = aData->string(attributeID());
279 QString aWidgetValue = myEditor->toPlainText();
280 aStringAttr->setValue(aWidgetValue.toStdString());
281 updateObject(myFeature);
283 // Try to get the value
285 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
286 if (anErrorMessage.empty()) {
287 ResultParameterPtr aParam =
288 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
290 AttributeDoublePtr aValueAttr =
291 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
292 if (aValueAttr.get()) {
293 double aValue = aValueAttr->value();
294 aStateMsg = "Result: " + QString::number(aValue);
298 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
300 myResultLabel->setText(aStateMsg);
304 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
306 // A rare case when plugin was not loaded.
309 DataPtr aData = myFeature->data();
310 AttributeStringPtr aStringAttr = aData->string(attributeID());
312 bool isBlocked = myEditor->blockSignals(true);
313 QTextCursor aCursor = myEditor->textCursor();
314 int pos = aCursor.position();
315 std::string aRestoredStr = aStringAttr->value();
316 myEditor->setPlainText(QString::fromStdString(aRestoredStr));
317 aCursor.setPosition(pos);
318 myEditor->setTextCursor(aCursor);
319 myEditor->blockSignals(isBlocked);
324 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
326 QList<QWidget*> result;
331 bool ModuleBase_WidgetExprEditor::processEnter()
333 bool isModified = getValueState() == ModifiedInPP;
335 emit valuesChanged();
336 myEditor->selectAll();
341 void ModuleBase_WidgetExprEditor::onTextChanged()
343 emit valuesChanged();