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