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