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);
92 if (myType == ModelAPI_AttributeTables::BOOLEAN)
93 connect(aEditor, SIGNAL(currentTextChanged(const QString&)),
94 SLOT(onEditItem(const QString&)));
96 connect(aEditor, SIGNAL(textEdited(const QString&)),
97 SLOT(onEditItem(const QString&)));
101 void DataTableItemDelegate::onEditItem(const QString& theText)
103 QWidget* aWgt = dynamic_cast<QWidget*>(sender());
109 //**********************************************************************************
110 //**********************************************************************************
111 //**********************************************************************************
112 CollectionPlugin_WidgetField::
113 CollectionPlugin_WidgetField(QWidget* theParent,
114 ModuleBase_IWorkshop* theWorkshop,
115 const Config_WidgetAPI* theData):
116 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
117 myIsTabEdit(false), myActivation(false)
119 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
121 // Types definition controls
122 QWidget* aTypesWgt = new QWidget(this);
123 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
124 aTypesLayout->setContentsMargins(0, 0, 0, 0);
125 aMainLayout->addWidget(aTypesWgt);
128 myShapeTypeCombo = new QComboBox(aTypesWgt);
129 QStringList aShapeTypes;
130 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
131 << tr("Solids") << tr("Objects") << tr("Parts");
132 myShapeTypeCombo->addItems(aShapeTypes);
133 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
136 myFieldTypeCombo = new QComboBox(aTypesWgt);
137 QStringList aFieldTypes;
138 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
140 myFieldTypeCombo->addItems(aFieldTypes);
141 myFieldTypeCombo->setCurrentIndex(2);
142 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
144 // Number of components
145 myNbComponentsSpn = new QSpinBox(aTypesWgt);
146 myNbComponentsSpn->setMinimum(1);
147 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
150 QFrame* aStepFrame = new QFrame(this);
151 aStepFrame->setFrameShape(QFrame::Box);
152 aStepFrame->setFrameStyle(QFrame::StyledPanel);
153 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
154 aMainLayout->addWidget(aStepFrame);
156 // Current step label
157 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
158 myCurStepLbl = new QLabel("1", aStepFrame);
159 QFont aFont = myCurStepLbl->font();
161 myCurStepLbl->setFont(aFont);
162 aStepLayout->addWidget(myCurStepLbl, 0, 1);
165 QWidget* aSliderWidget = new QWidget(aStepFrame);
166 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
167 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
168 aSliderLayout->setContentsMargins(0, 0, 0, 0);
170 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
172 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
173 myStepSlider->setTickPosition(QSlider::TicksBelow);
174 myStepSlider->setRange(1, 1);
175 myStepSlider->setPageStep(myStepSlider->singleStep());
176 aSliderLayout->addWidget(myStepSlider, 1);
178 myMaxLbl = new QLabel("1", aSliderWidget);
179 aSliderLayout->addWidget(myMaxLbl);
182 myCompNamesList << "Comp 1";
183 myStepWgt = new QStackedWidget(aStepFrame);
184 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
187 new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
188 myFieldTypeCombo->currentIndex());
190 appendStepControls();
193 QWidget* aBtnWgt = new QWidget(this);
194 aMainLayout->addWidget(aBtnWgt);
195 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
196 aBtnLayout->setContentsMargins(0, 0, 0, 0);
198 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
199 aBtnLayout->addWidget(aAddBtn);
201 aBtnLayout->addStretch(1);
203 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
204 aBtnLayout->addWidget(myRemoveBtn);
205 myRemoveBtn->setEnabled(false);
207 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
208 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
209 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
210 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
211 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
212 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
213 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
214 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
217 //**********************************************************************************
218 void CollectionPlugin_WidgetField::appendStepControls()
220 QWidget* aWidget = new QWidget(myStepWgt);
221 QGridLayout* aStepLayout = new QGridLayout(aWidget);
222 aStepLayout->setContentsMargins(0, 0, 0, 0);
224 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
226 QSpinBox* aStampSpn = new QSpinBox(aWidget);
227 aStepLayout->addWidget(aStampSpn, 0, 1);
229 myStampSpnList.append(aStampSpn);
232 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
233 aDataTbl->installEventFilter(this);
234 aDataTbl->setItemDelegate(myDelegate);
237 if (!myDataTblList.isEmpty()) {
238 QTableWidget* aFirstTable = myDataTblList.first();
239 for (int i = 0; i < aFirstTable->columnCount(); i++)
240 aColWidth.append(aFirstTable->columnWidth(i));
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 if (i < aColWidth.size())
268 aDataTbl->setColumnWidth(i, aColWidth.at(i));
271 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
272 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
274 myStepWgt->addWidget(aWidget);
275 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
278 //**********************************************************************************
279 void CollectionPlugin_WidgetField::deactivate()
281 ModuleBase_WidgetSelector::deactivate();
285 //**********************************************************************************
286 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
288 // myShapeTypeCombo->setEnabled(!isEditingMode());
289 // myFieldTypeCombo->setEnabled(!isEditingMode());
290 // myNbComponentsSpn->setEnabled(!isEditingMode());
293 //**********************************************************************************
294 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
296 QObject* aObject = 0;
297 foreach(QTableWidget* aTable, myDataTblList) {
298 if (aTable->horizontalHeader()->viewport() == theObject) {
304 if (theEvent->type() == QEvent::MouseButtonDblClick) {
305 if (myHeaderEditor) { //delete previous editor
306 myHeaderEditor->deleteLater();
309 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
310 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
311 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
313 int aShift = aTable->horizontalScrollBar()->value();
314 int aPos = aMouseEvent->x();
315 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
318 aRect.setLeft(aHeader->sectionPosition(aIndex));
319 aRect.setWidth(aHeader->sectionSize(aIndex));
321 aRect.setHeight(aHeader->height());
322 aRect.adjust(1, 1, -1, -1);
323 aRect.translate(-aShift, 0);
325 myHeaderEditor = new QLineEdit(aHeader->viewport());
326 myHeaderEditor->move(aRect.topLeft());
327 myHeaderEditor->resize(aRect.size());
328 myHeaderEditor->setFrame(false);
329 QString aText = aHeader->model()->
330 headerData(aIndex, aHeader->orientation()).toString();
331 myHeaderEditor->setText(aText);
332 myHeaderEditor->setFocus();
333 myEditIndex = aIndex; //save for future use
334 myHeaderEditor->installEventFilter(this); //catch focus out event
335 //if user presses Enter it should close editor
336 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
337 myHeaderEditor->show();
341 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
342 QString aNewTitle = myHeaderEditor->text();
344 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
345 myHeaderEditor->deleteLater(); //safely delete editor
347 // Store into data model
348 AttributeStringArrayPtr aStringsAttr =
349 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
350 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
351 foreach(QTableWidget* aTable, myDataTblList) {
352 updateHeaders(aTable);
354 } else if (theEvent->type() == QEvent::FocusIn) {
355 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
357 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
358 if (aPanel->activeWidget() != this) {
360 aPanel->activateWidget(this, false);
364 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
367 //**********************************************************************************
368 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
370 QTableWidgetItem* aItem = new QTableWidgetItem();
371 switch (myFieldTypeCombo->currentIndex()) {
372 case ModelAPI_AttributeTables::DOUBLE:
373 case ModelAPI_AttributeTables::INTEGER:
376 case ModelAPI_AttributeTables::BOOLEAN:
377 aItem->setText(MYFalse);
379 case ModelAPI_AttributeTables::STRING:
386 //**********************************************************************************
387 QTableWidgetItem* CollectionPlugin_WidgetField::
388 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
390 QTableWidgetItem* aItem = new QTableWidgetItem();
391 aItem->setText(getValueText(theVal));
395 //**********************************************************************************
396 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
398 switch (myFieldTypeCombo->currentIndex()) {
399 case ModelAPI_AttributeTables::DOUBLE:
400 return QString::number(theVal.myDouble);
401 case ModelAPI_AttributeTables::INTEGER:
402 return QString::number(theVal.myInt);
403 case ModelAPI_AttributeTables::BOOLEAN:
404 return theVal.myBool? MYTrue : MYFalse;
405 case ModelAPI_AttributeTables::STRING:
406 return theVal.myStr.c_str();
412 //**********************************************************************************
413 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
415 QStringList aHeaders;
416 aHeaders << tr(MYFirstCol);
417 aHeaders << myCompNamesList;
418 theDataTbl->setHorizontalHeaderLabels(aHeaders);
421 //**********************************************************************************
422 void CollectionPlugin_WidgetField::removeStepControls()
424 int aCurWgtId = myStepWgt->currentIndex();
425 QWidget* aWgt = myStepWgt->currentWidget();
426 myStepWgt->removeWidget(aWgt);
428 myStampSpnList.removeAt(aCurWgtId);
429 myDataTblList.removeAt(aCurWgtId);
433 //**********************************************************************************
434 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
436 QList<QWidget*> aControls;
437 // this control will accept focus and will be highlighted in the Property Panel
438 aControls.append(myShapeTypeCombo);
439 aControls.append(myFieldTypeCombo);
440 aControls.append(myNbComponentsSpn);
441 if (myStampSpnList.size() > 0)
442 aControls.append(myStampSpnList.first());
443 if (myDataTblList.size() > 0)
444 aControls.append(myDataTblList.first());
449 //**********************************************************************************
450 bool CollectionPlugin_WidgetField::storeValueCustom()
452 DataPtr aData = myFeature->data();
453 // Store number of components
454 AttributeStringArrayPtr aStringsAttr =
455 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
456 int aNbComps = myCompNamesList.size();
457 aStringsAttr->setSize(aNbComps);
458 for ( int i = 0; i < aNbComps; i++)
459 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
461 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
462 // Store number of steps
463 int aNbSteps = myDataTblList.size();
465 // Store Type of the field values
466 int aFldType = myFieldTypeCombo->currentIndex();
468 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
469 aStampsAttr->setSize(aNbSteps);
471 QTableWidget* aTable = myDataTblList.first();
472 int aRows = aTable->rowCount();
473 // first column contains selected names which should not be stored
474 int aColumns = aTable->columnCount() - 1;
476 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
477 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
478 for (int i = 0; i < aNbSteps; i++) {
479 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
480 aTable = myDataTblList.at(i);
481 for (int j = 0; j < aColumns; j++) {
482 for (int k = 0; k < aRows; k++) {
483 QString aTblVal = aTable->item(k, j + 1)->text();
484 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
488 updateObject(myFeature);
492 //**********************************************************************************
493 bool CollectionPlugin_WidgetField::restoreValueCustom()
496 DataPtr aData = myFeature->data();
498 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
499 std::string aTypeStr = aSelList->selectionType();
501 return false; // The attribute is not initialized
502 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
504 // Get number of components
505 AttributeStringArrayPtr aStringsAttr =
506 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
508 myCompNamesList.clear();
509 for (int i = 0; i < aStringsAttr->size(); i++) {
510 myCompNamesList.append(aStringsAttr->value(i).c_str());
512 isBlocked = myNbComponentsSpn->blockSignals(true);
513 myNbComponentsSpn->setValue(myCompNamesList.size());
514 myNbComponentsSpn->blockSignals(isBlocked);
516 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
517 // Get number of steps
518 int aNbSteps = aTablesAttr->tables();
519 myStepSlider->setMaximum(aNbSteps);
520 //myStepSlider->setValue(1);
522 while (myDataTblList.count() > aNbSteps) {
523 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
524 myStepWgt->removeWidget(aWgt);
527 myStampSpnList.removeLast();
528 myDataTblList.removeLast();
530 while (myDataTblList.count() < aNbSteps)
531 appendStepControls();
532 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
535 // Get Type of the field values
536 isBlocked = myFieldTypeCombo->blockSignals(true);
537 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
538 myFieldTypeCombo->blockSignals(isBlocked);
539 myDelegate->setDataType(aTablesAttr->type());
541 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
543 int aRows = aTablesAttr->rows();
544 int aCols = aTablesAttr->columns();
546 // Get width of columns
548 QTableWidget* aFirstTable = myDataTblList.first();
549 for (int i = 0; i < aFirstTable->columnCount(); i++)
550 aColWidth.append(aFirstTable->columnWidth(i));
552 QTableWidgetItem* aItem = 0;
553 for (int i = 0; i < aNbSteps; i++) {
554 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
555 QTableWidget* aTable = myDataTblList.at(i);
556 isBlocked = aTable->blockSignals(true);
557 aTable->setRowCount(aRows);
558 aTable->setColumnCount(aCols + 1);
559 updateHeaders(aTable);
560 for (int j = 0; j < aCols + 1; j++) {
561 for (int k = 0; k < aRows; k++) {
562 aItem = aTable->item(k, j);
563 if ((j == 0) && (k > 0)) {
564 // Add selection names
565 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
567 aItem->setText(aAttr->namingName().c_str());
569 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
570 aTable->setItem(k, j, aItem);
574 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
576 aItem->setText(getValueText(aVal));
578 aItem = createValueItem(aVal);
580 aItem->setBackgroundColor(Qt::lightGray);
581 aTable->setItem(k, j, aItem);
586 // Restore columns width
587 for (int i = 0; i < aTable->columnCount(); i++) {
588 if (i < aColWidth.size())
589 aTable->setColumnWidth(i, aColWidth.at(i));
592 aTable->blockSignals(isBlocked);
597 //**********************************************************************************
598 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
600 QString aType(theStr.c_str());
601 aType = aType.toLower();
602 if (aType == "vertex")
604 else if (aType == "edge")
606 else if (aType == "face")
608 else if (aType == "solid")
610 else if (aType == "object")
612 else if (aType == "part")
618 //**********************************************************************************
619 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
638 //**********************************************************************************
639 QIntList CollectionPlugin_WidgetField::shapeTypes() const
642 switch (myShapeTypeCombo->currentIndex()) {
644 aRes.append(ModuleBase_Tools::shapeType("vertex"));
647 aRes.append(ModuleBase_Tools::shapeType("edge"));
650 aRes.append(ModuleBase_Tools::shapeType("face"));
653 aRes.append(ModuleBase_Tools::shapeType("solid"));
656 aRes.append(ModuleBase_Tools::shapeType("object"));
659 // TODO: Selection mode for Parts
665 //**********************************************************************************
666 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
668 ModelAPI_AttributeTables::Value aVal;
669 switch (myFieldTypeCombo->currentIndex()) {
670 case ModelAPI_AttributeTables::BOOLEAN:
671 aVal.myBool = (theStrVal == MYTrue)? true : false;
673 case ModelAPI_AttributeTables::DOUBLE:
674 aVal.myDouble = theStrVal.toDouble();
676 case ModelAPI_AttributeTables::INTEGER:
677 aVal.myInt = theStrVal.toInt();
679 case ModelAPI_AttributeTables::STRING:
680 aVal.myStr = theStrVal.toStdString();
686 //**********************************************************************************
687 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
689 int aOldCol = myCompNamesList.count();
690 int aNbRows = myDataTblList.first()->rowCount();
691 int aDif = theVal - aOldCol;
692 QTableWidgetItem* aItem = 0;
694 while (myCompNamesList.count() != theVal) {
696 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
698 myCompNamesList.removeLast();
701 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
702 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
704 foreach(QTableWidget* aDataTbl, myDataTblList) {
705 aDataTbl->setColumnCount(theVal + 1);
706 updateHeaders(aDataTbl);
707 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
708 for (int j = 0; j < aNbRows; j++) {
709 aItem = aDataTbl->item(j, i + 1);
711 aItem = createDefaultItem();
713 aItem->setBackgroundColor(Qt::lightGray);
714 aDataTbl->setItem(j, i + 1, aItem);
719 emit valuesChanged();
722 //**********************************************************************************
723 void CollectionPlugin_WidgetField::onAddStep()
725 int aMax = myStepSlider->maximum();
727 myStepSlider->setMaximum(aMax);
728 myMaxLbl->setText(QString::number(aMax));
729 appendStepControls();
730 myStepSlider->setValue(aMax);
732 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
733 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
736 AttributeSelectionListPtr aSelList =
737 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
738 if (!aSelList->isInitialized())
740 int aSelNb = aSelList->size();
744 int aColumns = myNbComponentsSpn->value() + 1;
745 int aRows = aSelNb + 1;
746 QTableWidget* aTable = myDataTblList.last();
747 aTable->setRowCount(aRows);
748 QTableWidgetItem* aItem = 0;
749 for(int i = 0; i < aColumns; i++) {
751 for(int j = 1; j < aRows; j++) {
752 aItem = aTable->item(j, i);
754 aItem = new QTableWidgetItem();
755 aTable->setItem(j, i, aItem);
757 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
758 aItem->setText(aAttr->namingName().c_str());
759 aItem->setToolTip(aAttr->namingName().c_str());
762 QString aDefVal = aTable->item(0, i)->text();
763 for(int j = 1; j < aRows; j++) {
764 aItem = aTable->item(j, i);
766 aItem = new QTableWidgetItem();
767 aTable->setItem(j, i, aItem);
769 aItem->setText(aDefVal);
773 emit valuesChanged();
776 //**********************************************************************************
777 void CollectionPlugin_WidgetField::onRemoveStep()
779 int aMax = myStepSlider->maximum();
781 myMaxLbl->setText(QString::number(aMax));
782 removeStepControls();
783 myStepSlider->setMaximum(aMax);
785 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
786 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
787 emit valuesChanged();
790 //**********************************************************************************
791 void CollectionPlugin_WidgetField::clearData()
793 foreach(QTableWidget* aDataTbl, myDataTblList) {
794 aDataTbl->setRowCount(1);
798 //**********************************************************************************
799 void CollectionPlugin_WidgetField::onStepMove(int theStep)
801 myCurStepLbl->setText(QString::number(theStep));
802 myStepWgt->setCurrentIndex(theStep - 1);
805 //**********************************************************************************
806 bool CollectionPlugin_WidgetField::
807 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
809 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
812 //**********************************************************************************
813 bool CollectionPlugin_WidgetField::
814 setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
817 myActivation = false;
820 // Ignore selection for Parts mode
821 if (myShapeTypeCombo->currentIndex() == 5)
824 QList<ModuleBase_ViewerPrsPtr> aSelected;
825 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
826 for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
827 ModuleBase_ViewerPrsPtr aValue = *anIt;
828 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
829 if (theToValidate && aRes.get() && acceptSubShape(aValue->shape(), aRes))
830 aSelected.append(aValue);
832 AttributeSelectionListPtr aSelList =
833 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
834 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
840 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
841 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
842 aShape = aPrs->shape();
843 if (!aResult.get() && !aShape.get())
845 if (!aSelList->isInList(aResult, aShape)) {
846 aSelList->append(aResult, aShape);
850 int aColumns = myDataTblList.first()->columnCount();
851 int aRows = myDataTblList.first()->rowCount();
852 int aNewRows = aNbData + 1;
853 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
854 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
856 QTableWidgetItem* aItem = 0;
857 foreach(QTableWidget* aTable, myDataTblList) {
858 aTable->setRowCount(aNewRows);
859 if (aNewRows > aRows) {
861 for(int i = 0; i < aColumns; i++) {
863 for(int j = 1; j < aNewRows; j++) {
864 aItem = aTable->item(j, i);
866 aItem = new QTableWidgetItem();
867 aTable->setItem(j, i, aItem);
869 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
870 aItem->setText(aAttr->namingName().c_str());
871 aItem->setToolTip(aAttr->namingName().c_str());
874 QString aDefVal = aTable->item(0, i)->text();
875 for(int j = aRows; j < aNewRows; j++) {
876 aItem = aTable->item(j, i);
878 aItem = new QTableWidgetItem();
879 aTable->setItem(j, i, aItem);
881 aItem->setText(aDefVal);
886 // Update only selection name
887 for(int j = 1; j < aNewRows - 1; j++) {
888 AttributeSelectionPtr aAttr = aSelList->value(j);
889 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
890 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
897 //**********************************************************************************
898 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
900 ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
901 if (aOldType != theIdx) {
902 myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
903 int aColumns = myDataTblList.first()->columnCount();
904 int aRows = myDataTblList.first()->rowCount();
905 foreach(QTableWidget* aTable, myDataTblList) {
906 for(int i = 1; i < aColumns; i++) {
907 for(int j = 0; j < aRows; j++) {
909 case ModelAPI_AttributeTables::DOUBLE:
910 case ModelAPI_AttributeTables::INTEGER:
911 aTable->item(j, i)->setText("0");
913 case ModelAPI_AttributeTables::BOOLEAN:
914 aTable->item(j, i)->setText(MYFalse);
916 case ModelAPI_AttributeTables::STRING:
917 aTable->item(j, i)->setText("");
923 emit valuesChanged();
927 //**********************************************************************************
928 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
930 // Do not store here column of names
933 if (!myFeature.get())
935 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
936 int aNb = myDataTblList.indexOf(aTable);
939 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
941 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
942 if (aTablesAttr->isInitialized())
943 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
945 emit valuesChanged();
948 //**********************************************************************************
949 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
951 activateSelectionAndFilters(theType == 5? false:true);
953 AttributeSelectionListPtr aSelList =
954 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
956 std::string aTypeName = getSelectionType(theType);
957 if (aTypeName == aSelList->selectionType())
959 aSelList->setSelectionType(aTypeName);
961 //Clear old selection
964 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
965 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
966 emit valuesChanged();
969 //**********************************************************************************
970 bool CollectionPlugin_WidgetField::processEnter()
976 QWidget* aCurrWgt = qApp->focusWidget();
977 int aCurWgtId = myStepWgt->currentIndex();
978 if ((aCurrWgt == myShapeTypeCombo) ||
979 (aCurrWgt == myFieldTypeCombo) ||
980 (aCurrWgt == myNbComponentsSpn) ||
981 (aCurrWgt == myStampSpnList[aCurWgtId]) ||
982 (aCurrWgt == myDataTblList[aCurWgtId])) {
989 //**********************************************************************************
990 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
992 if (theNew && (!myIsTabEdit))
993 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
996 //**********************************************************************************
997 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
999 myMaxLbl->setText(QString::number(theMax));
1000 myRemoveBtn->setEnabled(theMax > 1);
1003 //**********************************************************************************
1004 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
1006 if (myDataTblList.count() < 2)
1008 QObject* aSender = sender();
1009 foreach(QTableWidget* aTable, myDataTblList) {
1010 if (aTable->horizontalHeader() != aSender)
1011 aTable->setColumnWidth(theIndex, theNew);
1015 //**********************************************************************************
1016 QList<std::shared_ptr<ModuleBase_ViewerPrs>>
1017 CollectionPlugin_WidgetField::getAttributeSelection() const
1019 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
1021 DataPtr aData = myFeature->data();
1022 AttributeSelectionListPtr aSelList =
1023 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
1024 AttributeSelectionPtr aAttr;
1026 for (int i = 0; i < aSelList->size(); i++) {
1027 aAttr = aSelList->value(i);
1028 ModuleBase_ViewerPrsPtr
1029 aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));