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