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 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 QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
275 myStepWgt->addWidget(aWidget);
276 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
279 //**********************************************************************************
280 void CollectionPlugin_WidgetField::deactivate()
282 ModuleBase_WidgetSelector::deactivate();
286 //**********************************************************************************
287 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
289 // myShapeTypeCombo->setEnabled(!isEditingMode());
290 // myFieldTypeCombo->setEnabled(!isEditingMode());
291 // myNbComponentsSpn->setEnabled(!isEditingMode());
294 //**********************************************************************************
295 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
297 QObject* aObject = 0;
298 foreach(QTableWidget* aTable, myDataTblList) {
299 if (aTable->horizontalHeader()->viewport() == theObject) {
305 if (theEvent->type() == QEvent::MouseButtonDblClick) {
306 if (myHeaderEditor) { //delete previous editor
307 myHeaderEditor->deleteLater();
310 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
311 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
312 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
314 int aShift = aTable->horizontalScrollBar()->value();
315 int aPos = aMouseEvent->x();
316 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
319 aRect.setLeft(aHeader->sectionPosition(aIndex));
320 aRect.setWidth(aHeader->sectionSize(aIndex));
322 aRect.setHeight(aHeader->height());
323 aRect.adjust(1, 1, -1, -1);
324 aRect.translate(-aShift, 0);
326 myHeaderEditor = new QLineEdit(aHeader->viewport());
327 myHeaderEditor->move(aRect.topLeft());
328 myHeaderEditor->resize(aRect.size());
329 myHeaderEditor->setFrame(false);
330 QString aText = aHeader->model()->
331 headerData(aIndex, aHeader->orientation()).toString();
332 myHeaderEditor->setText(aText);
333 myHeaderEditor->setFocus();
334 myEditIndex = aIndex; //save for future use
335 myHeaderEditor->installEventFilter(this); //catch focus out event
336 //if user presses Enter it should close editor
337 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
338 myHeaderEditor->show();
342 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
343 QString aNewTitle = myHeaderEditor->text();
345 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
346 myHeaderEditor->deleteLater(); //safely delete editor
348 // Store into data model
349 AttributeStringArrayPtr aStringsAttr =
350 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
351 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
352 foreach(QTableWidget* aTable, myDataTblList) {
353 updateHeaders(aTable);
355 } else if (theEvent->type() == QEvent::FocusIn) {
356 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
358 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
359 if (aPanel->activeWidget() != this) {
361 aPanel->activateWidget(this, false);
365 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
368 //**********************************************************************************
369 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
371 QTableWidgetItem* aItem = new QTableWidgetItem();
372 switch (myFieldTypeCombo->currentIndex()) {
373 case ModelAPI_AttributeTables::DOUBLE:
374 case ModelAPI_AttributeTables::INTEGER:
377 case ModelAPI_AttributeTables::BOOLEAN:
378 aItem->setText(MYFalse);
380 case ModelAPI_AttributeTables::STRING:
387 //**********************************************************************************
388 QTableWidgetItem* CollectionPlugin_WidgetField::
389 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
391 QTableWidgetItem* aItem = new QTableWidgetItem();
392 aItem->setText(getValueText(theVal));
396 //**********************************************************************************
397 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
399 switch (myFieldTypeCombo->currentIndex()) {
400 case ModelAPI_AttributeTables::DOUBLE:
401 return QString::number(theVal.myDouble);
402 case ModelAPI_AttributeTables::INTEGER:
403 return QString::number(theVal.myInt);
404 case ModelAPI_AttributeTables::BOOLEAN:
405 return theVal.myBool? MYTrue : MYFalse;
406 case ModelAPI_AttributeTables::STRING:
407 return theVal.myStr.c_str();
413 //**********************************************************************************
414 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
416 QStringList aHeaders;
417 aHeaders << tr(MYFirstCol);
418 aHeaders << myCompNamesList;
419 theDataTbl->setHorizontalHeaderLabels(aHeaders);
422 //**********************************************************************************
423 void CollectionPlugin_WidgetField::removeStepControls()
425 int aCurWgtId = myStepWgt->currentIndex();
426 QWidget* aWgt = myStepWgt->currentWidget();
427 myStepWgt->removeWidget(aWgt);
429 myStampSpnList.removeAt(aCurWgtId);
430 myDataTblList.removeAt(aCurWgtId);
434 //**********************************************************************************
435 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
437 QList<QWidget*> aControls;
438 // this control will accept focus and will be highlighted in the Property Panel
439 aControls.push_back(myShapeTypeCombo);
440 aControls.push_back(myFieldTypeCombo);
441 aControls.push_back(myNbComponentsSpn);
445 //**********************************************************************************
446 bool CollectionPlugin_WidgetField::storeValueCustom()
448 DataPtr aData = myFeature->data();
449 // Store number of components
450 AttributeStringArrayPtr aStringsAttr =
451 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
452 int aNbComps = myCompNamesList.size();
453 aStringsAttr->setSize(aNbComps);
454 for ( int i = 0; i < aNbComps; i++)
455 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
457 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
458 // Store number of steps
459 int aNbSteps = myDataTblList.size();
461 // Store Type of the field values
462 int aFldType = myFieldTypeCombo->currentIndex();
464 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
465 aStampsAttr->setSize(aNbSteps);
467 QTableWidget* aTable = myDataTblList.first();
468 int aRows = aTable->rowCount();
469 // first column contains selected names which should not be stored
470 int aColumns = aTable->columnCount() - 1;
472 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
473 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
474 for (int i = 0; i < aNbSteps; i++) {
475 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
476 aTable = myDataTblList.at(i);
477 for (int j = 0; j < aColumns; j++) {
478 for (int k = 0; k < aRows; k++) {
479 QString aTblVal = aTable->item(k, j + 1)->text();
480 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
484 updateObject(myFeature);
488 //**********************************************************************************
489 bool CollectionPlugin_WidgetField::restoreValueCustom()
492 DataPtr aData = myFeature->data();
494 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
495 std::string aTypeStr = aSelList->selectionType();
497 return false; // The attribute is not initialized
498 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
500 // Get number of components
501 AttributeStringArrayPtr aStringsAttr =
502 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
504 myCompNamesList.clear();
505 for (int i = 0; i < aStringsAttr->size(); i++) {
506 myCompNamesList.append(aStringsAttr->value(i).c_str());
508 isBlocked = myNbComponentsSpn->blockSignals(true);
509 myNbComponentsSpn->setValue(myCompNamesList.size());
510 myNbComponentsSpn->blockSignals(isBlocked);
512 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
513 // Get number of steps
514 int aNbSteps = aTablesAttr->tables();
515 myStepSlider->setMaximum(aNbSteps);
516 //myStepSlider->setValue(1);
518 while (myDataTblList.count() > aNbSteps) {
519 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
520 myStepWgt->removeWidget(aWgt);
523 myStampSpnList.removeLast();
524 myDataTblList.removeLast();
526 while (myDataTblList.count() < aNbSteps)
527 appendStepControls();
528 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
531 // Get Type of the field values
532 isBlocked = myFieldTypeCombo->blockSignals(true);
533 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
534 myFieldTypeCombo->blockSignals(isBlocked);
536 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
538 int aRows = aTablesAttr->rows();
539 int aCols = aTablesAttr->columns();
541 // Get width of columns
543 QTableWidget* aFirstTable = myDataTblList.first();
544 for (int i = 0; i < aFirstTable->columnCount(); i++)
545 aColWidth.append(aFirstTable->columnWidth(i));
547 QTableWidgetItem* aItem = 0;
548 for (int i = 0; i < aNbSteps; i++) {
549 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
550 QTableWidget* aTable = myDataTblList.at(i);
551 isBlocked = aTable->blockSignals(true);
552 aTable->setRowCount(aRows);
553 aTable->setColumnCount(aCols + 1);
554 updateHeaders(aTable);
555 for (int j = 0; j < aCols + 1; j++) {
556 for (int k = 0; k < aRows; k++) {
557 aItem = aTable->item(k, j);
558 if ((j == 0) && (k > 0)) {
559 // Add selection names
560 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
562 aItem->setText(aAttr->namingName().c_str());
564 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
565 aTable->setItem(k, j, aItem);
569 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
571 aItem->setText(getValueText(aVal));
573 aItem = createValueItem(aVal);
575 aItem->setBackgroundColor(Qt::lightGray);
576 aTable->setItem(k, j, aItem);
581 // Restore columns width
582 for (int i = 0; i < aTable->columnCount(); i++) {
583 if (i < aColWidth.size())
584 aTable->setColumnWidth(i, aColWidth.at(i));
587 aTable->blockSignals(isBlocked);
592 //**********************************************************************************
593 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
595 QString aType(theStr.c_str());
596 aType = aType.toLower();
597 if (aType == "vertex")
599 else if (aType == "edge")
601 else if (aType == "face")
603 else if (aType == "solid")
605 else if (aType == "object")
607 else if (aType == "part")
613 //**********************************************************************************
614 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
633 //**********************************************************************************
634 QIntList CollectionPlugin_WidgetField::shapeTypes() const
637 switch (myShapeTypeCombo->currentIndex()) {
639 aRes.append(ModuleBase_Tools::shapeType("vertex"));
642 aRes.append(ModuleBase_Tools::shapeType("edge"));
645 aRes.append(ModuleBase_Tools::shapeType("face"));
648 aRes.append(ModuleBase_Tools::shapeType("solid"));
651 aRes.append(ModuleBase_Tools::shapeType("object"));
654 // TODO: Selection mode for Parts
660 //**********************************************************************************
661 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
663 ModelAPI_AttributeTables::Value aVal;
664 switch (myFieldTypeCombo->currentIndex()) {
665 case ModelAPI_AttributeTables::BOOLEAN:
666 aVal.myBool = (theStrVal == MYTrue)? true : false;
668 case ModelAPI_AttributeTables::DOUBLE:
669 aVal.myDouble = theStrVal.toDouble();
671 case ModelAPI_AttributeTables::INTEGER:
672 aVal.myInt = theStrVal.toInt();
674 case ModelAPI_AttributeTables::STRING:
675 aVal.myStr = theStrVal.toStdString();
681 //**********************************************************************************
682 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
684 int aOldCol = myCompNamesList.count();
685 int aNbRows = myDataTblList.first()->rowCount();
686 int aDif = theVal - aOldCol;
687 QTableWidgetItem* aItem = 0;
689 while (myCompNamesList.count() != theVal) {
691 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
693 myCompNamesList.removeLast();
696 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
697 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
699 foreach(QTableWidget* aDataTbl, myDataTblList) {
700 aDataTbl->setColumnCount(theVal + 1);
701 updateHeaders(aDataTbl);
702 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
703 for (int j = 0; j < aNbRows; j++) {
704 aItem = aDataTbl->item(j, i + 1);
706 aItem = createDefaultItem();
708 aItem->setBackgroundColor(Qt::lightGray);
709 aDataTbl->setItem(j, i + 1, aItem);
714 emit valuesChanged();
717 //**********************************************************************************
718 void CollectionPlugin_WidgetField::onAddStep()
720 int aMax = myStepSlider->maximum();
722 myStepSlider->setMaximum(aMax);
723 myMaxLbl->setText(QString::number(aMax));
724 appendStepControls();
725 myStepSlider->setValue(aMax);
727 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
728 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
731 AttributeSelectionListPtr aSelList =
732 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
733 if (!aSelList->isInitialized())
735 int aSelNb = aSelList->size();
739 int aColumns = myNbComponentsSpn->value() + 1;
740 int aRows = aSelNb + 1;
741 QTableWidget* aTable = myDataTblList.last();
742 aTable->setRowCount(aRows);
743 QTableWidgetItem* aItem = 0;
744 for(int i = 0; i < aColumns; i++) {
746 for(int j = 1; j < aRows; j++) {
747 aItem = aTable->item(j, i);
749 aItem = new QTableWidgetItem();
750 aTable->setItem(j, i, aItem);
752 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
753 aItem->setText(aAttr->namingName().c_str());
754 aItem->setToolTip(aAttr->namingName().c_str());
757 QString aDefVal = aTable->item(0, i)->text();
758 for(int j = 1; j < aRows; j++) {
759 aItem = aTable->item(j, i);
761 aItem = new QTableWidgetItem();
762 aTable->setItem(j, i, aItem);
764 aItem->setText(aDefVal);
768 emit valuesChanged();
771 //**********************************************************************************
772 void CollectionPlugin_WidgetField::onRemoveStep()
774 int aMax = myStepSlider->maximum();
776 myMaxLbl->setText(QString::number(aMax));
777 removeStepControls();
778 myStepSlider->setMaximum(aMax);
780 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
781 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
782 emit valuesChanged();
785 //**********************************************************************************
786 void CollectionPlugin_WidgetField::clearData()
788 foreach(QTableWidget* aDataTbl, myDataTblList) {
789 aDataTbl->setRowCount(1);
793 //**********************************************************************************
794 void CollectionPlugin_WidgetField::onStepMove(int theStep)
796 myCurStepLbl->setText(QString::number(theStep));
797 myStepWgt->setCurrentIndex(theStep - 1);
800 //**********************************************************************************
801 bool CollectionPlugin_WidgetField::
802 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
804 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
807 //**********************************************************************************
808 void CollectionPlugin_WidgetField::onSelectionChanged()
810 //if (isEditingMode())
814 myActivation = false;
817 // Ignore selection for Parts mode
818 if (myShapeTypeCombo->currentIndex() == 5)
821 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
823 AttributeSelectionListPtr aSelList =
824 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
825 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
831 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
832 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
833 aShape = aPrs->shape();
834 if (!aResult.get() && !aShape.get())
836 if (!aSelList->isInList(aResult, aShape)) {
837 aSelList->append(aResult, aShape);
841 int aColumns = myDataTblList.first()->columnCount();
842 int aRows = myDataTblList.first()->rowCount();
843 int aNewRows = aNbData + 1;
844 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
845 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
847 QTableWidgetItem* aItem = 0;
848 foreach(QTableWidget* aTable, myDataTblList) {
849 aTable->setRowCount(aNewRows);
850 if (aNewRows > aRows) {
852 for(int i = 0; i < aColumns; i++) {
854 for(int j = 1; j < aNewRows; j++) {
855 aItem = aTable->item(j, i);
857 aItem = new QTableWidgetItem();
858 aTable->setItem(j, i, aItem);
860 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
861 aItem->setText(aAttr->namingName().c_str());
862 aItem->setToolTip(aAttr->namingName().c_str());
865 QString aDefVal = aTable->item(0, i)->text();
866 for(int j = aRows; j < aNewRows; j++) {
867 aItem = aTable->item(j, i);
869 aItem = new QTableWidgetItem();
870 aTable->setItem(j, i, aItem);
872 aItem->setText(aDefVal);
877 // Update only selection name
878 for(int j = 1; j < aNewRows - 1; j++) {
879 AttributeSelectionPtr aAttr = aSelList->value(j);
880 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
881 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
885 emit valuesChanged();
888 //**********************************************************************************
889 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
891 DataTableItemDelegate* aDelegate = 0;
892 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
894 ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
895 if (aOldType != theIdx) {
896 aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
897 int aColumns = myDataTblList.first()->columnCount();
898 int aRows = myDataTblList.first()->rowCount();
899 foreach(QTableWidget* aTable, myDataTblList) {
900 for(int i = 1; i < aColumns; i++) {
901 for(int j = 0; j < aRows; j++) {
903 case ModelAPI_AttributeTables::DOUBLE:
904 case ModelAPI_AttributeTables::INTEGER:
905 if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
906 (aOldType == ModelAPI_AttributeTables::STRING)) {
907 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();
925 //**********************************************************************************
926 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
928 // Do not store here column of names
931 if (!myFeature.get())
933 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
934 int aNb = myDataTblList.indexOf(aTable);
937 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
939 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
940 if (aTablesAttr->isInitialized())
941 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
943 emit valuesChanged();
946 //**********************************************************************************
947 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
949 activateSelectionAndFilters(theType == 5? false:true);
951 AttributeSelectionListPtr aSelList =
952 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
954 std::string aTypeName = getSelectionType(theType);
955 if (aTypeName == aSelList->selectionType())
957 aSelList->setSelectionType(aTypeName);
959 //Clear old selection
962 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
963 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
964 emit valuesChanged();
967 //**********************************************************************************
968 bool CollectionPlugin_WidgetField::processEnter()
977 //**********************************************************************************
978 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
980 if (theNew && (!myIsTabEdit))
981 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
984 //**********************************************************************************
985 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
987 myMaxLbl->setText(QString::number(theMax));
988 myRemoveBtn->setEnabled(theMax > 1);
991 //**********************************************************************************
992 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
994 if (myDataTblList.count() < 2)
996 QObject* aSender = sender();
997 foreach(QTableWidget* aTable, myDataTblList) {
998 if (aTable->horizontalHeader() != aSender)
999 aTable->setColumnWidth(theIndex, theNew);