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