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 DataTableItemDelegate::DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType)
44 : QStyledItemDelegate(), myType(theType)
49 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
50 const QStyleOptionViewItem & theOption,
51 const QModelIndex& theIndex ) const
54 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
55 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
56 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
57 aEdt->setReadOnly(true);
60 QLineEdit* aLineEdt = 0;
62 case ModelAPI_AttributeTables::DOUBLE:
63 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
67 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
71 case ModelAPI_AttributeTables::INTEGER:
72 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
76 aLineEdt->setValidator(new QIntValidator(aLineEdt));
80 case ModelAPI_AttributeTables::BOOLEAN:
82 QComboBox* aBox = new QComboBox(theParent);
83 aBox->addItem(MYFalse);
84 aBox->addItem(MYTrue);
89 aEditor = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
90 //QObject* aThat = (QObject*) this;
91 //aEditor->installEventFilter(aThat);
95 //bool DataTableItemDelegate::eventFilter(QObject* theObj, QEvent* theEvent)
97 // qDebug("### Type = %i", theEvent->type());
98 // if (theEvent->type() == QEvent::Close) {
99 // QWidget* aWgt = dynamic_cast<QWidget*>(theObj);
102 // return QStyledItemDelegate::eventFilter(theObj, theEvent);
106 //**********************************************************************************
107 //**********************************************************************************
108 //**********************************************************************************
109 CollectionPlugin_WidgetField::
110 CollectionPlugin_WidgetField(QWidget* theParent,
111 ModuleBase_IWorkshop* theWorkshop,
112 const Config_WidgetAPI* theData):
113 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
114 myIsTabEdit(false), myActivation(false)
116 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
118 // Types definition controls
119 QWidget* aTypesWgt = new QWidget(this);
120 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
121 aTypesLayout->setContentsMargins(0, 0, 0, 0);
122 aMainLayout->addWidget(aTypesWgt);
125 myShapeTypeCombo = new QComboBox(aTypesWgt);
126 QStringList aShapeTypes;
127 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
128 << tr("Solids") << tr("Objects") << tr("Parts");
129 myShapeTypeCombo->addItems(aShapeTypes);
130 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
133 myFieldTypeCombo = new QComboBox(aTypesWgt);
134 QStringList aFieldTypes;
135 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
137 myFieldTypeCombo->addItems(aFieldTypes);
138 myFieldTypeCombo->setCurrentIndex(2);
139 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
141 // Number of components
142 myNbComponentsSpn = new QSpinBox(aTypesWgt);
143 myNbComponentsSpn->setMinimum(1);
144 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
147 QFrame* aStepFrame = new QFrame(this);
148 aStepFrame->setFrameShape(QFrame::Box);
149 aStepFrame->setFrameStyle(QFrame::StyledPanel);
150 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
151 aMainLayout->addWidget(aStepFrame);
153 // Current step label
154 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
155 myCurStepLbl = new QLabel("1", aStepFrame);
156 QFont aFont = myCurStepLbl->font();
158 myCurStepLbl->setFont(aFont);
159 aStepLayout->addWidget(myCurStepLbl, 0, 1);
162 QWidget* aSliderWidget = new QWidget(aStepFrame);
163 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
164 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
165 aSliderLayout->setContentsMargins(0, 0, 0, 0);
167 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
169 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
170 myStepSlider->setTickPosition(QSlider::TicksBelow);
171 myStepSlider->setRange(1, 1);
172 myStepSlider->setPageStep(myStepSlider->singleStep());
173 aSliderLayout->addWidget(myStepSlider, 1);
175 myMaxLbl = new QLabel("1", aSliderWidget);
176 aSliderLayout->addWidget(myMaxLbl);
179 myCompNamesList << "Comp 1";
180 myStepWgt = new QStackedWidget(aStepFrame);
181 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
184 new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
185 myFieldTypeCombo->currentIndex());
187 appendStepControls();
190 QWidget* aBtnWgt = new QWidget(this);
191 aMainLayout->addWidget(aBtnWgt);
192 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
193 aBtnLayout->setContentsMargins(0, 0, 0, 0);
195 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
196 aBtnLayout->addWidget(aAddBtn);
198 aBtnLayout->addStretch(1);
200 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
201 aBtnLayout->addWidget(myRemoveBtn);
202 myRemoveBtn->setEnabled(false);
204 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
205 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
206 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
207 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
208 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
209 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
210 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
211 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
214 //**********************************************************************************
215 void CollectionPlugin_WidgetField::appendStepControls()
217 QWidget* aWidget = new QWidget(myStepWgt);
218 QGridLayout* aStepLayout = new QGridLayout(aWidget);
219 aStepLayout->setContentsMargins(0, 0, 0, 0);
221 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
223 QSpinBox* aStampSpn = new QSpinBox(aWidget);
224 aStepLayout->addWidget(aStampSpn, 0, 1);
226 myStampSpnList.append(aStampSpn);
229 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
230 aDataTbl->installEventFilter(this);
231 aDataTbl->setItemDelegate(myDelegate);
234 if (!myDataTblList.isEmpty()) {
235 QTableWidget* aFirstTable = myDataTblList.first();
236 for (int i = 0; i < aFirstTable->columnCount(); i++)
237 aColWidth.append(aFirstTable->columnWidth(i));
239 myDataTblList.append(aDataTbl);
241 aDataTbl->verticalHeader()->hide();
242 aDataTbl->setRowHeight(0, 25);
243 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
245 connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
246 SLOT(onColumnResize(int, int, int)));
248 updateHeaders(aDataTbl);
250 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
251 aItem->setBackgroundColor(Qt::lightGray);
252 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
253 aDataTbl->setItem(0, 0, aItem);
255 // Set default value item
256 for (int i = 0; i < myCompNamesList.count(); i++) {
257 aItem = createDefaultItem();
258 aItem->setBackgroundColor(Qt::lightGray);
259 aDataTbl->setItem(0, i + 1, aItem);
262 if (aColWidth.length() > 0) {
263 for (int i = 0; i < aDataTbl->columnCount(); i++) {
264 if (i < aColWidth.size())
265 aDataTbl->setColumnWidth(i, aColWidth.at(i));
268 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
269 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
271 myStepWgt->addWidget(aWidget);
272 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
275 //**********************************************************************************
276 void CollectionPlugin_WidgetField::deactivate()
278 ModuleBase_WidgetSelector::deactivate();
282 //**********************************************************************************
283 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
285 // myShapeTypeCombo->setEnabled(!isEditingMode());
286 // myFieldTypeCombo->setEnabled(!isEditingMode());
287 // myNbComponentsSpn->setEnabled(!isEditingMode());
290 //**********************************************************************************
291 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
293 QObject* aObject = 0;
294 foreach(QTableWidget* aTable, myDataTblList) {
295 if (aTable->horizontalHeader()->viewport() == theObject) {
301 if (theEvent->type() == QEvent::MouseButtonDblClick) {
302 if (myHeaderEditor) { //delete previous editor
303 myHeaderEditor->deleteLater();
306 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
307 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
308 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
310 int aShift = aTable->horizontalScrollBar()->value();
311 int aPos = aMouseEvent->x();
312 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
315 aRect.setLeft(aHeader->sectionPosition(aIndex));
316 aRect.setWidth(aHeader->sectionSize(aIndex));
318 aRect.setHeight(aHeader->height());
319 aRect.adjust(1, 1, -1, -1);
320 aRect.translate(-aShift, 0);
322 myHeaderEditor = new QLineEdit(aHeader->viewport());
323 myHeaderEditor->move(aRect.topLeft());
324 myHeaderEditor->resize(aRect.size());
325 myHeaderEditor->setFrame(false);
326 QString aText = aHeader->model()->
327 headerData(aIndex, aHeader->orientation()).toString();
328 myHeaderEditor->setText(aText);
329 myHeaderEditor->setFocus();
330 myEditIndex = aIndex; //save for future use
331 myHeaderEditor->installEventFilter(this); //catch focus out event
332 //if user presses Enter it should close editor
333 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
334 myHeaderEditor->show();
338 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
339 QString aNewTitle = myHeaderEditor->text();
341 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
342 myHeaderEditor->deleteLater(); //safely delete editor
344 // Store into data model
345 AttributeStringArrayPtr aStringsAttr =
346 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
347 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
348 foreach(QTableWidget* aTable, myDataTblList) {
349 updateHeaders(aTable);
351 } else if (theEvent->type() == QEvent::FocusIn) {
352 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
354 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
355 if (aPanel->activeWidget() != this) {
357 aPanel->activateWidget(this, false);
361 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
364 //**********************************************************************************
365 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
367 QTableWidgetItem* aItem = new QTableWidgetItem();
368 switch (myFieldTypeCombo->currentIndex()) {
369 case ModelAPI_AttributeTables::DOUBLE:
370 case ModelAPI_AttributeTables::INTEGER:
373 case ModelAPI_AttributeTables::BOOLEAN:
374 aItem->setText(MYFalse);
376 case ModelAPI_AttributeTables::STRING:
383 //**********************************************************************************
384 QTableWidgetItem* CollectionPlugin_WidgetField::
385 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
387 QTableWidgetItem* aItem = new QTableWidgetItem();
388 aItem->setText(getValueText(theVal));
392 //**********************************************************************************
393 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
395 switch (myFieldTypeCombo->currentIndex()) {
396 case ModelAPI_AttributeTables::DOUBLE:
397 return QString::number(theVal.myDouble);
398 case ModelAPI_AttributeTables::INTEGER:
399 return QString::number(theVal.myInt);
400 case ModelAPI_AttributeTables::BOOLEAN:
401 return theVal.myBool? MYTrue : MYFalse;
402 case ModelAPI_AttributeTables::STRING:
403 return theVal.myStr.c_str();
409 //**********************************************************************************
410 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
412 QStringList aHeaders;
413 aHeaders << tr(MYFirstCol);
414 aHeaders << myCompNamesList;
415 theDataTbl->setHorizontalHeaderLabels(aHeaders);
418 //**********************************************************************************
419 void CollectionPlugin_WidgetField::removeStepControls()
421 int aCurWgtId = myStepWgt->currentIndex();
422 QWidget* aWgt = myStepWgt->currentWidget();
423 myStepWgt->removeWidget(aWgt);
425 myStampSpnList.removeAt(aCurWgtId);
426 myDataTblList.removeAt(aCurWgtId);
430 //**********************************************************************************
431 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
433 QList<QWidget*> aControls;
434 // this control will accept focus and will be highlighted in the Property Panel
435 aControls.append(myShapeTypeCombo);
436 aControls.append(myFieldTypeCombo);
437 aControls.append(myNbComponentsSpn);
438 if (myStampSpnList.size() > 0)
439 aControls.append(myStampSpnList.first());
440 if (myDataTblList.size() > 0)
441 aControls.append(myDataTblList.first());
446 //**********************************************************************************
447 bool CollectionPlugin_WidgetField::storeValueCustom()
449 DataPtr aData = myFeature->data();
450 // Store number of components
451 AttributeStringArrayPtr aStringsAttr =
452 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
453 int aNbComps = myCompNamesList.size();
454 aStringsAttr->setSize(aNbComps);
455 for ( int i = 0; i < aNbComps; i++)
456 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
458 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
459 // Store number of steps
460 int aNbSteps = myDataTblList.size();
462 // Store Type of the field values
463 int aFldType = myFieldTypeCombo->currentIndex();
465 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
466 aStampsAttr->setSize(aNbSteps);
468 QTableWidget* aTable = myDataTblList.first();
469 int aRows = aTable->rowCount();
470 // first column contains selected names which should not be stored
471 int aColumns = aTable->columnCount() - 1;
473 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
474 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
475 for (int i = 0; i < aNbSteps; i++) {
476 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
477 aTable = myDataTblList.at(i);
478 for (int j = 0; j < aColumns; j++) {
479 for (int k = 0; k < aRows; k++) {
480 QString aTblVal = aTable->item(k, j + 1)->text();
481 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
485 updateObject(myFeature);
489 //**********************************************************************************
490 bool CollectionPlugin_WidgetField::restoreValueCustom()
493 DataPtr aData = myFeature->data();
495 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
496 std::string aTypeStr = aSelList->selectionType();
498 return false; // The attribute is not initialized
499 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
501 // Get number of components
502 AttributeStringArrayPtr aStringsAttr =
503 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
505 myCompNamesList.clear();
506 for (int i = 0; i < aStringsAttr->size(); i++) {
507 myCompNamesList.append(aStringsAttr->value(i).c_str());
509 isBlocked = myNbComponentsSpn->blockSignals(true);
510 myNbComponentsSpn->setValue(myCompNamesList.size());
511 myNbComponentsSpn->blockSignals(isBlocked);
513 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
514 // Get number of steps
515 int aNbSteps = aTablesAttr->tables();
516 myStepSlider->setMaximum(aNbSteps);
517 //myStepSlider->setValue(1);
519 while (myDataTblList.count() > aNbSteps) {
520 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
521 myStepWgt->removeWidget(aWgt);
524 myStampSpnList.removeLast();
525 myDataTblList.removeLast();
527 while (myDataTblList.count() < aNbSteps)
528 appendStepControls();
529 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
532 // Get Type of the field values
533 isBlocked = myFieldTypeCombo->blockSignals(true);
534 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
535 myFieldTypeCombo->blockSignals(isBlocked);
536 myDelegate->setDataType(aTablesAttr->type());
538 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
540 int aRows = aTablesAttr->rows();
541 int aCols = aTablesAttr->columns();
543 // Get width of columns
545 QTableWidget* aFirstTable = myDataTblList.first();
546 for (int i = 0; i < aFirstTable->columnCount(); i++)
547 aColWidth.append(aFirstTable->columnWidth(i));
549 QTableWidgetItem* aItem = 0;
550 for (int i = 0; i < aNbSteps; i++) {
551 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
552 QTableWidget* aTable = myDataTblList.at(i);
553 isBlocked = aTable->blockSignals(true);
554 aTable->setRowCount(aRows);
555 aTable->setColumnCount(aCols + 1);
556 updateHeaders(aTable);
557 for (int j = 0; j < aCols + 1; j++) {
558 for (int k = 0; k < aRows; k++) {
559 aItem = aTable->item(k, j);
560 if ((j == 0) && (k > 0)) {
561 // Add selection names
562 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
564 aItem->setText(aAttr->namingName().c_str());
566 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
567 aTable->setItem(k, j, aItem);
571 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
573 aItem->setText(getValueText(aVal));
575 aItem = createValueItem(aVal);
577 aItem->setBackgroundColor(Qt::lightGray);
578 aTable->setItem(k, j, aItem);
583 // Restore columns width
584 for (int i = 0; i < aTable->columnCount(); i++) {
585 if (i < aColWidth.size())
586 aTable->setColumnWidth(i, aColWidth.at(i));
589 aTable->blockSignals(isBlocked);
594 //**********************************************************************************
595 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
597 QString aType(theStr.c_str());
598 aType = aType.toLower();
599 if (aType == "vertex")
601 else if (aType == "edge")
603 else if (aType == "face")
605 else if (aType == "solid")
607 else if (aType == "object")
609 else if (aType == "part")
615 //**********************************************************************************
616 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
635 //**********************************************************************************
636 QIntList CollectionPlugin_WidgetField::shapeTypes() const
639 switch (myShapeTypeCombo->currentIndex()) {
641 aRes.append(ModuleBase_Tools::shapeType("vertex"));
644 aRes.append(ModuleBase_Tools::shapeType("edge"));
647 aRes.append(ModuleBase_Tools::shapeType("face"));
650 aRes.append(ModuleBase_Tools::shapeType("solid"));
653 aRes.append(ModuleBase_Tools::shapeType("object"));
656 // TODO: Selection mode for Parts
662 //**********************************************************************************
663 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
665 ModelAPI_AttributeTables::Value aVal;
666 switch (myFieldTypeCombo->currentIndex()) {
667 case ModelAPI_AttributeTables::BOOLEAN:
668 aVal.myBool = (theStrVal == MYTrue)? true : false;
670 case ModelAPI_AttributeTables::DOUBLE:
671 aVal.myDouble = theStrVal.toDouble();
673 case ModelAPI_AttributeTables::INTEGER:
674 aVal.myInt = theStrVal.toInt();
676 case ModelAPI_AttributeTables::STRING:
677 aVal.myStr = theStrVal.toStdString();
683 //**********************************************************************************
684 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
686 int aOldCol = myCompNamesList.count();
687 int aNbRows = myDataTblList.first()->rowCount();
688 int aDif = theVal - aOldCol;
689 QTableWidgetItem* aItem = 0;
691 while (myCompNamesList.count() != theVal) {
693 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
695 myCompNamesList.removeLast();
698 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
699 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
701 foreach(QTableWidget* aDataTbl, myDataTblList) {
702 aDataTbl->setColumnCount(theVal + 1);
703 updateHeaders(aDataTbl);
704 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
705 for (int j = 0; j < aNbRows; j++) {
706 aItem = aDataTbl->item(j, i + 1);
708 aItem = createDefaultItem();
710 aItem->setBackgroundColor(Qt::lightGray);
711 aDataTbl->setItem(j, i + 1, aItem);
716 emit valuesChanged();
719 //**********************************************************************************
720 void CollectionPlugin_WidgetField::onAddStep()
722 int aMax = myStepSlider->maximum();
724 myStepSlider->setMaximum(aMax);
725 myMaxLbl->setText(QString::number(aMax));
726 appendStepControls();
727 myStepSlider->setValue(aMax);
729 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
730 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
733 AttributeSelectionListPtr aSelList =
734 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
735 if (!aSelList->isInitialized())
737 int aSelNb = aSelList->size();
741 int aColumns = myNbComponentsSpn->value() + 1;
742 int aRows = aSelNb + 1;
743 QTableWidget* aTable = myDataTblList.last();
744 aTable->setRowCount(aRows);
745 QTableWidgetItem* aItem = 0;
746 for(int i = 0; i < aColumns; i++) {
748 for(int j = 1; j < aRows; j++) {
749 aItem = aTable->item(j, i);
751 aItem = new QTableWidgetItem();
752 aTable->setItem(j, i, aItem);
754 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
755 aItem->setText(aAttr->namingName().c_str());
756 aItem->setToolTip(aAttr->namingName().c_str());
759 QString aDefVal = aTable->item(0, i)->text();
760 for(int j = 1; j < aRows; j++) {
761 aItem = aTable->item(j, i);
763 aItem = new QTableWidgetItem();
764 aTable->setItem(j, i, aItem);
766 aItem->setText(aDefVal);
770 emit valuesChanged();
773 //**********************************************************************************
774 void CollectionPlugin_WidgetField::onRemoveStep()
776 int aMax = myStepSlider->maximum();
778 myMaxLbl->setText(QString::number(aMax));
779 removeStepControls();
780 myStepSlider->setMaximum(aMax);
782 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
783 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
784 emit valuesChanged();
787 //**********************************************************************************
788 void CollectionPlugin_WidgetField::clearData()
790 foreach(QTableWidget* aDataTbl, myDataTblList) {
791 aDataTbl->setRowCount(1);
795 //**********************************************************************************
796 void CollectionPlugin_WidgetField::onStepMove(int theStep)
798 myCurStepLbl->setText(QString::number(theStep));
799 myStepWgt->setCurrentIndex(theStep - 1);
802 //**********************************************************************************
803 bool CollectionPlugin_WidgetField::
804 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
806 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
809 //**********************************************************************************
810 bool CollectionPlugin_WidgetField::
811 setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
814 myActivation = false;
817 // Ignore selection for Parts mode
818 if (myShapeTypeCombo->currentIndex() == 5)
821 QList<ModuleBase_ViewerPrsPtr> aSelected;
822 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
823 for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
824 ModuleBase_ViewerPrsPtr aValue = *anIt;
825 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
826 if (theToValidate && aRes.get() && acceptSubShape(aValue->shape(), aRes))
827 aSelected.append(aValue);
829 AttributeSelectionListPtr aSelList =
830 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
831 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
837 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
838 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
839 aShape = aPrs->shape();
840 if (!aResult.get() && !aShape.get())
842 if (!aSelList->isInList(aResult, aShape)) {
843 aSelList->append(aResult, aShape);
847 int aColumns = myDataTblList.first()->columnCount();
848 int aRows = myDataTblList.first()->rowCount();
849 int aNewRows = aNbData + 1;
850 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
851 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
853 QTableWidgetItem* aItem = 0;
854 foreach(QTableWidget* aTable, myDataTblList) {
855 aTable->setRowCount(aNewRows);
856 if (aNewRows > aRows) {
858 for(int i = 0; i < aColumns; i++) {
860 for(int j = 1; j < aNewRows; j++) {
861 aItem = aTable->item(j, i);
863 aItem = new QTableWidgetItem();
864 aTable->setItem(j, i, aItem);
866 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
867 aItem->setText(aAttr->namingName().c_str());
868 aItem->setToolTip(aAttr->namingName().c_str());
871 QString aDefVal = aTable->item(0, i)->text();
872 for(int j = aRows; j < aNewRows; j++) {
873 aItem = aTable->item(j, i);
875 aItem = new QTableWidgetItem();
876 aTable->setItem(j, i, aItem);
878 aItem->setText(aDefVal);
883 // Update only selection name
884 for(int j = 1; j < aNewRows - 1; j++) {
885 AttributeSelectionPtr aAttr = aSelList->value(j);
886 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
887 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
894 //**********************************************************************************
895 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
897 ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
898 if (aOldType != theIdx) {
899 myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
900 int aColumns = myDataTblList.first()->columnCount();
901 int aRows = myDataTblList.first()->rowCount();
902 foreach(QTableWidget* aTable, myDataTblList) {
903 for(int i = 1; i < aColumns; i++) {
904 for(int j = 0; j < aRows; j++) {
906 case ModelAPI_AttributeTables::DOUBLE:
907 case ModelAPI_AttributeTables::INTEGER:
908 aTable->item(j, i)->setText("0");
910 case ModelAPI_AttributeTables::BOOLEAN:
911 aTable->item(j, i)->setText(MYFalse);
913 case ModelAPI_AttributeTables::STRING:
914 aTable->item(j, i)->setText("");
920 emit valuesChanged();
924 //**********************************************************************************
925 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
927 // Do not store here column of names
930 if (!myFeature.get())
932 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
933 int aNb = myDataTblList.indexOf(aTable);
936 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
938 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
939 if (aTablesAttr->isInitialized())
940 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
942 emit valuesChanged();
945 //**********************************************************************************
946 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
948 activateSelectionAndFilters(theType == 5? false:true);
950 AttributeSelectionListPtr aSelList =
951 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
953 std::string aTypeName = getSelectionType(theType);
954 if (aTypeName == aSelList->selectionType())
956 aSelList->setSelectionType(aTypeName);
958 //Clear old selection
961 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
962 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
963 emit valuesChanged();
966 //**********************************************************************************
967 bool CollectionPlugin_WidgetField::processEnter()
973 QWidget* aCurrWgt = qApp->focusWidget();
974 int aCurWgtId = myStepWgt->currentIndex();
975 if ((aCurrWgt == myShapeTypeCombo) ||
976 (aCurrWgt == myFieldTypeCombo) ||
977 (aCurrWgt == myNbComponentsSpn) ||
978 (aCurrWgt == myStampSpnList[aCurWgtId]) ||
979 (aCurrWgt == myDataTblList[aCurWgtId])) {
986 //**********************************************************************************
987 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
989 if (theNew && (!myIsTabEdit))
990 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
993 //**********************************************************************************
994 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
996 myMaxLbl->setText(QString::number(theMax));
997 myRemoveBtn->setEnabled(theMax > 1);
1000 //**********************************************************************************
1001 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
1003 if (myDataTblList.count() < 2)
1005 QObject* aSender = sender();
1006 foreach(QTableWidget* aTable, myDataTblList) {
1007 if (aTable->horizontalHeader() != aSender)
1008 aTable->setColumnWidth(theIndex, theNew);
1012 //**********************************************************************************
1013 QList<std::shared_ptr<ModuleBase_ViewerPrs>>
1014 CollectionPlugin_WidgetField::getAttributeSelection() const
1016 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
1018 DataPtr aData = myFeature->data();
1019 AttributeSelectionListPtr aSelList =
1020 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
1021 AttributeSelectionPtr aAttr;
1023 for (int i = 0; i < aSelList->size(); i++) {
1024 aAttr = aSelList->value(i);
1025 ModuleBase_ViewerPrsPtr
1026 aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));