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"
8 #include "CollectionPlugin_Field.h"
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_ISelection.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_AttributeStringArray.h>
16 #include <ModelAPI_AttributeInteger.h>
17 #include <ModelAPI_AttributeIntArray.h>
21 #include <QFormLayout>
26 #include <QTableWidget>
27 #include <QPushButton>
28 #include <QHeaderView>
29 #include <QStackedWidget>
31 #include <QStyledItemDelegate>
34 #include <QMouseEvent>
36 #include <QApplication>
38 const char* MYFirstCol = "Shape";
39 const char* MYTrue = "True";
40 const char* MYFalse = "False";
42 class DataTableItemDelegate : public QStyledItemDelegate
45 DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType) :
46 QStyledItemDelegate() { myType = theType; }
48 virtual QWidget* createEditor(QWidget* theParent,
49 const QStyleOptionViewItem & theOption,
50 const QModelIndex& theIndex) const;
52 ModelAPI_AttributeTables::ValueType dataType() const { return myType; }
54 void setDataType(ModelAPI_AttributeTables::ValueType theType) { myType = theType; }
60 ModelAPI_AttributeTables::ValueType myType;
63 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
64 const QStyleOptionViewItem & theOption,
65 const QModelIndex& theIndex ) const
67 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
68 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
69 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
70 aEdt->setReadOnly(true);
73 QLineEdit* aLineEdt = 0;
75 case ModelAPI_AttributeTables::DOUBLE:
76 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
80 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
84 case ModelAPI_AttributeTables::INTEGER:
85 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
89 aLineEdt->setValidator(new QIntValidator(aLineEdt));
93 case ModelAPI_AttributeTables::BOOLEAN:
95 QComboBox* aBox = new QComboBox(theParent);
96 aBox->addItem(MYFalse);
97 aBox->addItem(MYTrue);
102 return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
107 //**********************************************************************************
108 //**********************************************************************************
109 //**********************************************************************************
110 CollectionPlugin_WidgetField::
111 CollectionPlugin_WidgetField(QWidget* theParent,
112 ModuleBase_IWorkshop* theWorkshop,
113 const Config_WidgetAPI* theData):
114 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
117 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
119 // Types definition controls
120 QWidget* aTypesWgt = new QWidget(this);
121 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
122 aTypesLayout->setContentsMargins(0, 0, 0, 0);
123 aMainLayout->addWidget(aTypesWgt);
126 myShapeTypeCombo = new QComboBox(aTypesWgt);
127 QStringList aShapeTypes;
128 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
129 << tr("Solids") << tr("Objects") << tr("Parts");
130 myShapeTypeCombo->addItems(aShapeTypes);
131 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
134 myFieldTypeCombo = new QComboBox(aTypesWgt);
135 QStringList aFieldTypes;
136 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
138 myFieldTypeCombo->addItems(aFieldTypes);
139 myFieldTypeCombo->setCurrentIndex(2);
140 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
142 // Number of components
143 myNbComponentsSpn = new QSpinBox(aTypesWgt);
144 myNbComponentsSpn->setMinimum(1);
145 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
148 QFrame* aStepFrame = new QFrame(this);
149 aStepFrame->setFrameShape(QFrame::Box);
150 aStepFrame->setFrameStyle(QFrame::StyledPanel);
151 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
152 aMainLayout->addWidget(aStepFrame);
154 // Current step label
155 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
156 myCurStepLbl = new QLabel("1", aStepFrame);
157 QFont aFont = myCurStepLbl->font();
159 myCurStepLbl->setFont(aFont);
160 aStepLayout->addWidget(myCurStepLbl, 0, 1);
163 QWidget* aSliderWidget = new QWidget(aStepFrame);
164 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
165 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
166 aSliderLayout->setContentsMargins(0, 0, 0, 0);
168 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
170 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
171 myStepSlider->setTickPosition(QSlider::TicksBelow);
172 myStepSlider->setRange(1, 1);
173 myStepSlider->setPageStep(myStepSlider->singleStep());
174 aSliderLayout->addWidget(myStepSlider, 1);
176 myMaxLbl = new QLabel("1", aSliderWidget);
177 aSliderLayout->addWidget(myMaxLbl);
180 myCompNamesList << "Comp 1";
181 myStepWgt = new QStackedWidget(aStepFrame);
182 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
183 appendStepControls();
186 QWidget* aBtnWgt = new QWidget(this);
187 aMainLayout->addWidget(aBtnWgt);
188 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
189 aBtnLayout->setContentsMargins(0, 0, 0, 0);
191 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
192 aBtnLayout->addWidget(aAddBtn);
194 aBtnLayout->addStretch(1);
196 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
197 aBtnLayout->addWidget(myRemoveBtn);
198 myRemoveBtn->setEnabled(false);
200 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
201 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
202 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
203 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
204 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
205 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
206 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
207 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
210 //**********************************************************************************
211 void CollectionPlugin_WidgetField::appendStepControls()
213 QWidget* aWidget = new QWidget(myStepWgt);
214 QGridLayout* aStepLayout = new QGridLayout(aWidget);
215 aStepLayout->setContentsMargins(0, 0, 0, 0);
217 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
219 QSpinBox* aStampSpn = new QSpinBox(aWidget);
220 aStepLayout->addWidget(aStampSpn, 0, 1);
222 myStampSpnList.append(aStampSpn);
225 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
226 DataTableItemDelegate* aDelegate = 0;
227 if (myDataTblList.isEmpty())
228 aDelegate = new DataTableItemDelegate(
229 (ModelAPI_AttributeTables::ValueType) myFieldTypeCombo->currentIndex());
231 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
233 aDataTbl->setItemDelegate(aDelegate);
234 myDataTblList.append(aDataTbl);
236 aDataTbl->verticalHeader()->hide();
237 aDataTbl->setRowHeight(0, 25);
238 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
240 updateHeaders(aDataTbl);
242 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
243 aItem->setBackgroundColor(Qt::lightGray);
244 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
245 aDataTbl->setItem(0, 0, aItem);
247 // Set default value item
248 for (int i = 0; i < myCompNamesList.count(); i++) {
249 aItem = createDefaultItem();
250 aItem->setBackgroundColor(Qt::lightGray);
251 aDataTbl->setItem(0, i + 1, aItem);
253 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
254 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
256 QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
257 myStepWgt->addWidget(aWidget);
258 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
261 //**********************************************************************************
262 void CollectionPlugin_WidgetField::deactivate()
264 ModuleBase_WidgetSelector::deactivate();
269 //**********************************************************************************
270 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
272 QObject* aObject = 0;
273 foreach(QTableWidget* aTable, myDataTblList) {
274 if (aTable->horizontalHeader()->viewport() == theObject) {
280 if (theEvent->type() == QEvent::MouseButtonDblClick) {
281 if (myHeaderEditor) { //delete previous editor
282 myHeaderEditor->deleteLater();
285 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
286 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
287 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
289 int aShift = aTable->horizontalScrollBar()->value();
290 int aPos = aMouseEvent->x();
291 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
294 aRect.setLeft(aHeader->sectionPosition(aIndex));
295 aRect.setWidth(aHeader->sectionSize(aIndex));
297 aRect.setHeight(aHeader->height());
298 aRect.adjust(1, 1, -1, -1);
299 aRect.translate(-aShift, 0);
301 myHeaderEditor = new QLineEdit(aHeader->viewport());
302 myHeaderEditor->move(aRect.topLeft());
303 myHeaderEditor->resize(aRect.size());
304 myHeaderEditor->setFrame(false);
305 QString aText = aHeader->model()->
306 headerData(aIndex, aHeader->orientation()).toString();
307 myHeaderEditor->setText(aText);
308 myHeaderEditor->setFocus();
309 myEditIndex = aIndex; //save for future use
310 myHeaderEditor->installEventFilter(this); //catch focus out event
311 //if user presses Enter it should close editor
312 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
313 myHeaderEditor->show();
317 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
318 //QHeaderView* aHeader =
319 // static_cast<QHeaderView*>(myHeaderEditor->parentWidget()->parentWidget());
320 QString aNewTitle = myHeaderEditor->text();
322 //aHeader->model()->setHeaderData(myEditIndex, aHeader->orientation(), aNewTitle);
323 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
324 myHeaderEditor->deleteLater(); //safely delete editor
326 // Store into data model
327 AttributeStringArrayPtr aStringsAttr =
328 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
329 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
330 foreach(QTableWidget* aTable, myDataTblList) {
331 updateHeaders(aTable);
334 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
337 //**********************************************************************************
338 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
340 QTableWidgetItem* aItem = new QTableWidgetItem();
341 switch (myFieldTypeCombo->currentIndex()) {
342 case ModelAPI_AttributeTables::DOUBLE:
343 case ModelAPI_AttributeTables::INTEGER:
346 case ModelAPI_AttributeTables::BOOLEAN:
347 aItem->setText(MYFalse);
353 //**********************************************************************************
354 QTableWidgetItem* CollectionPlugin_WidgetField::
355 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
357 QTableWidgetItem* aItem = new QTableWidgetItem();
358 switch (myFieldTypeCombo->currentIndex()) {
359 case ModelAPI_AttributeTables::DOUBLE:
360 aItem->setText(QString::number(theVal.myDouble));
362 case ModelAPI_AttributeTables::INTEGER:
363 aItem->setText(QString::number(theVal.myInt));
365 case ModelAPI_AttributeTables::BOOLEAN:
366 aItem->setText(theVal.myBool? MYTrue : MYFalse);
368 case ModelAPI_AttributeTables::STRING:
369 aItem->setText(theVal.myStr.c_str());
374 //**********************************************************************************
375 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
377 QStringList aHeaders;
378 aHeaders << tr(MYFirstCol);
379 aHeaders << myCompNamesList;
380 theDataTbl->setHorizontalHeaderLabels(aHeaders);
383 //**********************************************************************************
384 void CollectionPlugin_WidgetField::removeStepControls()
386 int aCurWgtId = myStepWgt->currentIndex();
387 QWidget* aWgt = myStepWgt->currentWidget();
388 myStepWgt->removeWidget(aWgt);
390 myStampSpnList.removeAt(aCurWgtId);
391 myDataTblList.removeAt(aCurWgtId);
395 //**********************************************************************************
396 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
398 QList<QWidget*> aControls;
399 // this control will accept focus and will be highlighted in the Property Panel
400 aControls.push_back(myShapeTypeCombo);
401 //aControls.push_back(myFieldTypeCombo);
402 //aControls.push_back(myNbComponentsSpn);
406 //**********************************************************************************
407 bool CollectionPlugin_WidgetField::storeValueCustom()
409 DataPtr aData = myFeature->data();
410 // Store number of components
411 AttributeStringArrayPtr aStringsAttr =
412 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
413 int aNbComps = myCompNamesList.size();
414 aStringsAttr->setSize(aNbComps);
415 for ( int i = 0; i < aNbComps; i++)
416 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
418 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
419 // Store number of steps
420 int aNbSteps = myDataTblList.size();
422 // Store Type of the field values
423 int aFldType = myFieldTypeCombo->currentIndex();
425 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
426 aStampsAttr->setSize(aNbSteps);
428 QTableWidget* aTable = myDataTblList.first();
429 int aRows = aTable->rowCount();
430 // first column contains selected names which should not be stored
431 int aColumns = aTable->columnCount() - 1;
433 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
434 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
435 for (int i = 0; i < aNbSteps; i++) {
436 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
437 aTable = myDataTblList.at(i);
438 for (int j = 0; j < aColumns; j++) {
439 for (int k = 0; k < aRows; k++) {
440 QString aTblVal = aTable->item(k, j + 1)->text();
441 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
445 updateObject(myFeature);
449 //**********************************************************************************
450 bool CollectionPlugin_WidgetField::restoreValueCustom()
453 DataPtr aData = myFeature->data();
455 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
456 if (!aSelList->isInitialized())
458 std::string aTypeStr = aSelList->selectionType();
459 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
461 // Get number of components
462 AttributeStringArrayPtr aStringsAttr =
463 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
465 if (!aStringsAttr->isInitialized())
468 myCompNamesList.clear();
469 for (int i = 0; i < aStringsAttr->size(); i++) {
470 myCompNamesList.append(aStringsAttr->value(i).c_str());
472 isBlocked = myNbComponentsSpn->blockSignals(true);
473 myNbComponentsSpn->setValue(myCompNamesList.size());
474 myNbComponentsSpn->blockSignals(isBlocked);
476 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
477 // Get number of steps
478 int aNbSteps = aTablesAttr->tables();
479 myStepSlider->setMaximum(aNbSteps);
480 //myStepSlider->setValue(1);
482 myStampSpnList.clear();
483 myDataTblList.clear();
484 while (myStepWgt->count()) {
485 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
486 myStepWgt->removeWidget(aWgt);
490 while (myStepWgt->count() < aNbSteps)
491 appendStepControls();
492 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
494 // Get Type of the field values
495 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
497 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
499 int aRows = aTablesAttr->rows();
500 int aCols = aTablesAttr->columns();
502 QTableWidgetItem* aItem = 0;
503 for (int i = 0; i < aNbSteps; i++) {
504 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
505 QTableWidget* aTable = myDataTblList.at(i);
506 isBlocked = aTable->blockSignals(true);
507 aTable->setRowCount(aRows);
508 for (int j = 0; j < aCols + 1; j++) {
509 for (int k = 0; k < aRows; k++) {
510 if ((j == 0) && (k > 0)) {
511 // Add selection names
512 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
513 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
514 aTable->setItem(k, j, aItem);
517 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
518 aItem = createValueItem(aVal);
520 aItem->setBackgroundColor(Qt::lightGray);
521 aTable->setItem(k, j, aItem);
526 aTable->blockSignals(isBlocked);
531 //**********************************************************************************
532 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
534 if (theStr == "vertex")
536 else if (theStr == "edge")
538 else if (theStr == "face")
540 else if (theStr == "solid")
542 else if (theStr == "object")
544 else if (theStr == "part")
550 //**********************************************************************************
551 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
570 //**********************************************************************************
571 QIntList CollectionPlugin_WidgetField::shapeTypes() const
574 switch (myShapeTypeCombo->currentIndex()) {
576 aRes.append(ModuleBase_Tools::shapeType("vertex"));
579 aRes.append(ModuleBase_Tools::shapeType("edge"));
582 aRes.append(ModuleBase_Tools::shapeType("face"));
585 aRes.append(ModuleBase_Tools::shapeType("solid"));
588 aRes.append(ModuleBase_Tools::shapeType("object"));
591 // TODO: Selection mode for Parts
597 //**********************************************************************************
598 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
600 ModelAPI_AttributeTables::Value aVal;
601 switch (myFieldTypeCombo->currentIndex()) {
602 case ModelAPI_AttributeTables::BOOLEAN:
603 aVal.myBool = (theStrVal == MYTrue)? true : false;
605 case ModelAPI_AttributeTables::DOUBLE:
606 aVal.myDouble = theStrVal.toDouble();
608 case ModelAPI_AttributeTables::INTEGER:
609 aVal.myInt = theStrVal.toInt();
611 case ModelAPI_AttributeTables::STRING:
612 aVal.myStr = theStrVal.toStdString();
618 //**********************************************************************************
619 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
621 int aOldCol = myCompNamesList.count();
622 int aNbRows = myDataTblList.first()->rowCount();
623 int aDif = theVal - aOldCol;
624 QTableWidgetItem* aItem = 0;
626 while (myCompNamesList.count() != theVal) {
628 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
630 myCompNamesList.removeLast();
633 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
634 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
636 foreach(QTableWidget* aDataTbl, myDataTblList) {
637 aDataTbl->setColumnCount(theVal + 1);
638 updateHeaders(aDataTbl);
639 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
640 for (int j = 0; j < aNbRows; j++) {
641 aItem = createDefaultItem();
643 aItem->setBackgroundColor(Qt::lightGray);
644 aDataTbl->setItem(j, i + 1, aItem);
648 emit valuesChanged();
651 //**********************************************************************************
652 void CollectionPlugin_WidgetField::onAddStep()
654 int aMax = myStepSlider->maximum();
656 myStepSlider->setMaximum(aMax);
657 myMaxLbl->setText(QString::number(aMax));
658 appendStepControls();
659 myStepSlider->setValue(aMax);
661 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
662 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
665 AttributeSelectionListPtr aSelList =
666 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
667 if (!aSelList->isInitialized())
669 int aSelNb = aSelList->size();
673 int aColumns = myNbComponentsSpn->value() + 1;
674 int aRows = aSelNb + 1;
675 QTableWidget* aTable = myDataTblList.last();
676 aTable->setRowCount(aRows);
677 QTableWidgetItem* aItem = 0;
678 for(int i = 0; i < aColumns; i++) {
680 for(int j = 1; j < aRows; j++) {
681 aItem = new QTableWidgetItem();
682 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
683 aItem->setText(aAttr->namingName().c_str());
684 aItem->setToolTip(aAttr->namingName().c_str());
685 aTable->setItem(j, i, aItem);
688 QString aDefVal = aTable->item(0, i)->text();
689 for(int j = 1; j < aRows; j++) {
690 aItem = new QTableWidgetItem();
691 aItem->setText(aDefVal);
692 aTable->setItem(j, i, aItem);
698 //**********************************************************************************
699 void CollectionPlugin_WidgetField::onRemoveStep()
701 int aMax = myStepSlider->maximum();
703 myMaxLbl->setText(QString::number(aMax));
704 removeStepControls();
705 myStepSlider->setMaximum(aMax);
707 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
708 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
711 //**********************************************************************************
712 void CollectionPlugin_WidgetField::clearData()
714 foreach(QTableWidget* aDataTbl, myDataTblList) {
715 aDataTbl->setRowCount(1);
719 //**********************************************************************************
720 void CollectionPlugin_WidgetField::onStepMove(int theStep)
722 myCurStepLbl->setText(QString::number(theStep));
723 myStepWgt->setCurrentIndex(theStep - 1);
726 //**********************************************************************************
727 bool CollectionPlugin_WidgetField::
728 isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
730 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
733 //**********************************************************************************
734 void CollectionPlugin_WidgetField::onSelectionChanged()
736 // Ignore selection for Parts mode
737 if (myShapeTypeCombo->currentIndex() == 5)
740 QList<ModuleBase_ViewerPrsPtr> aSelected =
741 myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
744 AttributeSelectionListPtr aSelList =
745 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
747 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
752 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
753 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
754 aShape = aPrs->shape();
755 if (!aResult.get() && !aShape.get())
757 if (!aSelList->isInList(aResult, aShape)) {
758 aSelList->append(aResult, aShape);
762 int aColumns = myDataTblList.first()->columnCount();
763 int aRows = myDataTblList.first()->rowCount();
764 int aNewRows = aNbData + 1;
765 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
766 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
768 QTableWidgetItem* aItem = 0;
769 foreach(QTableWidget* aTable, myDataTblList) {
770 aTable->setRowCount(aNewRows);
771 if (aNewRows > aRows) {
773 for(int i = 0; i < aColumns; i++) {
775 for(int j = 1; j < aNewRows; j++) {
776 aItem = new QTableWidgetItem();
777 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
778 aItem->setText(aAttr->namingName().c_str());
779 aItem->setToolTip(aAttr->namingName().c_str());
780 aTable->setItem(j, i, aItem);
783 QString aDefVal = aTable->item(0, i)->text();
784 for(int j = aRows; j < aNewRows; j++) {
785 aItem = new QTableWidgetItem();
786 aItem->setText(aDefVal);
787 aTable->setItem(j, i, aItem);
792 // Update only selection name
793 for(int j = 1; j < aNewRows - 1; j++) {
794 AttributeSelectionPtr aAttr = aSelList->value(j);
795 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
796 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
800 emit valuesChanged();
803 //**********************************************************************************
804 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
806 DataTableItemDelegate* aDelegate = 0;
807 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
809 ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
810 if (aOldType != theIdx) {
811 aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
812 int aColumns = myDataTblList.first()->columnCount();
813 int aRows = myDataTblList.first()->rowCount();
814 foreach(QTableWidget* aTable, myDataTblList) {
815 for(int i = 1; i < aColumns; i++) {
816 for(int j = 0; j < aRows; j++) {
818 case ModelAPI_AttributeTables::DOUBLE:
819 case ModelAPI_AttributeTables::INTEGER:
820 if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
821 (aOldType == ModelAPI_AttributeTables::STRING)) {
822 aTable->item(j, i)->setText("0");
825 case ModelAPI_AttributeTables::BOOLEAN:
826 aTable->item(j, i)->setText(MYFalse);
832 emit valuesChanged();
837 //**********************************************************************************
838 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
840 // Do not store here column of names
843 if (!myFeature.get())
845 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
846 int aNb = myDataTblList.indexOf(aTable);
849 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
851 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
852 if (aTablesAttr->isInitialized())
853 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
855 emit valuesChanged();
858 //**********************************************************************************
859 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
861 activateSelectionAndFilters(theType == 5? false:true);
863 AttributeSelectionListPtr aSelList =
864 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
865 if (!aSelList->isInitialized())
867 std::string aTypeName = getSelectionType(theType);
868 if (aTypeName == aSelList->selectionType())
871 //Clear old selection
874 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
875 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
876 emit valuesChanged();
879 //**********************************************************************************
880 bool CollectionPlugin_WidgetField::processEnter()
889 //**********************************************************************************
890 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
892 if (theNew && (!myIsEditing))
893 myIsEditing = dynamic_cast<QLineEdit*>(theNew);
896 //**********************************************************************************
897 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
899 myMaxLbl->setText(QString::number(theMax));
900 myRemoveBtn->setEnabled(theMax > 1);