From 78ec6f117638de12099c5675e9ace258c63de47a Mon Sep 17 00:00:00 2001 From: vsv Date: Mon, 18 Apr 2016 18:55:56 +0300 Subject: [PATCH] Issue #979: Manage parameters --- src/ModuleBase/ModuleBase_Dialog.cpp | 26 +- src/ModuleBase/ModuleBase_Dialog.h | 3 + .../ParametersPlugin_Parameter.cpp | 1 + .../ParametersPlugin_Parameter.h | 7 + .../ParametersPlugin_ParametersMgr.h | 4 + .../ParametersPlugin_WidgetParamsMgr.cpp | 236 +++++++++++++++--- .../ParametersPlugin_WidgetParamsMgr.h | 34 ++- src/XGUI/XGUI_Workshop.cpp | 4 +- 8 files changed, 274 insertions(+), 41 deletions(-) diff --git a/src/ModuleBase/ModuleBase_Dialog.cpp b/src/ModuleBase/ModuleBase_Dialog.cpp index db3799b3b..42a7d53c5 100644 --- a/src/ModuleBase/ModuleBase_Dialog.cpp +++ b/src/ModuleBase/ModuleBase_Dialog.cpp @@ -8,6 +8,8 @@ #include "ModuleBase_PageWidget.h" #include +#include +#include #include #include @@ -20,7 +22,8 @@ ModuleBase_Dialog::ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QStr QDialog(theParent->desktop()), myId(theId), myDescription(theDescription), - myWorkshop(theParent) + myWorkshop(theParent), + myActiveWidget(0) { ModuleBase_WidgetFactory aFactory(myDescription, myWorkshop); @@ -31,6 +34,8 @@ ModuleBase_Dialog::ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QStr myFeature = aDoc->addFeature(myId.toStdString()); if (!myFeature.get()) return; + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED)); + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); QVBoxLayout* aLayout = new QVBoxLayout(this); aLayout->setContentsMargins(0, 0, 0, 0); @@ -62,8 +67,6 @@ ModuleBase_Dialog::ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QStr connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept())); connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject())); - - } void ModuleBase_Dialog::initializeWidget(ModuleBase_ModelWidget* theWidget) @@ -74,6 +77,17 @@ void ModuleBase_Dialog::initializeWidget(ModuleBase_ModelWidget* theWidget) void ModuleBase_Dialog::showEvent(QShowEvent* theEvent) { QDialog::showEvent(theEvent); - if (myWidgets.length() > 0) - myWidgets.first()->activate(); -} \ No newline at end of file + if (myWidgets.length() > 0) { + myActiveWidget = myWidgets.first(); + myActiveWidget->activate(); + } +} + +void ModuleBase_Dialog::accept() +{ + if (myActiveWidget) { + if (!myActiveWidget->storeValue()) + return; + } + QDialog::accept(); +} diff --git a/src/ModuleBase/ModuleBase_Dialog.h b/src/ModuleBase/ModuleBase_Dialog.h index fa54ca256..6b3074de5 100644 --- a/src/ModuleBase/ModuleBase_Dialog.h +++ b/src/ModuleBase/ModuleBase_Dialog.h @@ -24,6 +24,8 @@ public: ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QString& theId, const std::string& theDescription); + virtual void accept(); + protected: virtual void showEvent(QShowEvent* theEvent); @@ -36,6 +38,7 @@ private: ModuleBase_IWorkshop* myWorkshop; FeaturePtr myFeature; QList myWidgets; + ModuleBase_ModelWidget* myActiveWidget; }; #endif diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 953bb6c40..949e14c85 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -34,6 +34,7 @@ void ParametersPlugin_Parameter::initAttributes() { data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(COMMENT_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId()); data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false); diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.h b/src/ParametersPlugin/ParametersPlugin_Parameter.h index 47a0d3072..50be185b2 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.h +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.h @@ -55,6 +55,13 @@ class ParametersPlugin_Parameter : public ModelAPI_Feature return MY_ARGUMENTS_ID; } + /// return comment attribute Id + inline static const std::string& COMMENT_ID() + { + static const std::string MY_COMMENT_ID("comment"); + return MY_COMMENT_ID; + } + /// Returns the kind of a feature PARAMETERSPLUGIN_EXPORT virtual const std::string& getKind() { diff --git a/src/ParametersPlugin/ParametersPlugin_ParametersMgr.h b/src/ParametersPlugin/ParametersPlugin_ParametersMgr.h index 374399a75..5c82367ad 100644 --- a/src/ParametersPlugin/ParametersPlugin_ParametersMgr.h +++ b/src/ParametersPlugin/ParametersPlugin_ParametersMgr.h @@ -50,6 +50,10 @@ public: /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false. PARAMETERSPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; } + + /// Returns true if result is persistent (stored in document) and on undo-redo, save-open + /// it is not needed to recompute it. + PARAMETERSPLUGIN_EXPORT virtual bool isPersistentResult() {return false;} }; diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp index d7bf5e111..b9f9720d0 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp @@ -11,6 +11,11 @@ #include #include #include +#include +#include +#include + +#include #include #include @@ -18,6 +23,8 @@ #include #include #include +#include +#include enum ColumnType { Col_Name, @@ -26,6 +33,9 @@ enum ColumnType { Col_Comment }; +const char* NoName = ""; +const char* NoValue = ""; + class ParametersPlugin_ItemDelegate : public QStyledItemDelegate { public: @@ -65,10 +75,15 @@ void ParametersPlugin_ItemDelegate::paint(QPainter* painter, painter->setPen(Qt::darkGray); painter->drawRect(option.rect); + painter->setPen(aPen); + //QString aText = index.data().toString(); + //if ((aText == NoName) || (aText == NoValue)) + // painter->setPen(Qt::red); + QStyledItemDelegate::paint(painter, option, index); - painter->setPen(aPen); + //painter->setPen(aPen); painter->setBrush(aBrush); } @@ -127,19 +142,27 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP QHBoxLayout* aBtnLayout = new QHBoxLayout(this); QToolButton* aUpBtn = new QToolButton(this); - aUpBtn->setArrowType(Qt::DownArrow); + aUpBtn->setArrowType(Qt::UpArrow); + connect(aUpBtn, SIGNAL(clicked(bool)), SLOT(onUp())); aBtnLayout->addWidget(aUpBtn); + QToolButton* aDownBtn = new QToolButton(this); - aDownBtn->setArrowType(Qt::UpArrow); + aDownBtn->setArrowType(Qt::DownArrow); + connect(aDownBtn, SIGNAL(clicked(bool)), SLOT(onDown())); aBtnLayout->addWidget(aDownBtn); aBtnLayout->addStretch(); QPushButton* aAddBtn = new QPushButton(tr("Add"), this); + connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd())); aBtnLayout->addWidget(aAddBtn); + QPushButton* aInsertBtn = new QPushButton(tr("Insert"), this); + connect(aInsertBtn, SIGNAL(clicked(bool)), SLOT(onInsert())); aBtnLayout->addWidget(aInsertBtn); + QPushButton* aRemoveBtn = new QPushButton(tr("Remove"), this); + connect(aRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemove())); aBtnLayout->addWidget(aRemoveBtn); aLayout->addLayout(aBtnLayout); @@ -152,8 +175,23 @@ QList ParametersPlugin_WidgetParamsMgr::getControls() const return aList; } -bool ParametersPlugin_WidgetParamsMgr::storeValueCustom() const +bool ParametersPlugin_WidgetParamsMgr::storeValueCustom() { + for(int i = 0; i < myParameters->childCount(); i++) { + QTreeWidgetItem* aItem = myParameters->child(i); + if ((aItem->text(Col_Name) == NoName) || (aItem->text(Col_Equation) == NoValue)) { + QMessageBox::warning(this, tr("Warning"), tr("Created parameter is not defined properly.")); + + // The index of myParameters item + QModelIndex aParent = myTable->model()->index(0, 0); + int aRow = myParameters->indexOfChild(aItem); + QModelIndex aIndex = myTable->model()->index(aRow, Col_Name, aParent); + myTable->selectionModel()->select(aIndex, + QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); + myTable->scrollToItem(aItem); + return false; + } + } return true; } @@ -175,6 +213,7 @@ void ParametersPlugin_WidgetParamsMgr::activateCustom() aObj = aDoc->object(ModelAPI_ResultParameter::group(), i); aParam = std::dynamic_pointer_cast(aObj); if (aParam.get()) { + // Set parameter feature aParamFeature = ModelAPI_Feature::feature(aParam); QStringList aValues; @@ -184,30 +223,36 @@ void ParametersPlugin_WidgetParamsMgr::activateCustom() AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); aValues << QString::number(aValueAttribute->value()); - //AttributeRefListPtr aParams = aParamFeature->reflist(ParametersPlugin_Parameter::ARGUMENTS_ID()); - //std::list aList = aParams->list(); - //std::string aName; - //std::list::iterator aIt; - //for(aIt = aList.begin(); aIt != aList.end(); aIt++) { - // aObj = (*aIt); - // aName = aObj->data()->name(); - //} + aValues << aParamFeature->string(ParametersPlugin_Parameter::COMMENT_ID())->value().c_str(); + aItem = new QTreeWidgetItem(aValues); aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); myParameters->addChild(aItem); - myFeatureList.append(aParamFeature); + myParametersList.append(aParamFeature); + // Set features where the parameter is used const std::set>& aRefs = aParam->data()->refsToMe(); std::set >::const_iterator aIt; for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) { - FeaturePtr aReferenced = std::dynamic_pointer_cast((*aIt)->owner()); + std::shared_ptr aAttr = (*aIt); + FeaturePtr aReferenced = std::dynamic_pointer_cast(aAttr->owner()); if (aReferenced.get()) { QStringList aValNames; aValNames << aReferenced->data()->name().c_str(); - //AttributeDoublePtr aValue = aReferenced->data()->real(SketchPlugin_Constraint::VALUE()); - //aReferenced + AttributeDoublePtr aDouble = std::dynamic_pointer_cast(aAttr); + if (aDouble.get()) { + aValNames << aDouble->text().c_str(); + aValNames << QString::number(aDouble->value()); + } else { + AttributeIntegerPtr aInt = std::dynamic_pointer_cast(aAttr); + if (aInt.get()) { + aValNames << aInt->text().c_str(); + aValNames << QString::number(aInt->value()); + } + } + aItem = new QTreeWidgetItem(aValNames); myFeatures->addChild(aItem); } @@ -230,22 +275,153 @@ void ParametersPlugin_WidgetParamsMgr::onDoubleClick(const QModelIndex& theIndex void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, QAbstractItemDelegate::EndEditHint theHint) { - if (myEditingIndex.column() == Col_Equation) { - QTreeWidgetItem* aItem = myParameters->child(myEditingIndex.row()); - QString aText = aItem->text(myEditingIndex.column()); - if (!aText.isEmpty()) { - FeaturePtr aFeature = myFeatureList.at(myEditingIndex.row()); + FeaturePtr aFeature = myParametersList.at(myEditingIndex.row()); + QTreeWidgetItem* aItem = myParameters->child(myEditingIndex.row()); + int aColumn = myEditingIndex.column(); + QString aText = aItem->text(aColumn); + bool isModified = false; + + switch (aColumn) { + case Col_Name: + { + AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::VARIABLE_ID()); + if (!aText.isEmpty()) { + if (hasName(aText)) { + myMessage = tr("Name %1 already exists.").arg(aText); + aItem->setText(Col_Name, aStringAttr->value().c_str()); + QTimer::singleShot(50, this, SLOT(sendWarning())); + return; + } + aStringAttr->setValue(aText.toStdString()); + isModified = true; + } else { + aItem->setText(Col_Name, aStringAttr->value().c_str()); + } + } + break; + case Col_Equation: + { AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ID()); - aStringAttr->setValue(aText.toStdString()); - aFeature->execute(); - ResultParameterPtr aResult = - std::dynamic_pointer_cast(aFeature->firstResult()); - if (aResult.get()) { - AttributeDoublePtr aValueAttribute = - aResult->data()->real(ModelAPI_ResultParameter::VALUE()); - aItem->setText(Col_Result, QString::number(aValueAttribute->value())); + if (!aText.isEmpty()) { + if (aText != aStringAttr->value().c_str()) { + aStringAttr->setValue(aText.toStdString()); + isModified = true; + } + } else { + aItem->setText(Col_Equation, aStringAttr->value().c_str()); } } + break; + case Col_Comment: + { + AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::COMMENT_ID()); + aStringAttr->setValue(aText.toStdString()); + isModified = true; + } + break; + } + + if (!isModified) + return; + Events_Loop* aLoop = Events_Loop::loop(); + aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); + + ResultParameterPtr aResult = + std::dynamic_pointer_cast(aFeature->firstResult()); + if (aResult.get()) { + AttributeDoublePtr aValueAttribute = + aResult->data()->real(ModelAPI_ResultParameter::VALUE()); + aItem->setText(Col_Result, QString::number(aValueAttribute->value())); } myEditingIndex = QModelIndex(); -} \ No newline at end of file + updateFeaturesPart(); +} + +void ParametersPlugin_WidgetParamsMgr::updateFeaturesPart() +{ + std::shared_ptr aParam; + int i = 0; + foreach(FeaturePtr aFeature, myParametersList) { + aParam = std::dynamic_pointer_cast(aFeature->firstResult()); + const std::set>& aRefs = aParam->data()->refsToMe(); + std::set >::const_iterator aIt; + for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) { + std::shared_ptr aAttr = (*aIt); + FeaturePtr aReferenced = std::dynamic_pointer_cast(aAttr->owner()); + if (aReferenced.get()) { + QStringList aValNames; + aValNames << aReferenced->data()->name().c_str(); + + AttributeDoublePtr aDouble = std::dynamic_pointer_cast(aAttr); + if (aDouble.get()) { + aValNames << aDouble->text().c_str(); + aValNames << QString::number(aDouble->value()); + } else { + AttributeIntegerPtr aInt = std::dynamic_pointer_cast(aAttr); + if (aInt.get()) { + aValNames << aInt->text().c_str(); + aValNames << QString::number(aInt->value()); + } + } + + QTreeWidgetItem* aItem = myFeatures->child(i++); + for(int i = 0; i < aValNames.count(); i++) + aItem->setText(i, aValNames.at(i)); + } + } + } +} + +void ParametersPlugin_WidgetParamsMgr::onAdd() +{ + SessionPtr aMgr = ModelAPI_Session::get(); + std::shared_ptr aDoc = aMgr->activeDocument(); + + FeaturePtr aFeature = aDoc->addFeature(ParametersPlugin_Parameter::ID()); + if (!aFeature.get()) + return; + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED)); + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); + + QStringList aValues; + aValues << NoName; + aValues << NoValue; + + QTreeWidgetItem* aItem = new QTreeWidgetItem(aValues); + aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); + myParameters->addChild(aItem); + myParametersList.append(aFeature); + + myTable->scrollToItem(aItem); +} + +void ParametersPlugin_WidgetParamsMgr::onInsert() +{ +} + +void ParametersPlugin_WidgetParamsMgr::onRemove() +{ +} + +void ParametersPlugin_WidgetParamsMgr::onUp() +{ +} + +void ParametersPlugin_WidgetParamsMgr::onDown() +{ +} + + +bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const +{ + foreach(FeaturePtr aFeature, myParametersList) { + if (aFeature->data()->name() == theName.toStdString()) + return true; + } + return false; +} + +void ParametersPlugin_WidgetParamsMgr::sendWarning() +{ + QMessageBox::warning(this, tr("Warning"), myMessage); +} diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h index aba839b9f..9c574c09d 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h @@ -36,7 +36,7 @@ public: protected: /// Saves the internal parameters to the given feature /// \return True in success - virtual bool storeValueCustom() const; + virtual bool storeValueCustom(); /// Restore value from attribute data to the widget's control virtual bool restoreValueCustom(); @@ -45,10 +45,36 @@ protected: virtual void activateCustom(); private slots: - void onDoubleClick(const QModelIndex&); + /// Slot for reaction on double click in the table (start editing) + /// \param theIndex the clicked index + void onDoubleClick(const QModelIndex& theIndex); + + /// Slot for reaction on end of cell editing + /// \param theEditor the editor widget + /// \param theHint end of edit type void onCloseEditor(QWidget* theEditor, QAbstractItemDelegate::EndEditHint theHint); + /// Slot for reaction on add parameter + void onAdd(); + + /// Slot for reaction on insert parameter + void onInsert(); + + /// Slot for reaction on remove parameter + void onRemove(); + + /// Slot for reaction on shift up + void onUp(); + + /// Slot for reaction on shift down + void onDown(); + + void sendWarning(); + private: + void updateFeaturesPart(); + + bool hasName(const QString& theName) const; QTreeWidget* myTable; QTreeWidgetItem* myFeatures; @@ -56,7 +82,9 @@ private: ParametersPlugin_ItemDelegate* myDelegate; QModelIndex myEditingIndex; - QList myFeatureList; + QList myParametersList; + + QString myMessage; }; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 540991d64..e6dfc4bc0 100755 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -153,8 +153,8 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector) SLOT(onContextMenuCommand(const QString&, bool))); myViewerProxy = new XGUI_ViewerProxy(this); - connect(myViewerProxy, SIGNAL(selectionChanged()), - myActionsMgr, SLOT(updateOnViewSelection())); + //connect(myViewerProxy, SIGNAL(selectionChanged()), + // myActionsMgr, SLOT(updateOnViewSelection())); myModuleConnector = new XGUI_ModuleConnector(this); -- 2.39.2