Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_WidgetParamsMgr.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ParametersPlugin_WidgetParamsMgr.h"
21 #include "ParametersPlugin_Parameter.h"
22 #include "ParametersPlugin_Validators.h"
23
24 #include <Events_InfoMessage.h>
25
26 #include <ModelAPI_ResultParameter.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_Expression.h>
35
36 #include <GeomDataAPI_Point.h>
37 #include <GeomDataAPI_Point2D.h>
38
39 #include <ModuleBase_Tools.h>
40 #include <ModuleBase_IWorkshop.h>
41 #include <ModuleBase_IViewer.h>
42
43 #include <Events_Loop.h>
44 #include <Config_PropManager.h>
45
46 #include <QLayout>
47 #include <QPushButton>
48 #include <QToolButton>
49 #include <QStyledItemDelegate>
50 #include <QPainter>
51 #include <QMessageBox>
52 #include <QTimer>
53 #include <QEvent>
54 #include <QKeyEvent>
55 #include <QDialogButtonBox>
56 #include <QShortcut>
57 #include <QFileDialog>
58
59 #ifdef WIN32
60 #pragma warning(disable : 4456) // for nested foreach
61 #endif
62
63 enum ColumnType {
64   Col_Name,
65   Col_Equation,
66   Col_Result,
67   Col_Comment
68 };
69
70 const char* NoName = "<NoName>";
71 const char* NoValue = "<NoValue>";
72
73 /*!
74  * \ingroup GUI
75  * ItemDelegate object in order to redefine items behavior
76  */
77 class ParametersPlugin_ItemDelegate : public QStyledItemDelegate
78 {
79 public:
80   /// Constructor
81   /// \param thaParent a parent
82   ParametersPlugin_ItemDelegate(QObject* thaParent) :
83       QStyledItemDelegate(thaParent) {}
84
85   /// Redefinition of virtual method
86   /// \param painter a painter object
87   /// \param option the item options
88   /// \param index the current index
89   virtual void paint(QPainter* painter,
90     const QStyleOptionViewItem& option,
91     const QModelIndex& index ) const;
92
93   /// Redefinition of virtual method
94   /// \param parent a parent widget
95   /// \param option the item options
96   /// \param index the current index
97   virtual QWidget* createEditor(QWidget* parent,
98                                 const QStyleOptionViewItem& option,
99                                 const QModelIndex& index) const;
100
101   /// Returns True if the given index is editable item
102   /// \param theIndex an item index
103   bool isEditable(const QModelIndex& theIndex) const;
104
105   /// Returns currently editing index
106   QModelIndex editIndex() const { return myEditingIdx; }
107
108 private:
109   mutable QModelIndex myEditingIdx;
110 };
111
112 bool ParametersPlugin_ItemDelegate::isEditable(const QModelIndex& theIndex) const
113 {
114   QModelIndex aParent = theIndex.parent();
115   if (aParent.isValid() && (aParent.row() == 0)) {
116     if (theIndex.column() == 2)
117       return false;
118   } else
119     return false;
120   return true;
121 }
122
123 void ParametersPlugin_ItemDelegate::paint(QPainter* painter,
124                                           const QStyleOptionViewItem& option,
125                                           const QModelIndex& index ) const
126 {
127   QBrush aBrush = painter->brush();
128   QPen aPen = painter->pen();
129   //if (!isEditable(index))
130   //  painter->setBrush(Qt::lightGray);
131   if (!index.parent().isValid())
132     painter->setBrush(Qt::lightGray);
133
134   painter->setPen(Qt::lightGray);
135   painter->drawRect(option.rect);
136   painter->setPen(aPen);
137
138   QStyledItemDelegate::paint(painter, option, index);
139   painter->setBrush(aBrush);
140 }
141
142 QWidget* ParametersPlugin_ItemDelegate::createEditor(QWidget* parent,
143                                                      const QStyleOptionViewItem& option,
144                                                      const QModelIndex& index) const
145 {
146   myEditingIdx = index;
147   return QStyledItemDelegate::createEditor(parent, option, index);
148 }
149
150 /////////////////////////////////////////////////////////////////////////////////////////////////
151 void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor,
152                                               QAbstractItemDelegate::EndEditHint theHint)
153 {
154   if (theHint == QAbstractItemDelegate::EditNextItem) {
155     QModelIndex aCurrent = currentIndex();
156     QModelIndex aParent = model()->index(0, 0);
157     int aNbRows = model()->rowCount(aParent);
158     QModelIndex aIdx;
159     switch (aCurrent.column()) {
160     case 0:
161       aIdx = model()->index(aCurrent.row(), 1, aParent);
162       break;
163     case 1:
164       if (aCurrent.row() < (aNbRows - 1))
165         aIdx = model()->index(aCurrent.row() + 1, 0, aParent);
166       else {
167         QTreeWidget::closeEditor(theEditor, QAbstractItemDelegate::NoHint);
168         return;
169       }
170       break;
171     case 3:
172       QTreeWidget::closeEditor(theEditor, theHint);
173       return;
174     }
175     if (aIdx.isValid()) {
176       QTreeWidget::closeEditor(theEditor, QAbstractItemDelegate::NoHint);
177       setCurrentIndex(aIdx);
178       edit(aIdx);
179       return;
180     }
181   }
182   QTreeWidget::closeEditor(theEditor, theHint);
183 }
184
185 /////////////////////////////////////////////////////////////////////////////////////////////////
186
187 ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent,
188   const Config_WidgetAPI* theData, ModuleBase_IWorkshop* theWorkshop)
189   : ModuleBase_ModelDialogWidget(theParent, theData),
190   myWorkshop(theWorkshop), isUpplyBlocked(false)
191 {
192   QVBoxLayout* aLayout = new QVBoxLayout(this);
193
194   myTable = new ParametersPlugin_TreeWidget(this);
195   myTable->setColumnCount(4);
196   QStringList aHeaders;
197   aHeaders << translate("Name") << translate("Expression")
198            << translate("Result") << translate("Comment");
199   myTable->setHeaderLabels(aHeaders);
200   myTable->setColumnWidth(Col_Name, 200);
201   myTable->setColumnWidth(Col_Equation, 100);
202   myTable->setColumnWidth(Col_Result, 80);
203   myTable->setColumnWidth(Col_Comment, 200);
204   myTable->setMinimumWidth(600);
205   myTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
206   myTable->setSelectionMode(QAbstractItemView::SingleSelection);
207
208   connect(myTable, SIGNAL(doubleClicked(const QModelIndex&)),
209           SLOT(onDoubleClick(const QModelIndex&)));
210   connect(myTable, SIGNAL(itemSelectionChanged()), SLOT(onSelectionChanged()));
211
212   myDelegate = new ParametersPlugin_ItemDelegate(myTable);
213   connect(myDelegate, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)),
214           SLOT(onCloseEditor(QWidget*, QAbstractItemDelegate::EndEditHint)));
215
216   myTable->setItemDelegate(myDelegate);
217   aLayout->addWidget(myTable);
218
219   // Define root nodes
220   QStringList aNames;
221   aNames<<translate("Parameters");
222   myParameters = new QTreeWidgetItem(aNames);
223   myParameters->setFlags(Qt::ItemIsEnabled);
224   myTable->addTopLevelItem(myParameters);
225
226   aNames.clear();
227   aNames<<translate("Features");
228   myFeatures = new QTreeWidgetItem(aNames);
229   myFeatures->setFlags(Qt::ItemIsEnabled);
230   myTable->addTopLevelItem(myFeatures);
231
232   QHBoxLayout* aBtnLayout = new QHBoxLayout(this);
233
234   myUpBtn = new QToolButton(this);
235   myUpBtn->setArrowType(Qt::UpArrow);
236   connect(myUpBtn, SIGNAL(clicked(bool)), SLOT(onUp()));
237   aBtnLayout->addWidget(myUpBtn);
238
239   myDownBtn = new QToolButton(this);
240   myDownBtn->setArrowType(Qt::DownArrow);
241   connect(myDownBtn, SIGNAL(clicked(bool)), SLOT(onDown()));
242   aBtnLayout->addWidget(myDownBtn);
243
244   aBtnLayout->addStretch();
245
246   myAddBtn = new QPushButton(translate("Add"), this);
247   connect(myAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd()));
248   aBtnLayout->addWidget(myAddBtn);
249
250   QString aAddStr(Config_PropManager::string("Shortcuts", "add_parameter_shortcut").c_str());
251   if (aAddStr.isEmpty())
252     aAddStr = "Ctrl+A";
253
254   QShortcut* aAddShc = new QShortcut(QKeySequence(aAddStr), myAddBtn);
255   connect(aAddShc, SIGNAL(activated()), SLOT(onAdd()));
256
257   myInsertBtn = new QPushButton(translate("Insert"), this);
258   connect(myInsertBtn, SIGNAL(clicked(bool)), SLOT(onInsert()));
259   aBtnLayout->addWidget(myInsertBtn);
260
261   myImportBtn = new QPushButton(translate("Import file"), this);
262   connect(myImportBtn, SIGNAL(clicked(bool)), SLOT(onImport()));
263   aBtnLayout->addWidget(myImportBtn);
264
265   myRemoveBtn = new QPushButton(translate("Remove"), this);
266   connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemove()));
267   aBtnLayout->addWidget(myRemoveBtn);
268
269   aLayout->addLayout(aBtnLayout);
270
271   onSelectionChanged();
272 }
273
274 void ParametersPlugin_WidgetParamsMgr::setDialogButtons(QDialogButtonBox* theButtons)
275 {
276   ModuleBase_ModelDialogWidget::setDialogButtons(theButtons);
277
278   QWidget* aBtnParentWgt = myOkCancelBtn->parentWidget();
279   QHBoxLayout* aBtnParentLayout = dynamic_cast<QHBoxLayout*>(aBtnParentWgt->layout());
280
281   QPushButton* aPreviewBtn = new QPushButton(translate("See preview"), aBtnParentWgt);
282   aBtnParentLayout->insertWidget(0, aPreviewBtn);
283   aBtnParentLayout->insertStretch(1, 1);
284   connect(aPreviewBtn, SIGNAL(clicked(bool)), SLOT(onShowPreview()));
285 }
286
287
288 QList<QWidget*> ParametersPlugin_WidgetParamsMgr::getControls() const
289 {
290   QList<QWidget*> aList;
291
292   return aList;
293 }
294
295 void ParametersPlugin_WidgetParamsMgr::selectItemScroll(QTreeWidgetItem* aItem)
296 {
297   myTable->clearSelection();
298   QModelIndex aParent = myTable->model()->index(0, 0);
299   int aChildIdx = myParameters->indexOfChild(aItem);
300   QModelIndex aIndex = myTable->model()->index(aChildIdx, Col_Name, aParent);
301   myTable->selectionModel()->select(aIndex,
302     QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
303   myTable->scrollToItem(aItem);
304 }
305
306
307 bool ParametersPlugin_WidgetParamsMgr::storeValueCustom()
308 {
309   ParametersPlugin_ExpressionValidator aValidator;
310   std::list<std::string> aArgs;
311   std::string aAttrId = ParametersPlugin_Parameter::VARIABLE_ID();
312   Events_InfoMessage aErr;
313   int aId = 0;
314   foreach(FeaturePtr aFeature, myParametersList) {
315     if (!aValidator.isValid(aFeature->attribute(aAttrId), aArgs, aErr)) {
316       QMessageBox::warning(this, translate("Warning"), aErr.messageString().c_str());
317       selectItemScroll(myParameters->child(aId));
318       return false;
319     }
320     aId++;
321   }
322   // #2813 : make the current feature the latest in the document
323   std::list<FeaturePtr> allFeatures = myFeature->document()->allFeatures();
324   if (!allFeatures.empty()) {
325     myFeature->document()->setCurrentFeature(*(allFeatures.rbegin()), true);
326   }
327   return true;
328 }
329
330 bool ParametersPlugin_WidgetParamsMgr::restoreValueCustom()
331 {
332   return true;
333 }
334
335 void ParametersPlugin_WidgetParamsMgr::activateCustom()
336 {
337   updateParametersFeatures();
338   updateParametersPart();
339   updateFeaturesPart();
340
341   myFeatures->setExpanded(true);
342   myParameters->setExpanded(true);
343 }
344
345 void ParametersPlugin_WidgetParamsMgr::updateParametersFeatures()
346 {
347   myParametersList.clear();
348   FeaturePtr aFeature = feature();
349   DocumentPtr aDoc = aFeature->document();
350   ObjectPtr aObj;
351   FeaturePtr aParamFeature;
352   int aNbFeatures = aDoc->numInternalFeatures();
353   for (int i = 0; i < aNbFeatures; i++) {
354     aParamFeature = aDoc->internalFeature(i);
355     if (aParamFeature && aParamFeature->getKind() == ParametersPlugin_Parameter::ID()) {
356       myParametersList.append(aParamFeature);
357     }
358   }
359 }
360
361 void ParametersPlugin_WidgetParamsMgr::updateFeaturesPart()
362 {
363   QList<FeaturePtr> aFeatureList;
364   updateItem(myFeatures, featuresItems(myParametersList, aFeatureList));
365 }
366
367 void ParametersPlugin_WidgetParamsMgr::updateParametersPart()
368 {
369   updateItem(myParameters, parametersItems(myParametersList));
370   bool aIsValid = isValid();
371   enableButtons(aIsValid);
372 }
373
374
375 QList<QStringList> ParametersPlugin_WidgetParamsMgr::
376   featuresItems(const QList<FeaturePtr>& theFeatures, QList<FeaturePtr>& theFeatureList) const
377 {
378   QList<QStringList> aItemsList;
379   ResultParameterPtr aParam;
380   foreach(FeaturePtr aParameter, theFeatures) {
381     aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParameter->firstResult());
382     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefs = aParam->data()->refsToMe();
383     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aIt;
384     for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) {
385       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
386       FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
387       if (aReferenced.get() && (aReferenced != aParameter)) {
388         if (aReferenced->getKind() == ParametersPlugin_Parameter::ID()) {
389           // Find referenced feature Recursive
390           QList<FeaturePtr> aList;
391           aList.append(aReferenced);
392           QList<QStringList> aItems = featuresItems(aList, theFeatureList);
393           aItemsList.append(aItems);
394         } else {
395           if (!theFeatureList.contains(aReferenced)) {
396             QStringList aValNames;
397             aValNames << QString::fromStdWString(aReferenced->data()->name());
398
399             std::string aId = aAttr->attributeType();
400             if (aId == ModelAPI_AttributeDouble::typeId()) {
401               AttributeDoublePtr aDouble =
402                 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aAttr);
403               aValNames << QString::fromStdWString(aDouble->text());
404               aValNames << QString::number(aDouble->value());
405             }
406             else if (aId == ModelAPI_AttributeInteger::typeId()) {
407               AttributeIntegerPtr aInt =
408                 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aAttr);
409               aValNames << QString::fromStdWString(aInt->text());
410               aValNames << QString::number(aInt->value());
411             }
412             else if (aId == GeomDataAPI_Point::typeId()) {
413               std::shared_ptr<GeomDataAPI_Point> aPnt =
414                 std::dynamic_pointer_cast<GeomDataAPI_Point>(aAttr);
415
416               QString aExpr = QString("%1,%2,%3").arg(QString::fromStdWString(aPnt->textX())).
417                                                   arg(QString::fromStdWString(aPnt->textY())).
418                                                   arg(QString::fromStdWString(aPnt->textZ()));
419               aValNames << aExpr;
420
421               QString aRes = QString("%1,%2,%3").arg(aPnt->x()).arg(aPnt->y()).arg(aPnt->z());
422               aValNames << aRes;
423             }
424             else if (aId == GeomDataAPI_Point2D::typeId()) {
425               std::shared_ptr<GeomDataAPI_Point2D> aPnt =
426                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
427
428               QString aExpr = QString("%1,%2").arg(QString::fromStdWString(aPnt->textX())).
429                                                arg(QString::fromStdWString(aPnt->textY()));
430               aValNames << aExpr;
431
432               QString aRes = QString("%1,%2").arg(aPnt->x()).arg(aPnt->y());
433               aValNames << aRes;
434             }
435             aItemsList.append(aValNames);
436             theFeatureList.append(aReferenced);
437           }
438         }
439       }
440     }
441   }
442   return aItemsList;
443 }
444
445
446 QList<QStringList> ParametersPlugin_WidgetParamsMgr::
447   parametersItems(const QList<FeaturePtr>& theFeatures) const
448 {
449   std::list<std::string> aArgs;
450   std::string aErr;
451   QList<QStringList> aItemsList;
452   foreach(FeaturePtr aParameter, theFeatures) {
453     ResultPtr aParam = aParameter->firstResult();
454     QStringList aValues;
455
456     std::string aName = aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->value();
457     if (aName.empty()) {
458       aValues << translate(NoName);
459     } else
460       aValues << aName.c_str();
461
462     std::string aExpr = aParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
463     if (aName.empty()) {
464       aValues << translate(NoValue);
465     } else
466       aValues << aExpr.c_str();
467
468     aErr = aParameter->data()->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
469     if (aErr.empty()) {
470       AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
471       aValues << QString::number(aValueAttribute->value());
472     } else {
473       aValues << aErr.c_str();
474     }
475     aValues << aParameter->string(ParametersPlugin_Parameter::COMMENT_ID())->value().c_str();
476     aItemsList.append(aValues);
477   }
478   return aItemsList;
479 }
480
481
482 void ParametersPlugin_WidgetParamsMgr::onDoubleClick(const QModelIndex& theIndex)
483 {
484   if (myDelegate->isEditable(theIndex)) {
485     myTable->setCurrentIndex(theIndex);
486     myTable->edit(theIndex);
487   }
488 }
489
490 void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
491                                                      QAbstractItemDelegate::EndEditHint theHint)
492 {
493   FeaturePtr aFeature = myParametersList.at(myDelegate->editIndex().row());
494   QTreeWidgetItem* aItem = myParameters->child(myDelegate->editIndex().row());
495   int aColumn = myDelegate->editIndex().column();
496   int aRow = myDelegate->editIndex().row();
497   QString aText = aItem->text(aColumn);
498   bool isModified = false;
499
500   switch (aColumn) {
501   case Col_Name:
502     {
503       AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::VARIABLE_ID());
504       if (!aText.isEmpty()) {
505         while (aText.indexOf(" ") != -1) {
506           aText.replace(" ", "");
507         }
508         if (hasName(aText, aRow)) {
509           myMessage = translate("Name '%1' already exists.").arg(aText);
510           QTimer::singleShot(50, this, SLOT(sendWarning()));
511           return;
512         }
513         aStringAttr->setValue(aText.toStdString());
514         isModified = true;
515       }
516     }
517     break;
518   case Col_Equation:
519     {
520       AttributeStringPtr aStringAttr =
521         aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ID());
522       if (!aText.isEmpty()) {
523         if (aText != aStringAttr->value().c_str()) {
524           aStringAttr->setValue(aText.toStdString());
525           aFeature->execute();
526           isModified = true;
527         }
528       }
529     }
530     break;
531   case Col_Comment:
532     {
533       AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::COMMENT_ID());
534       aStringAttr->setValue(aText.toStdWString());
535       isModified = true;
536     }
537     break;
538   }
539
540   if (!isModified)
541     return;
542   Events_Loop* aLoop = Events_Loop::loop();
543   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
544   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
545   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
546
547   if (aColumn != Col_Comment)
548     updateParametersPart();
549   updateFeaturesPart();
550
551   onSelectionChanged();
552 }
553
554 void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem,
555                                                   const QList<QStringList>& theFeaturesList)
556 {
557   if (theFeaturesList.count() != theItem->childCount()) {
558     if (theItem->childCount()  < theFeaturesList.count()) {
559       while (theItem->childCount() != theFeaturesList.count())
560         theItem->addChild(createNewItem(theItem));
561     } else {
562       while (theItem->childCount() != theFeaturesList.count())
563         theItem->removeChild(theItem->child(theItem->childCount() - 1));
564     }
565   }
566   int i = 0;
567   foreach(QStringList aFeature, theFeaturesList) {
568     int aCol = 0;
569     foreach(QString aText, aFeature) {
570       if (aText.length() > 0) {
571         theItem->child(i)->setText(aCol, aText);
572         theItem->child(i)->setToolTip(aCol, aText);
573       }
574       aCol++;
575     }
576     i++;
577   }
578 }
579
580 FeaturePtr ParametersPlugin_WidgetParamsMgr::createParameter() const
581 {
582   SessionPtr aMgr = ModelAPI_Session::get();
583   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
584
585   FeaturePtr aFeature = aDoc->addFeature(ParametersPlugin_Parameter::ID());
586   if (aFeature.get()) {
587     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
588     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
589   }
590   return aFeature;
591 }
592
593
594 QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::createNewItem(QTreeWidgetItem* theParent) const
595 {
596   QStringList aValues;
597   aValues << translate(NoName);
598   aValues << translate(NoValue);
599
600   QTreeWidgetItem* aItem = new QTreeWidgetItem(aValues);
601   if (theParent == myParameters) {
602     aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
603     aItem->setForeground(2, Qt::darkGray);
604   } else
605     aItem->setFlags(Qt::NoItemFlags);
606   return aItem;
607 }
608
609
610 void ParametersPlugin_WidgetParamsMgr::onAdd()
611 {
612   FeaturePtr aFeature = createParameter();
613   if (!aFeature.get())
614     return;
615
616   myParametersList.append(aFeature);
617   updateParametersPart();
618
619   QTreeWidgetItem* aItem = myParameters->child(myParameters->childCount() - 1);
620
621   myTable->scrollToItem(aItem);
622   myTable->setCurrentItem(aItem);
623   myTable->editItem(aItem);
624
625   enableButtons(false);
626 }
627
628 QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::selectedItem() const
629 {
630   QList<QTreeWidgetItem*> aItemsList = myTable->selectedItems();
631   if (aItemsList.count() == 0)
632     return 0;
633
634   QTreeWidgetItem* aCurrentItem = aItemsList.first();
635   if (aCurrentItem->parent() != myParameters)
636     return 0;
637
638   return aCurrentItem;
639 }
640
641
642 void ParametersPlugin_WidgetParamsMgr::onInsert()
643 {
644   QTreeWidgetItem* aCurrentItem = selectedItem();
645   if (!aCurrentItem)
646     return;
647
648   SessionPtr aMgr = ModelAPI_Session::get();
649   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
650
651   FeaturePtr aNewFeature = createParameter();
652   if (!aNewFeature.get())
653     return;
654
655   int aCurrentPos = myParameters->indexOfChild(aCurrentItem);
656   if (aCurrentPos == 0) {
657     aDoc->moveFeature(aNewFeature, FeaturePtr());
658   } else {
659     FeaturePtr aCurFeature = myParametersList.at(aCurrentPos - 1);
660     aDoc->moveFeature(aNewFeature, aCurFeature);
661   }
662   updateParametersFeatures();
663   updateParametersPart();
664
665   myTable->scrollToItem(aCurrentItem);
666   myTable->setCurrentItem(aCurrentItem);
667   myTable->editItem(aCurrentItem);
668 }
669
670 void ParametersPlugin_WidgetParamsMgr::onRemove()
671 {
672   QTreeWidgetItem* aCurrentItem = selectedItem();
673   if (!aCurrentItem)
674     return;
675
676   SessionPtr aMgr = ModelAPI_Session::get();
677   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
678
679   int aCurrentPos = myParameters->indexOfChild(aCurrentItem);
680   FeaturePtr aCurFeature = myParametersList.at(aCurrentPos);
681
682   QObjectPtrList anObjects;
683   anObjects.append(aCurFeature);
684
685   std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
686   std::set<FeaturePtr> aFeatures;
687   ModuleBase_Tools::convertToFeatures(anObjects, aFeatures);
688   ModelAPI_Tools::findAllReferences(aFeatures, aReferences);
689
690   std::set<FeaturePtr> aFeatureRefsToDelete;
691   if (ModuleBase_Tools::askToDelete(aFeatures, aReferences, this, aFeatureRefsToDelete)) {
692     if (!aFeatureRefsToDelete.empty())
693       aFeatures.insert(aFeatureRefsToDelete.begin(), aFeatureRefsToDelete.end());
694     ModelAPI_Tools::removeFeatures(aFeatures, false);
695
696     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED));
697     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
698     updateParametersFeatures();
699     updateFeaturesPart();
700     updateParametersPart();
701   }
702 }
703
704 void ParametersPlugin_WidgetParamsMgr::onImport()
705 {
706   std::string aWinText("Select txt file");
707   std::string aFileType("Text files (*.txt);;All files (*.*)");
708   QString aQPath = QFileDialog::getOpenFileName(nullptr,
709                                                 aWinText.c_str(), "",
710                                                 aFileType.c_str());
711   if (aQPath.size() == 0)
712     return;
713
714   std::string aPath(aQPath.toStdString());
715   std::shared_ptr<ModelAPI_ImportParametersMessage> aMessage =
716     std::shared_ptr<ModelAPI_ImportParametersMessage>(
717       new ModelAPI_ImportParametersMessage(ModelAPI_ImportParametersMessage::eventId()));
718   aMessage->setFilename(aPath);
719   Events_Loop::loop()->send(aMessage);
720
721   updateParametersFeatures();
722   updateParametersPart();
723 }
724
725 void ParametersPlugin_WidgetParamsMgr::onUp()
726 {
727   QTreeWidgetItem* aCurrentItem = selectedItem();
728   if (!aCurrentItem)
729     return;
730
731   int aCurrentPos = myParameters->indexOfChild(aCurrentItem);
732   if (aCurrentPos == 0)
733     return;
734   FeaturePtr aCurFeature = myParametersList.at(aCurrentPos);
735
736   SessionPtr aMgr = ModelAPI_Session::get();
737   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
738
739   if (aCurrentPos == 1)
740     aDoc->moveFeature(aCurFeature, FeaturePtr());
741   else
742     aDoc->moveFeature(aCurFeature, myParametersList.at(aCurrentPos - 2));
743
744   // add the updated also the feature that goes down
745   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED));
746   static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
747   ModelAPI_EventCreator::get()->sendUpdated(myParametersList.at(aCurrentPos - 1), EVENT_UPD);
748   Events_Loop::loop()->flush(EVENT_UPD);
749   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED));
750   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED));
751   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
752   updateParametersFeatures();
753   updateParametersPart();
754   updateFeaturesPart();
755
756   if (aCurrentPos > 0) {
757     aCurrentItem = myParameters->child(aCurrentPos - 1);
758     myTable->setCurrentItem(aCurrentItem);
759     selectItemScroll(aCurrentItem);
760   }
761 }
762
763 void ParametersPlugin_WidgetParamsMgr::onDown()
764 {
765   QTreeWidgetItem* aCurrentItem = selectedItem();
766   if (!aCurrentItem)
767     return;
768
769   int aCurrentPos = myParameters->indexOfChild(aCurrentItem);
770   if (aCurrentPos == (myParametersList.count() - 1))
771     return;
772   FeaturePtr aCurFeature = myParametersList.at(aCurrentPos);
773
774   SessionPtr aMgr = ModelAPI_Session::get();
775   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
776   aDoc->moveFeature(aCurFeature, myParametersList.at(aCurrentPos + 1));
777   // add the updated also the feature that goes up
778   static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
779   ModelAPI_EventCreator::get()->sendUpdated(myParametersList.at(aCurrentPos + 1), EVENT_UPD);
780
781   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED));
782   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED));
783   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED));
784   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED));
785   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
786   updateParametersFeatures();
787   updateParametersPart();
788   updateFeaturesPart();
789
790   if (aCurrentPos < myParameters->childCount() - 1) {
791     aCurrentItem = myParameters->child(aCurrentPos + 1);
792     myTable->setCurrentItem(aCurrentItem);
793     selectItemScroll(aCurrentItem);
794   }
795 }
796
797
798 bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName, int theIndex) const
799 {
800   int i = 0;
801   foreach(FeaturePtr aFeature, myParametersList) {
802     if ((i != theIndex) && (aFeature->data()->name() == theName.toStdWString()))
803       return true;
804     i++;
805   }
806   return false;
807 }
808
809 void ParametersPlugin_WidgetParamsMgr::sendWarning()
810 {
811   QMessageBox::warning(this, translate("Warning"), myMessage);
812   QTreeWidgetItem* aItem = myTable->currentItem();
813   if (aItem)
814     myTable->editItem(aItem);
815 }
816
817 void ParametersPlugin_WidgetParamsMgr::onSelectionChanged()
818 {
819   QList<QTreeWidgetItem*> aItemsList = myTable->selectedItems();
820   bool aIsValid = isValid();
821   if (aIsValid) {
822     bool isParameter = false;
823     foreach(QTreeWidgetItem* aItem, aItemsList) {
824       if (aItem->parent() == myParameters) {
825         isParameter = true;
826         break;
827       }
828     }
829     myInsertBtn->setEnabled(isParameter);
830     //myRemoveBtn->setEnabled(isParameter);
831     myUpBtn->setEnabled(isParameter);
832     myDownBtn->setEnabled(isParameter);
833     myImportBtn->setEnabled(true);
834   } else {
835     myInsertBtn->setEnabled(false);
836     //myRemoveBtn->setEnabled(false);
837     myUpBtn->setEnabled(false);
838     myDownBtn->setEnabled(false);
839     myImportBtn->setEnabled(false);
840   }
841   myRemoveBtn->setEnabled(!aItemsList.isEmpty());
842 }
843
844 void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable)
845 {
846   myAddBtn->setEnabled(theEnable);
847   if (theEnable)
848     onSelectionChanged();
849   else {
850     myInsertBtn->setEnabled(theEnable);
851     //myRemoveBtn->setEnabled(theEnable);
852     myUpBtn->setEnabled(theEnable);
853     myDownBtn->setEnabled(theEnable);
854     myImportBtn->setEnabled(theEnable);
855   }
856   myOkCancelBtn->button(QDialogButtonBox::Ok)->setEnabled(theEnable);
857 }
858
859 bool ParametersPlugin_WidgetParamsMgr::isValid()
860 {
861   QTreeWidgetItem* aItem;
862   for(int i = 0; i < myParameters->childCount(); i++) {
863     aItem = myParameters->child(i);
864     if ((aItem->text(Col_Name) == NoName) ||
865         (aItem->text(Col_Equation) == translate(NoValue)) ||
866         (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ||
867         (hasName(aItem->text(Col_Name), i)) ) {
868       return false;
869     }
870   }
871   return true;
872 }
873
874 void ParametersPlugin_WidgetParamsMgr::showEvent(QShowEvent* theEvent)
875 {
876   ModuleBase_ModelDialogWidget::showEvent(theEvent);
877   SessionPtr aMgr = ModelAPI_Session::get();
878   isUpplyBlocked = aMgr->isAutoUpdateBlocked();
879   aMgr->blockAutoUpdate(true);
880   Events_Loop* aLoop = Events_Loop::loop();
881   aLoop->flush(aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE));
882 }
883
884 void ParametersPlugin_WidgetParamsMgr::hideEvent(QHideEvent* theEvent)
885 {
886   ModuleBase_ModelDialogWidget::hideEvent(theEvent);
887   SessionPtr aMgr = ModelAPI_Session::get();
888   aMgr->blockAutoUpdate(isUpplyBlocked);
889 }
890
891 void ParametersPlugin_WidgetParamsMgr::onShowPreview()
892 {
893   SessionPtr aMgr = ModelAPI_Session::get();
894   aMgr->blockAutoUpdate(false);
895   aMgr->blockAutoUpdate(true);
896   myWorkshop->viewer()->update();
897 }