#include <ModuleBase_WidgetExprEditor.h>
#include <ModuleBase_Tools.h>
-#include <ModelAPI_AttributeString.h>
#include <ModelAPI_Data.h>
#include <ModelAPI_Object.h>
#include <ModelAPI_Validator.h>
#include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeDouble.h>
#include <Config_WidgetAPI.h>
#include <QSize>
#include <QShortcut>
#include <QScrollBar>
+#include <QFontMetrics>
+#include <QPainter>
#include <memory>
#include <string>
ExpressionEditor::ExpressionEditor(QWidget* theParent)
-: QPlainTextEdit(theParent)
+: QPlainTextEdit(theParent), myCompletedAndSelected(false)
{
myCompleter = new QCompleter(this);
myCompleter->setWidget(this);
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()
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);
}
return true;
}
-ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
- const Config_WidgetAPI* theData,
- const std::string& 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& theParentId,
+ const std::string& thePlaceHolder )
: ModuleBase_ModelWidget(theParent, theData, theParentId)
{
QVBoxLayout* aMainLay = new QVBoxLayout(this);
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()
{
}
+void ModuleBase_WidgetExprEditor::initializeValueByActivate()
+{
+}
+
bool ModuleBase_WidgetExprEditor::storeValueCustom() const
{
// A rare case when plugin was not loaded.
return false;
DataPtr aData = myFeature->data();
AttributeStringPtr aStringAttr = aData->string(attributeID());
+
QString aWidgetValue = myEditor->toPlainText();
aStringAttr->setValue(aWidgetValue.toStdString());
updateObject(myFeature);
- if(!myFeature->firstResult().get())
- return true;
-
- ResultParameterPtr aResult =
+ // Try to get the value
+ QString aStateMsg;
+ std::string anErrorMessage = myFeature->string("ExpressionError")->value();
+ if (anErrorMessage.empty()) {
+ ResultParameterPtr aParam =
std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
- AttributeStringPtr aErrorAttr = aResult->data()->string(ModelAPI_ResultParameter::STATE());
- myResultLabel->setText(QString::fromStdString(aErrorAttr->value()));
+ if(aParam.get()) {
+ AttributeDoublePtr aValueAttr =
+ aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+ if (aValueAttr.get()) {
+ double aValue = aValueAttr->value();
+ aStateMsg = "Result: " + QString::number(aValue);
+ }
+ }
+ } else {
+ 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.
if(!myFeature)
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();
}