X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetExprEditor.cpp;h=51f982cb0528894d35960ab8e202b153c78f90ee;hb=5fea3f89cb0f16848b1217193c13fda0a596c754;hp=f96eec92c48a15a98066430cb477ef62d8873925;hpb=df901d4270af49bacb374dcb862596e2f88b0c0f;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp b/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp index f96eec92c..51f982cb0 100644 --- a/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp +++ b/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp @@ -1,11 +1,21 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -/* - * ModuleBase_WidgetExprEditor.cpp - * - * Created on: Aug 28, 2014 - * Author: sbh - */ +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include #include @@ -29,12 +39,16 @@ #include #include #include +#include +#include +#include +#include #include #include ExpressionEditor::ExpressionEditor(QWidget* theParent) -: QPlainTextEdit(theParent) +: QPlainTextEdit(theParent), myCompletedAndSelected(false) { myCompleter = new QCompleter(this); myCompleter->setWidget(this); @@ -50,6 +64,10 @@ ExpressionEditor::ExpressionEditor(QWidget* theParent) this, SLOT(insertCompletion(const QString&))); (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")), this, SLOT(performCompletion())); + + connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged())); + + setTabChangesFocus(true); } ExpressionEditor::~ExpressionEditor() @@ -84,9 +102,7 @@ void ExpressionEditor::performCompletion() QTextCursor aCursor = textCursor(); aCursor.select(QTextCursor::WordUnderCursor); const QString aPrefix = aCursor.selectedText(); - if (!aPrefix.isEmpty() && aPrefix.at(aPrefix.length() - 1).isLetter()) { - performCompletion(aPrefix); - } + performCompletion(aPrefix); } void ExpressionEditor::performCompletion(const QString& theCompletionPrefix) @@ -115,16 +131,28 @@ void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent) switch (theEvent->key()) { case Qt::Key_Up: case Qt::Key_Down: + case Qt::Key_Escape: case Qt::Key_Enter: case Qt::Key_Return: - case Qt::Key_Escape: theEvent->ignore(); - return; + return; default: myCompleter->popup()->hide(); break; } } + else { + switch (theEvent->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + emit keyReleased(this, theEvent); + // do not react to the Enter key, the property panel processes it + return; + break; + default: + break; + } + } QPlainTextEdit::keyPressEvent(theEvent); } @@ -143,43 +171,109 @@ bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent) return true; } -ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent, - const Config_WidgetAPI* theData, - const std::string& theParentId) - : ModuleBase_ModelWidget(theParent, theData, theParentId) +void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText ) +{ + myPlaceHolderText = thePlaceHolderText; +} + +QString ExpressionEditor::placeHolderText() const +{ + return myPlaceHolderText; +} + +void ExpressionEditor::paintEvent( QPaintEvent* theEvent ) +{ + QPlainTextEdit::paintEvent( theEvent ); + + if( toPlainText().isEmpty() ) + { + QPainter aPainter( viewport() ); + QFontMetrics aFontMetrics = fontMetrics(); + + QPointF offset(contentOffset()); + QRect r = rect(); + int m = (int)document()->documentMargin(); + QRect lineRect( r.x() + m + offset.x(), offset.y(), + r.width() - 2*m, aFontMetrics.height() ); + + Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft ); + int minLB = qMax( 0, -aFontMetrics.minLeftBearing() ); + + QColor aColor = palette().text().color(); + aColor.setAlpha( 128 ); + QPen anOldpen = aPainter.pen(); + aPainter.setPen( aColor ); + lineRect.adjust(minLB, 0, 0, 0); + QString elidedText = + aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() ); + aPainter.drawText( lineRect, va, elidedText ); + aPainter.setPen( anOldpen ); + } +} + +void ExpressionEditor::onTextChanged() +{ + emit valueModified(); +} + + +ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent, + const Config_WidgetAPI* theData, + const std::string& thePlaceHolder ) +: ModuleBase_ModelWidget(theParent, theData) { QVBoxLayout* aMainLay = new QVBoxLayout(this); ModuleBase_Tools::adjustMargins(aMainLay); myResultLabel = new QLabel(this); myResultLabel->setWordWrap(true); + QFontMetrics fm(myResultLabel->font()); + myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum + myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom); aMainLay->addWidget(myResultLabel); myEditor = new ExpressionEditor(this); myEditor->setMinimumHeight(20); + myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) ); aMainLay->addWidget(myEditor); this->setLayout(aMainLay); - connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged())); + connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified())); + connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)), + this, SIGNAL(keyReleased(QObject*, QKeyEvent*))); } ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor() { } -bool ModuleBase_WidgetExprEditor::storeValueCustom() const +void ModuleBase_WidgetExprEditor::activateCustom() +{ + ModuleBase_ModelWidget::activateCustom(); + + QStringList aParameters; + ModuleBase_Tools::getParameters(aParameters); + myEditor->setCompletionList(aParameters); +} + +void ModuleBase_WidgetExprEditor::initializeValueByActivate() +{ +} + +bool ModuleBase_WidgetExprEditor::storeValueCustom() { - // A rare case when plugin was not loaded. + // A rare case when plugin was not loaded. if(!myFeature) return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); + QString aWidgetValue = myEditor->toPlainText(); aStringAttr->setValue(aWidgetValue.toStdString()); updateObject(myFeature); // Try to get the value QString aStateMsg; - std::string anErrorMessage = myFeature->error(); + std::string anErrorMessage = myFeature->string("ExpressionError")->value(); if (anErrorMessage.empty()) { ResultParameterPtr aParam = std::dynamic_pointer_cast(myFeature->firstResult()); @@ -192,15 +286,15 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const } } } else { - aStateMsg = QString::fromStdString(anErrorMessage); + aStateMsg = "Error: " + QString::fromStdString(anErrorMessage); } myResultLabel->setText(aStateMsg); return true; } -bool ModuleBase_WidgetExprEditor::restoreValue() +bool ModuleBase_WidgetExprEditor::restoreValueCustom() { - // A rare case when plugin was not loaded. + // A rare case when plugin was not loaded. if(!myFeature) return false; DataPtr aData = myFeature->data(); @@ -225,7 +319,17 @@ QList ModuleBase_WidgetExprEditor::getControls() const return result; } +bool ModuleBase_WidgetExprEditor::processEnter() +{ + bool isModified = getValueState() == ModifiedInPP; + if (isModified) { + emit valuesChanged(); + myEditor->selectAll(); + } + return isModified; +} + void ModuleBase_WidgetExprEditor::onTextChanged() { - storeValue(); + emit valuesChanged(); }