]> SALOME platform Git repositories - modules/shaper.git/blob - src/CollectionPlugin/CollectionPlugin_WidgetField.cpp
Salome HOME
Issue #1834: Fix length of lines
[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
9 #include <ModuleBase_Tools.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_ISelection.h>
12
13 #include <QLayout>
14 #include <QWidget>
15 #include <QFormLayout>
16 #include <QComboBox>
17 #include <QSpinBox>
18 #include <QLabel>
19 #include <QSlider>
20 #include <QTableWidget>
21 #include <QPushButton>
22 #include <QHeaderView>
23 #include <QStackedWidget>
24 #include <QValidator>
25 #include <QStyledItemDelegate>
26 #include <QLineEdit>
27
28
29 class DataTableItemDelegate : public QStyledItemDelegate
30 {
31 public:
32   enum DataType {
33     DoubleType,
34     IntegerType,
35     BooleanType,
36     StringType };
37
38   DataTableItemDelegate(DataType theType) : QStyledItemDelegate() { myType = theType; }
39
40   virtual QWidget* createEditor(QWidget* theParent, 
41                                 const QStyleOptionViewItem & theOption, 
42                                 const QModelIndex& theIndex) const;
43
44   virtual void setModelData(QWidget* theEditor, QAbstractItemModel* theModel, 
45                             const QModelIndex& theIndex) const;
46
47   DataType dataType() const { return myType; }
48
49   void setDataType(DataType theType) { myType = theType; }
50
51 private:
52   DataType myType;
53 };
54
55 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, 
56                                              const QStyleOptionViewItem & theOption, 
57                                              const QModelIndex& theIndex ) const
58 {
59   if ((theIndex.column() != 0) && (theIndex.row() != 0)) {
60     QLineEdit* aLineEdt = 0;
61     switch (myType) {
62     case DoubleType:
63       aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent, 
64                                                                             theOption, 
65                                                                             theIndex));
66       if (aLineEdt) {
67         aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
68         return aLineEdt;
69       }
70     case IntegerType:
71       aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent, 
72                                                                             theOption, 
73                                                                             theIndex));
74       if (aLineEdt) {
75         aLineEdt->setValidator(new QIntValidator(aLineEdt));
76         return aLineEdt;
77       }
78     case BooleanType: 
79       {
80         QComboBox* aBox = new QComboBox(theParent);
81         aBox->addItem("True");
82         aBox->addItem("False");
83         return aBox;
84       }
85     }
86   }
87   return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
88 }
89
90
91 void DataTableItemDelegate::setModelData(QWidget* theEditor, QAbstractItemModel* theModel, 
92                                          const QModelIndex& theIndex) const
93 {
94   QComboBox* aBox = dynamic_cast<QComboBox*>(theEditor);
95   if (aBox) {
96     theModel->setData(theIndex, aBox->currentText());
97   } else 
98     QStyledItemDelegate::setModelData(theEditor, theModel, theIndex);
99 }
100
101
102
103 CollectionPlugin_WidgetField::
104   CollectionPlugin_WidgetField(QWidget* theParent, 
105                                ModuleBase_IWorkshop* theWorkshop, 
106                                const Config_WidgetAPI* theData):
107 ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
108 {
109   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
110
111   // Types definition controls
112   QWidget* aTypesWgt = new QWidget(this);
113   QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
114   aTypesLayout->setContentsMargins(0, 0, 0, 0);
115   aMainLayout->addWidget(aTypesWgt);
116
117   // Type of shapes
118   myShapeTypeCombo = new QComboBox(aTypesWgt);
119   QStringList aShapeTypes;
120   aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces") 
121     << tr("Solids") << tr("Results") << tr("Parts");
122   myShapeTypeCombo->addItems(aShapeTypes);
123   aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
124
125   // Type of field
126   myFieldTypeCombo = new QComboBox(aTypesWgt);
127   QStringList aFieldTypes;
128   aFieldTypes << tr("Double") << tr("Integer") << tr("Boolean") 
129     << tr("String");
130   myFieldTypeCombo->addItems(aFieldTypes);
131   aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
132
133   // Number of components
134   myNbComponentsSpn = new QSpinBox(aTypesWgt);
135   myNbComponentsSpn->setMinimum(1);
136   aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
137
138   // Steps controls
139   QFrame* aStepFrame = new QFrame(this);
140   aStepFrame->setFrameShape(QFrame::Box);
141   aStepFrame->setFrameStyle(QFrame::StyledPanel);
142   QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
143   aMainLayout->addWidget(aStepFrame);
144
145   // Current step label
146   aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
147   myCurStepLbl = new QLabel("1", aStepFrame);
148   QFont aFont = myCurStepLbl->font();
149   aFont.setBold(true);
150   myCurStepLbl->setFont(aFont);
151   aStepLayout->addWidget(myCurStepLbl, 0, 1);
152
153   // Steps slider
154   QWidget* aSliderWidget = new QWidget(aStepFrame);
155   aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
156   QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
157   aSliderLayout->setContentsMargins(0, 0, 0, 0);
158
159   aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
160
161   myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
162   myStepSlider->setTickPosition(QSlider::TicksBelow);
163   myStepSlider->setRange(1, 1);
164   myStepSlider->setPageStep(myStepSlider->singleStep());
165   aSliderLayout->addWidget(myStepSlider, 1);
166
167   myMaxLbl = new QLabel("1", aSliderWidget);
168   aSliderLayout->addWidget(myMaxLbl);
169
170   // Stamp value
171   myCompNamesList << "Comp 1";
172   myStepWgt = new QStackedWidget(aStepFrame);
173   aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
174   appendStepControls();
175
176   // Buttons below
177   QWidget* aBtnWgt = new QWidget(this);
178   aMainLayout->addWidget(aBtnWgt);
179   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
180   aBtnLayout->setContentsMargins(0, 0, 0, 0);
181
182   QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
183   aBtnLayout->addWidget(aAddBtn);
184
185   aBtnLayout->addStretch(1);
186
187   myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
188   aBtnLayout->addWidget(myRemoveBtn);
189   myRemoveBtn->setEnabled(false);
190
191   connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
192   connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
193   connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
194   connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
195   connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
196 }
197
198 void CollectionPlugin_WidgetField::appendStepControls()
199 {
200   QWidget* aWidget = new QWidget(myStepWgt);
201   QGridLayout* aStepLayout = new QGridLayout(aWidget);
202   aStepLayout->setContentsMargins(0, 0, 0, 0);
203
204   aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
205
206   QSpinBox* aStampSpn = new QSpinBox(aWidget);
207   aStepLayout->addWidget(aStampSpn, 0, 1);
208
209   myStampSpnList.append(aStampSpn);
210
211   // Data table
212   QTableWidget* aDataTbl = new QTableWidget(2, myCompNamesList.count() + 1, aWidget);
213   aDataTbl->setItemDelegate(
214     new DataTableItemDelegate((DataTableItemDelegate::DataType) myFieldTypeCombo->currentIndex()));
215   aDataTbl->verticalHeader()->hide();
216   aDataTbl->horizontalHeader()->hide();
217   aDataTbl->setRowHeight(0, 25);
218   aDataTbl->setRowHeight(1, 25);
219
220   QTableWidgetItem* aItem = new QTableWidgetItem("Shape");
221   aItem->setBackgroundColor(Qt::lightGray);
222   aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
223   aDataTbl->setItem(0, 0, aItem);
224
225   aItem = new QTableWidgetItem("Default value");
226   aItem->setBackgroundColor(Qt::lightGray);
227   aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
228   aDataTbl->setItem(1, 0, aItem);
229
230   for (int i = 0; i < myCompNamesList.count(); i++) {
231     aItem = new QTableWidgetItem(myCompNamesList[i]);
232     aItem->setBackgroundColor(Qt::lightGray);
233     aDataTbl->setItem(0, i + 1, aItem);
234
235     aItem = new QTableWidgetItem("0");
236     aItem->setBackgroundColor(Qt::lightGray);
237     aDataTbl->setItem(1, i + 1, aItem);
238   }
239   aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
240
241   QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
242   myDataTblList.append(aDataTbl);
243
244   myStepWgt->addWidget(aWidget);
245 }
246
247 void CollectionPlugin_WidgetField::removeStepControls()
248 {
249   int aCurWgtId = myStepWgt->currentIndex();
250   myStepWgt->removeWidget(myStepWgt->currentWidget());
251
252   myStampSpnList.removeAt(aCurWgtId);
253   myDataTblList.removeAt(aCurWgtId);
254 }
255
256
257 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
258 {
259   QList<QWidget*> aControls;
260   // this control will accept focus and will be highlighted in the Property Panel
261   //aControls.push_back(myShapeTypeCombo);
262   //aControls.push_back(myFieldTypeCombo);
263   //aControls.push_back(myNbComponentsSpn);
264   return aControls;
265 }
266
267 bool CollectionPlugin_WidgetField::storeValueCustom()
268 {
269   //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
270   //AttributeIntegerPtr aValueAttribute =
271   //                      std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
272   //aValueAttribute->setValue(myComboBox->currentIndex());
273   //updateObject(myFeature);
274   return true;
275 }
276
277 bool CollectionPlugin_WidgetField::restoreValueCustom()
278 {
279   //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
280   //AttributeIntegerPtr aValueAttribute =
281   //                      std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
282
283   //bool isBlocked = myComboBox->blockSignals(true);
284   //myComboBox->setCurrentIndex(aValueAttribute->value());
285   //myComboBox->blockSignals(isBlocked);
286
287   return true;
288 }
289
290 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
291 {
292   int aOldCol = myCompNamesList.count();
293   int aNbRows = myDataTblList.first()->rowCount();
294   int aDif = theVal - aOldCol;
295   QTableWidgetItem* aItem = 0;
296
297   while (myCompNamesList.count() != theVal) {
298     if (aDif > 0)
299       myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
300     else
301       myCompNamesList.removeLast();
302   }
303
304   foreach(QTableWidget* aDataTbl, myDataTblList) {
305     aDataTbl->setColumnCount(theVal + 1);
306     for (int i = 0; i < myCompNamesList.count(); i++) {
307       for (int j = 0; j < aNbRows; j++) {
308         aItem = new QTableWidgetItem();
309         if (j == 0)
310           aItem->setText(myCompNamesList.at(i));
311         else
312           aItem->setText("0");
313         if (j < 3)
314           aItem->setBackgroundColor(Qt::lightGray);
315         aDataTbl->setItem(j, i + aOldCol + 1, aItem);
316       }
317     }
318   }
319 }
320
321 void CollectionPlugin_WidgetField::onAddStep()
322 {
323   int aMax = myStepSlider->maximum();
324   aMax++;
325   myStepSlider->setMaximum(aMax);
326   myMaxLbl->setText(QString::number(aMax));
327   appendStepControls();
328   myStepSlider->setValue(aMax);
329   myRemoveBtn->setEnabled(aMax > 1);
330 }
331
332 void CollectionPlugin_WidgetField::onRemoveStep()
333 {
334   int aMax = myStepSlider->maximum();
335   aMax--;
336   myMaxLbl->setText(QString::number(aMax));
337   removeStepControls();
338   myStepSlider->setMaximum(aMax);
339   myRemoveBtn->setEnabled(aMax > 1);
340 }
341
342 void CollectionPlugin_WidgetField::clearData()
343 {
344   int aNbRows = myDataTblList.first()->rowCount();
345   int aNbCol = myDataTblList.first()->columnCount();
346
347   foreach(QTableWidget* aDataTbl, myDataTblList) {
348     QString aDefValue;
349     for (int i = 1; i < aNbCol; i++) {
350       aDefValue = aDataTbl->item(1, i)->text();
351       for (int j = 2; j < aNbRows; j++) {
352         aDataTbl->item(j, i)->setText(aDefValue);
353       }
354     }
355   }
356 }
357
358 void CollectionPlugin_WidgetField::onStepMove(int theStep)
359 {
360   myCurStepLbl->setText(QString::number(theStep));
361   myStepWgt->setCurrentIndex(theStep - 1);
362 }
363
364 QIntList CollectionPlugin_WidgetField::shapeTypes() const
365 {
366   QIntList aRes;
367   switch (myShapeTypeCombo->currentIndex()) {
368   case 0: //"Vertices"
369     aRes.append(ModuleBase_Tools::shapeType("vertex"));
370     break;
371   case 1: // "Edges"
372     aRes.append(ModuleBase_Tools::shapeType("edge"));
373     break;
374   case 2: // "Faces"
375     aRes.append(ModuleBase_Tools::shapeType("face"));
376     break;
377   case 3: // "Solids"
378     aRes.append(ModuleBase_Tools::shapeType("solid"));
379     break;
380   case 4: // "Results"
381     aRes.append(ModuleBase_Tools::shapeType("object"));
382     break;
383   case 5: // "Parts"
384     // TODO: Selection mode for Parts
385     break;
386   }
387   return aRes;
388 }
389
390 bool CollectionPlugin_WidgetField::
391   isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
392 {
393   return true;
394 }
395
396 void CollectionPlugin_WidgetField::onSelectionChanged()
397 {
398   QList<ModuleBase_ViewerPrsPtr> aSelected = 
399     myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
400
401   clearData();
402
403   foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
404
405   }
406 }
407
408 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
409 {
410   DataTableItemDelegate* aDelegate = 0;
411   foreach(QTableWidget* aTable, myDataTblList) {
412     aDelegate = dynamic_cast<DataTableItemDelegate*>(aTable->itemDelegate());
413     if (aDelegate)
414       aDelegate->setDataType((DataTableItemDelegate::DataType)theIdx);
415   }
416 }