Salome HOME
Update copyrights
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_WidgetParamsMgr.cpp
index 7ab7fbf7397719ec3d2415bcbae757fa19af5fe2..a08b1572bd5d343b85e313cd1fdb33c99c4cec64 100644 (file)
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:        ParametersPlugin_WidgetParamsMgr.cpp
-// Created:     11 Apr 2016
-// Author:      Vitaly SMETANNIKOV
+// 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 "ParametersPlugin_WidgetParamsMgr.h"
 #include "ParametersPlugin_Parameter.h"
@@ -18,6 +31,7 @@
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_Expression.h>
 
 #include <ModuleBase_Tools.h>
 
@@ -54,23 +68,23 @@ class ParametersPlugin_ItemDelegate : public QStyledItemDelegate
 public:
   /// Constructor
   /// \param thaParent a parent
-  ParametersPlugin_ItemDelegate(QObject* thaParent) : 
+  ParametersPlugin_ItemDelegate(QObject* thaParent) :
       QStyledItemDelegate(thaParent) {}
 
   /// Redefinition of virtual method
   /// \param painter a painter object
   /// \param option the item options
   /// \param index the current index
-  virtual void paint(QPainter* painter, 
-    const QStyleOptionViewItem& option, 
+  virtual void paint(QPainter* painter,
+    const QStyleOptionViewItem& option,
     const QModelIndex& index ) const;
-  
+
   /// Redefinition of virtual method
   /// \param parent a parent widget
   /// \param option the item options
   /// \param index the current index
-  virtual QWidget* createEditor(QWidget* parent, 
-                                const QStyleOptionViewItem& option, 
+  virtual QWidget* createEditor(QWidget* parent,
+                                const QStyleOptionViewItem& option,
                                 const QModelIndex& index) const;
 
   /// Returns True if the given index is editable item
@@ -95,13 +109,14 @@ bool ParametersPlugin_ItemDelegate::isEditable(const QModelIndex& theIndex) cons
   return true;
 }
 
-void ParametersPlugin_ItemDelegate::paint(QPainter* painter, 
-                                          const QStyleOptionViewItem& option, 
+void ParametersPlugin_ItemDelegate::paint(QPainter* painter,
+                                          const QStyleOptionViewItem& option,
                                           const QModelIndex& index ) const
 {
   QBrush aBrush = painter->brush();
   QPen aPen = painter->pen();
-//  if (!isEditable(index))
+  //if (!isEditable(index))
+  //  painter->setBrush(Qt::lightGray);
   if (!index.parent().isValid())
     painter->setBrush(Qt::lightGray);
 
@@ -113,8 +128,8 @@ void ParametersPlugin_ItemDelegate::paint(QPainter* painter,
   painter->setBrush(aBrush);
 }
 
-QWidget* ParametersPlugin_ItemDelegate::createEditor(QWidget* parent, 
-                                                     const QStyleOptionViewItem& option, 
+QWidget* ParametersPlugin_ItemDelegate::createEditor(QWidget* parent,
+                                                     const QStyleOptionViewItem& option,
                                                      const QModelIndex& index) const
 {
   myEditingIdx = index;
@@ -122,7 +137,8 @@ QWidget* ParametersPlugin_ItemDelegate::createEditor(QWidget* parent,
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
-void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor, QAbstractItemDelegate::EndEditHint theHint)
+void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor,
+                                              QAbstractItemDelegate::EndEditHint theHint)
 {
   if (theHint == QAbstractItemDelegate::EditNextItem) {
     QModelIndex aCurrent = currentIndex();
@@ -157,15 +173,17 @@ void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor, QAbstractItemD
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
-ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent, const Config_WidgetAPI* theData)
-  : ModuleBase_ModelDialogWidget(theParent, theData)
+ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent,
+  const Config_WidgetAPI* theData)
+  : ModuleBase_ModelDialogWidget(theParent, theData),
+  isUpplyBlocked(false)
 {
   QVBoxLayout* aLayout = new QVBoxLayout(this);
 
   myTable = new ParametersPlugin_TreeWidget(this);
   myTable->setColumnCount(4);
   QStringList aHeaders;
-  aHeaders << tr("Name") << tr("Equation") << tr("Result") << tr("Comment");
+  aHeaders << tr("Name") << tr("Expression") << tr("Result") << tr("Comment");
   myTable->setHeaderLabels(aHeaders);
   myTable->setColumnWidth(Col_Name, 200);
   myTable->setColumnWidth(Col_Equation, 100);
@@ -180,7 +198,7 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP
   connect(myTable, SIGNAL(itemSelectionChanged()), SLOT(onSelectionChanged()));
 
   myDelegate = new ParametersPlugin_ItemDelegate(myTable);
-  connect(myDelegate, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), 
+  connect(myDelegate, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)),
           SLOT(onCloseEditor(QWidget*, QAbstractItemDelegate::EndEditHint)));
 
   myTable->setItemDelegate(myDelegate);
@@ -230,6 +248,20 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP
   onSelectionChanged();
 }
 
+void ParametersPlugin_WidgetParamsMgr::setDialogButtons(QDialogButtonBox* theButtons)
+{
+  ModuleBase_ModelDialogWidget::setDialogButtons(theButtons);
+
+  QWidget* aBtnParentWgt = myOkCancelBtn->parentWidget();
+  QHBoxLayout* aBtnParentLayout = dynamic_cast<QHBoxLayout*>(aBtnParentWgt->layout());
+
+  QPushButton* aPreviewBtn = new QPushButton(tr("See preview"), aBtnParentWgt);
+  aBtnParentLayout->insertWidget(0, aPreviewBtn);
+  aBtnParentLayout->insertStretch(1, 1);
+  connect(aPreviewBtn, SIGNAL(clicked(bool)), SLOT(onShowPreview()));
+}
+
+
 QList<QWidget*> ParametersPlugin_WidgetParamsMgr::getControls() const
 {
   QList<QWidget*> aList;
@@ -243,7 +275,7 @@ void ParametersPlugin_WidgetParamsMgr::selectItemScroll(QTreeWidgetItem* aItem)
   QModelIndex aParent = myTable->model()->index(0, 0);
   int aChildIdx = myParameters->indexOfChild(aItem);
   QModelIndex aIndex = myTable->model()->index(aChildIdx, Col_Name, aParent);
-  myTable->selectionModel()->select(aIndex, 
+  myTable->selectionModel()->select(aIndex,
     QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
   myTable->scrollToItem(aItem);
 }
@@ -265,6 +297,11 @@ bool ParametersPlugin_WidgetParamsMgr::storeValueCustom()
     }
     aId++;
   }
+  // #2813 : make the current feature the latest in the document
+  std::list<FeaturePtr> allFeatures = myFeature->document()->allFeatures();
+  if (!allFeatures.empty()) {
+    myFeature->document()->setCurrentFeature(*(allFeatures.rbegin()), true);
+  }
   return true;
 }
 
@@ -293,7 +330,7 @@ void ParametersPlugin_WidgetParamsMgr::updateParametersFeatures()
   int aNbFeatures = aDoc->numInternalFeatures();
   for (int i = 0; i < aNbFeatures; i++) {
     aParamFeature = aDoc->internalFeature(i);
-    if (aParamFeature->getKind() == ParametersPlugin_Parameter::ID()) {
+    if (aParamFeature && aParamFeature->getKind() == ParametersPlugin_Parameter::ID()) {
       myParametersList.append(aParamFeature);
     }
   }
@@ -325,7 +362,7 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
     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()) {
+      if (aReferenced.get() && (aReferenced != aParameter)) {
         if (aReferenced->getKind() == ParametersPlugin_Parameter::ID()) {
           // Find referenced feature Recursive
           QList<FeaturePtr> aList;
@@ -337,12 +374,14 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
             QStringList aValNames;
             aValNames << aReferenced->data()->name().c_str();
 
-            AttributeDoublePtr aDouble = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aAttr);
+            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);
+              AttributeIntegerPtr aInt =
+                std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aAttr);
               if (aInt.get()) {
                 aValNames << aInt->text().c_str();
                 aValNames << QString::number(aInt->value());
@@ -381,7 +420,8 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
     } else
       aValues << aExpr.c_str();
 
-    std::string aErr = aParameter->data()->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
+    std::string aErr =
+      aParameter->data()->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
     if (aErr.empty()) {
       AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
       aValues << QString::number(aValueAttribute->value());
@@ -403,7 +443,7 @@ void ParametersPlugin_WidgetParamsMgr::onDoubleClick(const QModelIndex& theIndex
   }
 }
 
-void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, 
+void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
                                                      QAbstractItemDelegate::EndEditHint theHint)
 {
   FeaturePtr aFeature = myParametersList.at(myDelegate->editIndex().row());
@@ -427,12 +467,13 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
         }
         aStringAttr->setValue(aText.toStdString());
         isModified = true;
-      } 
+      }
     }
     break;
   case Col_Equation:
     {
-      AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ID());
+      AttributeStringPtr aStringAttr =
+        aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ID());
       if (!aText.isEmpty()) {
         if (aText != aStringAttr->value().c_str()) {
           aStringAttr->setValue(aText.toStdString());
@@ -461,19 +502,19 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
   if (aColumn != Col_Comment)
     updateParametersPart();
   updateFeaturesPart();
-          
+
   onSelectionChanged();
 }
 
-void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem, 
+void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem,
                                                   const QList<QStringList>& theFeaturesList)
 {
   if (theFeaturesList.count() != theItem->childCount()) {
     if (theItem->childCount()  < theFeaturesList.count()) {
-      while (theItem->childCount() != theFeaturesList.count()) 
+      while (theItem->childCount() != theFeaturesList.count())
         theItem->addChild(createNewItem(theItem));
     } else {
-      while (theItem->childCount() != theFeaturesList.count()) 
+      while (theItem->childCount() != theFeaturesList.count())
         theItem->removeChild(theItem->child(theItem->childCount() - 1));
     }
   }
@@ -512,9 +553,10 @@ QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::createNewItem(QTreeWidgetItem
   aValues << NoValue;
 
   QTreeWidgetItem* aItem = new QTreeWidgetItem(aValues);
-  if (theParent == myParameters)
+  if (theParent == myParameters) {
     aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
-  else 
+    aItem->setForeground(2, Qt::darkGray);
+  } else
     aItem->setFlags(Qt::NoItemFlags);
   return aItem;
 }
@@ -527,7 +569,7 @@ void ParametersPlugin_WidgetParamsMgr::onAdd()
     return;
 
   myParametersList.append(aFeature);
-  updateParametersPart();    
+  updateParametersPart();
 
   QTreeWidgetItem* aItem = myParameters->child(myParameters->childCount() - 1);
 
@@ -735,11 +777,11 @@ void ParametersPlugin_WidgetParamsMgr::onSelectionChanged()
 void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable)
 {
   myAddBtn->setEnabled(theEnable);
-  if (theEnable) 
+  if (theEnable)
     onSelectionChanged();
   else {
     myInsertBtn->setEnabled(theEnable);
-    myRemoveBtn->setEnabled(theEnable);
+    //myRemoveBtn->setEnabled(theEnable);
     myUpBtn->setEnabled(theEnable);
     myDownBtn->setEnabled(theEnable);
   }
@@ -749,14 +791,37 @@ void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable)
 bool ParametersPlugin_WidgetParamsMgr::isValid()
 {
   QTreeWidgetItem* aItem;
-  bool aIsValid = true;
   for(int i = 0; i < myParameters->childCount(); i++) {
     aItem = myParameters->child(i);
-    if ((aItem->text(Col_Name) == NoName) || (aItem->text(Col_Equation) == NoValue)) {
-      aIsValid = false;
-      break;
+    if ((aItem->text(Col_Name) == NoName) ||
+        (aItem->text(Col_Equation) == NoValue) ||
+        (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ) {
+      return false;
     }
   }
-  return aIsValid;
+  return true;
+}
+
+void ParametersPlugin_WidgetParamsMgr::showEvent(QShowEvent* theEvent)
+{
+  ModuleBase_ModelDialogWidget::showEvent(theEvent);
+  SessionPtr aMgr = ModelAPI_Session::get();
+  isUpplyBlocked = aMgr->isAutoUpdateBlocked();
+  aMgr->blockAutoUpdate(true);
+  Events_Loop* aLoop = Events_Loop::loop();
+  aLoop->flush(aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE));
 }
 
+void ParametersPlugin_WidgetParamsMgr::hideEvent(QHideEvent* theEvent)
+{
+  ModuleBase_ModelDialogWidget::hideEvent(theEvent);
+  SessionPtr aMgr = ModelAPI_Session::get();
+  aMgr->blockAutoUpdate(isUpplyBlocked);
+}
+
+void ParametersPlugin_WidgetParamsMgr::onShowPreview()
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  aMgr->blockAutoUpdate(false);
+  aMgr->blockAutoUpdate(true);
+}
\ No newline at end of file