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>
13 #include <ModuleBase_IPropertyPanel.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_AttributeStringArray.h>
17 #include <ModelAPI_AttributeInteger.h>
18 #include <ModelAPI_AttributeIntArray.h>
22 #include <QFormLayout>
27 #include <QTableWidget>
28 #include <QPushButton>
29 #include <QHeaderView>
30 #include <QStackedWidget>
32 #include <QStyledItemDelegate>
35 #include <QMouseEvent>
37 #include <QApplication>
39 const char* MYFirstCol = "Shape";
40 const char* MYTrue = "True";
41 const char* MYFalse = "False";
43 class DataTableItemDelegate : public QStyledItemDelegate
46 DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType) :
47 QStyledItemDelegate() { myType = theType; }
49 virtual QWidget* createEditor(QWidget* theParent,
50 const QStyleOptionViewItem & theOption,
51 const QModelIndex& theIndex) const;
53 ModelAPI_AttributeTables::ValueType dataType() const { return myType; }
55 void setDataType(ModelAPI_AttributeTables::ValueType theType) { myType = theType; }
61 ModelAPI_AttributeTables::ValueType myType;
64 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
65 const QStyleOptionViewItem & theOption,
66 const QModelIndex& theIndex ) const
68 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
69 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
70 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
71 aEdt->setReadOnly(true);
74 QLineEdit* aLineEdt = 0;
76 case ModelAPI_AttributeTables::DOUBLE:
77 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
81 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
85 case ModelAPI_AttributeTables::INTEGER:
86 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
90 aLineEdt->setValidator(new QIntValidator(aLineEdt));
94 case ModelAPI_AttributeTables::BOOLEAN:
96 QComboBox* aBox = new QComboBox(theParent);
97 aBox->addItem(MYFalse);
98 aBox->addItem(MYTrue);
103 return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
108 //**********************************************************************************
109 //**********************************************************************************
110 //**********************************************************************************
111 CollectionPlugin_WidgetField::
112 CollectionPlugin_WidgetField(QWidget* theParent,
113 ModuleBase_IWorkshop* theWorkshop,
114 const Config_WidgetAPI* theData):
115 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
116 myIsTabEdit(false), myActivation(false)
118 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
120 // Types definition controls
121 QWidget* aTypesWgt = new QWidget(this);
122 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
123 aTypesLayout->setContentsMargins(0, 0, 0, 0);
124 aMainLayout->addWidget(aTypesWgt);
127 myShapeTypeCombo = new QComboBox(aTypesWgt);
128 QStringList aShapeTypes;
129 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
130 << tr("Solids") << tr("Objects") << tr("Parts");
131 myShapeTypeCombo->addItems(aShapeTypes);
132 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
135 myFieldTypeCombo = new QComboBox(aTypesWgt);
136 QStringList aFieldTypes;
137 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
139 myFieldTypeCombo->addItems(aFieldTypes);
140 myFieldTypeCombo->setCurrentIndex(2);
141 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
143 // Number of components
144 myNbComponentsSpn = new QSpinBox(aTypesWgt);
145 myNbComponentsSpn->setMinimum(1);
146 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
149 QFrame* aStepFrame = new QFrame(this);
150 aStepFrame->setFrameShape(QFrame::Box);
151 aStepFrame->setFrameStyle(QFrame::StyledPanel);
152 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
153 aMainLayout->addWidget(aStepFrame);
155 // Current step label
156 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
157 myCurStepLbl = new QLabel("1", aStepFrame);
158 QFont aFont = myCurStepLbl->font();
160 myCurStepLbl->setFont(aFont);
161 aStepLayout->addWidget(myCurStepLbl, 0, 1);
164 QWidget* aSliderWidget = new QWidget(aStepFrame);
165 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
166 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
167 aSliderLayout->setContentsMargins(0, 0, 0, 0);
169 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
171 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
172 myStepSlider->setTickPosition(QSlider::TicksBelow);
173 myStepSlider->setRange(1, 1);
174 myStepSlider->setPageStep(myStepSlider->singleStep());
175 aSliderLayout->addWidget(myStepSlider, 1);
177 myMaxLbl = new QLabel("1", aSliderWidget);
178 aSliderLayout->addWidget(myMaxLbl);
181 myCompNamesList << "Comp 1";
182 myStepWgt = new QStackedWidget(aStepFrame);
183 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
184 appendStepControls();
187 QWidget* aBtnWgt = new QWidget(this);
188 aMainLayout->addWidget(aBtnWgt);
189 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
190 aBtnLayout->setContentsMargins(0, 0, 0, 0);
192 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
193 aBtnLayout->addWidget(aAddBtn);
195 aBtnLayout->addStretch(1);
197 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
198 aBtnLayout->addWidget(myRemoveBtn);
199 myRemoveBtn->setEnabled(false);
201 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
202 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
203 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
204 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
205 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
206 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
207 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
208 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
211 //**********************************************************************************
212 void CollectionPlugin_WidgetField::appendStepControls()
214 QWidget* aWidget = new QWidget(myStepWgt);
215 QGridLayout* aStepLayout = new QGridLayout(aWidget);
216 aStepLayout->setContentsMargins(0, 0, 0, 0);
218 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
220 QSpinBox* aStampSpn = new QSpinBox(aWidget);
221 aStepLayout->addWidget(aStampSpn, 0, 1);
223 myStampSpnList.append(aStampSpn);
226 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
227 aDataTbl->installEventFilter(this);
228 DataTableItemDelegate* aDelegate = 0;
229 if (myDataTblList.isEmpty())
230 aDelegate = new DataTableItemDelegate(
231 (ModelAPI_AttributeTables::ValueType) myFieldTypeCombo->currentIndex());
233 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
236 if (!myDataTblList.isEmpty()) {
237 QTableWidget* aFirstTable = myDataTblList.first();
238 for (int i = 0; i < aFirstTable->columnCount(); i++)
239 aColWidth.append(aFirstTable->columnWidth(i));
241 aDataTbl->setItemDelegate(aDelegate);
242 myDataTblList.append(aDataTbl);
244 aDataTbl->verticalHeader()->hide();
245 aDataTbl->setRowHeight(0, 25);
246 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
248 connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
249 SLOT(onColumnResize(int, int, int)));
251 updateHeaders(aDataTbl);
253 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
254 aItem->setBackgroundColor(Qt::lightGray);
255 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
256 aDataTbl->setItem(0, 0, aItem);
258 // Set default value item
259 for (int i = 0; i < myCompNamesList.count(); i++) {
260 aItem = createDefaultItem();
261 aItem->setBackgroundColor(Qt::lightGray);
262 aDataTbl->setItem(0, i + 1, aItem);
265 if (aColWidth.length() > 0) {
266 for (int i = 0; i < aDataTbl->columnCount(); i++)
267 aDataTbl->setColumnWidth(i, aColWidth.at(i));
269 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
270 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
272 QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
273 myStepWgt->addWidget(aWidget);
274 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
277 //**********************************************************************************
278 void CollectionPlugin_WidgetField::deactivate()
280 ModuleBase_WidgetSelector::deactivate();
284 //**********************************************************************************
285 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
287 // myShapeTypeCombo->setEnabled(!isEditingMode());
288 // myFieldTypeCombo->setEnabled(!isEditingMode());
289 // myNbComponentsSpn->setEnabled(!isEditingMode());
292 //**********************************************************************************
293 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
295 QObject* aObject = 0;
296 foreach(QTableWidget* aTable, myDataTblList) {
297 if (aTable->horizontalHeader()->viewport() == theObject) {
303 if (theEvent->type() == QEvent::MouseButtonDblClick) {
304 if (myHeaderEditor) { //delete previous editor
305 myHeaderEditor->deleteLater();
308 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
309 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
310 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
312 int aShift = aTable->horizontalScrollBar()->value();
313 int aPos = aMouseEvent->x();
314 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
317 aRect.setLeft(aHeader->sectionPosition(aIndex));
318 aRect.setWidth(aHeader->sectionSize(aIndex));
320 aRect.setHeight(aHeader->height());
321 aRect.adjust(1, 1, -1, -1);
322 aRect.translate(-aShift, 0);
324 myHeaderEditor = new QLineEdit(aHeader->viewport());
325 myHeaderEditor->move(aRect.topLeft());
326 myHeaderEditor->resize(aRect.size());
327 myHeaderEditor->setFrame(false);
328 QString aText = aHeader->model()->
329 headerData(aIndex, aHeader->orientation()).toString();
330 myHeaderEditor->setText(aText);
331 myHeaderEditor->setFocus();
332 myEditIndex = aIndex; //save for future use
333 myHeaderEditor->installEventFilter(this); //catch focus out event
334 //if user presses Enter it should close editor
335 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
336 myHeaderEditor->show();
340 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
341 QString aNewTitle = myHeaderEditor->text();
343 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
344 myHeaderEditor->deleteLater(); //safely delete editor
346 // Store into data model
347 AttributeStringArrayPtr aStringsAttr =
348 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
349 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
350 foreach(QTableWidget* aTable, myDataTblList) {
351 updateHeaders(aTable);
353 } else if (theEvent->type() == QEvent::FocusIn) {
354 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
356 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
357 if (aPanel->activeWidget() != this) {
359 aPanel->activateWidget(this, false);
363 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
366 //**********************************************************************************
367 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
369 QTableWidgetItem* aItem = new QTableWidgetItem();
370 switch (myFieldTypeCombo->currentIndex()) {
371 case ModelAPI_AttributeTables::DOUBLE:
372 case ModelAPI_AttributeTables::INTEGER:
375 case ModelAPI_AttributeTables::BOOLEAN:
376 aItem->setText(MYFalse);
378 case ModelAPI_AttributeTables::STRING:
385 //**********************************************************************************
386 QTableWidgetItem* CollectionPlugin_WidgetField::
387 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
389 QTableWidgetItem* aItem = new QTableWidgetItem();
390 aItem->setText(getValueText(theVal));
394 //**********************************************************************************
395 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
397 switch (myFieldTypeCombo->currentIndex()) {
398 case ModelAPI_AttributeTables::DOUBLE:
399 return QString::number(theVal.myDouble);
400 case ModelAPI_AttributeTables::INTEGER:
401 return QString::number(theVal.myInt);
402 case ModelAPI_AttributeTables::BOOLEAN:
403 return theVal.myBool? MYTrue : MYFalse;
404 case ModelAPI_AttributeTables::STRING:
405 return theVal.myStr.c_str();
411 //**********************************************************************************
412 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
414 QStringList aHeaders;
415 aHeaders << tr(MYFirstCol);
416 aHeaders << myCompNamesList;
417 theDataTbl->setHorizontalHeaderLabels(aHeaders);
420 //**********************************************************************************
421 void CollectionPlugin_WidgetField::removeStepControls()
423 int aCurWgtId = myStepWgt->currentIndex();
424 QWidget* aWgt = myStepWgt->currentWidget();
425 myStepWgt->removeWidget(aWgt);
427 myStampSpnList.removeAt(aCurWgtId);
428 myDataTblList.removeAt(aCurWgtId);
432 //**********************************************************************************
433 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
435 QList<QWidget*> aControls;
436 // this control will accept focus and will be highlighted in the Property Panel
437 aControls.push_back(myShapeTypeCombo);
438 aControls.push_back(myFieldTypeCombo);
439 aControls.push_back(myNbComponentsSpn);
443 //**********************************************************************************
444 bool CollectionPlugin_WidgetField::storeValueCustom()
446 DataPtr aData = myFeature->data();
447 // Store number of components
448 AttributeStringArrayPtr aStringsAttr =
449 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
450 int aNbComps = myCompNamesList.size();
451 aStringsAttr->setSize(aNbComps);
452 for ( int i = 0; i < aNbComps; i++)
453 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
455 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
456 // Store number of steps
457 int aNbSteps = myDataTblList.size();
459 // Store Type of the field values
460 int aFldType = myFieldTypeCombo->currentIndex();
462 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
463 aStampsAttr->setSize(aNbSteps);
465 QTableWidget* aTable = myDataTblList.first();
466 int aRows = aTable->rowCount();
467 // first column contains selected names which should not be stored
468 int aColumns = aTable->columnCount() - 1;
470 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
471 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
472 for (int i = 0; i < aNbSteps; i++) {
473 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
474 aTable = myDataTblList.at(i);
475 for (int j = 0; j < aColumns; j++) {
476 for (int k = 0; k < aRows; k++) {
477 QString aTblVal = aTable->item(k, j + 1)->text();
478 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
482 updateObject(myFeature);
486 //**********************************************************************************
487 bool CollectionPlugin_WidgetField::restoreValueCustom()
490 DataPtr aData = myFeature->data();
492 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
493 std::string aTypeStr = aSelList->selectionType();
495 return false; // The attribute is not initialized
496 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
498 // Get number of components
499 AttributeStringArrayPtr aStringsAttr =
500 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
502 myCompNamesList.clear();
503 for (int i = 0; i < aStringsAttr->size(); i++) {
504 myCompNamesList.append(aStringsAttr->value(i).c_str());
506 isBlocked = myNbComponentsSpn->blockSignals(true);
507 myNbComponentsSpn->setValue(myCompNamesList.size());
508 myNbComponentsSpn->blockSignals(isBlocked);
510 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
511 // Get number of steps
512 int aNbSteps = aTablesAttr->tables();
513 myStepSlider->setMaximum(aNbSteps);
514 //myStepSlider->setValue(1);
516 while (myDataTblList.count() > aNbSteps) {
517 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
518 myStepWgt->removeWidget(aWgt);
521 myStampSpnList.removeLast();
522 myDataTblList.removeLast();
524 while (myDataTblList.count() < aNbSteps)
525 appendStepControls();
526 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
529 // Get Type of the field values
530 isBlocked = myFieldTypeCombo->blockSignals(true);
531 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
532 myFieldTypeCombo->blockSignals(isBlocked);
534 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
536 int aRows = aTablesAttr->rows();
537 int aCols = aTablesAttr->columns();
539 // Get width of columns
541 QTableWidget* aFirstTable = myDataTblList.first();
542 for (int i = 0; i < aFirstTable->columnCount(); i++)
543 aColWidth.append(aFirstTable->columnWidth(i));
545 QTableWidgetItem* aItem = 0;
546 for (int i = 0; i < aNbSteps; i++) {
547 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
548 QTableWidget* aTable = myDataTblList.at(i);
549 isBlocked = aTable->blockSignals(true);
550 aTable->setRowCount(aRows);
551 for (int j = 0; j < aCols + 1; j++) {
552 for (int k = 0; k < aRows; k++) {
553 aItem = aTable->item(k, j);
554 if ((j == 0) && (k > 0)) {
555 // Add selection names
556 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
558 aItem->setText(aAttr->namingName().c_str());
560 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
561 aTable->setItem(k, j, aItem);
565 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
567 aItem->setText(getValueText(aVal));
569 aItem = createValueItem(aVal);
571 aItem->setBackgroundColor(Qt::lightGray);
572 aTable->setItem(k, j, aItem);
577 // Restore columns width
578 for (int i = 0; i < aTable->columnCount(); i++)
579 aTable->setColumnWidth(i, aColWidth.at(i));
581 aTable->blockSignals(isBlocked);
586 //**********************************************************************************
587 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
589 QString aType(theStr.c_str());
590 aType = aType.toLower();
591 if (aType == "vertex")
593 else if (aType == "edge")
595 else if (aType == "face")
597 else if (aType == "solid")
599 else if (aType == "object")
601 else if (aType == "part")
607 //**********************************************************************************
608 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
627 //**********************************************************************************
628 QIntList CollectionPlugin_WidgetField::shapeTypes() const
631 switch (myShapeTypeCombo->currentIndex()) {
633 aRes.append(ModuleBase_Tools::shapeType("vertex"));
636 aRes.append(ModuleBase_Tools::shapeType("edge"));
639 aRes.append(ModuleBase_Tools::shapeType("face"));
642 aRes.append(ModuleBase_Tools::shapeType("solid"));
645 aRes.append(ModuleBase_Tools::shapeType("object"));
648 // TODO: Selection mode for Parts
654 //**********************************************************************************
655 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
657 ModelAPI_AttributeTables::Value aVal;
658 switch (myFieldTypeCombo->currentIndex()) {
659 case ModelAPI_AttributeTables::BOOLEAN:
660 aVal.myBool = (theStrVal == MYTrue)? true : false;
662 case ModelAPI_AttributeTables::DOUBLE:
663 aVal.myDouble = theStrVal.toDouble();
665 case ModelAPI_AttributeTables::INTEGER:
666 aVal.myInt = theStrVal.toInt();
668 case ModelAPI_AttributeTables::STRING:
669 aVal.myStr = theStrVal.toStdString();
675 //**********************************************************************************
676 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
678 int aOldCol = myCompNamesList.count();
679 int aNbRows = myDataTblList.first()->rowCount();
680 int aDif = theVal - aOldCol;
681 QTableWidgetItem* aItem = 0;
683 while (myCompNamesList.count() != theVal) {
685 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
687 myCompNamesList.removeLast();
690 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
691 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
693 foreach(QTableWidget* aDataTbl, myDataTblList) {
694 aDataTbl->setColumnCount(theVal + 1);
695 updateHeaders(aDataTbl);
696 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
697 for (int j = 0; j < aNbRows; j++) {
698 aItem = aDataTbl->item(j, i + 1);
700 aItem = createDefaultItem();
702 aItem->setBackgroundColor(Qt::lightGray);
703 aDataTbl->setItem(j, i + 1, aItem);
708 emit valuesChanged();
711 //**********************************************************************************
712 void CollectionPlugin_WidgetField::onAddStep()
714 int aMax = myStepSlider->maximum();
716 myStepSlider->setMaximum(aMax);
717 myMaxLbl->setText(QString::number(aMax));
718 appendStepControls();
719 myStepSlider->setValue(aMax);
721 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
722 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
725 AttributeSelectionListPtr aSelList =
726 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
727 if (!aSelList->isInitialized())
729 int aSelNb = aSelList->size();
733 int aColumns = myNbComponentsSpn->value() + 1;
734 int aRows = aSelNb + 1;
735 QTableWidget* aTable = myDataTblList.last();
736 aTable->setRowCount(aRows);
737 QTableWidgetItem* aItem = 0;
738 for(int i = 0; i < aColumns; i++) {
740 for(int j = 1; j < aRows; j++) {
741 aItem = aTable->item(j, i);
743 aItem = new QTableWidgetItem();
744 aTable->setItem(j, i, aItem);
746 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
747 aItem->setText(aAttr->namingName().c_str());
748 aItem->setToolTip(aAttr->namingName().c_str());
751 QString aDefVal = aTable->item(0, i)->text();
752 for(int j = 1; j < aRows; j++) {
753 aItem = aTable->item(j, i);
755 aItem = new QTableWidgetItem();
756 aTable->setItem(j, i, aItem);
758 aItem->setText(aDefVal);
764 //**********************************************************************************
765 void CollectionPlugin_WidgetField::onRemoveStep()
767 int aMax = myStepSlider->maximum();
769 myMaxLbl->setText(QString::number(aMax));
770 removeStepControls();
771 myStepSlider->setMaximum(aMax);
773 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
774 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
777 //**********************************************************************************
778 void CollectionPlugin_WidgetField::clearData()
780 foreach(QTableWidget* aDataTbl, myDataTblList) {
781 aDataTbl->setRowCount(1);
785 //**********************************************************************************
786 void CollectionPlugin_WidgetField::onStepMove(int theStep)
788 myCurStepLbl->setText(QString::number(theStep));
789 myStepWgt->setCurrentIndex(theStep - 1);
792 //**********************************************************************************
793 bool CollectionPlugin_WidgetField::
794 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
796 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
799 //**********************************************************************************
800 void CollectionPlugin_WidgetField::onSelectionChanged()
802 //if (isEditingMode())
806 myActivation = false;
809 // Ignore selection for Parts mode
810 if (myShapeTypeCombo->currentIndex() == 5)
813 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
815 AttributeSelectionListPtr aSelList =
816 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
817 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
823 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
824 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
825 aShape = aPrs->shape();
826 if (!aResult.get() && !aShape.get())
828 if (!aSelList->isInList(aResult, aShape)) {
829 aSelList->append(aResult, aShape);
833 int aColumns = myDataTblList.first()->columnCount();
834 int aRows = myDataTblList.first()->rowCount();
835 int aNewRows = aNbData + 1;
836 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
837 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
839 QTableWidgetItem* aItem = 0;
840 foreach(QTableWidget* aTable, myDataTblList) {
841 aTable->setRowCount(aNewRows);
842 if (aNewRows > aRows) {
844 for(int i = 0; i < aColumns; i++) {
846 for(int j = 1; j < aNewRows; j++) {
847 aItem = aTable->item(j, i);
849 aItem = new QTableWidgetItem();
850 aTable->setItem(j, i, aItem);
852 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
853 aItem->setText(aAttr->namingName().c_str());
854 aItem->setToolTip(aAttr->namingName().c_str());
857 QString aDefVal = aTable->item(0, i)->text();
858 for(int j = aRows; j < aNewRows; j++) {
859 aItem = aTable->item(j, i);
861 aItem = new QTableWidgetItem();
862 aTable->setItem(j, i, aItem);
864 aItem->setText(aDefVal);
869 // Update only selection name
870 for(int j = 1; j < aNewRows - 1; j++) {
871 AttributeSelectionPtr aAttr = aSelList->value(j);
872 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
873 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
877 emit valuesChanged();
880 //**********************************************************************************
881 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
883 DataTableItemDelegate* aDelegate = 0;
884 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
886 ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
887 if (aOldType != theIdx) {
888 aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
889 int aColumns = myDataTblList.first()->columnCount();
890 int aRows = myDataTblList.first()->rowCount();
891 foreach(QTableWidget* aTable, myDataTblList) {
892 for(int i = 1; i < aColumns; i++) {
893 for(int j = 0; j < aRows; j++) {
895 case ModelAPI_AttributeTables::DOUBLE:
896 case ModelAPI_AttributeTables::INTEGER:
897 if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
898 (aOldType == ModelAPI_AttributeTables::STRING)) {
899 aTable->item(j, i)->setText("0");
902 case ModelAPI_AttributeTables::BOOLEAN:
903 aTable->item(j, i)->setText(MYFalse);
905 case ModelAPI_AttributeTables::STRING:
906 aTable->item(j, i)->setText("");
912 emit valuesChanged();
917 //**********************************************************************************
918 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
920 // Do not store here column of names
923 if (!myFeature.get())
925 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
926 int aNb = myDataTblList.indexOf(aTable);
929 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
931 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
932 if (aTablesAttr->isInitialized())
933 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
935 emit valuesChanged();
938 //**********************************************************************************
939 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
941 activateSelectionAndFilters(theType == 5? false:true);
943 AttributeSelectionListPtr aSelList =
944 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
946 std::string aTypeName = getSelectionType(theType);
947 if (aTypeName == aSelList->selectionType())
949 aSelList->setSelectionType(aTypeName);
951 //Clear old selection
954 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
955 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
956 emit valuesChanged();
959 //**********************************************************************************
960 bool CollectionPlugin_WidgetField::processEnter()
969 //**********************************************************************************
970 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
972 if (theNew && (!myIsTabEdit))
973 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
976 //**********************************************************************************
977 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
979 myMaxLbl->setText(QString::number(theMax));
980 myRemoveBtn->setEnabled(theMax > 1);
983 //**********************************************************************************
984 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
986 if (myDataTblList.count() < 2)
988 QObject* aSender = sender();
989 foreach(QTableWidget* aTable, myDataTblList) {
990 if (aTable->horizontalHeader() != aSender)
991 aTable->setColumnWidth(theIndex, theNew);