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   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
611   aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
612
613   foreach(QTableWidget* aDataTbl, myDataTblList) {
614     aDataTbl->setColumnCount(theVal + 1);
615     updateHeaders(aDataTbl);
616     for (int i = aOldCol; i < myCompNamesList.count(); i++) {
617       for (int j = 0; j < aNbRows; j++) {
618         aItem = createDefaultItem();
619         if (j == 0)
620           aItem->setBackgroundColor(Qt::lightGray);
621         aDataTbl->setItem(j, i + 1, aItem);
622       }
623     }
624   }
625   emit valuesChanged();
626 }
627
628 //**********************************************************************************
629 void CollectionPlugin_WidgetField::onAddStep()
630 {
631   int aMax = myStepSlider->maximum();
632   aMax++;
633   myStepSlider->setMaximum(aMax);
634   myMaxLbl->setText(QString::number(aMax));
635   appendStepControls();
636   myStepSlider->setValue(aMax);
637   myRemoveBtn->setEnabled(aMax > 1);
638 }
639
640 //**********************************************************************************
641 void CollectionPlugin_WidgetField::onRemoveStep()
642 {
643   int aMax = myStepSlider->maximum();
644   aMax--;
645   myMaxLbl->setText(QString::number(aMax));
646   removeStepControls();
647   myStepSlider->setMaximum(aMax);
648   myRemoveBtn->setEnabled(aMax > 1);
649 }
650
651 //**********************************************************************************
652 void CollectionPlugin_WidgetField::clearData()
653 {
654   foreach(QTableWidget* aDataTbl, myDataTblList) {
655     aDataTbl->setRowCount(1);
656   }
657 }
658
659 //**********************************************************************************
660 void CollectionPlugin_WidgetField::onStepMove(int theStep)
661 {
662   myCurStepLbl->setText(QString::number(theStep));
663   myStepWgt->setCurrentIndex(theStep - 1);
664 }
665
666 //**********************************************************************************
667 bool CollectionPlugin_WidgetField::
668   isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
669 {
670   return true;
671 }
672
673 //**********************************************************************************
674 void CollectionPlugin_WidgetField::onSelectionChanged()
675 {
676   QList<ModuleBase_ViewerPrsPtr> aSelected = 
677     myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
678
679   clearData();
680   AttributeSelectionListPtr aSelList = 
681     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
682   aSelList->clear();
683   aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
684
685   ResultPtr aResult;
686   GeomShapePtr aShape;
687   int aNbData = 0;
688   foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
689     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
690     aShape = aPrs->shape();
691     if (!aResult.get() && !aShape.get())
692       continue;
693     if (!aSelList->isInList(aResult, aShape)) {
694       aSelList->append(aResult, aShape);
695       aNbData++;
696     }
697   }
698   int aColumns = myDataTblList.first()->columnCount();
699   int aRows = myDataTblList.first()->rowCount();
700   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
701   aTablesAttr->setSize(aRows + aNbData, aColumns - 1, myDataTblList.size());
702
703   QTableWidgetItem* aItem = 0;
704   foreach(QTableWidget* aTable, myDataTblList) {
705     aTable->setRowCount(aRows + aNbData);
706     for(int i = 0; i < aColumns; i++) {
707       QString aDefVal = aTable->item(0, i)->text();
708       for(int j = 1; j < aRows + aNbData; j++) {
709         aItem = new QTableWidgetItem();
710         if (i == 0) {
711           AttributeSelectionPtr aAttr = aSelList->value(j - 1);
712           aItem->setText(aAttr->namingName().c_str());
713           aItem->setToolTip(aAttr->namingName().c_str());
714         } else {
715           aItem->setText(aDefVal);
716         }
717         aTable->setItem(j, i, aItem);
718       }
719     }
720   }
721   emit valuesChanged();
722 }
723
724 //**********************************************************************************
725 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
726 {
727   DataTableItemDelegate* aDelegate = 0;
728   aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
729   if (aDelegate) {
730     ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
731     if (aOldType != theIdx) {
732       aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
733       int aColumns = myDataTblList.first()->columnCount();
734       int aRows = myDataTblList.first()->rowCount();
735       foreach(QTableWidget* aTable, myDataTblList) {
736         for(int i = 1; i < aColumns; i++) {
737           for(int j = 0; j < aRows; j++) {
738             switch (theIdx) {
739             case ModelAPI_AttributeTables::DOUBLE:
740             case ModelAPI_AttributeTables::INTEGER:
741               if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
742                   (aOldType == ModelAPI_AttributeTables::STRING)) {
743                     aTable->item(j, i)->setText("0");
744               }
745               break;
746             case ModelAPI_AttributeTables::BOOLEAN: 
747               aTable->item(j, i)->setText(MYFalse);
748               break;
749             }
750           }
751         }
752       }
753       emit valuesChanged();
754     }
755   }
756 }
757
758 //**********************************************************************************
759 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
760 {
761   // Do not store here column of names
762   if (theCol == 0)
763     return;
764   if (!myFeature.get())
765     return;
766   QTableWidget* aTable = static_cast<QTableWidget*>(sender());
767   int aNb = myDataTblList.indexOf(aTable);
768   if (aNb == -1)
769     return;
770   ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
771
772   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
773   if (aTablesAttr->isInitialized())
774     aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
775   else
776     emit valuesChanged();
777 }
778
779 //**********************************************************************************
780 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
781 {
782   activateSelectionAndFilters(true);
783
784   AttributeSelectionListPtr aSelList = 
785     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
786   if (!aSelList->isInitialized())
787     return;
788   std::string aTypeName = getSelectionType(theType);
789   if (aTypeName == aSelList->selectionType())
790     return;
791
792   //Clear old selection
793   clearData();
794   aSelList->clear();
795   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
796   aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
797 }