Salome HOME
Issue #1903: Set default value for string
[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   myIsEditing(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 //**********************************************************************************
289 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
290 {
291   QObject* aObject = 0;
292   foreach(QTableWidget* aTable, myDataTblList) {
293     if (aTable->horizontalHeader()->viewport() == theObject) {
294       aObject = theObject;
295       break;
296     }
297   }
298   if (aObject) {
299     if (theEvent->type() == QEvent::MouseButtonDblClick) {
300       if (myHeaderEditor) { //delete previous editor
301         myHeaderEditor->deleteLater();
302         myHeaderEditor = 0;
303       }
304       QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
305       QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
306       QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
307
308       int aShift = aTable->horizontalScrollBar()->value();
309       int aPos = aMouseEvent->x();
310       int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
311       if (aIndex > 0) {
312         QRect aRect;
313         aRect.setLeft(aHeader->sectionPosition(aIndex));
314         aRect.setWidth(aHeader->sectionSize(aIndex));
315         aRect.setTop(0);
316         aRect.setHeight(aHeader->height());
317         aRect.adjust(1, 1, -1, -1);
318         aRect.translate(-aShift, 0);
319
320         myHeaderEditor = new QLineEdit(aHeader->viewport());
321         myHeaderEditor->move(aRect.topLeft());
322         myHeaderEditor->resize(aRect.size());
323         myHeaderEditor->setFrame(false);
324         QString aText = aHeader->model()->
325           headerData(aIndex, aHeader->orientation()).toString();
326         myHeaderEditor->setText(aText);
327         myHeaderEditor->setFocus();
328         myEditIndex = aIndex; //save for future use
329         myHeaderEditor->installEventFilter(this); //catch focus out event
330         //if user presses Enter it should close editor
331         connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
332         myHeaderEditor->show();
333         return true;
334       }
335     }
336   } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
337     QString aNewTitle = myHeaderEditor->text();
338     //save item text
339     myCompNamesList.replace(myEditIndex - 1, aNewTitle);
340     myHeaderEditor->deleteLater(); //safely delete editor
341     myHeaderEditor = 0;
342     // Store into data model
343     AttributeStringArrayPtr aStringsAttr =
344       myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
345     aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
346     foreach(QTableWidget* aTable, myDataTblList) {
347       updateHeaders(aTable);
348     }
349   } else if (theEvent->type() == QEvent::FocusIn) {
350     QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
351     if (aTable) {
352       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
353       XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
354       if (aPanel->activeWidget() != this) {
355         myActivation = true;
356         aPanel->activateWidget(this, false);
357       }
358     }
359   }
360   return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
361 }
362
363 //**********************************************************************************
364 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
365 {
366   QTableWidgetItem* aItem = new QTableWidgetItem();
367   switch (myFieldTypeCombo->currentIndex()) {
368   case ModelAPI_AttributeTables::DOUBLE:
369   case ModelAPI_AttributeTables::INTEGER:
370     aItem->setText("0");
371     break;
372   case ModelAPI_AttributeTables::BOOLEAN:
373     aItem->setText(MYFalse);
374     break;
375   case ModelAPI_AttributeTables::STRING:
376     aItem->setText("");
377     break;
378   }
379   return aItem;
380 }
381
382 //**********************************************************************************
383 QTableWidgetItem* CollectionPlugin_WidgetField::
384   createValueItem(ModelAPI_AttributeTables::Value& theVal) const
385 {
386   QTableWidgetItem* aItem = new QTableWidgetItem();
387   aItem->setText(getValueText(theVal));
388   return aItem;
389 }
390
391 //**********************************************************************************
392 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
393 {
394   switch (myFieldTypeCombo->currentIndex()) {
395   case ModelAPI_AttributeTables::DOUBLE:
396     return QString::number(theVal.myDouble);
397   case ModelAPI_AttributeTables::INTEGER:
398     return QString::number(theVal.myInt);
399   case ModelAPI_AttributeTables::BOOLEAN:
400     return theVal.myBool? MYTrue : MYFalse;
401   case ModelAPI_AttributeTables::STRING:
402     return theVal.myStr.c_str();
403   }
404   return "";
405 }
406
407
408 //**********************************************************************************
409 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
410 {
411   QStringList aHeaders;
412   aHeaders << tr(MYFirstCol);
413   aHeaders << myCompNamesList;
414   theDataTbl->setHorizontalHeaderLabels(aHeaders);
415 }
416
417 //**********************************************************************************
418 void CollectionPlugin_WidgetField::removeStepControls()
419 {
420   int aCurWgtId = myStepWgt->currentIndex();
421   QWidget* aWgt = myStepWgt->currentWidget();
422   myStepWgt->removeWidget(aWgt);
423
424   myStampSpnList.removeAt(aCurWgtId);
425   myDataTblList.removeAt(aCurWgtId);
426   aWgt->deleteLater();
427 }
428
429 //**********************************************************************************
430 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
431 {
432   QList<QWidget*> aControls;
433   // this control will accept focus and will be highlighted in the Property Panel
434   aControls.push_back(myShapeTypeCombo);
435   aControls.push_back(myFieldTypeCombo);
436   aControls.push_back(myNbComponentsSpn);
437   return aControls;
438 }
439
440 //**********************************************************************************
441 bool CollectionPlugin_WidgetField::storeValueCustom()
442 {
443   DataPtr aData = myFeature->data();
444   // Store number of components
445   AttributeStringArrayPtr aStringsAttr =
446     aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
447   int aNbComps = myCompNamesList.size();
448   aStringsAttr->setSize(aNbComps);
449   for ( int i = 0; i < aNbComps; i++)
450     aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
451
452   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
453   // Store number of steps
454   int aNbSteps =  myDataTblList.size();
455
456   // Store Type of the field values
457   int aFldType = myFieldTypeCombo->currentIndex();
458
459   AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
460   aStampsAttr->setSize(aNbSteps);
461   // Store data
462   QTableWidget* aTable = myDataTblList.first();
463   int aRows = aTable->rowCount();
464   // first column contains selected names which should not be stored
465   int aColumns = aTable->columnCount() - 1;
466
467   aTablesAttr->setSize(aRows, aColumns, aNbSteps);
468   aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
469   for (int i = 0; i < aNbSteps; i++) {
470     aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
471     aTable = myDataTblList.at(i);
472     for (int j = 0; j < aColumns; j++) {
473       for (int k = 0; k < aRows; k++) {
474         QString aTblVal = aTable->item(k, j + 1)->text();
475         aTablesAttr->setValue(getValue(aTblVal), k, j, i);
476       }
477     }
478   }
479   updateObject(myFeature);
480   return true;
481 }
482
483 //**********************************************************************************
484 bool CollectionPlugin_WidgetField::restoreValueCustom()
485 {
486   bool isBlocked;
487   DataPtr aData = myFeature->data();
488
489   AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
490   std::string aTypeStr = aSelList->selectionType();
491   if (aTypeStr == "")
492     return false; // The attribute is not initialized
493   myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
494
495   // Get number of components
496   AttributeStringArrayPtr aStringsAttr =
497   aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
498
499   myCompNamesList.clear();
500   for (int i = 0; i < aStringsAttr->size(); i++) {
501     myCompNamesList.append(aStringsAttr->value(i).c_str());
502   }
503   isBlocked = myNbComponentsSpn->blockSignals(true);
504   myNbComponentsSpn->setValue(myCompNamesList.size());
505   myNbComponentsSpn->blockSignals(isBlocked);
506
507   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
508   // Get number of steps
509   int aNbSteps = aTablesAttr->tables();
510   myStepSlider->setMaximum(aNbSteps);
511   //myStepSlider->setValue(1);
512   // Clear old tables
513   while (myDataTblList.count() > aNbSteps) {
514     QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
515     myStepWgt->removeWidget(aWgt);
516     aWgt->deleteLater();
517
518     myStampSpnList.removeLast();
519     myDataTblList.removeLast();
520   }
521   while (myDataTblList.count() < aNbSteps)
522     appendStepControls();
523   //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
524   clearData();
525
526   // Get Type of the field values
527   isBlocked = myFieldTypeCombo->blockSignals(true);
528   myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
529   myFieldTypeCombo->blockSignals(isBlocked);
530
531   AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
532   // Fill data table
533   int aRows = aTablesAttr->rows();
534   int aCols = aTablesAttr->columns();
535
536   // Get width of columns
537   QIntList aColWidth;
538   QTableWidget* aFirstTable = myDataTblList.first();
539   for (int i = 0; i < aFirstTable->columnCount(); i++)
540     aColWidth.append(aFirstTable->columnWidth(i));
541
542   QTableWidgetItem* aItem = 0;
543   for (int i = 0; i < aNbSteps; i++) {
544     myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
545     QTableWidget* aTable = myDataTblList.at(i);
546     isBlocked = aTable->blockSignals(true);
547     aTable->setRowCount(aRows);
548     for (int j = 0; j < aCols + 1; j++) {
549       for (int k = 0; k < aRows; k++) {
550         aItem = aTable->item(k, j);
551         if ((j == 0) && (k > 0)) {
552           // Add selection names
553           AttributeSelectionPtr aAttr = aSelList->value(k - 1);
554           if (aItem) {
555             aItem->setText(aAttr->namingName().c_str());
556           } else {
557             aItem = new QTableWidgetItem(aAttr->namingName().c_str());
558             aTable->setItem(k, j, aItem);
559           }
560         } else if (j > 0) {
561           // Add Values
562           ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
563           if (aItem) {
564             aItem->setText(getValueText(aVal));
565           } else {
566             aItem = createValueItem(aVal);
567             if (k == 0)
568               aItem->setBackgroundColor(Qt::lightGray);
569             aTable->setItem(k, j, aItem);
570           }
571         }
572       }
573     }
574     // Restore columns width
575     for (int i = 0; i < aTable->columnCount(); i++)
576       aTable->setColumnWidth(i, aColWidth.at(i));
577
578     aTable->blockSignals(isBlocked);
579   }
580   return true;
581 }
582
583 //**********************************************************************************
584 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
585 {
586   if (theStr == "vertex")
587     return 0;
588   else if (theStr == "edge")
589     return 1;
590   else if (theStr == "face")
591     return 2;
592   else if (theStr == "solid")
593     return 3;
594   else if (theStr == "object")
595     return 4;
596   else if (theStr == "part")
597     return 5;
598   return -1;
599 }
600
601
602 //**********************************************************************************
603 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
604 {
605   switch (theType) {
606   case 0: //"Vertices"
607     return "vertex";
608   case 1: // "Edges"
609     return "edge";
610   case 2: // "Faces"
611     return "face";
612   case 3: // "Solids"
613     return "solid";
614   case 4: // "Results"
615     return "object";
616   case 5: // "Parts"
617     return "part";
618   }
619   return "";
620 }
621
622 //**********************************************************************************
623 QIntList CollectionPlugin_WidgetField::shapeTypes() const
624 {
625   QIntList aRes;
626   switch (myShapeTypeCombo->currentIndex()) {
627   case 0: //"Vertices"
628     aRes.append(ModuleBase_Tools::shapeType("vertex"));
629     break;
630   case 1: // "Edges"
631     aRes.append(ModuleBase_Tools::shapeType("edge"));
632     break;
633   case 2: // "Faces"
634     aRes.append(ModuleBase_Tools::shapeType("face"));
635     break;
636   case 3: // "Solids"
637     aRes.append(ModuleBase_Tools::shapeType("solid"));
638     break;
639   case 4: // "Results"
640     aRes.append(ModuleBase_Tools::shapeType("object"));
641     break;
642   case 5: // "Parts"
643     // TODO: Selection mode for Parts
644     break;
645   }
646   return aRes;
647 }
648
649 //**********************************************************************************
650 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
651 {
652   ModelAPI_AttributeTables::Value aVal;
653   switch (myFieldTypeCombo->currentIndex()) {
654   case ModelAPI_AttributeTables::BOOLEAN:
655     aVal.myBool = (theStrVal == MYTrue)? true : false;
656     break;
657   case ModelAPI_AttributeTables::DOUBLE:
658     aVal.myDouble = theStrVal.toDouble();
659     break;
660   case ModelAPI_AttributeTables::INTEGER:
661     aVal.myInt = theStrVal.toInt();
662     break;
663   case ModelAPI_AttributeTables::STRING:
664     aVal.myStr = theStrVal.toStdString();
665   }
666   return aVal;
667 }
668
669
670 //**********************************************************************************
671 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
672 {
673   int aOldCol = myCompNamesList.count();
674   int aNbRows = myDataTblList.first()->rowCount();
675   int aDif = theVal - aOldCol;
676   QTableWidgetItem* aItem = 0;
677
678   while (myCompNamesList.count() != theVal) {
679     if (aDif > 0)
680       myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
681     else
682       myCompNamesList.removeLast();
683   }
684
685   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
686   aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
687
688   foreach(QTableWidget* aDataTbl, myDataTblList) {
689     aDataTbl->setColumnCount(theVal + 1);
690     updateHeaders(aDataTbl);
691     for (int i = aOldCol; i < myCompNamesList.count(); i++) {
692       for (int j = 0; j < aNbRows; j++) {
693         aItem = aDataTbl->item(j, i + 1);
694         if (!aItem) {
695           aItem = createDefaultItem();
696           if (j == 0)
697             aItem->setBackgroundColor(Qt::lightGray);
698           aDataTbl->setItem(j, i + 1, aItem);
699         }
700       }
701     }
702   }
703   emit valuesChanged();
704 }
705
706 //**********************************************************************************
707 void CollectionPlugin_WidgetField::onAddStep()
708 {
709   int aMax = myStepSlider->maximum();
710   aMax++;
711   myStepSlider->setMaximum(aMax);
712   myMaxLbl->setText(QString::number(aMax));
713   appendStepControls();
714   myStepSlider->setValue(aMax);
715
716   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
717   aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
718
719
720   AttributeSelectionListPtr aSelList =
721     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
722   if (!aSelList->isInitialized())
723     return;
724   int aSelNb = aSelList->size();
725   if (aSelNb == 0)
726     return;
727
728   int aColumns = myNbComponentsSpn->value() + 1;
729   int aRows = aSelNb + 1;
730   QTableWidget* aTable = myDataTblList.last();
731   aTable->setRowCount(aRows);
732   QTableWidgetItem* aItem = 0;
733   for(int i = 0; i < aColumns; i++) {
734     if (i == 0) {
735       for(int j = 1; j < aRows; j++) {
736         aItem = aTable->item(j, i);
737         if (!aItem) {
738           aItem = new QTableWidgetItem();
739           aTable->setItem(j, i, aItem);
740         }
741         AttributeSelectionPtr aAttr = aSelList->value(j - 1);
742         aItem->setText(aAttr->namingName().c_str());
743         aItem->setToolTip(aAttr->namingName().c_str());
744       }
745     } else {
746       QString aDefVal = aTable->item(0, i)->text();
747       for(int j = 1; j < aRows; j++) {
748         aItem = aTable->item(j, i);
749         if (!aItem) {
750           aItem = new QTableWidgetItem();
751           aTable->setItem(j, i, aItem);
752         }
753         aItem->setText(aDefVal);
754       }
755     }
756   }
757 }
758
759 //**********************************************************************************
760 void CollectionPlugin_WidgetField::onRemoveStep()
761 {
762   int aMax = myStepSlider->maximum();
763   aMax--;
764   myMaxLbl->setText(QString::number(aMax));
765   removeStepControls();
766   myStepSlider->setMaximum(aMax);
767
768   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
769   aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
770 }
771
772 //**********************************************************************************
773 void CollectionPlugin_WidgetField::clearData()
774 {
775   foreach(QTableWidget* aDataTbl, myDataTblList) {
776     aDataTbl->setRowCount(1);
777   }
778 }
779
780 //**********************************************************************************
781 void CollectionPlugin_WidgetField::onStepMove(int theStep)
782 {
783   myCurStepLbl->setText(QString::number(theStep));
784   myStepWgt->setCurrentIndex(theStep - 1);
785 }
786
787 //**********************************************************************************
788 bool CollectionPlugin_WidgetField::
789   isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
790 {
791   return (myShapeTypeCombo->currentIndex() == 5)? false : true;
792 }
793
794 //**********************************************************************************
795 void CollectionPlugin_WidgetField::onSelectionChanged()
796 {
797   if (myActivation) {
798     myActivation = false;
799     return;
800   }
801   // Ignore selection for Parts mode
802   if (myShapeTypeCombo->currentIndex() == 5)
803     return;
804
805   QList<ModuleBase_ViewerPrsPtr> aSelected =
806     myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
807
808   AttributeSelectionListPtr aSelList =
809     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
810   aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
811   aSelList->clear();
812
813   ResultPtr aResult;
814   GeomShapePtr aShape;
815   int aNbData = 0;
816   foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
817     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
818     aShape = aPrs->shape();
819     if (!aResult.get() && !aShape.get())
820       continue;
821     if (!aSelList->isInList(aResult, aShape)) {
822       aSelList->append(aResult, aShape);
823       aNbData++;
824     }
825   }
826   int aColumns = myDataTblList.first()->columnCount();
827   int aRows = myDataTblList.first()->rowCount();
828   int aNewRows = aNbData + 1;
829   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
830   aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
831
832   QTableWidgetItem* aItem = 0;
833   foreach(QTableWidget* aTable, myDataTblList) {
834     aTable->setRowCount(aNewRows);
835     if (aNewRows > aRows) {
836       // Add new data
837       for(int i = 0; i < aColumns; i++) {
838         if (i == 0) {
839           for(int j = 1; j < aNewRows; j++) {
840             aItem = aTable->item(j, i);
841             if (!aItem) {
842               aItem = new QTableWidgetItem();
843               aTable->setItem(j, i, aItem);
844             }
845             AttributeSelectionPtr aAttr = aSelList->value(j - 1);
846             aItem->setText(aAttr->namingName().c_str());
847             aItem->setToolTip(aAttr->namingName().c_str());
848           }
849         } else {
850           QString aDefVal = aTable->item(0, i)->text();
851           for(int j = aRows; j < aNewRows; j++) {
852             aItem = aTable->item(j, i);
853             if (!aItem) {
854               aItem = new QTableWidgetItem();
855               aTable->setItem(j, i, aItem);
856             }
857             aItem->setText(aDefVal);
858           }
859         }
860       }
861     } else {
862       // Update only selection name
863       for(int j = 1; j < aNewRows - 1; j++) {
864         AttributeSelectionPtr aAttr = aSelList->value(j);
865         aTable->item(j, 0)->setText(aAttr->namingName().c_str());
866         aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
867       }
868     }
869   }
870   emit valuesChanged();
871 }
872
873 //**********************************************************************************
874 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
875 {
876   DataTableItemDelegate* aDelegate = 0;
877   aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
878   if (aDelegate) {
879     ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
880     if (aOldType != theIdx) {
881       aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
882       int aColumns = myDataTblList.first()->columnCount();
883       int aRows = myDataTblList.first()->rowCount();
884       foreach(QTableWidget* aTable, myDataTblList) {
885         for(int i = 1; i < aColumns; i++) {
886           for(int j = 0; j < aRows; j++) {
887             switch (theIdx) {
888             case ModelAPI_AttributeTables::DOUBLE:
889             case ModelAPI_AttributeTables::INTEGER:
890               if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
891                   (aOldType == ModelAPI_AttributeTables::STRING)) {
892                     aTable->item(j, i)->setText("0");
893               }
894               break;
895             case ModelAPI_AttributeTables::BOOLEAN:
896               aTable->item(j, i)->setText(MYFalse);
897               break;
898             case ModelAPI_AttributeTables::STRING:
899               aTable->item(j, i)->setText("");
900               break;
901             }
902           }
903         }
904       }
905       emit valuesChanged();
906     }
907   }
908 }
909
910 //**********************************************************************************
911 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
912 {
913   // Do not store here column of names
914   if (theCol == 0)
915     return;
916   if (!myFeature.get())
917     return;
918   QTableWidget* aTable = static_cast<QTableWidget*>(sender());
919   int aNb = myDataTblList.indexOf(aTable);
920   if (aNb == -1)
921     return;
922   ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
923
924   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
925   if (aTablesAttr->isInitialized())
926     aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
927   else
928     emit valuesChanged();
929 }
930
931 //**********************************************************************************
932 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
933 {
934   activateSelectionAndFilters(theType == 5? false:true);
935
936   AttributeSelectionListPtr aSelList =
937     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
938
939   std::string aTypeName = getSelectionType(theType);
940   if (aTypeName == aSelList->selectionType())
941     return;
942   aSelList->setSelectionType(aTypeName);
943
944   //Clear old selection
945   clearData();
946   aSelList->clear();
947   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
948   aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
949   emit valuesChanged();
950 }
951
952 //**********************************************************************************
953 bool CollectionPlugin_WidgetField::processEnter()
954 {
955   if (myIsEditing) {
956     myIsEditing = false;
957     return true;
958   }
959   return false;
960 }
961
962 //**********************************************************************************
963 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
964 {
965   if (theNew && (!myIsEditing))
966     myIsEditing = dynamic_cast<QLineEdit*>(theNew);
967 }
968
969 //**********************************************************************************
970 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
971 {
972   myMaxLbl->setText(QString::number(theMax));
973   myRemoveBtn->setEnabled(theMax > 1);
974 }
975
976 //**********************************************************************************
977 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
978 {
979   if (myDataTblList.count() < 2)
980     return;
981   QObject* aSender = sender();
982   foreach(QTableWidget* aTable, myDataTblList) {
983     if (aTable->horizontalHeader() != aSender)
984       aTable->setColumnWidth(theIndex, theNew);
985   }
986 }