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