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