]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Issue #979: Manage parameters
authorvsv <vitaly.smetannikov@opencascade.com>
Mon, 18 Apr 2016 15:55:56 +0000 (18:55 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Tue, 19 Apr 2016 07:51:54 +0000 (10:51 +0300)
src/ModuleBase/ModuleBase_Dialog.cpp
src/ModuleBase/ModuleBase_Dialog.h
src/ParametersPlugin/ParametersPlugin_Parameter.cpp
src/ParametersPlugin/ParametersPlugin_Parameter.h
src/ParametersPlugin/ParametersPlugin_ParametersMgr.h
src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp
src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h
src/XGUI/XGUI_Workshop.cpp

index db3799b3b4e50c0df8c7262a7019669d443b11f6..42a7d53c5a02f49ea8ae9953ec0cd00bc6d8f4b3 100644 (file)
@@ -8,6 +8,8 @@
 #include "ModuleBase_PageWidget.h"
 
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Events.h>
+#include <Events_Loop.h>
 
 #include <QMainWindow>
 #include <QLayout>
@@ -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();
+}
index fa54ca256bc89062898605b4a630dc577cedf3d0..6b3074de5605d9fffa23d70461731ceafda3efab 100644 (file)
@@ -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<ModuleBase_ModelWidget*> myWidgets;
+  ModuleBase_ModelWidget* myActiveWidget;
 };
 
 #endif
index 953bb6c4053e60f01609d4ca1f2dd25e2383f3f5..949e14c8568b94480db1d7602a3157fb4f381fac 100644 (file)
@@ -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);
index 47a0d30721b693f78600e3be5bfc059212f4a92f..50be185b2fa2796f826e3776b12a4a6afb91f092 100644 (file)
@@ -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()
   {
index 374399a753db951f12d5e56d21e5a788180eb0f4..5c82367ad6367b560232e2610e36b5a5e4b27d46 100644 (file)
@@ -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;}
 };
 
 
index d7bf5e1112d57772b75e8dff7fdd01ddf0b2c0e4..b9f9720d02b2f0581b1808d208e20a049b73e50a 100644 (file)
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Session.h>
+
+#include <Events_Loop.h>
 
 #include <QLayout>
 #include <QTreeWidget>
@@ -18,6 +23,8 @@
 #include <QToolButton>
 #include <QStyledItemDelegate>
 #include <QPainter>
+#include <QMessageBox>
+#include <QTimer>
 
 enum ColumnType {
   Col_Name,
@@ -26,6 +33,9 @@ enum ColumnType {
   Col_Comment
 };
 
+const char* NoName = "<NoName>";
+const char* NoValue = "<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<QWidget*> 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<ModelAPI_ResultParameter>(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<ObjectPtr> aList = aParams->list();
-      //std::string aName;
-      //std::list<ObjectPtr>::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<std::shared_ptr<ModelAPI_Attribute>>& aRefs = aParam->data()->refsToMe();
       std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aIt;
       for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) {
-        FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aIt)->owner());
+        std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
+        FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(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<ModelAPI_AttributeDouble>(aAttr);
+          if (aDouble.get()) {
+            aValNames << aDouble->text().c_str();
+            aValNames << QString::number(aDouble->value());
+          } else {
+            AttributeIntegerPtr aInt = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(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<ModelAPI_ResultParameter>(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<ModelAPI_ResultParameter>(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<ModelAPI_ResultParameter> aParam;
+  int i = 0;
+  foreach(FeaturePtr aFeature, myParametersList) {
+    aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
+    const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefs = aParam->data()->refsToMe();
+    std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aIt;
+    for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) {
+      std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
+      FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
+      if (aReferenced.get()) {
+        QStringList aValNames;
+        aValNames << aReferenced->data()->name().c_str();
+
+        AttributeDoublePtr aDouble = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aAttr);
+        if (aDouble.get()) {
+          aValNames << aDouble->text().c_str();
+          aValNames << QString::number(aDouble->value());
+        } else {
+          AttributeIntegerPtr aInt = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(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<ModelAPI_Document> 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);
+}
index aba839b9ff68d673d3c350ba878899b7d1a7c39e..9c574c09ddb26030efb48000c45475e33d3c6813 100644 (file)
@@ -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<FeaturePtr> myFeatureList;
+  QList<FeaturePtr> myParametersList;
+
+  QString myMessage;
 };
 
 
index 540991d6464553772a00ac55dfad7e56a5841a61..e6dfc4bc047ab12a130dd7a1fa460a84cece11dd 100755 (executable)
@@ -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);