1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: CollectionPlugin_WidgetField.cpp
4 // Created: 16 Nov 2016
5 // Author: Vitaly SMETANNIKOV
7 #include "CollectionPlugin_WidgetField.h"
9 #include <ModuleBase_Tools.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_ISelection.h>
15 #include <QFormLayout>
20 #include <QTableWidget>
21 #include <QPushButton>
22 #include <QHeaderView>
23 #include <QStackedWidget>
25 #include <QStyledItemDelegate>
29 class DataTableItemDelegate : public QStyledItemDelegate
38 DataTableItemDelegate(DataType theType) : QStyledItemDelegate() { myType = theType; }
40 virtual QWidget* createEditor(QWidget* theParent,
41 const QStyleOptionViewItem & theOption,
42 const QModelIndex& theIndex) const;
44 virtual void setModelData(QWidget* theEditor, QAbstractItemModel* theModel,
45 const QModelIndex& theIndex) const;
47 DataType dataType() const { return myType; }
49 void setDataType(DataType theType) { myType = theType; }
55 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
56 const QStyleOptionViewItem & theOption,
57 const QModelIndex& theIndex ) const
59 if ((theIndex.column() != 0) && (theIndex.row() != 0)) {
60 QLineEdit* aLineEdt = 0;
63 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
67 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
71 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
75 aLineEdt->setValidator(new QIntValidator(aLineEdt));
80 QComboBox* aBox = new QComboBox(theParent);
81 aBox->addItem("True");
82 aBox->addItem("False");
87 return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
91 void DataTableItemDelegate::setModelData(QWidget* theEditor, QAbstractItemModel* theModel,
92 const QModelIndex& theIndex) const
94 QComboBox* aBox = dynamic_cast<QComboBox*>(theEditor);
96 theModel->setData(theIndex, aBox->currentText());
98 QStyledItemDelegate::setModelData(theEditor, theModel, theIndex);
103 CollectionPlugin_WidgetField::
104 CollectionPlugin_WidgetField(QWidget* theParent,
105 ModuleBase_IWorkshop* theWorkshop,
106 const Config_WidgetAPI* theData):
107 ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
109 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
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);
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);
126 myFieldTypeCombo = new QComboBox(aTypesWgt);
127 QStringList aFieldTypes;
128 aFieldTypes << tr("Double") << tr("Integer") << tr("Boolean")
130 myFieldTypeCombo->addItems(aFieldTypes);
131 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
133 // Number of components
134 myNbComponentsSpn = new QSpinBox(aTypesWgt);
135 myNbComponentsSpn->setMinimum(1);
136 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
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);
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();
150 myCurStepLbl->setFont(aFont);
151 aStepLayout->addWidget(myCurStepLbl, 0, 1);
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);
159 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
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);
167 myMaxLbl = new QLabel("1", aSliderWidget);
168 aSliderLayout->addWidget(myMaxLbl);
171 myCompNamesList << "Comp 1";
172 myStepWgt = new QStackedWidget(aStepFrame);
173 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
174 appendStepControls();
177 QWidget* aBtnWgt = new QWidget(this);
178 aMainLayout->addWidget(aBtnWgt);
179 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
180 aBtnLayout->setContentsMargins(0, 0, 0, 0);
182 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
183 aBtnLayout->addWidget(aAddBtn);
185 aBtnLayout->addStretch(1);
187 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
188 aBtnLayout->addWidget(myRemoveBtn);
189 myRemoveBtn->setEnabled(false);
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)));
198 void CollectionPlugin_WidgetField::appendStepControls()
200 QWidget* aWidget = new QWidget(myStepWgt);
201 QGridLayout* aStepLayout = new QGridLayout(aWidget);
202 aStepLayout->setContentsMargins(0, 0, 0, 0);
204 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
206 QSpinBox* aStampSpn = new QSpinBox(aWidget);
207 aStepLayout->addWidget(aStampSpn, 0, 1);
209 myStampSpnList.append(aStampSpn);
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);
220 QTableWidgetItem* aItem = new QTableWidgetItem("Shape");
221 aItem->setBackgroundColor(Qt::lightGray);
222 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
223 aDataTbl->setItem(0, 0, aItem);
225 aItem = new QTableWidgetItem("Default value");
226 aItem->setBackgroundColor(Qt::lightGray);
227 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
228 aDataTbl->setItem(1, 0, aItem);
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);
235 aItem = new QTableWidgetItem("0");
236 aItem->setBackgroundColor(Qt::lightGray);
237 aDataTbl->setItem(1, i + 1, aItem);
239 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
241 QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
242 myDataTblList.append(aDataTbl);
244 myStepWgt->addWidget(aWidget);
247 void CollectionPlugin_WidgetField::removeStepControls()
249 int aCurWgtId = myStepWgt->currentIndex();
250 myStepWgt->removeWidget(myStepWgt->currentWidget());
252 myStampSpnList.removeAt(aCurWgtId);
253 myDataTblList.removeAt(aCurWgtId);
257 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
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);
267 bool CollectionPlugin_WidgetField::storeValueCustom()
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);
277 bool CollectionPlugin_WidgetField::restoreValueCustom()
279 //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
280 //AttributeIntegerPtr aValueAttribute =
281 // std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
283 //bool isBlocked = myComboBox->blockSignals(true);
284 //myComboBox->setCurrentIndex(aValueAttribute->value());
285 //myComboBox->blockSignals(isBlocked);
290 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
292 int aOldCol = myCompNamesList.count();
293 int aNbRows = myDataTblList.first()->rowCount();
294 int aDif = theVal - aOldCol;
295 QTableWidgetItem* aItem = 0;
297 while (myCompNamesList.count() != theVal) {
299 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
301 myCompNamesList.removeLast();
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();
310 aItem->setText(myCompNamesList.at(i));
314 aItem->setBackgroundColor(Qt::lightGray);
315 aDataTbl->setItem(j, i + aOldCol + 1, aItem);
321 void CollectionPlugin_WidgetField::onAddStep()
323 int aMax = myStepSlider->maximum();
325 myStepSlider->setMaximum(aMax);
326 myMaxLbl->setText(QString::number(aMax));
327 appendStepControls();
328 myStepSlider->setValue(aMax);
329 myRemoveBtn->setEnabled(aMax > 1);
332 void CollectionPlugin_WidgetField::onRemoveStep()
334 int aMax = myStepSlider->maximum();
336 myMaxLbl->setText(QString::number(aMax));
337 removeStepControls();
338 myStepSlider->setMaximum(aMax);
339 myRemoveBtn->setEnabled(aMax > 1);
342 void CollectionPlugin_WidgetField::clearData()
344 int aNbRows = myDataTblList.first()->rowCount();
345 int aNbCol = myDataTblList.first()->columnCount();
347 foreach(QTableWidget* aDataTbl, myDataTblList) {
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);
358 void CollectionPlugin_WidgetField::onStepMove(int theStep)
360 myCurStepLbl->setText(QString::number(theStep));
361 myStepWgt->setCurrentIndex(theStep - 1);
364 QIntList CollectionPlugin_WidgetField::shapeTypes() const
367 switch (myShapeTypeCombo->currentIndex()) {
369 aRes.append(ModuleBase_Tools::shapeType("vertex"));
372 aRes.append(ModuleBase_Tools::shapeType("edge"));
375 aRes.append(ModuleBase_Tools::shapeType("face"));
378 aRes.append(ModuleBase_Tools::shapeType("solid"));
381 aRes.append(ModuleBase_Tools::shapeType("object"));
384 // TODO: Selection mode for Parts
390 bool CollectionPlugin_WidgetField::
391 isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
396 void CollectionPlugin_WidgetField::onSelectionChanged()
398 QList<ModuleBase_ViewerPrsPtr> aSelected =
399 myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
403 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
408 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
410 DataTableItemDelegate* aDelegate = 0;
411 foreach(QTableWidget* aTable, myDataTblList) {
412 aDelegate = dynamic_cast<DataTableItemDelegate*>(aTable->itemDelegate());
414 aDelegate->setDataType((DataTableItemDelegate::DataType)theIdx);