Salome HOME
Merge branch 'master' into cgt/devCEA
[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   updateObject(myFeature);
503   return true;
504 }
505
506 //**********************************************************************************
507 bool CollectionPlugin_WidgetField::restoreValueCustom()
508 {
509   bool isBlocked;
510   DataPtr aData = myFeature->data();
511
512   AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
513   std::string aTypeStr = aSelList->selectionType();
514   if (aTypeStr == "")
515     return false; // The attribute is not initialized
516   isBlocked = myShapeTypeCombo->blockSignals(true);
517   myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
518   myShapeTypeCombo->blockSignals(isBlocked);
519
520   // Get number of components
521   AttributeStringArrayPtr aStringsAttr =
522   aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
523
524   myCompNamesList.clear();
525   for (int i = 0; i < aStringsAttr->size(); i++) {
526     myCompNamesList.append(aStringsAttr->value(i).c_str());
527   }
528   isBlocked = myNbComponentsSpn->blockSignals(true);
529   myNbComponentsSpn->setValue(myCompNamesList.size());
530   myNbComponentsSpn->blockSignals(isBlocked);
531
532   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
533   // Get number of steps
534   int aNbSteps = aTablesAttr->tables();
535   myStepSlider->setMaximum(aNbSteps);
536   //myStepSlider->setValue(1);
537   // Clear old tables
538   while (myDataTblList.count() > aNbSteps) {
539     QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
540     myStepWgt->removeWidget(aWgt);
541     aWgt->deleteLater();
542
543     myStampSpnList.removeLast();
544     myDataTblList.removeLast();
545   }
546   while (myDataTblList.count() < aNbSteps)
547     appendStepControls();
548   //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
549   clearData();
550
551   // Get Type of the field values
552   isBlocked = myFieldTypeCombo->blockSignals(true);
553   myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
554   myFieldTypeCombo->blockSignals(isBlocked);
555   myDelegate->setDataType(aTablesAttr->type());
556
557   AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
558   // Fill data table
559   int aRows = aTablesAttr->rows();
560   int aCols = aTablesAttr->columns();
561
562   // Get width of columns
563   QIntList aColWidth;
564   QTableWidget* aFirstTable = myDataTblList.first();
565   for (int i = 0; i < aFirstTable->columnCount(); i++)
566     aColWidth.append(aFirstTable->columnWidth(i));
567
568   QTableWidgetItem* aItem = 0;
569   for (int i = 0; i < aNbSteps; i++) {
570     myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
571     QTableWidget* aTable = myDataTblList.at(i);
572     isBlocked = aTable->blockSignals(true);
573     aTable->setRowCount(aRows);
574     aTable->setColumnCount(aCols + 1);
575     updateHeaders(aTable);
576     for (int j = 0; j < aCols + 1; j++) {
577       for (int k = 0; k < aRows; k++) {
578         aItem = aTable->item(k, j);
579         if ((j == 0) && (k > 0)) {
580           // Add selection names
581           AttributeSelectionPtr aAttr = aSelList->value(k - 1);
582           if (aItem) {
583             aItem->setText(aAttr->namingName().c_str());
584           } else {
585             aItem = new QTableWidgetItem(aAttr->namingName().c_str());
586             aTable->setItem(k, j, aItem);
587           }
588         } else if (j > 0) {
589           // Add Values
590           ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
591           if (aItem) {
592             aItem->setText(getValueText(aVal));
593           } else {
594             aItem = createValueItem(aVal);
595             if (k == 0)
596               aItem->setBackgroundColor(Qt::lightGray);
597             aTable->setItem(k, j, aItem);
598           }
599         }
600       }
601     }
602     // Restore columns width
603     for (int i = 0; i < aTable->columnCount(); i++) {
604       if (i < aColWidth.size())
605         aTable->setColumnWidth(i, aColWidth.at(i));
606     }
607
608     aTable->blockSignals(isBlocked);
609   }
610   return true;
611 }
612
613 //**********************************************************************************
614 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
615 {
616   QString aType(theStr.c_str());
617   aType = aType.toLower();
618   if (aType == "vertex")
619     return 0;
620   else if (aType == "edge")
621     return 1;
622   else if (aType == "face")
623     return 2;
624   else if (aType == "solid")
625     return 3;
626   else if (aType == "object")
627     return 4;
628   else if (aType == "part")
629     return 5;
630   return -1;
631 }
632
633
634 //**********************************************************************************
635 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
636 {
637   switch (theType) {
638   case 0: //"Vertices"
639     return "vertex";
640   case 1: // "Edges"
641     return "edge";
642   case 2: // "Faces"
643     return "face";
644   case 3: // "Solids"
645     return "solid";
646   case 4: // "Results"
647     return "object";
648   case 5: // "Parts"
649     return "part";
650   }
651   return "";
652 }
653
654 //**********************************************************************************
655 QIntList CollectionPlugin_WidgetField::shapeTypes() const
656 {
657   QIntList aRes;
658   switch (myShapeTypeCombo->currentIndex()) {
659   case 0: //"Vertices"
660     aRes.append(ModuleBase_Tools::shapeType("vertex"));
661     break;
662   case 1: // "Edges"
663     aRes.append(ModuleBase_Tools::shapeType("edge"));
664     break;
665   case 2: // "Faces"
666     aRes.append(ModuleBase_Tools::shapeType("face"));
667     break;
668   case 3: // "Solids"
669     aRes.append(ModuleBase_Tools::shapeType("solid"));
670     break;
671   case 4: // "Results"
672     aRes.append(ModuleBase_Tools::shapeType("object"));
673     break;
674   case 5: // "Parts"
675     // TODO: Selection mode for Parts
676     break;
677   }
678   return aRes;
679 }
680
681 //**********************************************************************************
682 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
683 {
684   ModelAPI_AttributeTables::Value aVal;
685   switch (myFieldTypeCombo->currentIndex()) {
686   case ModelAPI_AttributeTables::BOOLEAN:
687     aVal.myBool = (theStrVal == MYTrue)? true : false;
688     break;
689   case ModelAPI_AttributeTables::DOUBLE:
690     aVal.myDouble = theStrVal.toDouble();
691     break;
692   case ModelAPI_AttributeTables::INTEGER:
693     aVal.myInt = theStrVal.toInt();
694     break;
695   case ModelAPI_AttributeTables::STRING:
696     aVal.myStr = theStrVal.toStdString();
697   }
698   return aVal;
699 }
700
701
702 //**********************************************************************************
703 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
704 {
705   int aOldCol = myCompNamesList.count();
706   int aNbRows = myDataTblList.first()->rowCount();
707   int aDif = theVal - aOldCol;
708   QTableWidgetItem* aItem = 0;
709
710   while (myCompNamesList.count() != theVal) {
711     if (aDif > 0)
712       myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
713     else
714       myCompNamesList.removeLast();
715   }
716
717   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
718   aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
719
720   foreach(QTableWidget* aDataTbl, myDataTblList) {
721     aDataTbl->setColumnCount(theVal + 1);
722     updateHeaders(aDataTbl);
723     for (int i = aOldCol; i < myCompNamesList.count(); i++) {
724       for (int j = 0; j < aNbRows; j++) {
725         aItem = aDataTbl->item(j, i + 1);
726         if (!aItem) {
727           aItem = createDefaultItem();
728           if (j == 0)
729             aItem->setBackgroundColor(Qt::lightGray);
730           aDataTbl->setItem(j, i + 1, aItem);
731         }
732       }
733     }
734   }
735   emit valuesChanged();
736 }
737
738 //**********************************************************************************
739 void CollectionPlugin_WidgetField::onAddStep()
740 {
741   int aMax = myStepSlider->maximum();
742   aMax++;
743   myStepSlider->setMaximum(aMax);
744   myMaxLbl->setText(QString::number(aMax));
745   appendStepControls();
746   myStepSlider->setValue(aMax);
747
748   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
749   aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
750
751
752   AttributeSelectionListPtr aSelList =
753     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
754   if (!aSelList->isInitialized())
755     return;
756   int aSelNb = aSelList->size();
757   if (aSelNb == 0)
758     return;
759
760   int aColumns = myNbComponentsSpn->value() + 1;
761   int aRows = aSelNb + 1;
762   QTableWidget* aTable = myDataTblList.last();
763   aTable->setRowCount(aRows);
764   QTableWidgetItem* aItem = 0;
765   for(int i = 0; i < aColumns; i++) {
766     if (i == 0) {
767       for(int j = 1; j < aRows; j++) {
768         aItem = aTable->item(j, i);
769         if (!aItem) {
770           aItem = new QTableWidgetItem();
771           aTable->setItem(j, i, aItem);
772         }
773         AttributeSelectionPtr aAttr = aSelList->value(j - 1);
774         aItem->setText(aAttr->namingName().c_str());
775         aItem->setToolTip(aAttr->namingName().c_str());
776       }
777     } else {
778       QString aDefVal = aTable->item(0, i)->text();
779       for(int j = 1; j < aRows; j++) {
780         aItem = aTable->item(j, i);
781         if (!aItem) {
782           aItem = new QTableWidgetItem();
783           aTable->setItem(j, i, aItem);
784         }
785         aItem->setText(aDefVal);
786       }
787     }
788   }
789   emit valuesChanged();
790 }
791
792 //**********************************************************************************
793 void CollectionPlugin_WidgetField::onRemoveStep()
794 {
795   int aMax = myStepSlider->maximum();
796   aMax--;
797   myMaxLbl->setText(QString::number(aMax));
798   removeStepControls();
799   myStepSlider->setMaximum(aMax);
800
801   //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
802   //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
803   emit valuesChanged();
804 }
805
806 //**********************************************************************************
807 void CollectionPlugin_WidgetField::clearData()
808 {
809   foreach(QTableWidget* aDataTbl, myDataTblList) {
810     aDataTbl->setRowCount(1);
811   }
812 }
813
814 //**********************************************************************************
815 void CollectionPlugin_WidgetField::onStepMove(int theStep)
816 {
817   myCurStepLbl->setText(QString::number(theStep));
818   myStepWgt->setCurrentIndex(theStep - 1);
819 }
820
821 //**********************************************************************************
822 bool CollectionPlugin_WidgetField::
823   isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
824 {
825   return (myShapeTypeCombo->currentIndex() == 5)? false : true;
826 }
827
828 //**********************************************************************************
829 bool CollectionPlugin_WidgetField::
830   setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
831 {
832   if (myActivation) {
833     myActivation = false;
834     return false;
835   }
836   // Ignore selection for Parts mode
837   if (myShapeTypeCombo->currentIndex() == 5)
838     return false;
839
840   QList<ModuleBase_ViewerPrsPtr> aSelected;
841   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
842   for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
843     ModuleBase_ViewerPrsPtr aValue = *anIt;
844     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
845     if (theToValidate && aRes.get()) {
846       if (myShapeTypeCombo->currentIndex() > 3)
847         aSelected.append(aValue);
848       else if (acceptSubShape(aValue->shape(), aRes))
849         aSelected.append(aValue);
850     }
851   }
852   AttributeSelectionListPtr aSelList =
853     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
854   aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
855   aSelList->clear();
856
857   ResultPtr aResult;
858   GeomShapePtr aShape;
859   int aNbData = 0;
860   foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
861     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
862     aShape = aPrs->shape();
863     if (!aResult.get() && !aShape.get())
864       continue;
865     if (!aSelList->isInList(aResult, aShape)) {
866       aSelList->append(aResult, aShape);
867       aNbData++;
868     }
869   }
870   int aColumns = myDataTblList.first()->columnCount();
871   int aRows = myDataTblList.first()->rowCount();
872   int aNewRows = aNbData + 1;
873   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
874   aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
875
876   QTableWidgetItem* aItem = 0;
877   foreach(QTableWidget* aTable, myDataTblList) {
878     aTable->setRowCount(aNewRows);
879     if (aNewRows > aRows) {
880       // Add new data
881       for(int i = 0; i < aColumns; i++) {
882         if (i == 0) {
883           for(int j = 1; j < aNewRows; j++) {
884             aItem = aTable->item(j, i);
885             if (!aItem) {
886               aItem = new QTableWidgetItem();
887               aTable->setItem(j, i, aItem);
888             }
889             AttributeSelectionPtr aAttr = aSelList->value(j - 1);
890             aItem->setText(aAttr->namingName().c_str());
891             aItem->setToolTip(aAttr->namingName().c_str());
892           }
893         } else {
894           QString aDefVal = aTable->item(0, i)->text();
895           for(int j = aRows; j < aNewRows; j++) {
896             aItem = aTable->item(j, i);
897             if (!aItem) {
898               aItem = new QTableWidgetItem();
899               aTable->setItem(j, i, aItem);
900             }
901             aItem->setText(aDefVal);
902           }
903         }
904       }
905     } else {
906       // Update only selection name
907       for(int j = 1; j < aNewRows - 1; j++) {
908         AttributeSelectionPtr aAttr = aSelList->value(j);
909         aTable->item(j, 0)->setText(aAttr->namingName().c_str());
910         aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
911       }
912     }
913   }
914   return true;
915 }
916
917 //**********************************************************************************
918 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
919 {
920   ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
921   if (aOldType != theIdx) {
922     myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
923     int aColumns = myDataTblList.first()->columnCount();
924     int aRows = myDataTblList.first()->rowCount();
925     foreach(QTableWidget* aTable, myDataTblList) {
926       for(int i = 1; i < aColumns; i++) {
927         for(int j = 0; j < aRows; j++) {
928           switch (theIdx) {
929           case ModelAPI_AttributeTables::DOUBLE:
930           case ModelAPI_AttributeTables::INTEGER:
931             aTable->item(j, i)->setText("0");
932             break;
933           case ModelAPI_AttributeTables::BOOLEAN:
934             aTable->item(j, i)->setText(MYFalse);
935             break;
936           case ModelAPI_AttributeTables::STRING:
937             aTable->item(j, i)->setText("");
938             break;
939           }
940         }
941       }
942     }
943     emit valuesChanged();
944   }
945 }
946
947 //**********************************************************************************
948 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
949 {
950   // Do not store here column of names
951   if (theCol == 0)
952     return;
953   if (!myFeature.get())
954     return;
955   QTableWidget* aTable = static_cast<QTableWidget*>(sender());
956   int aNb = myDataTblList.indexOf(aTable);
957   if (aNb == -1)
958     return;
959   ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
960
961   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
962   if (aTablesAttr->isInitialized())
963     aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
964   else
965     emit valuesChanged();
966 }
967
968 //**********************************************************************************
969 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
970 {
971   activateSelectionAndFilters(theType == 5? false:true);
972
973   AttributeSelectionListPtr aSelList =
974     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
975
976   std::string aTypeName = getSelectionType(theType);
977   if (aTypeName == aSelList->selectionType())
978     return;
979   aSelList->setSelectionType(aTypeName);
980
981   //Clear old selection
982   clearData();
983   aSelList->clear();
984   AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
985   aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
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 }