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