Salome HOME
Issue #1901: Restore field from Python script
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_WidgetField.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        CollectionPlugin_WidgetField.cpp
4 // Created:     16 Nov 2016
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "CollectionPlugin_WidgetField.h"
8 #include "CollectionPlugin_Field.h"
9
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_ISelection.h>
13
14 #include <XGUI_Tools.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_PropertyPanel.h>
17
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_AttributeStringArray.h>
20 #include <ModelAPI_AttributeInteger.h>
21 #include <ModelAPI_AttributeIntArray.h>
22
23 #include <QLayout>
24 #include <QWidget>
25 #include <QFormLayout>
26 #include <QComboBox>
27 #include <QSpinBox>
28 #include <QLabel>
29 #include <QSlider>
30 #include <QTableWidget>
31 #include <QPushButton>
32 #include <QHeaderView>
33 #include <QStackedWidget>
34 #include <QValidator>
35 #include <QStyledItemDelegate>
36 #include <QLineEdit>
37 #include <QEvent>
38 #include <QMouseEvent>
39 #include <QScrollBar>
40 #include <QApplication>
41
42 const char* MYFirstCol = "Shape";
43 const char* MYTrue = "True";
44 const char* MYFalse = "False";
45
46 class DataTableItemDelegate : public QStyledItemDelegate
47 {
48 public:
49   DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType) :
50       QStyledItemDelegate() { myType = theType; }
51
52   virtual QWidget* createEditor(QWidget* theParent,
53                                 const QStyleOptionViewItem & theOption,
54                                 const QModelIndex& theIndex) const;
55
56   ModelAPI_AttributeTables::ValueType dataType() const { return myType; }
57
58   void setDataType(ModelAPI_AttributeTables::ValueType theType) { myType = theType; }
59
60 signals:
61   void startEditing();
62
63 private:
64   ModelAPI_AttributeTables::ValueType myType;
65 };
66
67 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
68                                              const QStyleOptionViewItem & theOption,
69                                              const QModelIndex& theIndex ) const
70 {
71   if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
72     QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
73     QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
74     aEdt->setReadOnly(true);
75     return aEdt;
76   } else {
77     QLineEdit* aLineEdt = 0;
78     switch (myType) {
79     case ModelAPI_AttributeTables::DOUBLE:
80       aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
81                                                                             theOption,
82                                                                             theIndex));
83       if (aLineEdt) {
84         aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
85         return aLineEdt;
86       }
87       break;
88     case ModelAPI_AttributeTables::INTEGER:
89       aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
90                                                                             theOption,
91                                                                             theIndex));
92       if (aLineEdt) {
93         aLineEdt->setValidator(new QIntValidator(aLineEdt));
94         return aLineEdt;
95       }
96       break;
97     case ModelAPI_AttributeTables::BOOLEAN:
98       {
99         QComboBox* aBox = new QComboBox(theParent);
100         aBox->addItem(MYFalse);
101         aBox->addItem(MYTrue);
102         return aBox;
103       }
104     }
105   }
106   return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
107 }
108
109
110
111 //**********************************************************************************
112 //**********************************************************************************
113 //**********************************************************************************
114 CollectionPlugin_WidgetField::
115   CollectionPlugin_WidgetField(QWidget* theParent,
116                                ModuleBase_IWorkshop* theWorkshop,
117                                const Config_WidgetAPI* theData):
118 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
119   myIsTabEdit(false), myActivation(false)
120 {
121   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
122
123   // Types definition controls
124   QWidget* aTypesWgt = new QWidget(this);
125   QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
126   aTypesLayout->setContentsMargins(0, 0, 0, 0);
127   aMainLayout->addWidget(aTypesWgt);
128
129   // Type of shapes
130   myShapeTypeCombo = new QComboBox(aTypesWgt);
131   QStringList aShapeTypes;
132   aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
133     << tr("Solids") << tr("Objects") << tr("Parts");
134   myShapeTypeCombo->addItems(aShapeTypes);
135   aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
136
137   // Type of field
138   myFieldTypeCombo = new QComboBox(aTypesWgt);
139   QStringList aFieldTypes;
140   aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
141     << tr("String");
142   myFieldTypeCombo->addItems(aFieldTypes);
143   myFieldTypeCombo->setCurrentIndex(2);
144   aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
145
146   // Number of components
147   myNbComponentsSpn = new QSpinBox(aTypesWgt);
148   myNbComponentsSpn->setMinimum(1);
149   aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
150
151   // Steps controls
152   QFrame* aStepFrame = new QFrame(this);
153   aStepFrame->setFrameShape(QFrame::Box);
154   aStepFrame->setFrameStyle(QFrame::StyledPanel);
155   QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
156   aMainLayout->addWidget(aStepFrame);
157
158   // Current step label
159   aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
160   myCurStepLbl = new QLabel("1", aStepFrame);
161   QFont aFont = myCurStepLbl->font();
162   aFont.setBold(true);
163   myCurStepLbl->setFont(aFont);
164   aStepLayout->addWidget(myCurStepLbl, 0, 1);
165
166   // Steps slider
167   QWidget* aSliderWidget = new QWidget(aStepFrame);
168   aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
169   QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
170   aSliderLayout->setContentsMargins(0, 0, 0, 0);
171
172   aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
173
174   myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
175   myStepSlider->setTickPosition(QSlider::TicksBelow);
176   myStepSlider->setRange(1, 1);
177   myStepSlider->setPageStep(myStepSlider->singleStep());
178   aSliderLayout->addWidget(myStepSlider, 1);
179
180   myMaxLbl = new QLabel("1", aSliderWidget);
181   aSliderLayout->addWidget(myMaxLbl);
182
183   // Stamp value
184   myCompNamesList << "Comp 1";
185   myStepWgt = new QStackedWidget(aStepFrame);
186   aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
187   appendStepControls();
188
189   // Buttons below
190   QWidget* aBtnWgt = new QWidget(this);
191   aMainLayout->addWidget(aBtnWgt);
192   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
193   aBtnLayout->setContentsMargins(0, 0, 0, 0);
194
195   QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
196   aBtnLayout->addWidget(aAddBtn);
197
198   aBtnLayout->addStretch(1);
199
200   myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
201   aBtnLayout->addWidget(myRemoveBtn);
202   myRemoveBtn->setEnabled(false);
203
204   connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
205   connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
206   connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
207   connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
208   connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
209   connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
210   connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
211   connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
212 }
213
214 //**********************************************************************************
215 void CollectionPlugin_WidgetField::appendStepControls()
216 {
217   QWidget* aWidget = new QWidget(myStepWgt);
218   QGridLayout* aStepLayout = new QGridLayout(aWidget);
219   aStepLayout->setContentsMargins(0, 0, 0, 0);
220
221   aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
222
223   QSpinBox* aStampSpn = new QSpinBox(aWidget);
224   aStepLayout->addWidget(aStampSpn, 0, 1);
225
226   myStampSpnList.append(aStampSpn);
227
228   // Data table
229   QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
230   aDataTbl->installEventFilter(this);
231   DataTableItemDelegate* aDelegate = 0;
232   if (myDataTblList.isEmpty())
233     aDelegate = new DataTableItemDelegate(
234       (ModelAPI_AttributeTables::ValueType) myFieldTypeCombo->currentIndex());
235   else
236     aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
237
238   QIntList aColWidth;
239   if (!myDataTblList.isEmpty()) {
240     QTableWidget* aFirstTable = myDataTblList.first();
241     for (int i = 0; i < aFirstTable->columnCount(); i++)
242       aColWidth.append(aFirstTable->columnWidth(i));
243   }
244   aDataTbl->setItemDelegate(aDelegate);
245   myDataTblList.append(aDataTbl);
246
247   aDataTbl->verticalHeader()->hide();
248   aDataTbl->setRowHeight(0, 25);
249   aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
250
251   connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
252           SLOT(onColumnResize(int, int, int)));
253
254   updateHeaders(aDataTbl);
255
256   QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
257   aItem->setBackgroundColor(Qt::lightGray);
258   aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
259   aDataTbl->setItem(0, 0, aItem);
260
261   // Set default value item
262   for (int i = 0; i < myCompNamesList.count(); i++) {
263     aItem = createDefaultItem();
264     aItem->setBackgroundColor(Qt::lightGray);
265     aDataTbl->setItem(0, i + 1, aItem);
266   }
267
268   if (aColWidth.length() > 0) {
269     for (int i = 0; i < aDataTbl->columnCount(); i++)
270       aDataTbl->setColumnWidth(i, aColWidth.at(i));
271   }
272   aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
273   connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
274
275   QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
276   myStepWgt->addWidget(aWidget);
277   aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
278 }
279
280 //**********************************************************************************
281 void CollectionPlugin_WidgetField::deactivate()
282 {
283   ModuleBase_WidgetSelector::deactivate();
284   storeValueCustom();
285 }
286
287 //**********************************************************************************
288 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
289 //{
290 //  myShapeTypeCombo->setEnabled(!isEditingMode());
291 //  myFieldTypeCombo->setEnabled(!isEditingMode());
292 //  myNbComponentsSpn->setEnabled(!isEditingMode());
293 //}
294
295 //**********************************************************************************
296 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
297 {
298   QObject* aObject = 0;
299   foreach(QTableWidget* aTable, myDataTblList) {
300     if (aTable->horizontalHeader()->viewport() == theObject) {
301       aObject = theObject;
302       break;
303     }
304   }
305   if (aObject) {
306     if (theEvent->type() == QEvent::MouseButtonDblClick) {
307       if (myHeaderEditor) { //delete previous editor
308         myHeaderEditor->deleteLater();
309         myHeaderEditor = 0;
310       }
311       QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
312       QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
313       QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
314
315       int aShift = aTable->horizontalScrollBar()->value();
316       int aPos = aMouseEvent->x();
317       int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
318       if (aIndex > 0) {
319         QRect aRect;
320         aRect.setLeft(aHeader->sectionPosition(aIndex));
321         aRect.setWidth(aHeader->sectionSize(aIndex));
322         aRect.setTop(0);
323         aRect.setHeight(aHeader->height());
324         aRect.adjust(1, 1, -1, -1);
325         aRect.translate(-aShift, 0);
326
327         myHeaderEditor = new QLineEdit(aHeader->viewport());
328         myHeaderEditor->move(aRect.topLeft());
329         myHeaderEditor->resize(aRect.size());
330         myHeaderEditor->setFrame(false);
331         QString aText = aHeader->model()->
332           headerData(aIndex, aHeader->orientation()).toString();
333         myHeaderEditor->setText(aText);
334         myHeaderEditor->setFocus();
335         myEditIndex = aIndex; //save for future use
336         myHeaderEditor->installEventFilter(this); //catch focus out event
337         //if user presses Enter it should close editor
338         connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
339         myHeaderEditor->show();
340         return true;
341       }
342     }
343   } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
344     QString aNewTitle = myHeaderEditor->text();
345     //save item text
346     myCompNamesList.replace(myEditIndex - 1, aNewTitle);
347     myHeaderEditor->deleteLater(); //safely delete editor
348     myHeaderEditor = 0;
349     // Store into data model
350     AttributeStringArrayPtr aStringsAttr =
351       myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
352     aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
353     foreach(QTableWidget* aTable, myDataTblList) {
354       updateHeaders(aTable);
355     }
356   } else if (theEvent->type() == QEvent::FocusIn) {
357     QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
358     if (aTable) {
359       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
360       XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
361       if (aPanel->activeWidget() != this) {
362         myActivation = true;
363         aPanel->activateWidget(this, false);
364       }
365     }
366   }
367   return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
368 }
369
370 //**********************************************************************************
371 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
372 {
373   QTableWidgetItem* aItem = new QTableWidgetItem();
374   switch (myFieldTypeCombo->currentIndex()) {
375   case ModelAPI_AttributeTables::DOUBLE:
376   case ModelAPI_AttributeTables::INTEGER:
377     aItem->setText("0");
378     break;
379   case ModelAPI_AttributeTables::BOOLEAN:
380     aItem->setText(MYFalse);
381     break;
382   case ModelAPI_AttributeTables::STRING:
383     aItem->setText("");
384     break;
385   }
386   return aItem;
387 }
388
389 //**********************************************************************************
390 QTableWidgetItem* CollectionPlugin_WidgetField::
391   createValueItem(ModelAPI_AttributeTables::Value& theVal) const
392 {
393   QTableWidgetItem* aItem = new QTableWidgetItem();
394   aItem->setText(getValueText(theVal));
395   return aItem;
396 }
397
398 //**********************************************************************************
399 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
400 {
401   switch (myFieldTypeCombo->currentIndex()) {
402   case ModelAPI_AttributeTables::DOUBLE:
403     return QString::number(theVal.myDouble);
404   case ModelAPI_AttributeTables::INTEGER:
405     return QString::number(theVal.myInt);
406   case ModelAPI_AttributeTables::BOOLEAN:
407     return theVal.myBool? MYTrue : MYFalse;
408   case ModelAPI_AttributeTables::STRING:
409     return theVal.myStr.c_str();
410   }
411   return "";
412 }
413
414
415 //**********************************************************************************
416 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
417 {
418   QStringList aHeaders;
419   aHeaders << tr(MYFirstCol);
420   aHeaders << myCompNamesList;
421   theDataTbl->setHorizontalHeaderLabels(aHeaders);
422 }
423
424 //**********************************************************************************
425 void CollectionPlugin_WidgetField::removeStepControls()
426 {
427   int aCurWgtId = myStepWgt->currentIndex();
428   QWidget* aWgt = myStepWgt->currentWidget();
429   myStepWgt->removeWidget(aWgt);
430
431   myStampSpnList.removeAt(aCurWgtId);
432   myDataTblList.removeAt(aCurWgtId);
433   aWgt->deleteLater();
434 }
435
436 //**********************************************************************************
437 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
438 {
439   QList<QWidget*> aControls;
440   // this control will accept focus and will be highlighted in the Property Panel
441   aControls.push_back(myShapeTypeCombo);
442   aControls.push_back(myFieldTypeCombo);
443   aControls.push_back(myNbComponentsSpn);
444   return aControls;
445 }
446
447 //**********************************************************************************
448 bool CollectionPlugin_WidgetField::storeValueCustom()
449 {
450   DataPtr aData = myFeature->data();
451   // Store number of components
452   AttributeStringArrayPtr aStringsAttr =
453     aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
454   int aNbComps = myCompNamesList.size();
455   aStringsAttr->setSize(aNbComps);
456   for ( int i = 0; i < aNbComps; i++)
457     aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
458
459   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
460   // Store number of steps
461   int aNbSteps =  myDataTblList.size();
462
463   // Store Type of the field values
464   int aFldType = myFieldTypeCombo->currentIndex();
465
466   AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
467   aStampsAttr->setSize(aNbSteps);
468   // Store data
469   QTableWidget* aTable = myDataTblList.first();
470   int aRows = aTable->rowCount();
471   // first column contains selected names which should not be stored
472   int aColumns = aTable->columnCount() - 1;
473
474   aTablesAttr->setSize(aRows, aColumns, aNbSteps);
475   aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
476   for (int i = 0; i < aNbSteps; i++) {
477     aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
478     aTable = myDataTblList.at(i);
479     for (int j = 0; j < aColumns; j++) {
480       for (int k = 0; k < aRows; k++) {
481         QString aTblVal = aTable->item(k, j + 1)->text();
482         aTablesAttr->setValue(getValue(aTblVal), k, j, i);
483       }
484     }
485   }
486   updateObject(myFeature);
487   return true;
488 }
489
490 //**********************************************************************************
491 bool CollectionPlugin_WidgetField::restoreValueCustom()
492 {
493   bool isBlocked;
494   DataPtr aData = myFeature->data();
495
496   AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
497   std::string aTypeStr = aSelList->selectionType();
498   if (aTypeStr == "")
499     return false; // The attribute is not initialized
500   myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
501
502   // Get number of components
503   AttributeStringArrayPtr aStringsAttr =
504   aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
505
506   myCompNamesList.clear();
507   for (int i = 0; i < aStringsAttr->size(); i++) {
508     myCompNamesList.append(aStringsAttr->value(i).c_str());
509   }
510   isBlocked = myNbComponentsSpn->blockSignals(true);
511   myNbComponentsSpn->setValue(myCompNamesList.size());
512   myNbComponentsSpn->blockSignals(isBlocked);
513
514   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
515   // Get number of steps
516   int aNbSteps = aTablesAttr->tables();
517   myStepSlider->setMaximum(aNbSteps);
518   //myStepSlider->setValue(1);
519   // Clear old tables
520   while (myDataTblList.count() > aNbSteps) {
521     QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
522     myStepWgt->removeWidget(aWgt);
523     aWgt->deleteLater();
524
525     myStampSpnList.removeLast();
526     myDataTblList.removeLast();
527   }
528   while (myDataTblList.count() < aNbSteps)
529     appendStepControls();
530   //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
531   clearData();
532
533   // Get Type of the field values
534   isBlocked = myFieldTypeCombo->blockSignals(true);
535   myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
536   myFieldTypeCombo->blockSignals(isBlocked);
537
538   AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
539   // Fill data table
540   int aRows = aTablesAttr->rows();
541   int aCols = aTablesAttr->columns();
542
543   // Get width of columns
544   QIntList aColWidth;
545   QTableWidget* aFirstTable = myDataTblList.first();
546   for (int i = 0; i < aFirstTable->columnCount(); i++)
547     aColWidth.append(aFirstTable->columnWidth(i));
548
549   QTableWidgetItem* aItem = 0;
550   for (int i = 0; i < aNbSteps; i++) {
551     myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
552     QTableWidget* aTable = myDataTblList.at(i);
553     isBlocked = aTable->blockSignals(true);
554     aTable->setRowCount(aRows);
555     for (int j = 0; j < aCols + 1; j++) {
556       for (int k = 0; k < aRows; k++) {
557         aItem = aTable->item(k, j);
558         if ((j == 0) && (k > 0)) {
559           // Add selection names
560           AttributeSelectionPtr aAttr = aSelList->value(k - 1);
561           if (aItem) {
562             aItem->setText(aAttr->namingName().c_str());
563           } else {
564             aItem = new QTableWidgetItem(aAttr->namingName().c_str());
565             aTable->setItem(k, j, aItem);
566           }
567         } else if (j > 0) {
568           // Add Values
569           ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
570           if (aItem) {
571             aItem->setText(getValueText(aVal));
572           } else {
573             aItem = createValueItem(aVal);
574             if (k == 0)
575               aItem->setBackgroundColor(Qt::lightGray);
576             aTable->setItem(k, j, aItem);
577           }
578         }
579       }
580     }
581     // Restore columns width
582     for (int i = 0; i < aTable->columnCount(); i++)
583       aTable->setColumnWidth(i, aColWidth.at(i));
584
585     aTable->blockSignals(isBlocked);
586   }
587   return true;
588 }
589
590 //**********************************************************************************
591 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
592 {
593   QString aType(theStr.c_str());
594   aType = aType.toLower();
595   if (aType == "vertex")
596     return 0;
597   else if (aType == "edge")
598     return 1;
599   else if (aType == "face")
600     return 2;
601   else if (aType == "solid")
602     return 3;
603   else if (aType == "object")
604     return 4;
605   else if (aType == "part")
606     return 5;
607   return -1;
608 }
609
610
611 //**********************************************************************************
612 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
613 {
614   switch (theType) {
615   case 0: //"Vertices"
616     return "vertex";
617   case 1: // "Edges"
618     return "edge";
619   case 2: // "Faces"
620     return "face";
621   case 3: // "Solids"
622     return "solid";
623   case 4: // "Results"
624     return "object";
625   case 5: // "Parts"
626     return "part";
627   }
628   return "";
629 }
630
631 //**********************************************************************************
632 QIntList CollectionPlugin_WidgetField::shapeTypes() const
633 {
634   QIntList aRes;
635   switch (myShapeTypeCombo->currentIndex()) {
636   case 0: //"Vertices"
637     aRes.append(ModuleBase_Tools::shapeType("vertex"));
638     break;
639   case 1: // "Edges"
640     aRes.append(ModuleBase_Tools::shapeType("edge"));
641     break;
642   case 2: // "Faces"
643     aRes.append(ModuleBase_Tools::shapeType("face"));
644     break;
645   case 3: // "Solids"
646     aRes.append(ModuleBase_Tools::shapeType("solid"));
647     break;
648   case 4: // "Results"
649     aRes.append(ModuleBase_Tools::shapeType("object"));
650     break;
651   case 5: // "Parts"
652     // TODO: Selection mode for Parts
653     break;
654   }
655   return aRes;
656 }
657
658 //**********************************************************************************
659 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
660 {
661   ModelAPI_AttributeTables::Value aVal;
662   switch (myFieldTypeCombo->currentIndex()) {
663   case ModelAPI_AttributeTables::BOOLEAN:
664     aVal.myBool = (theStrVal == MYTrue)? true : false;
665     break;
666   case ModelAPI_AttributeTables::DOUBLE:
667     aVal.myDouble = theStrVal.toDouble();
668     break;
669   case ModelAPI_AttributeTables::INTEGER:
670     aVal.myInt = theStrVal.toInt();
671     break;
672   case ModelAPI_AttributeTables::STRING:
673     aVal.myStr = theStrVal.toStdString();
674   }
675   return aVal;
676 }
677
678
679 //**********************************************************************************
680 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
681 {
682   int aOldCol = myCompNamesList.count();
683   int aNbRows = myDataTblList.first()->rowCount();
684   int aDif = theVal - aOldCol;
685   QTableWidgetItem* aItem = 0;
686
687   while (myCompNamesList.count() != theVal) {
688     if (aDif > 0)
689       myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
690     else
691       myCompNamesList.removeLast();
692   }
693
694   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
695   aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
696
697   foreach(QTableWidget* aDataTbl, myDataTblList) {
698     aDataTbl->setColumnCount(theVal + 1);
699     updateHeaders(aDataTbl);
700     for (int i = aOldCol; i < myCompNamesList.count(); i++) {
701       for (int j = 0; j < aNbRows; j++) {
702         aItem = aDataTbl->item(j, i + 1);
703         if (!aItem) {
704           aItem = createDefaultItem();
705           if (j == 0)
706             aItem->setBackgroundColor(Qt::lightGray);
707           aDataTbl->setItem(j, i + 1, aItem);
708         }
709       }
710     }
711   }
712   emit valuesChanged();
713 }
714
715 //**********************************************************************************
716 void CollectionPlugin_WidgetField::onAddStep()
717 {
718   int aMax = myStepSlider->maximum();
719   aMax++;
720   myStepSlider->setMaximum(aMax);
721   myMaxLbl->setText(QString::number(aMax));
722   appendStepControls();
723   myStepSlider->setValue(aMax);
724
725   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
726   aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
727
728
729   AttributeSelectionListPtr aSelList =
730     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
731   if (!aSelList->isInitialized())
732     return;
733   int aSelNb = aSelList->size();
734   if (aSelNb == 0)
735     return;
736
737   int aColumns = myNbComponentsSpn->value() + 1;
738   int aRows = aSelNb + 1;
739   QTableWidget* aTable = myDataTblList.last();
740   aTable->setRowCount(aRows);
741   QTableWidgetItem* aItem = 0;
742   for(int i = 0; i < aColumns; i++) {
743     if (i == 0) {
744       for(int j = 1; j < aRows; j++) {
745         aItem = aTable->item(j, i);
746         if (!aItem) {
747           aItem = new QTableWidgetItem();
748           aTable->setItem(j, i, aItem);
749         }
750         AttributeSelectionPtr aAttr = aSelList->value(j - 1);
751         aItem->setText(aAttr->namingName().c_str());
752         aItem->setToolTip(aAttr->namingName().c_str());
753       }
754     } else {
755       QString aDefVal = aTable->item(0, i)->text();
756       for(int j = 1; j < aRows; j++) {
757         aItem = aTable->item(j, i);
758         if (!aItem) {
759           aItem = new QTableWidgetItem();
760           aTable->setItem(j, i, aItem);
761         }
762         aItem->setText(aDefVal);
763       }
764     }
765   }
766 }
767
768 //**********************************************************************************
769 void CollectionPlugin_WidgetField::onRemoveStep()
770 {
771   int aMax = myStepSlider->maximum();
772   aMax--;
773   myMaxLbl->setText(QString::number(aMax));
774   removeStepControls();
775   myStepSlider->setMaximum(aMax);
776
777   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
778   aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
779 }
780
781 //**********************************************************************************
782 void CollectionPlugin_WidgetField::clearData()
783 {
784   foreach(QTableWidget* aDataTbl, myDataTblList) {
785     aDataTbl->setRowCount(1);
786   }
787 }
788
789 //**********************************************************************************
790 void CollectionPlugin_WidgetField::onStepMove(int theStep)
791 {
792   myCurStepLbl->setText(QString::number(theStep));
793   myStepWgt->setCurrentIndex(theStep - 1);
794 }
795
796 //**********************************************************************************
797 bool CollectionPlugin_WidgetField::
798   isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
799 {
800   return (myShapeTypeCombo->currentIndex() == 5)? false : true;
801 }
802
803 //**********************************************************************************
804 void CollectionPlugin_WidgetField::onSelectionChanged()
805 {
806   //if (isEditingMode())
807   //  return;
808
809   if (myActivation) {
810     myActivation = false;
811     return;
812   }
813   // Ignore selection for Parts mode
814   if (myShapeTypeCombo->currentIndex() == 5)
815     return;
816
817   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
818
819   AttributeSelectionListPtr aSelList =
820     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
821   aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
822   aSelList->clear();
823
824   ResultPtr aResult;
825   GeomShapePtr aShape;
826   int aNbData = 0;
827   foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
828     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
829     aShape = aPrs->shape();
830     if (!aResult.get() && !aShape.get())
831       continue;
832     if (!aSelList->isInList(aResult, aShape)) {
833       aSelList->append(aResult, aShape);
834       aNbData++;
835     }
836   }
837   int aColumns = myDataTblList.first()->columnCount();
838   int aRows = myDataTblList.first()->rowCount();
839   int aNewRows = aNbData + 1;
840   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
841   aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
842
843   QTableWidgetItem* aItem = 0;
844   foreach(QTableWidget* aTable, myDataTblList) {
845     aTable->setRowCount(aNewRows);
846     if (aNewRows > aRows) {
847       // Add new data
848       for(int i = 0; i < aColumns; i++) {
849         if (i == 0) {
850           for(int j = 1; j < aNewRows; j++) {
851             aItem = aTable->item(j, i);
852             if (!aItem) {
853               aItem = new QTableWidgetItem();
854               aTable->setItem(j, i, aItem);
855             }
856             AttributeSelectionPtr aAttr = aSelList->value(j - 1);
857             aItem->setText(aAttr->namingName().c_str());
858             aItem->setToolTip(aAttr->namingName().c_str());
859           }
860         } else {
861           QString aDefVal = aTable->item(0, i)->text();
862           for(int j = aRows; j < aNewRows; j++) {
863             aItem = aTable->item(j, i);
864             if (!aItem) {
865               aItem = new QTableWidgetItem();
866               aTable->setItem(j, i, aItem);
867             }
868             aItem->setText(aDefVal);
869           }
870         }
871       }
872     } else {
873       // Update only selection name
874       for(int j = 1; j < aNewRows - 1; j++) {
875         AttributeSelectionPtr aAttr = aSelList->value(j);
876         aTable->item(j, 0)->setText(aAttr->namingName().c_str());
877         aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
878       }
879     }
880   }
881   emit valuesChanged();
882 }
883
884 //**********************************************************************************
885 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
886 {
887   DataTableItemDelegate* aDelegate = 0;
888   aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
889   if (aDelegate) {
890     ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
891     if (aOldType != theIdx) {
892       aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
893       int aColumns = myDataTblList.first()->columnCount();
894       int aRows = myDataTblList.first()->rowCount();
895       foreach(QTableWidget* aTable, myDataTblList) {
896         for(int i = 1; i < aColumns; i++) {
897           for(int j = 0; j < aRows; j++) {
898             switch (theIdx) {
899             case ModelAPI_AttributeTables::DOUBLE:
900             case ModelAPI_AttributeTables::INTEGER:
901               if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
902                   (aOldType == ModelAPI_AttributeTables::STRING)) {
903                     aTable->item(j, i)->setText("0");
904               }
905               break;
906             case ModelAPI_AttributeTables::BOOLEAN:
907               aTable->item(j, i)->setText(MYFalse);
908               break;
909             case ModelAPI_AttributeTables::STRING:
910               aTable->item(j, i)->setText("");
911               break;
912             }
913           }
914         }
915       }
916       emit valuesChanged();
917     }
918   }
919 }
920
921 //**********************************************************************************
922 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
923 {
924   // Do not store here column of names
925   if (theCol == 0)
926     return;
927   if (!myFeature.get())
928     return;
929   QTableWidget* aTable = static_cast<QTableWidget*>(sender());
930   int aNb = myDataTblList.indexOf(aTable);
931   if (aNb == -1)
932     return;
933   ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
934
935   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
936   if (aTablesAttr->isInitialized())
937     aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
938   else
939     emit valuesChanged();
940 }
941
942 //**********************************************************************************
943 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
944 {
945   activateSelectionAndFilters(theType == 5? false:true);
946
947   AttributeSelectionListPtr aSelList =
948     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
949
950   std::string aTypeName = getSelectionType(theType);
951   if (aTypeName == aSelList->selectionType())
952     return;
953   aSelList->setSelectionType(aTypeName);
954
955   //Clear old selection
956   clearData();
957   aSelList->clear();
958   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
959   aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
960   emit valuesChanged();
961 }
962
963 //**********************************************************************************
964 bool CollectionPlugin_WidgetField::processEnter()
965 {
966   if (myIsTabEdit) {
967     myIsTabEdit = false;
968     return true;
969   }
970   return false;
971 }
972
973 //**********************************************************************************
974 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
975 {
976   if (theNew && (!myIsTabEdit))
977     myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
978 }
979
980 //**********************************************************************************
981 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
982 {
983   myMaxLbl->setText(QString::number(theMax));
984   myRemoveBtn->setEnabled(theMax > 1);
985 }
986
987 //**********************************************************************************
988 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
989 {
990   if (myDataTblList.count() < 2)
991     return;
992   QObject* aSender = sender();
993   foreach(QTableWidget* aTable, myDataTblList) {
994     if (aTable->horizontalHeader() != aSender)
995       aTable->setColumnWidth(theIndex, theNew);
996   }
997 }