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