Salome HOME
Merge branch 'master' into cgt/devCEA
[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 <QLayout>
10 #include <QWidget>
11 #include <QFormLayout>
12 #include <QComboBox>
13 #include <QSpinBox>
14 #include <QLabel>
15 #include <QSlider>
16 #include <QTableWidget>
17 #include <QPushButton>
18 #include <QHeaderView>
19
20 CollectionPlugin_WidgetField::
21   CollectionPlugin_WidgetField(QWidget* theParent, const Config_WidgetAPI* theData):
22 ModuleBase_ModelWidget(theParent, theData)
23 {
24   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
25
26   // Types definition controls
27   QWidget* aTypesWgt = new QWidget(this);
28   QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
29   aTypesLayout->setContentsMargins(0, 0, 0, 0);
30   aMainLayout->addWidget(aTypesWgt);
31
32   // Type of shapes
33   myShapeTypeCombo = new QComboBox(aTypesWgt);
34   QStringList aShapeTypes;
35   aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces") 
36     << tr("Solids") << tr("Results") << tr("Parts");
37   myShapeTypeCombo->addItems(aShapeTypes);
38   aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
39
40   // Type of field
41   myFieldTypeCombo = new QComboBox(aTypesWgt);
42   QStringList aFieldTypes;
43   aFieldTypes << tr("Double") << tr("Integer") << tr("Boolean") 
44     << tr("String");
45   myFieldTypeCombo->addItems(aFieldTypes);
46   aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
47
48   // Number of components
49   myNbComponentsSpn = new QSpinBox(aTypesWgt);
50   myNbComponentsSpn->setMinimum(1);
51   aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
52
53   // Steps controls
54   QFrame* aStepFrame = new QFrame(this);
55   aStepFrame->setFrameShape(QFrame::Box);
56   aStepFrame->setFrameStyle(QFrame::StyledPanel);
57   QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
58   aMainLayout->addWidget(aStepFrame);
59
60   // Current step label
61   aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
62   myCurStepLbl = new QLabel("1", aStepFrame);
63   QFont aFont = myCurStepLbl->font();
64   aFont.setBold(true);
65   myCurStepLbl->setFont(aFont);
66   aStepLayout->addWidget(myCurStepLbl, 0, 1);
67
68   // Steps slider
69   QWidget* aSliderWidget = new QWidget(aStepFrame);
70   aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
71   QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
72   aSliderLayout->setContentsMargins(0, 0, 0, 0);
73
74   aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
75
76   myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
77   myStepSlider->setTickPosition(QSlider::TicksBelow);
78   myStepSlider->setRange(1, 1);
79   myStepSlider->setPageStep(myStepSlider->singleStep());
80   aSliderLayout->addWidget(myStepSlider, 1);
81
82   myMaxLbl = new QLabel("1", aSliderWidget);
83   aSliderLayout->addWidget(myMaxLbl);
84
85   // Stamp value
86   aStepLayout->addWidget(new QLabel(tr("Stamp"), aStepFrame), 2, 0);
87   myStampSpn = new QSpinBox(aStepFrame);
88   aStepLayout->addWidget(myStampSpn, 2, 1);
89
90   // Data table
91   myDataTbl = new QTableWidget(2, 2, aStepFrame);
92   myDataTbl->verticalHeader()->hide();
93   myDataTbl->horizontalHeader()->hide();
94   myDataTbl->setRowHeight(0, 25);
95   myDataTbl->setRowHeight(1, 25);
96
97   QTableWidgetItem* aItem = new QTableWidgetItem("Shape");
98   aItem->setBackgroundColor(Qt::lightGray);
99   aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
100   myDataTbl->setItem(0, 0, aItem);
101
102   aItem = new QTableWidgetItem("Comp 1");
103   aItem->setBackgroundColor(Qt::lightGray);
104   myDataTbl->setItem(0, 1, aItem);
105
106   aItem = new QTableWidgetItem("Default value");
107   aItem->setBackgroundColor(Qt::lightGray);
108   aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
109   myDataTbl->setItem(1, 0, aItem);
110
111   aItem = new QTableWidgetItem("0");
112   aItem->setBackgroundColor(Qt::lightGray);
113   myDataTbl->setItem(1, 1, aItem);
114   
115   aStepLayout->addWidget(myDataTbl, 3, 0, 1, 2);
116
117   // Buttons below
118   QWidget* aBtnWgt = new QWidget(this);
119   aMainLayout->addWidget(aBtnWgt);
120   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
121   aBtnLayout->setContentsMargins(0, 0, 0, 0);
122
123   QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
124   aBtnLayout->addWidget(aAddBtn);
125
126   aBtnLayout->addStretch(1);
127
128   QPushButton* aRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
129   aBtnLayout->addWidget(aRemoveBtn);
130
131   connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
132   connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
133   connect(aRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
134   connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
135 }
136
137 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
138 {
139   QList<QWidget*> aControls;
140   // this control will accept focus and will be highlighted in the Property Panel
141   //aControls.push_back(myComboBox);
142   return aControls;
143 }
144
145 bool CollectionPlugin_WidgetField::storeValueCustom()
146 {
147   //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
148   //AttributeIntegerPtr aValueAttribute =
149   //                      std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
150   //aValueAttribute->setValue(myComboBox->currentIndex());
151   //updateObject(myFeature);
152   return true;
153 }
154
155 bool CollectionPlugin_WidgetField::restoreValueCustom()
156 {
157   //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
158   //AttributeIntegerPtr aValueAttribute =
159   //                      std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
160
161   //bool isBlocked = myComboBox->blockSignals(true);
162   //myComboBox->setCurrentIndex(aValueAttribute->value());
163   //myComboBox->blockSignals(isBlocked);
164
165   return true;
166 }
167
168 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
169 {
170   int aOldCol = myDataTbl->columnCount() - 1;
171   int aNbRows = myDataTbl->rowCount();
172   myDataTbl->setColumnCount(theVal + 1);
173   int aDif = theVal - aOldCol;
174   QTableWidgetItem* aItem = 0;
175   for (int i = 0; i < aDif; i++) {
176     for (int j = 0; j < aNbRows; j++) {
177       aItem = new QTableWidgetItem();
178       if (j == 0)
179         aItem->setText(QString("Comp %1").arg(i + aOldCol + 1));
180       else
181         aItem->setText("0");
182       if (j < 3)
183         aItem->setBackgroundColor(Qt::lightGray);
184       myDataTbl->setItem(j, i + aOldCol + 1, aItem);
185     }
186   }
187 }
188
189 void CollectionPlugin_WidgetField::onAddStep()
190 {
191   int aMax = myStepSlider->maximum();
192   aMax++;
193   myStepSlider->setMaximum(aMax);
194   myMaxLbl->setText(QString::number(aMax));
195   clearData();
196   myStepSlider->setValue(aMax);
197 }
198
199 void CollectionPlugin_WidgetField::onRemoveStep()
200 {
201   int aMax = myStepSlider->maximum();
202   aMax--;
203   myStepSlider->setMaximum(aMax);
204   myMaxLbl->setText(QString::number(aMax));
205 }
206
207 void CollectionPlugin_WidgetField::clearData()
208 {
209   int aNbRows = myDataTbl->rowCount();
210   int aNbCol = myDataTbl->columnCount();
211
212   QString aDefValue;
213   for (int i = 1; i < aNbCol; i++) {
214     aDefValue = myDataTbl->item(1, i)->text();
215     for (int j = 2; j < aNbRows; j++) {
216       myDataTbl->item(j, i)->setText(aDefValue);
217     }
218   }
219 }
220
221 void CollectionPlugin_WidgetField::onStepMove(int theStep)
222 {
223   myCurStepLbl->setText(QString::number(theStep));
224 }