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