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_IModule.h>
13 #include <ModuleBase_ISelection.h>
14 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_AttributeStringArray.h>
18 #include <ModelAPI_AttributeInteger.h>
19 #include <ModelAPI_AttributeIntArray.h>
23 #include <QFormLayout>
28 #include <QTableWidget>
29 #include <QPushButton>
30 #include <QHeaderView>
31 #include <QStackedWidget>
35 #include <QMouseEvent>
37 #include <QApplication>
39 const char* MYFirstCol = "Shape";
40 const char* MYTrue = "True";
41 const char* MYFalse = "False";
43 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
44 const QStyleOptionViewItem & theOption,
45 const QModelIndex& theIndex ) const
47 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
48 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
49 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
50 aEdt->setReadOnly(true);
53 QLineEdit* aLineEdt = 0;
55 case ModelAPI_AttributeTables::DOUBLE:
56 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
60 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
64 case ModelAPI_AttributeTables::INTEGER:
65 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
69 aLineEdt->setValidator(new QIntValidator(aLineEdt));
73 case ModelAPI_AttributeTables::BOOLEAN:
75 QComboBox* aBox = new QComboBox(theParent);
76 aBox->addItem(MYFalse);
77 aBox->addItem(MYTrue);
82 return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
87 //**********************************************************************************
88 //**********************************************************************************
89 //**********************************************************************************
90 CollectionPlugin_WidgetField::
91 CollectionPlugin_WidgetField(QWidget* theParent,
92 ModuleBase_IWorkshop* theWorkshop,
93 const Config_WidgetAPI* theData):
94 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
95 myIsTabEdit(false), myActivation(false)
97 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
99 // Types definition controls
100 QWidget* aTypesWgt = new QWidget(this);
101 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
102 aTypesLayout->setContentsMargins(0, 0, 0, 0);
103 aMainLayout->addWidget(aTypesWgt);
106 myShapeTypeCombo = new QComboBox(aTypesWgt);
107 QStringList aShapeTypes;
108 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
109 << tr("Solids") << tr("Objects") << tr("Parts");
110 myShapeTypeCombo->addItems(aShapeTypes);
111 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
114 myFieldTypeCombo = new QComboBox(aTypesWgt);
115 QStringList aFieldTypes;
116 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
118 myFieldTypeCombo->addItems(aFieldTypes);
119 myFieldTypeCombo->setCurrentIndex(2);
120 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
122 // Number of components
123 myNbComponentsSpn = new QSpinBox(aTypesWgt);
124 myNbComponentsSpn->setMinimum(1);
125 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
128 QFrame* aStepFrame = new QFrame(this);
129 aStepFrame->setFrameShape(QFrame::Box);
130 aStepFrame->setFrameStyle(QFrame::StyledPanel);
131 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
132 aMainLayout->addWidget(aStepFrame);
134 // Current step label
135 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
136 myCurStepLbl = new QLabel("1", aStepFrame);
137 QFont aFont = myCurStepLbl->font();
139 myCurStepLbl->setFont(aFont);
140 aStepLayout->addWidget(myCurStepLbl, 0, 1);
143 QWidget* aSliderWidget = new QWidget(aStepFrame);
144 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
145 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
146 aSliderLayout->setContentsMargins(0, 0, 0, 0);
148 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
150 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
151 myStepSlider->setTickPosition(QSlider::TicksBelow);
152 myStepSlider->setRange(1, 1);
153 myStepSlider->setPageStep(myStepSlider->singleStep());
154 aSliderLayout->addWidget(myStepSlider, 1);
156 myMaxLbl = new QLabel("1", aSliderWidget);
157 aSliderLayout->addWidget(myMaxLbl);
160 myCompNamesList << "Comp 1";
161 myStepWgt = new QStackedWidget(aStepFrame);
162 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
165 new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
166 myFieldTypeCombo->currentIndex());
168 appendStepControls();
171 QWidget* aBtnWgt = new QWidget(this);
172 aMainLayout->addWidget(aBtnWgt);
173 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
174 aBtnLayout->setContentsMargins(0, 0, 0, 0);
176 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
177 aBtnLayout->addWidget(aAddBtn);
179 aBtnLayout->addStretch(1);
181 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
182 aBtnLayout->addWidget(myRemoveBtn);
183 myRemoveBtn->setEnabled(false);
185 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
186 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
187 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
188 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
189 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
190 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
191 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
192 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
195 //**********************************************************************************
196 void CollectionPlugin_WidgetField::appendStepControls()
198 QWidget* aWidget = new QWidget(myStepWgt);
199 QGridLayout* aStepLayout = new QGridLayout(aWidget);
200 aStepLayout->setContentsMargins(0, 0, 0, 0);
202 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
204 QSpinBox* aStampSpn = new QSpinBox(aWidget);
205 aStepLayout->addWidget(aStampSpn, 0, 1);
207 myStampSpnList.append(aStampSpn);
210 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
211 aDataTbl->installEventFilter(this);
212 aDataTbl->setItemDelegate(myDelegate);
215 if (!myDataTblList.isEmpty()) {
216 QTableWidget* aFirstTable = myDataTblList.first();
217 for (int i = 0; i < aFirstTable->columnCount(); i++)
218 aColWidth.append(aFirstTable->columnWidth(i));
220 myDataTblList.append(aDataTbl);
222 aDataTbl->verticalHeader()->hide();
223 aDataTbl->setRowHeight(0, 25);
224 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
226 connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
227 SLOT(onColumnResize(int, int, int)));
229 updateHeaders(aDataTbl);
231 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
232 aItem->setBackgroundColor(Qt::lightGray);
233 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
234 aDataTbl->setItem(0, 0, aItem);
236 // Set default value item
237 for (int i = 0; i < myCompNamesList.count(); i++) {
238 aItem = createDefaultItem();
239 aItem->setBackgroundColor(Qt::lightGray);
240 aDataTbl->setItem(0, i + 1, aItem);
243 if (aColWidth.length() > 0) {
244 for (int i = 0; i < aDataTbl->columnCount(); i++) {
245 if (i < aColWidth.size())
246 aDataTbl->setColumnWidth(i, aColWidth.at(i));
249 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
250 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
252 myStepWgt->addWidget(aWidget);
253 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
256 //**********************************************************************************
257 void CollectionPlugin_WidgetField::deactivate()
259 ModuleBase_WidgetSelector::deactivate();
263 //**********************************************************************************
264 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
266 // myShapeTypeCombo->setEnabled(!isEditingMode());
267 // myFieldTypeCombo->setEnabled(!isEditingMode());
268 // myNbComponentsSpn->setEnabled(!isEditingMode());
271 //**********************************************************************************
272 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
274 QObject* aObject = 0;
275 foreach(QTableWidget* aTable, myDataTblList) {
276 if (aTable->horizontalHeader()->viewport() == theObject) {
282 if (theEvent->type() == QEvent::MouseButtonDblClick) {
283 if (myHeaderEditor) { //delete previous editor
284 myHeaderEditor->deleteLater();
287 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
288 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
289 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
291 int aShift = aTable->horizontalScrollBar()->value();
292 int aPos = aMouseEvent->x();
293 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
296 aRect.setLeft(aHeader->sectionPosition(aIndex));
297 aRect.setWidth(aHeader->sectionSize(aIndex));
299 aRect.setHeight(aHeader->height());
300 aRect.adjust(1, 1, -1, -1);
301 aRect.translate(-aShift, 0);
303 myHeaderEditor = new QLineEdit(aHeader->viewport());
304 myHeaderEditor->move(aRect.topLeft());
305 myHeaderEditor->resize(aRect.size());
306 myHeaderEditor->setFrame(false);
307 QString aText = aHeader->model()->
308 headerData(aIndex, aHeader->orientation()).toString();
309 myHeaderEditor->setText(aText);
310 myHeaderEditor->setFocus();
311 myEditIndex = aIndex; //save for future use
312 myHeaderEditor->installEventFilter(this); //catch focus out event
313 //if user presses Enter it should close editor
314 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
315 myHeaderEditor->show();
319 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
320 QString aNewTitle = myHeaderEditor->text();
322 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
323 myHeaderEditor->deleteLater(); //safely delete editor
325 // Store into data model
326 AttributeStringArrayPtr aStringsAttr =
327 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
328 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
329 foreach(QTableWidget* aTable, myDataTblList) {
330 updateHeaders(aTable);
332 } else if (theEvent->type() == QEvent::FocusIn) {
333 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
335 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
336 if (aPanel->activeWidget() != this) {
338 aPanel->activateWidget(this, false);
342 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
345 //**********************************************************************************
346 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
348 QTableWidgetItem* aItem = new QTableWidgetItem();
349 switch (myFieldTypeCombo->currentIndex()) {
350 case ModelAPI_AttributeTables::DOUBLE:
351 case ModelAPI_AttributeTables::INTEGER:
354 case ModelAPI_AttributeTables::BOOLEAN:
355 aItem->setText(MYFalse);
357 case ModelAPI_AttributeTables::STRING:
364 //**********************************************************************************
365 QTableWidgetItem* CollectionPlugin_WidgetField::
366 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
368 QTableWidgetItem* aItem = new QTableWidgetItem();
369 aItem->setText(getValueText(theVal));
373 //**********************************************************************************
374 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
376 switch (myFieldTypeCombo->currentIndex()) {
377 case ModelAPI_AttributeTables::DOUBLE:
378 return QString::number(theVal.myDouble);
379 case ModelAPI_AttributeTables::INTEGER:
380 return QString::number(theVal.myInt);
381 case ModelAPI_AttributeTables::BOOLEAN:
382 return theVal.myBool? MYTrue : MYFalse;
383 case ModelAPI_AttributeTables::STRING:
384 return theVal.myStr.c_str();
390 //**********************************************************************************
391 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
393 QStringList aHeaders;
394 aHeaders << tr(MYFirstCol);
395 aHeaders << myCompNamesList;
396 theDataTbl->setHorizontalHeaderLabels(aHeaders);
399 //**********************************************************************************
400 void CollectionPlugin_WidgetField::removeStepControls()
402 int aCurWgtId = myStepWgt->currentIndex();
403 QWidget* aWgt = myStepWgt->currentWidget();
404 myStepWgt->removeWidget(aWgt);
406 myStampSpnList.removeAt(aCurWgtId);
407 myDataTblList.removeAt(aCurWgtId);
411 //**********************************************************************************
412 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
414 QList<QWidget*> aControls;
415 // this control will accept focus and will be highlighted in the Property Panel
416 aControls.push_back(myShapeTypeCombo);
417 aControls.push_back(myFieldTypeCombo);
418 aControls.push_back(myNbComponentsSpn);
422 //**********************************************************************************
423 bool CollectionPlugin_WidgetField::storeValueCustom()
425 DataPtr aData = myFeature->data();
426 // Store number of components
427 AttributeStringArrayPtr aStringsAttr =
428 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
429 int aNbComps = myCompNamesList.size();
430 aStringsAttr->setSize(aNbComps);
431 for ( int i = 0; i < aNbComps; i++)
432 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
434 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
435 // Store number of steps
436 int aNbSteps = myDataTblList.size();
438 // Store Type of the field values
439 int aFldType = myFieldTypeCombo->currentIndex();
441 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
442 aStampsAttr->setSize(aNbSteps);
444 QTableWidget* aTable = myDataTblList.first();
445 int aRows = aTable->rowCount();
446 // first column contains selected names which should not be stored
447 int aColumns = aTable->columnCount() - 1;
449 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
450 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
451 for (int i = 0; i < aNbSteps; i++) {
452 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
453 aTable = myDataTblList.at(i);
454 for (int j = 0; j < aColumns; j++) {
455 for (int k = 0; k < aRows; k++) {
456 QString aTblVal = aTable->item(k, j + 1)->text();
457 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
461 updateObject(myFeature);
465 //**********************************************************************************
466 bool CollectionPlugin_WidgetField::restoreValueCustom()
469 DataPtr aData = myFeature->data();
471 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
472 std::string aTypeStr = aSelList->selectionType();
474 return false; // The attribute is not initialized
475 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
477 // Get number of components
478 AttributeStringArrayPtr aStringsAttr =
479 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
481 myCompNamesList.clear();
482 for (int i = 0; i < aStringsAttr->size(); i++) {
483 myCompNamesList.append(aStringsAttr->value(i).c_str());
485 isBlocked = myNbComponentsSpn->blockSignals(true);
486 myNbComponentsSpn->setValue(myCompNamesList.size());
487 myNbComponentsSpn->blockSignals(isBlocked);
489 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
490 // Get number of steps
491 int aNbSteps = aTablesAttr->tables();
492 myStepSlider->setMaximum(aNbSteps);
493 //myStepSlider->setValue(1);
495 while (myDataTblList.count() > aNbSteps) {
496 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
497 myStepWgt->removeWidget(aWgt);
500 myStampSpnList.removeLast();
501 myDataTblList.removeLast();
503 while (myDataTblList.count() < aNbSteps)
504 appendStepControls();
505 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
508 // Get Type of the field values
509 isBlocked = myFieldTypeCombo->blockSignals(true);
510 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
511 myFieldTypeCombo->blockSignals(isBlocked);
512 myDelegate->setDataType(aTablesAttr->type());
514 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
516 int aRows = aTablesAttr->rows();
517 int aCols = aTablesAttr->columns();
519 // Get width of columns
521 QTableWidget* aFirstTable = myDataTblList.first();
522 for (int i = 0; i < aFirstTable->columnCount(); i++)
523 aColWidth.append(aFirstTable->columnWidth(i));
525 QTableWidgetItem* aItem = 0;
526 for (int i = 0; i < aNbSteps; i++) {
527 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
528 QTableWidget* aTable = myDataTblList.at(i);
529 isBlocked = aTable->blockSignals(true);
530 aTable->setRowCount(aRows);
531 aTable->setColumnCount(aCols + 1);
532 updateHeaders(aTable);
533 for (int j = 0; j < aCols + 1; j++) {
534 for (int k = 0; k < aRows; k++) {
535 aItem = aTable->item(k, j);
536 if ((j == 0) && (k > 0)) {
537 // Add selection names
538 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
540 aItem->setText(aAttr->namingName().c_str());
542 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
543 aTable->setItem(k, j, aItem);
547 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
549 aItem->setText(getValueText(aVal));
551 aItem = createValueItem(aVal);
553 aItem->setBackgroundColor(Qt::lightGray);
554 aTable->setItem(k, j, aItem);
559 // Restore columns width
560 for (int i = 0; i < aTable->columnCount(); i++) {
561 if (i < aColWidth.size())
562 aTable->setColumnWidth(i, aColWidth.at(i));
565 aTable->blockSignals(isBlocked);
570 //**********************************************************************************
571 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
573 QString aType(theStr.c_str());
574 aType = aType.toLower();
575 if (aType == "vertex")
577 else if (aType == "edge")
579 else if (aType == "face")
581 else if (aType == "solid")
583 else if (aType == "object")
585 else if (aType == "part")
591 //**********************************************************************************
592 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
611 //**********************************************************************************
612 QIntList CollectionPlugin_WidgetField::shapeTypes() const
615 switch (myShapeTypeCombo->currentIndex()) {
617 aRes.append(ModuleBase_Tools::shapeType("vertex"));
620 aRes.append(ModuleBase_Tools::shapeType("edge"));
623 aRes.append(ModuleBase_Tools::shapeType("face"));
626 aRes.append(ModuleBase_Tools::shapeType("solid"));
629 aRes.append(ModuleBase_Tools::shapeType("object"));
632 // TODO: Selection mode for Parts
638 //**********************************************************************************
639 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
641 ModelAPI_AttributeTables::Value aVal;
642 switch (myFieldTypeCombo->currentIndex()) {
643 case ModelAPI_AttributeTables::BOOLEAN:
644 aVal.myBool = (theStrVal == MYTrue)? true : false;
646 case ModelAPI_AttributeTables::DOUBLE:
647 aVal.myDouble = theStrVal.toDouble();
649 case ModelAPI_AttributeTables::INTEGER:
650 aVal.myInt = theStrVal.toInt();
652 case ModelAPI_AttributeTables::STRING:
653 aVal.myStr = theStrVal.toStdString();
659 //**********************************************************************************
660 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
662 int aOldCol = myCompNamesList.count();
663 int aNbRows = myDataTblList.first()->rowCount();
664 int aDif = theVal - aOldCol;
665 QTableWidgetItem* aItem = 0;
667 while (myCompNamesList.count() != theVal) {
669 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
671 myCompNamesList.removeLast();
674 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
675 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
677 foreach(QTableWidget* aDataTbl, myDataTblList) {
678 aDataTbl->setColumnCount(theVal + 1);
679 updateHeaders(aDataTbl);
680 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
681 for (int j = 0; j < aNbRows; j++) {
682 aItem = aDataTbl->item(j, i + 1);
684 aItem = createDefaultItem();
686 aItem->setBackgroundColor(Qt::lightGray);
687 aDataTbl->setItem(j, i + 1, aItem);
692 emit valuesChanged();
695 //**********************************************************************************
696 void CollectionPlugin_WidgetField::onAddStep()
698 int aMax = myStepSlider->maximum();
700 myStepSlider->setMaximum(aMax);
701 myMaxLbl->setText(QString::number(aMax));
702 appendStepControls();
703 myStepSlider->setValue(aMax);
705 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
706 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
709 AttributeSelectionListPtr aSelList =
710 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
711 if (!aSelList->isInitialized())
713 int aSelNb = aSelList->size();
717 int aColumns = myNbComponentsSpn->value() + 1;
718 int aRows = aSelNb + 1;
719 QTableWidget* aTable = myDataTblList.last();
720 aTable->setRowCount(aRows);
721 QTableWidgetItem* aItem = 0;
722 for(int i = 0; i < aColumns; i++) {
724 for(int j = 1; j < aRows; j++) {
725 aItem = aTable->item(j, i);
727 aItem = new QTableWidgetItem();
728 aTable->setItem(j, i, aItem);
730 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
731 aItem->setText(aAttr->namingName().c_str());
732 aItem->setToolTip(aAttr->namingName().c_str());
735 QString aDefVal = aTable->item(0, i)->text();
736 for(int j = 1; j < aRows; j++) {
737 aItem = aTable->item(j, i);
739 aItem = new QTableWidgetItem();
740 aTable->setItem(j, i, aItem);
742 aItem->setText(aDefVal);
746 emit valuesChanged();
749 //**********************************************************************************
750 void CollectionPlugin_WidgetField::onRemoveStep()
752 int aMax = myStepSlider->maximum();
754 myMaxLbl->setText(QString::number(aMax));
755 removeStepControls();
756 myStepSlider->setMaximum(aMax);
758 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
759 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
760 emit valuesChanged();
763 //**********************************************************************************
764 void CollectionPlugin_WidgetField::clearData()
766 foreach(QTableWidget* aDataTbl, myDataTblList) {
767 aDataTbl->setRowCount(1);
771 //**********************************************************************************
772 void CollectionPlugin_WidgetField::onStepMove(int theStep)
774 myCurStepLbl->setText(QString::number(theStep));
775 myStepWgt->setCurrentIndex(theStep - 1);
778 //**********************************************************************************
779 bool CollectionPlugin_WidgetField::
780 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
782 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
785 //**********************************************************************************
786 bool CollectionPlugin_WidgetField::
787 setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
790 myActivation = false;
793 // Ignore selection for Parts mode
794 if (myShapeTypeCombo->currentIndex() == 5)
797 QList<ModuleBase_ViewerPrsPtr> aSelected;
798 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
799 for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
800 ModuleBase_ViewerPrsPtr aValue = *anIt;
801 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
802 if (theToValidate && aRes.get() && acceptSubShape(aValue->shape(), aRes))
803 aSelected.append(aValue);
805 AttributeSelectionListPtr aSelList =
806 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
807 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
813 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
814 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
815 aShape = aPrs->shape();
816 if (!aResult.get() && !aShape.get())
818 if (!aSelList->isInList(aResult, aShape)) {
819 aSelList->append(aResult, aShape);
823 int aColumns = myDataTblList.first()->columnCount();
824 int aRows = myDataTblList.first()->rowCount();
825 int aNewRows = aNbData + 1;
826 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
827 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
829 QTableWidgetItem* aItem = 0;
830 foreach(QTableWidget* aTable, myDataTblList) {
831 aTable->setRowCount(aNewRows);
832 if (aNewRows > aRows) {
834 for(int i = 0; i < aColumns; i++) {
836 for(int j = 1; j < aNewRows; j++) {
837 aItem = aTable->item(j, i);
839 aItem = new QTableWidgetItem();
840 aTable->setItem(j, i, aItem);
842 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
843 aItem->setText(aAttr->namingName().c_str());
844 aItem->setToolTip(aAttr->namingName().c_str());
847 QString aDefVal = aTable->item(0, i)->text();
848 for(int j = aRows; j < aNewRows; j++) {
849 aItem = aTable->item(j, i);
851 aItem = new QTableWidgetItem();
852 aTable->setItem(j, i, aItem);
854 aItem->setText(aDefVal);
859 // Update only selection name
860 for(int j = 1; j < aNewRows - 1; j++) {
861 AttributeSelectionPtr aAttr = aSelList->value(j);
862 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
863 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
870 //**********************************************************************************
871 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
873 ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
874 if (aOldType != theIdx) {
875 myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
876 int aColumns = myDataTblList.first()->columnCount();
877 int aRows = myDataTblList.first()->rowCount();
878 foreach(QTableWidget* aTable, myDataTblList) {
879 for(int i = 1; i < aColumns; i++) {
880 for(int j = 0; j < aRows; j++) {
882 case ModelAPI_AttributeTables::DOUBLE:
883 case ModelAPI_AttributeTables::INTEGER:
884 aTable->item(j, i)->setText("0");
886 case ModelAPI_AttributeTables::BOOLEAN:
887 aTable->item(j, i)->setText(MYFalse);
889 case ModelAPI_AttributeTables::STRING:
890 aTable->item(j, i)->setText("");
896 emit valuesChanged();
900 //**********************************************************************************
901 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
903 // Do not store here column of names
906 if (!myFeature.get())
908 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
909 int aNb = myDataTblList.indexOf(aTable);
912 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
914 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
915 if (aTablesAttr->isInitialized())
916 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
918 emit valuesChanged();
921 //**********************************************************************************
922 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
924 activateSelectionAndFilters(theType == 5? false:true);
926 AttributeSelectionListPtr aSelList =
927 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
929 std::string aTypeName = getSelectionType(theType);
930 if (aTypeName == aSelList->selectionType())
932 aSelList->setSelectionType(aTypeName);
934 //Clear old selection
937 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
938 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
939 emit valuesChanged();
942 //**********************************************************************************
943 bool CollectionPlugin_WidgetField::processEnter()
952 //**********************************************************************************
953 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
955 if (theNew && (!myIsTabEdit))
956 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
959 //**********************************************************************************
960 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
962 myMaxLbl->setText(QString::number(theMax));
963 myRemoveBtn->setEnabled(theMax > 1);
966 //**********************************************************************************
967 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
969 if (myDataTblList.count() < 2)
971 QObject* aSender = sender();
972 foreach(QTableWidget* aTable, myDataTblList) {
973 if (aTable->horizontalHeader() != aSender)
974 aTable->setColumnWidth(theIndex, theNew);
978 //**********************************************************************************
979 QList<std::shared_ptr<ModuleBase_ViewerPrs>>
980 CollectionPlugin_WidgetField::getAttributeSelection() const
982 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
984 DataPtr aData = myFeature->data();
985 AttributeSelectionListPtr aSelList =
986 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
987 AttributeSelectionPtr aAttr;
989 for (int i = 0; i < aSelList->size(); i++) {
990 aAttr = aSelList->value(i);
991 ModuleBase_ViewerPrsPtr
992 aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));