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 performCompletion(aPrefix);
109 void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
112 if (theCompletionPrefix != myCompleter->completionPrefix()) {
113 myCompleter->setCompletionPrefix(theCompletionPrefix);
114 myCompleter->popup()->setCurrentIndex(myCompleter->completionModel()->index(0, 0));
116 if (myCompleter->completionCount() == 1) {
117 insertCompletion(myCompleter->currentCompletion(), true);
119 QRect aRect = cursorRect();
120 aRect.setWidth(myCompleter->popup()->sizeHintForColumn(0)
121 + myCompleter->popup()->verticalScrollBar()->sizeHint().width());
122 myCompleter->complete(aRect);
126 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
128 if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
130 myCompletedAndSelected = false;
131 if (myCompleter->popup()->isVisible()) {
132 switch (theEvent->key()) {
141 myCompleter->popup()->hide();
146 switch (theEvent->key()) {
149 emit keyReleased(this, theEvent);
150 // do not react to the Enter key, the property panel processes it
157 QPlainTextEdit::keyPressEvent(theEvent);
160 bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
162 myCompletedAndSelected = false;
163 QTextCursor aCursor = textCursor();
164 switch (theEvent->key()) {
166 case Qt::Key_Return: aCursor.clearSelection(); break;
167 case Qt::Key_Escape: aCursor.removeSelectedText(); break;
168 default: return false;
170 setTextCursor(aCursor);
175 void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
177 myPlaceHolderText = thePlaceHolderText;
180 QString ExpressionEditor::placeHolderText() const
182 return myPlaceHolderText;
185 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
187 QPlainTextEdit::paintEvent( theEvent );
189 if( toPlainText().isEmpty() )
191 QPainter aPainter( viewport() );
192 QFontMetrics aFontMetrics = fontMetrics();
194 QPointF offset(contentOffset());
196 int m = (int)document()->documentMargin();
197 QRect lineRect( r.x() + m + offset.x(), offset.y(),
198 r.width() - 2*m, aFontMetrics.height() );
200 Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
201 int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
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);
209 aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
210 aPainter.drawText( lineRect, va, elidedText );
211 aPainter.setPen( anOldpen );
215 void ExpressionEditor::onTextChanged()
217 emit valueModified();
221 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
222 const Config_WidgetAPI* theData,
223 const std::string& thePlaceHolder )
224 : ModuleBase_ModelWidget(theParent, theData)
226 QVBoxLayout* aMainLay = new QVBoxLayout(this);
227 ModuleBase_Tools::adjustMargins(aMainLay);
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);
241 connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
242 connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
243 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
246 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
250 void ModuleBase_WidgetExprEditor::activateCustom()
252 ModuleBase_ModelWidget::activateCustom();
254 QStringList aParameters;
255 ModuleBase_Tools::getParameters(aParameters);
256 myEditor->setCompletionList(aParameters);
259 void ModuleBase_WidgetExprEditor::initializeValueByActivate()
263 bool ModuleBase_WidgetExprEditor::storeValueCustom()
265 // A rare case when plugin was not loaded.
268 DataPtr aData = myFeature->data();
269 AttributeStringPtr aStringAttr = aData->string(attributeID());
271 QString aWidgetValue = myEditor->toPlainText();
272 aStringAttr->setValue(aWidgetValue.toStdString());
273 updateObject(myFeature);
275 // Try to get the value
277 std::string anErrorMessage = myFeature->string("ExpressionError")->value();
278 if (anErrorMessage.empty()) {
279 ResultParameterPtr aParam =
280 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
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);
290 aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
292 myResultLabel->setText(aStateMsg);
296 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
298 // A rare case when plugin was not loaded.
301 DataPtr aData = myFeature->data();
302 AttributeStringPtr aStringAttr = aData->string(attributeID());
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);
316 QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
318 QList<QWidget*> result;
323 bool ModuleBase_WidgetExprEditor::processEnter()
325 bool isModified = getValueState() == ModifiedInPP;
327 emit valuesChanged();
328 myEditor->selectAll();
333 void ModuleBase_WidgetExprEditor::onTextChanged()
335 emit valuesChanged();