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