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