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