1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CollectionPlugin_WidgetField.h"
21 #include "CollectionPlugin_Field.h"
23 #include <ModuleBase_Tools.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_IModule.h>
26 #include <ModuleBase_ISelection.h>
27 #include <ModuleBase_IPropertyPanel.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeStringArray.h>
31 #include <ModelAPI_AttributeInteger.h>
32 #include <ModelAPI_AttributeIntArray.h>
36 #include <QFormLayout>
41 #include <QTableWidget>
42 #include <QPushButton>
43 #include <QHeaderView>
44 #include <QStackedWidget>
48 #include <QMouseEvent>
50 #include <QApplication>
52 const char* MYFirstCol = "Shape";
53 const char* MYTrue = "True";
54 const char* MYFalse = "False";
56 DataTableItemDelegate::DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType)
57 : QStyledItemDelegate(), myType(theType)
62 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
63 const QStyleOptionViewItem & theOption,
64 const QModelIndex& theIndex ) const
67 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
68 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
69 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
70 aEdt->setReadOnly(true);
73 QLineEdit* aLineEdt = 0;
75 case ModelAPI_AttributeTables::DOUBLE:
76 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
80 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
84 case ModelAPI_AttributeTables::INTEGER:
85 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
89 aLineEdt->setValidator(new QIntValidator(aLineEdt));
93 case ModelAPI_AttributeTables::BOOLEAN:
95 QComboBox* aBox = new QComboBox(theParent);
96 aBox->addItem(MYFalse);
97 aBox->addItem(MYTrue);
102 aEditor = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
105 if (myType == ModelAPI_AttributeTables::BOOLEAN)
106 connect(aEditor, SIGNAL(currentTextChanged(const QString&)),
107 SLOT(onEditItem(const QString&)));
109 connect(aEditor, SIGNAL(textEdited(const QString&)),
110 SLOT(onEditItem(const QString&)));
114 void DataTableItemDelegate::onEditItem(const QString& theText)
116 QWidget* aWgt = dynamic_cast<QWidget*>(sender());
122 //**********************************************************************************
123 //**********************************************************************************
124 //**********************************************************************************
125 CollectionPlugin_WidgetField::
126 CollectionPlugin_WidgetField(QWidget* theParent,
127 ModuleBase_IWorkshop* theWorkshop,
128 const Config_WidgetAPI* theData):
129 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
130 myIsTabEdit(false), myActivation(false)
132 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
134 // Types definition controls
135 QWidget* aTypesWgt = new QWidget(this);
136 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
137 aTypesLayout->setContentsMargins(0, 0, 0, 0);
138 aMainLayout->addWidget(aTypesWgt);
141 myShapeTypeCombo = new QComboBox(aTypesWgt);
142 QStringList aShapeTypes;
143 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
144 << tr("Solids") << tr("Objects") << tr("Parts");
145 myShapeTypeCombo->addItems(aShapeTypes);
146 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
149 myFieldTypeCombo = new QComboBox(aTypesWgt);
150 QStringList aFieldTypes;
151 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
153 myFieldTypeCombo->addItems(aFieldTypes);
154 myFieldTypeCombo->setCurrentIndex(2);
155 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
157 // Number of components
158 myNbComponentsSpn = new QSpinBox(aTypesWgt);
159 myNbComponentsSpn->setMinimum(1);
160 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
163 QFrame* aStepFrame = new QFrame(this);
164 aStepFrame->setFrameShape(QFrame::Box);
165 aStepFrame->setFrameStyle(QFrame::StyledPanel);
166 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
167 aMainLayout->addWidget(aStepFrame);
169 // Current step label
170 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
171 myCurStepLbl = new QLabel("1", aStepFrame);
172 QFont aFont = myCurStepLbl->font();
174 myCurStepLbl->setFont(aFont);
175 aStepLayout->addWidget(myCurStepLbl, 0, 1);
178 QWidget* aSliderWidget = new QWidget(aStepFrame);
179 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
180 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
181 aSliderLayout->setContentsMargins(0, 0, 0, 0);
183 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
185 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
186 myStepSlider->setTickPosition(QSlider::TicksBelow);
187 myStepSlider->setRange(1, 1);
188 myStepSlider->setPageStep(myStepSlider->singleStep());
189 aSliderLayout->addWidget(myStepSlider, 1);
191 myMaxLbl = new QLabel("1", aSliderWidget);
192 aSliderLayout->addWidget(myMaxLbl);
195 myCompNamesList << "Comp 1";
196 myStepWgt = new QStackedWidget(aStepFrame);
197 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
200 new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
201 myFieldTypeCombo->currentIndex());
203 appendStepControls();
206 QWidget* aBtnWgt = new QWidget(this);
207 aMainLayout->addWidget(aBtnWgt);
208 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
209 aBtnLayout->setContentsMargins(0, 0, 0, 0);
211 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
212 aBtnLayout->addWidget(aAddBtn);
214 aBtnLayout->addStretch(1);
216 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
217 aBtnLayout->addWidget(myRemoveBtn);
218 myRemoveBtn->setEnabled(false);
220 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
221 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
222 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
223 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
224 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
225 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
226 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
227 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
230 //**********************************************************************************
231 void CollectionPlugin_WidgetField::appendStepControls()
233 QWidget* aWidget = new QWidget(myStepWgt);
234 QGridLayout* aStepLayout = new QGridLayout(aWidget);
235 aStepLayout->setContentsMargins(0, 0, 0, 0);
237 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
239 QSpinBox* aStampSpn = new QSpinBox(aWidget);
240 aStepLayout->addWidget(aStampSpn, 0, 1);
242 myStampSpnList.append(aStampSpn);
245 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
246 aDataTbl->installEventFilter(this);
247 aDataTbl->setItemDelegate(myDelegate);
250 if (!myDataTblList.isEmpty()) {
251 QTableWidget* aFirstTable = myDataTblList.first();
252 for (int i = 0; i < aFirstTable->columnCount(); i++)
253 aColWidth.append(aFirstTable->columnWidth(i));
255 myDataTblList.append(aDataTbl);
257 aDataTbl->verticalHeader()->hide();
258 aDataTbl->setRowHeight(0, 25);
259 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
261 connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
262 SLOT(onColumnResize(int, int, int)));
264 updateHeaders(aDataTbl);
266 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
267 aItem->setBackgroundColor(Qt::lightGray);
268 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
269 aDataTbl->setItem(0, 0, aItem);
271 // Set default value item
272 for (int i = 0; i < myCompNamesList.count(); i++) {
273 aItem = createDefaultItem();
274 aItem->setBackgroundColor(Qt::lightGray);
275 aDataTbl->setItem(0, i + 1, aItem);
278 if (aColWidth.length() > 0) {
279 for (int i = 0; i < aDataTbl->columnCount(); i++) {
280 if (i < aColWidth.size())
281 aDataTbl->setColumnWidth(i, aColWidth.at(i));
284 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
285 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
287 myStepWgt->addWidget(aWidget);
288 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
291 //**********************************************************************************
292 void CollectionPlugin_WidgetField::deactivate()
294 ModuleBase_WidgetSelector::deactivate();
298 //**********************************************************************************
299 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
301 // myShapeTypeCombo->setEnabled(!isEditingMode());
302 // myFieldTypeCombo->setEnabled(!isEditingMode());
303 // myNbComponentsSpn->setEnabled(!isEditingMode());
306 //**********************************************************************************
307 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
309 QObject* aObject = 0;
310 foreach(QTableWidget* aTable, myDataTblList) {
311 if (aTable->horizontalHeader()->viewport() == theObject) {
317 if (theEvent->type() == QEvent::MouseButtonDblClick) {
318 if (myHeaderEditor) { //delete previous editor
319 myHeaderEditor->deleteLater();
322 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
323 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
324 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
326 int aShift = aTable->horizontalScrollBar()->value();
327 int aPos = aMouseEvent->x();
328 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
331 aRect.setLeft(aHeader->sectionPosition(aIndex));
332 aRect.setWidth(aHeader->sectionSize(aIndex));
334 aRect.setHeight(aHeader->height());
335 aRect.adjust(1, 1, -1, -1);
336 aRect.translate(-aShift, 0);
338 myHeaderEditor = new QLineEdit(aHeader->viewport());
339 myHeaderEditor->move(aRect.topLeft());
340 myHeaderEditor->resize(aRect.size());
341 myHeaderEditor->setFrame(false);
342 QString aText = aHeader->model()->
343 headerData(aIndex, aHeader->orientation()).toString();
344 myHeaderEditor->setText(aText);
345 myHeaderEditor->setFocus();
346 myEditIndex = aIndex; //save for future use
347 myHeaderEditor->installEventFilter(this); //catch focus out event
348 //if user presses Enter it should close editor
349 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
350 myHeaderEditor->show();
354 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
355 QString aNewTitle = myHeaderEditor->text();
357 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
358 myHeaderEditor->deleteLater(); //safely delete editor
360 // Store into data model
361 AttributeStringArrayPtr aStringsAttr =
362 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
363 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
364 foreach(QTableWidget* aTable, myDataTblList) {
365 updateHeaders(aTable);
367 } else if (theEvent->type() == QEvent::FocusIn) {
368 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
370 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
371 if (aPanel->activeWidget() != this) {
373 aPanel->activateWidget(this, false);
377 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
380 //**********************************************************************************
381 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
383 QTableWidgetItem* aItem = new QTableWidgetItem();
384 switch (myFieldTypeCombo->currentIndex()) {
385 case ModelAPI_AttributeTables::DOUBLE:
386 case ModelAPI_AttributeTables::INTEGER:
389 case ModelAPI_AttributeTables::BOOLEAN:
390 aItem->setText(MYFalse);
392 case ModelAPI_AttributeTables::STRING:
399 //**********************************************************************************
400 QTableWidgetItem* CollectionPlugin_WidgetField::
401 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
403 QTableWidgetItem* aItem = new QTableWidgetItem();
404 aItem->setText(getValueText(theVal));
408 //**********************************************************************************
409 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
411 switch (myFieldTypeCombo->currentIndex()) {
412 case ModelAPI_AttributeTables::DOUBLE:
413 return QString::number(theVal.myDouble);
414 case ModelAPI_AttributeTables::INTEGER:
415 return QString::number(theVal.myInt);
416 case ModelAPI_AttributeTables::BOOLEAN:
417 return theVal.myBool? MYTrue : MYFalse;
418 case ModelAPI_AttributeTables::STRING:
419 return theVal.myStr.c_str();
425 //**********************************************************************************
426 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
428 QStringList aHeaders;
429 aHeaders << tr(MYFirstCol);
430 aHeaders << myCompNamesList;
431 theDataTbl->setHorizontalHeaderLabels(aHeaders);
434 //**********************************************************************************
435 void CollectionPlugin_WidgetField::removeStepControls()
437 int aCurWgtId = myStepWgt->currentIndex();
438 QWidget* aWgt = myStepWgt->currentWidget();
439 myStepWgt->removeWidget(aWgt);
441 myStampSpnList.removeAt(aCurWgtId);
442 myDataTblList.removeAt(aCurWgtId);
446 //**********************************************************************************
447 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
449 QList<QWidget*> aControls;
450 // this control will accept focus and will be highlighted in the Property Panel
451 aControls.append(myShapeTypeCombo);
452 aControls.append(myFieldTypeCombo);
453 aControls.append(myNbComponentsSpn);
454 if (myStampSpnList.size() > 0)
455 aControls.append(myStampSpnList.first());
456 if (myDataTblList.size() > 0)
457 aControls.append(myDataTblList.first());
462 //**********************************************************************************
463 bool CollectionPlugin_WidgetField::storeValueCustom()
465 DataPtr aData = myFeature->data();
466 // Store number of components
467 AttributeStringArrayPtr aStringsAttr =
468 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
469 int aNbComps = myCompNamesList.size();
470 aStringsAttr->setSize(aNbComps);
471 for ( int i = 0; i < aNbComps; i++)
472 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
474 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
475 // Store number of steps
476 int aNbSteps = myDataTblList.size();
478 // Store Type of the field values
479 int aFldType = myFieldTypeCombo->currentIndex();
481 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
482 aStampsAttr->setSize(aNbSteps);
484 QTableWidget* aTable = myDataTblList.first();
485 int aRows = aTable->rowCount();
486 // first column contains selected names which should not be stored
487 int aColumns = aTable->columnCount() - 1;
489 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
490 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
491 for (int i = 0; i < aNbSteps; i++) {
492 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
493 aTable = myDataTblList.at(i);
494 for (int j = 0; j < aColumns; j++) {
495 for (int k = 0; k < aRows; k++) {
496 QString aTblVal = aTable->item(k, j + 1)->text();
497 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
504 //**********************************************************************************
505 bool CollectionPlugin_WidgetField::restoreValueCustom()
508 DataPtr aData = myFeature->data();
510 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
511 std::string aTypeStr = aSelList->selectionType();
513 return false; // The attribute is not initialized
514 isBlocked = myShapeTypeCombo->blockSignals(true);
515 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
516 myShapeTypeCombo->blockSignals(isBlocked);
518 // Get number of components
519 AttributeStringArrayPtr aStringsAttr =
520 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
522 myCompNamesList.clear();
523 for (int i = 0; i < aStringsAttr->size(); i++) {
524 myCompNamesList.append(aStringsAttr->value(i).c_str());
526 isBlocked = myNbComponentsSpn->blockSignals(true);
527 myNbComponentsSpn->setValue(myCompNamesList.size());
528 myNbComponentsSpn->blockSignals(isBlocked);
530 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
531 // Get number of steps
532 int aNbSteps = aTablesAttr->tables();
533 myStepSlider->setMaximum(aNbSteps);
534 //myStepSlider->setValue(1);
536 while (myDataTblList.count() > aNbSteps) {
537 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
538 myStepWgt->removeWidget(aWgt);
541 myStampSpnList.removeLast();
542 myDataTblList.removeLast();
544 while (myDataTblList.count() < aNbSteps)
545 appendStepControls();
546 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
549 // Get Type of the field values
550 isBlocked = myFieldTypeCombo->blockSignals(true);
551 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
552 myFieldTypeCombo->blockSignals(isBlocked);
553 myDelegate->setDataType(aTablesAttr->type());
555 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
557 int aRows = aTablesAttr->rows();
558 int aCols = aTablesAttr->columns();
560 // Get width of columns
562 QTableWidget* aFirstTable = myDataTblList.first();
563 for (int i = 0; i < aFirstTable->columnCount(); i++)
564 aColWidth.append(aFirstTable->columnWidth(i));
566 QTableWidgetItem* aItem = 0;
567 for (int i = 0; i < aNbSteps; i++) {
568 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
569 QTableWidget* aTable = myDataTblList.at(i);
570 isBlocked = aTable->blockSignals(true);
571 aTable->setRowCount(aRows);
572 aTable->setColumnCount(aCols + 1);
573 updateHeaders(aTable);
574 for (int j = 0; j < aCols + 1; j++) {
575 for (int k = 0; k < aRows; k++) {
576 aItem = aTable->item(k, j);
577 if ((j == 0) && (k > 0)) {
578 // Add selection names
579 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
581 aItem->setText(aAttr->namingName().c_str());
583 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
584 aTable->setItem(k, j, aItem);
588 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
590 aItem->setText(getValueText(aVal));
592 aItem = createValueItem(aVal);
594 aItem->setBackgroundColor(Qt::lightGray);
595 aTable->setItem(k, j, aItem);
600 // Restore columns width
601 for (int col = 0; col < aTable->columnCount(); col++) {
602 if (col < aColWidth.size())
603 aTable->setColumnWidth(col, aColWidth.at(col));
606 aTable->blockSignals(isBlocked);
611 //**********************************************************************************
612 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
614 QString aType(theStr.c_str());
615 aType = aType.toLower();
616 if (aType == "vertex")
618 else if (aType == "edge")
620 else if (aType == "face")
622 else if (aType == "solid")
624 else if (aType == "object")
626 else if (aType == "part")
632 //**********************************************************************************
633 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
652 //**********************************************************************************
653 QIntList CollectionPlugin_WidgetField::shapeTypes() const
656 switch (myShapeTypeCombo->currentIndex()) {
658 aRes.append(ModuleBase_Tools::shapeType("vertex"));
661 aRes.append(ModuleBase_Tools::shapeType("edge"));
664 aRes.append(ModuleBase_Tools::shapeType("face"));
667 aRes.append(ModuleBase_Tools::shapeType("solid"));
670 aRes.append(ModuleBase_Tools::shapeType("object"));
673 // TODO: Selection mode for Parts
679 //**********************************************************************************
680 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
682 ModelAPI_AttributeTables::Value aVal;
683 switch (myFieldTypeCombo->currentIndex()) {
684 case ModelAPI_AttributeTables::BOOLEAN:
685 aVal.myBool = (theStrVal == MYTrue)? true : false;
687 case ModelAPI_AttributeTables::DOUBLE:
688 aVal.myDouble = theStrVal.toDouble();
690 case ModelAPI_AttributeTables::INTEGER:
691 aVal.myInt = theStrVal.toInt();
693 case ModelAPI_AttributeTables::STRING:
694 aVal.myStr = theStrVal.toStdString();
700 //**********************************************************************************
701 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
703 int aOldCol = myCompNamesList.count();
704 int aNbRows = myDataTblList.first()->rowCount();
705 int aDif = theVal - aOldCol;
706 QTableWidgetItem* aItem = 0;
708 while (myCompNamesList.count() != theVal) {
710 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
712 myCompNamesList.removeLast();
715 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
716 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
718 foreach(QTableWidget* aDataTbl, myDataTblList) {
719 aDataTbl->setColumnCount(theVal + 1);
720 updateHeaders(aDataTbl);
721 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
722 for (int j = 0; j < aNbRows; j++) {
723 aItem = aDataTbl->item(j, i + 1);
725 aItem = createDefaultItem();
727 aItem->setBackgroundColor(Qt::lightGray);
728 aDataTbl->setItem(j, i + 1, aItem);
733 emit valuesChanged();
736 //**********************************************************************************
737 void CollectionPlugin_WidgetField::onAddStep()
739 int aMax = myStepSlider->maximum();
741 myStepSlider->setMaximum(aMax);
742 myMaxLbl->setText(QString::number(aMax));
743 appendStepControls();
744 myStepSlider->setValue(aMax);
746 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
747 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
750 AttributeSelectionListPtr aSelList =
751 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
752 if (!aSelList->isInitialized())
754 int aSelNb = aSelList->size();
758 int aColumns = myNbComponentsSpn->value() + 1;
759 int aRows = aSelNb + 1;
760 QTableWidget* aTable = myDataTblList.last();
761 aTable->setRowCount(aRows);
762 QTableWidgetItem* aItem = 0;
763 for(int i = 0; i < aColumns; i++) {
765 for(int j = 1; j < aRows; j++) {
766 aItem = aTable->item(j, i);
768 aItem = new QTableWidgetItem();
769 aTable->setItem(j, i, aItem);
771 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
772 aItem->setText(aAttr->namingName().c_str());
773 aItem->setToolTip(aAttr->namingName().c_str());
776 QString aDefVal = aTable->item(0, i)->text();
777 for(int j = 1; j < aRows; j++) {
778 aItem = aTable->item(j, i);
780 aItem = new QTableWidgetItem();
781 aTable->setItem(j, i, aItem);
783 aItem->setText(aDefVal);
787 emit valuesChanged();
790 //**********************************************************************************
791 void CollectionPlugin_WidgetField::onRemoveStep()
793 int aMax = myStepSlider->maximum();
795 myMaxLbl->setText(QString::number(aMax));
796 removeStepControls();
797 myStepSlider->setMaximum(aMax);
799 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
800 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
801 emit valuesChanged();
804 //**********************************************************************************
805 void CollectionPlugin_WidgetField::clearData()
807 foreach(QTableWidget* aDataTbl, myDataTblList) {
808 aDataTbl->setRowCount(1);
812 //**********************************************************************************
813 void CollectionPlugin_WidgetField::onStepMove(int theStep)
815 myCurStepLbl->setText(QString::number(theStep));
816 myStepWgt->setCurrentIndex(theStep - 1);
819 //**********************************************************************************
820 bool CollectionPlugin_WidgetField::
821 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
823 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
826 //**********************************************************************************
827 bool CollectionPlugin_WidgetField::
828 setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
831 myActivation = false;
834 // Ignore selection for Parts mode
835 if (myShapeTypeCombo->currentIndex() == 5)
838 QList<ModuleBase_ViewerPrsPtr> aSelected;
839 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
840 for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
841 ModuleBase_ViewerPrsPtr aValue = *anIt;
842 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
843 if (theToValidate && aRes.get()) {
844 if (myShapeTypeCombo->currentIndex() > 3)
845 aSelected.append(aValue);
846 else if (acceptSubShape(aValue->shape(), aRes))
847 aSelected.append(aValue);
850 AttributeSelectionListPtr aSelList =
851 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
852 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
858 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
859 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
860 aShape = aPrs->shape();
861 if (!aResult.get() && !aShape.get())
863 if (!aSelList->isInList(aResult, aShape)) {
864 aSelList->append(aResult, aShape);
868 int aColumns = myDataTblList.first()->columnCount();
869 int aRows = myDataTblList.first()->rowCount();
870 int aNewRows = aNbData + 1;
871 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
872 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
874 QTableWidgetItem* aItem = 0;
875 foreach(QTableWidget* aTable, myDataTblList) {
876 aTable->setRowCount(aNewRows);
877 if (aNewRows > aRows) {
879 for(int i = 0; i < aColumns; i++) {
881 for(int j = 1; j < aNewRows; j++) {
882 aItem = aTable->item(j, i);
884 aItem = new QTableWidgetItem();
885 aTable->setItem(j, i, aItem);
887 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
888 aItem->setText(aAttr->namingName().c_str());
889 aItem->setToolTip(aAttr->namingName().c_str());
892 QString aDefVal = aTable->item(0, i)->text();
893 for(int j = aRows; j < aNewRows; j++) {
894 aItem = aTable->item(j, i);
896 aItem = new QTableWidgetItem();
897 aTable->setItem(j, i, aItem);
899 aItem->setText(aDefVal);
904 // Update only selection name
905 for(int j = 1; j < aNewRows - 1; j++) {
906 AttributeSelectionPtr aAttr = aSelList->value(j);
907 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
908 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
915 //**********************************************************************************
916 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
918 ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
919 if (aOldType != theIdx) {
920 myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
921 int aColumns = myDataTblList.first()->columnCount();
922 int aRows = myDataTblList.first()->rowCount();
923 foreach(QTableWidget* aTable, myDataTblList) {
924 for(int i = 1; i < aColumns; i++) {
925 for(int j = 0; j < aRows; j++) {
927 case ModelAPI_AttributeTables::DOUBLE:
928 case ModelAPI_AttributeTables::INTEGER:
929 aTable->item(j, i)->setText("0");
931 case ModelAPI_AttributeTables::BOOLEAN:
932 aTable->item(j, i)->setText(MYFalse);
934 case ModelAPI_AttributeTables::STRING:
935 aTable->item(j, i)->setText("");
941 emit valuesChanged();
945 //**********************************************************************************
946 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
948 // Do not store here column of names
951 if (!myFeature.get())
953 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
954 int aNb = myDataTblList.indexOf(aTable);
957 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
959 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
960 if (aTablesAttr->isInitialized())
961 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
963 emit valuesChanged();
966 //**********************************************************************************
967 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
969 updateSelectionModesAndFilters(theType == 5? false:true);
971 AttributeSelectionListPtr aSelList =
972 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
974 std::string aTypeName = getSelectionType(theType);
975 if (aTypeName == aSelList->selectionType())
977 aSelList->setSelectionType(aTypeName);
978 // Updated event has to be sent here in case if type of shapes
979 // was changed from Part to any other in order to updater Apply button status
980 myFeature->data()->sendAttributeUpdated(aSelList.get());
982 //Clear old selection
985 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
986 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
987 updateObject(myFeature);
988 emit valuesChanged();
991 //**********************************************************************************
992 bool CollectionPlugin_WidgetField::processEnter()
998 QWidget* aCurrWgt = qApp->focusWidget();
999 int aCurWgtId = myStepWgt->currentIndex();
1000 if ((aCurrWgt == myShapeTypeCombo) ||
1001 (aCurrWgt == myFieldTypeCombo) ||
1002 (aCurrWgt == myNbComponentsSpn) ||
1003 (aCurrWgt == myStampSpnList[aCurWgtId]) ||
1004 (aCurrWgt == myDataTblList[aCurWgtId])) {
1011 //**********************************************************************************
1012 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
1014 if (theNew && (!myIsTabEdit))
1015 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
1018 //**********************************************************************************
1019 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
1021 myMaxLbl->setText(QString::number(theMax));
1022 myRemoveBtn->setEnabled(theMax > 1);
1025 //**********************************************************************************
1026 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
1028 if (myDataTblList.count() < 2)
1030 QObject* aSender = sender();
1031 foreach(QTableWidget* aTable, myDataTblList) {
1032 if (aTable->horizontalHeader() != aSender)
1033 aTable->setColumnWidth(theIndex, theNew);
1037 //**********************************************************************************
1038 QList<std::shared_ptr<ModuleBase_ViewerPrs>>
1039 CollectionPlugin_WidgetField::getAttributeSelection() const
1041 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
1043 DataPtr aData = myFeature->data();
1044 AttributeSelectionListPtr aSelList =
1045 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
1046 AttributeSelectionPtr aAttr;
1048 for (int i = 0; i < aSelList->size(); i++) {
1049 aAttr = aSelList->value(i);
1050 ModuleBase_ViewerPrsPtr
1051 aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));