1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "CollectionPlugin_WidgetField.h"
22 #include "CollectionPlugin_Field.h"
24 #include <ModuleBase_Tools.h>
25 #include <ModuleBase_IWorkshop.h>
26 #include <ModuleBase_IModule.h>
27 #include <ModuleBase_ISelection.h>
28 #include <ModuleBase_IPropertyPanel.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_AttributeStringArray.h>
32 #include <ModelAPI_AttributeInteger.h>
33 #include <ModelAPI_AttributeIntArray.h>
37 #include <QFormLayout>
42 #include <QTableWidget>
43 #include <QPushButton>
44 #include <QHeaderView>
45 #include <QStackedWidget>
49 #include <QMouseEvent>
51 #include <QApplication>
53 const char* MYFirstCol = "Shape";
54 const char* MYTrue = "True";
55 const char* MYFalse = "False";
57 DataTableItemDelegate::DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType)
58 : QStyledItemDelegate(), myType(theType)
63 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
64 const QStyleOptionViewItem & theOption,
65 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 aEditor = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
106 if (myType == ModelAPI_AttributeTables::BOOLEAN)
107 connect(aEditor, SIGNAL(currentTextChanged(const QString&)),
108 SLOT(onEditItem(const QString&)));
110 connect(aEditor, SIGNAL(textEdited(const QString&)),
111 SLOT(onEditItem(const QString&)));
115 void DataTableItemDelegate::onEditItem(const QString& theText)
117 QWidget* aWgt = dynamic_cast<QWidget*>(sender());
123 //**********************************************************************************
124 //**********************************************************************************
125 //**********************************************************************************
126 CollectionPlugin_WidgetField::
127 CollectionPlugin_WidgetField(QWidget* theParent,
128 ModuleBase_IWorkshop* theWorkshop,
129 const Config_WidgetAPI* theData):
130 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
131 myIsTabEdit(false), myActivation(false)
133 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
135 // Types definition controls
136 QWidget* aTypesWgt = new QWidget(this);
137 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
138 aTypesLayout->setContentsMargins(0, 0, 0, 0);
139 aMainLayout->addWidget(aTypesWgt);
142 myShapeTypeCombo = new QComboBox(aTypesWgt);
143 QStringList aShapeTypes;
144 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
145 << tr("Solids") << tr("Objects") << tr("Parts");
146 myShapeTypeCombo->addItems(aShapeTypes);
147 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
150 myFieldTypeCombo = new QComboBox(aTypesWgt);
151 QStringList aFieldTypes;
152 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
154 myFieldTypeCombo->addItems(aFieldTypes);
155 myFieldTypeCombo->setCurrentIndex(2);
156 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
158 // Number of components
159 myNbComponentsSpn = new QSpinBox(aTypesWgt);
160 myNbComponentsSpn->setMinimum(1);
161 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
164 QFrame* aStepFrame = new QFrame(this);
165 aStepFrame->setFrameShape(QFrame::Box);
166 aStepFrame->setFrameStyle(QFrame::StyledPanel);
167 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
168 aMainLayout->addWidget(aStepFrame);
170 // Current step label
171 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
172 myCurStepLbl = new QLabel("1", aStepFrame);
173 QFont aFont = myCurStepLbl->font();
175 myCurStepLbl->setFont(aFont);
176 aStepLayout->addWidget(myCurStepLbl, 0, 1);
179 QWidget* aSliderWidget = new QWidget(aStepFrame);
180 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
181 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
182 aSliderLayout->setContentsMargins(0, 0, 0, 0);
184 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
186 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
187 myStepSlider->setTickPosition(QSlider::TicksBelow);
188 myStepSlider->setRange(1, 1);
189 myStepSlider->setPageStep(myStepSlider->singleStep());
190 aSliderLayout->addWidget(myStepSlider, 1);
192 myMaxLbl = new QLabel("1", aSliderWidget);
193 aSliderLayout->addWidget(myMaxLbl);
196 myCompNamesList << "Comp 1";
197 myStepWgt = new QStackedWidget(aStepFrame);
198 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
201 new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
202 myFieldTypeCombo->currentIndex());
204 appendStepControls();
207 QWidget* aBtnWgt = new QWidget(this);
208 aMainLayout->addWidget(aBtnWgt);
209 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
210 aBtnLayout->setContentsMargins(0, 0, 0, 0);
212 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
213 aBtnLayout->addWidget(aAddBtn);
215 aBtnLayout->addStretch(1);
217 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
218 aBtnLayout->addWidget(myRemoveBtn);
219 myRemoveBtn->setEnabled(false);
221 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
222 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
223 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
224 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
225 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
226 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
227 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
228 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
231 //**********************************************************************************
232 void CollectionPlugin_WidgetField::appendStepControls()
234 QWidget* aWidget = new QWidget(myStepWgt);
235 QGridLayout* aStepLayout = new QGridLayout(aWidget);
236 aStepLayout->setContentsMargins(0, 0, 0, 0);
238 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
240 QSpinBox* aStampSpn = new QSpinBox(aWidget);
241 aStepLayout->addWidget(aStampSpn, 0, 1);
243 myStampSpnList.append(aStampSpn);
246 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
247 aDataTbl->installEventFilter(this);
248 aDataTbl->setItemDelegate(myDelegate);
251 if (!myDataTblList.isEmpty()) {
252 QTableWidget* aFirstTable = myDataTblList.first();
253 for (int i = 0; i < aFirstTable->columnCount(); i++)
254 aColWidth.append(aFirstTable->columnWidth(i));
256 myDataTblList.append(aDataTbl);
258 aDataTbl->verticalHeader()->hide();
259 aDataTbl->setRowHeight(0, 25);
260 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
262 connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
263 SLOT(onColumnResize(int, int, int)));
265 updateHeaders(aDataTbl);
267 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
268 aItem->setBackgroundColor(Qt::lightGray);
269 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
270 aDataTbl->setItem(0, 0, aItem);
272 // Set default value item
273 for (int i = 0; i < myCompNamesList.count(); i++) {
274 aItem = createDefaultItem();
275 aItem->setBackgroundColor(Qt::lightGray);
276 aDataTbl->setItem(0, i + 1, aItem);
279 if (aColWidth.length() > 0) {
280 for (int i = 0; i < aDataTbl->columnCount(); i++) {
281 if (i < aColWidth.size())
282 aDataTbl->setColumnWidth(i, aColWidth.at(i));
285 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
286 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
288 myStepWgt->addWidget(aWidget);
289 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
292 //**********************************************************************************
293 void CollectionPlugin_WidgetField::deactivate()
295 ModuleBase_WidgetSelector::deactivate();
299 //**********************************************************************************
300 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
302 // myShapeTypeCombo->setEnabled(!isEditingMode());
303 // myFieldTypeCombo->setEnabled(!isEditingMode());
304 // myNbComponentsSpn->setEnabled(!isEditingMode());
307 //**********************************************************************************
308 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
310 QObject* aObject = 0;
311 foreach(QTableWidget* aTable, myDataTblList) {
312 if (aTable->horizontalHeader()->viewport() == theObject) {
318 if (theEvent->type() == QEvent::MouseButtonDblClick) {
319 if (myHeaderEditor) { //delete previous editor
320 myHeaderEditor->deleteLater();
323 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
324 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
325 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
327 int aShift = aTable->horizontalScrollBar()->value();
328 int aPos = aMouseEvent->x();
329 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
332 aRect.setLeft(aHeader->sectionPosition(aIndex));
333 aRect.setWidth(aHeader->sectionSize(aIndex));
335 aRect.setHeight(aHeader->height());
336 aRect.adjust(1, 1, -1, -1);
337 aRect.translate(-aShift, 0);
339 myHeaderEditor = new QLineEdit(aHeader->viewport());
340 myHeaderEditor->move(aRect.topLeft());
341 myHeaderEditor->resize(aRect.size());
342 myHeaderEditor->setFrame(false);
343 QString aText = aHeader->model()->
344 headerData(aIndex, aHeader->orientation()).toString();
345 myHeaderEditor->setText(aText);
346 myHeaderEditor->setFocus();
347 myEditIndex = aIndex; //save for future use
348 myHeaderEditor->installEventFilter(this); //catch focus out event
349 //if user presses Enter it should close editor
350 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
351 myHeaderEditor->show();
355 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
356 QString aNewTitle = myHeaderEditor->text();
358 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
359 myHeaderEditor->deleteLater(); //safely delete editor
361 // Store into data model
362 AttributeStringArrayPtr aStringsAttr =
363 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
364 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
365 foreach(QTableWidget* aTable, myDataTblList) {
366 updateHeaders(aTable);
368 } else if (theEvent->type() == QEvent::FocusIn) {
369 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
371 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
372 if (aPanel->activeWidget() != this) {
374 aPanel->activateWidget(this, false);
378 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
381 //**********************************************************************************
382 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
384 QTableWidgetItem* aItem = new QTableWidgetItem();
385 switch (myFieldTypeCombo->currentIndex()) {
386 case ModelAPI_AttributeTables::DOUBLE:
387 case ModelAPI_AttributeTables::INTEGER:
390 case ModelAPI_AttributeTables::BOOLEAN:
391 aItem->setText(MYFalse);
393 case ModelAPI_AttributeTables::STRING:
400 //**********************************************************************************
401 QTableWidgetItem* CollectionPlugin_WidgetField::
402 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
404 QTableWidgetItem* aItem = new QTableWidgetItem();
405 aItem->setText(getValueText(theVal));
409 //**********************************************************************************
410 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
412 switch (myFieldTypeCombo->currentIndex()) {
413 case ModelAPI_AttributeTables::DOUBLE:
414 return QString::number(theVal.myDouble);
415 case ModelAPI_AttributeTables::INTEGER:
416 return QString::number(theVal.myInt);
417 case ModelAPI_AttributeTables::BOOLEAN:
418 return theVal.myBool? MYTrue : MYFalse;
419 case ModelAPI_AttributeTables::STRING:
420 return theVal.myStr.c_str();
426 //**********************************************************************************
427 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
429 QStringList aHeaders;
430 aHeaders << tr(MYFirstCol);
431 aHeaders << myCompNamesList;
432 theDataTbl->setHorizontalHeaderLabels(aHeaders);
435 //**********************************************************************************
436 void CollectionPlugin_WidgetField::removeStepControls()
438 int aCurWgtId = myStepWgt->currentIndex();
439 QWidget* aWgt = myStepWgt->currentWidget();
440 myStepWgt->removeWidget(aWgt);
442 myStampSpnList.removeAt(aCurWgtId);
443 myDataTblList.removeAt(aCurWgtId);
447 //**********************************************************************************
448 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
450 QList<QWidget*> aControls;
451 // this control will accept focus and will be highlighted in the Property Panel
452 aControls.append(myShapeTypeCombo);
453 aControls.append(myFieldTypeCombo);
454 aControls.append(myNbComponentsSpn);
455 if (myStampSpnList.size() > 0)
456 aControls.append(myStampSpnList.first());
457 if (myDataTblList.size() > 0)
458 aControls.append(myDataTblList.first());
463 //**********************************************************************************
464 bool CollectionPlugin_WidgetField::storeValueCustom()
466 DataPtr aData = myFeature->data();
467 // Store number of components
468 AttributeStringArrayPtr aStringsAttr =
469 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
470 int aNbComps = myCompNamesList.size();
471 aStringsAttr->setSize(aNbComps);
472 for ( int i = 0; i < aNbComps; i++)
473 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
475 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
476 // Store number of steps
477 int aNbSteps = myDataTblList.size();
479 // Store Type of the field values
480 int aFldType = myFieldTypeCombo->currentIndex();
482 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
483 aStampsAttr->setSize(aNbSteps);
485 QTableWidget* aTable = myDataTblList.first();
486 int aRows = aTable->rowCount();
487 // first column contains selected names which should not be stored
488 int aColumns = aTable->columnCount() - 1;
490 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
491 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
492 for (int i = 0; i < aNbSteps; i++) {
493 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
494 aTable = myDataTblList.at(i);
495 for (int j = 0; j < aColumns; j++) {
496 for (int k = 0; k < aRows; k++) {
497 QString aTblVal = aTable->item(k, j + 1)->text();
498 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
502 updateObject(myFeature);
506 //**********************************************************************************
507 bool CollectionPlugin_WidgetField::restoreValueCustom()
510 DataPtr aData = myFeature->data();
512 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
513 std::string aTypeStr = aSelList->selectionType();
515 return false; // The attribute is not initialized
516 isBlocked = myShapeTypeCombo->blockSignals(true);
517 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
518 myShapeTypeCombo->blockSignals(isBlocked);
520 // Get number of components
521 AttributeStringArrayPtr aStringsAttr =
522 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
524 myCompNamesList.clear();
525 for (int i = 0; i < aStringsAttr->size(); i++) {
526 myCompNamesList.append(aStringsAttr->value(i).c_str());
528 isBlocked = myNbComponentsSpn->blockSignals(true);
529 myNbComponentsSpn->setValue(myCompNamesList.size());
530 myNbComponentsSpn->blockSignals(isBlocked);
532 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
533 // Get number of steps
534 int aNbSteps = aTablesAttr->tables();
535 myStepSlider->setMaximum(aNbSteps);
536 //myStepSlider->setValue(1);
538 while (myDataTblList.count() > aNbSteps) {
539 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
540 myStepWgt->removeWidget(aWgt);
543 myStampSpnList.removeLast();
544 myDataTblList.removeLast();
546 while (myDataTblList.count() < aNbSteps)
547 appendStepControls();
548 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
551 // Get Type of the field values
552 isBlocked = myFieldTypeCombo->blockSignals(true);
553 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
554 myFieldTypeCombo->blockSignals(isBlocked);
555 myDelegate->setDataType(aTablesAttr->type());
557 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
559 int aRows = aTablesAttr->rows();
560 int aCols = aTablesAttr->columns();
562 // Get width of columns
564 QTableWidget* aFirstTable = myDataTblList.first();
565 for (int i = 0; i < aFirstTable->columnCount(); i++)
566 aColWidth.append(aFirstTable->columnWidth(i));
568 QTableWidgetItem* aItem = 0;
569 for (int i = 0; i < aNbSteps; i++) {
570 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
571 QTableWidget* aTable = myDataTblList.at(i);
572 isBlocked = aTable->blockSignals(true);
573 aTable->setRowCount(aRows);
574 aTable->setColumnCount(aCols + 1);
575 updateHeaders(aTable);
576 for (int j = 0; j < aCols + 1; j++) {
577 for (int k = 0; k < aRows; k++) {
578 aItem = aTable->item(k, j);
579 if ((j == 0) && (k > 0)) {
580 // Add selection names
581 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
583 aItem->setText(aAttr->namingName().c_str());
585 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
586 aTable->setItem(k, j, aItem);
590 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
592 aItem->setText(getValueText(aVal));
594 aItem = createValueItem(aVal);
596 aItem->setBackgroundColor(Qt::lightGray);
597 aTable->setItem(k, j, aItem);
602 // Restore columns width
603 for (int i = 0; i < aTable->columnCount(); i++) {
604 if (i < aColWidth.size())
605 aTable->setColumnWidth(i, aColWidth.at(i));
608 aTable->blockSignals(isBlocked);
613 //**********************************************************************************
614 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
616 QString aType(theStr.c_str());
617 aType = aType.toLower();
618 if (aType == "vertex")
620 else if (aType == "edge")
622 else if (aType == "face")
624 else if (aType == "solid")
626 else if (aType == "object")
628 else if (aType == "part")
634 //**********************************************************************************
635 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
654 //**********************************************************************************
655 QIntList CollectionPlugin_WidgetField::shapeTypes() const
658 switch (myShapeTypeCombo->currentIndex()) {
660 aRes.append(ModuleBase_Tools::shapeType("vertex"));
663 aRes.append(ModuleBase_Tools::shapeType("edge"));
666 aRes.append(ModuleBase_Tools::shapeType("face"));
669 aRes.append(ModuleBase_Tools::shapeType("solid"));
672 aRes.append(ModuleBase_Tools::shapeType("object"));
675 // TODO: Selection mode for Parts
681 //**********************************************************************************
682 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
684 ModelAPI_AttributeTables::Value aVal;
685 switch (myFieldTypeCombo->currentIndex()) {
686 case ModelAPI_AttributeTables::BOOLEAN:
687 aVal.myBool = (theStrVal == MYTrue)? true : false;
689 case ModelAPI_AttributeTables::DOUBLE:
690 aVal.myDouble = theStrVal.toDouble();
692 case ModelAPI_AttributeTables::INTEGER:
693 aVal.myInt = theStrVal.toInt();
695 case ModelAPI_AttributeTables::STRING:
696 aVal.myStr = theStrVal.toStdString();
702 //**********************************************************************************
703 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
705 int aOldCol = myCompNamesList.count();
706 int aNbRows = myDataTblList.first()->rowCount();
707 int aDif = theVal - aOldCol;
708 QTableWidgetItem* aItem = 0;
710 while (myCompNamesList.count() != theVal) {
712 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
714 myCompNamesList.removeLast();
717 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
718 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
720 foreach(QTableWidget* aDataTbl, myDataTblList) {
721 aDataTbl->setColumnCount(theVal + 1);
722 updateHeaders(aDataTbl);
723 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
724 for (int j = 0; j < aNbRows; j++) {
725 aItem = aDataTbl->item(j, i + 1);
727 aItem = createDefaultItem();
729 aItem->setBackgroundColor(Qt::lightGray);
730 aDataTbl->setItem(j, i + 1, aItem);
735 emit valuesChanged();
738 //**********************************************************************************
739 void CollectionPlugin_WidgetField::onAddStep()
741 int aMax = myStepSlider->maximum();
743 myStepSlider->setMaximum(aMax);
744 myMaxLbl->setText(QString::number(aMax));
745 appendStepControls();
746 myStepSlider->setValue(aMax);
748 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
749 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
752 AttributeSelectionListPtr aSelList =
753 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
754 if (!aSelList->isInitialized())
756 int aSelNb = aSelList->size();
760 int aColumns = myNbComponentsSpn->value() + 1;
761 int aRows = aSelNb + 1;
762 QTableWidget* aTable = myDataTblList.last();
763 aTable->setRowCount(aRows);
764 QTableWidgetItem* aItem = 0;
765 for(int i = 0; i < aColumns; i++) {
767 for(int j = 1; j < aRows; j++) {
768 aItem = aTable->item(j, i);
770 aItem = new QTableWidgetItem();
771 aTable->setItem(j, i, aItem);
773 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
774 aItem->setText(aAttr->namingName().c_str());
775 aItem->setToolTip(aAttr->namingName().c_str());
778 QString aDefVal = aTable->item(0, i)->text();
779 for(int j = 1; j < aRows; j++) {
780 aItem = aTable->item(j, i);
782 aItem = new QTableWidgetItem();
783 aTable->setItem(j, i, aItem);
785 aItem->setText(aDefVal);
789 emit valuesChanged();
792 //**********************************************************************************
793 void CollectionPlugin_WidgetField::onRemoveStep()
795 int aMax = myStepSlider->maximum();
797 myMaxLbl->setText(QString::number(aMax));
798 removeStepControls();
799 myStepSlider->setMaximum(aMax);
801 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
802 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
803 emit valuesChanged();
806 //**********************************************************************************
807 void CollectionPlugin_WidgetField::clearData()
809 foreach(QTableWidget* aDataTbl, myDataTblList) {
810 aDataTbl->setRowCount(1);
814 //**********************************************************************************
815 void CollectionPlugin_WidgetField::onStepMove(int theStep)
817 myCurStepLbl->setText(QString::number(theStep));
818 myStepWgt->setCurrentIndex(theStep - 1);
821 //**********************************************************************************
822 bool CollectionPlugin_WidgetField::
823 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
825 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
828 //**********************************************************************************
829 bool CollectionPlugin_WidgetField::
830 setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
833 myActivation = false;
836 // Ignore selection for Parts mode
837 if (myShapeTypeCombo->currentIndex() == 5)
840 QList<ModuleBase_ViewerPrsPtr> aSelected;
841 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
842 for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
843 ModuleBase_ViewerPrsPtr aValue = *anIt;
844 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
845 if (theToValidate && aRes.get()) {
846 if (myShapeTypeCombo->currentIndex() > 3)
847 aSelected.append(aValue);
848 else if (acceptSubShape(aValue->shape(), aRes))
849 aSelected.append(aValue);
852 AttributeSelectionListPtr aSelList =
853 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
854 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
860 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
861 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
862 aShape = aPrs->shape();
863 if (!aResult.get() && !aShape.get())
865 if (!aSelList->isInList(aResult, aShape)) {
866 aSelList->append(aResult, aShape);
870 int aColumns = myDataTblList.first()->columnCount();
871 int aRows = myDataTblList.first()->rowCount();
872 int aNewRows = aNbData + 1;
873 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
874 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
876 QTableWidgetItem* aItem = 0;
877 foreach(QTableWidget* aTable, myDataTblList) {
878 aTable->setRowCount(aNewRows);
879 if (aNewRows > aRows) {
881 for(int i = 0; i < aColumns; i++) {
883 for(int j = 1; j < aNewRows; j++) {
884 aItem = aTable->item(j, i);
886 aItem = new QTableWidgetItem();
887 aTable->setItem(j, i, aItem);
889 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
890 aItem->setText(aAttr->namingName().c_str());
891 aItem->setToolTip(aAttr->namingName().c_str());
894 QString aDefVal = aTable->item(0, i)->text();
895 for(int j = aRows; j < aNewRows; j++) {
896 aItem = aTable->item(j, i);
898 aItem = new QTableWidgetItem();
899 aTable->setItem(j, i, aItem);
901 aItem->setText(aDefVal);
906 // Update only selection name
907 for(int j = 1; j < aNewRows - 1; j++) {
908 AttributeSelectionPtr aAttr = aSelList->value(j);
909 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
910 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
917 //**********************************************************************************
918 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
920 ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
921 if (aOldType != theIdx) {
922 myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
923 int aColumns = myDataTblList.first()->columnCount();
924 int aRows = myDataTblList.first()->rowCount();
925 foreach(QTableWidget* aTable, myDataTblList) {
926 for(int i = 1; i < aColumns; i++) {
927 for(int j = 0; j < aRows; j++) {
929 case ModelAPI_AttributeTables::DOUBLE:
930 case ModelAPI_AttributeTables::INTEGER:
931 aTable->item(j, i)->setText("0");
933 case ModelAPI_AttributeTables::BOOLEAN:
934 aTable->item(j, i)->setText(MYFalse);
936 case ModelAPI_AttributeTables::STRING:
937 aTable->item(j, i)->setText("");
943 emit valuesChanged();
947 //**********************************************************************************
948 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
950 // Do not store here column of names
953 if (!myFeature.get())
955 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
956 int aNb = myDataTblList.indexOf(aTable);
959 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
961 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
962 if (aTablesAttr->isInitialized())
963 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
965 emit valuesChanged();
968 //**********************************************************************************
969 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
971 activateSelectionAndFilters(theType == 5? false:true);
973 AttributeSelectionListPtr aSelList =
974 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
976 std::string aTypeName = getSelectionType(theType);
977 if (aTypeName == aSelList->selectionType())
979 aSelList->setSelectionType(aTypeName);
981 //Clear old selection
984 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
985 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
986 emit valuesChanged();
989 //**********************************************************************************
990 bool CollectionPlugin_WidgetField::processEnter()
996 QWidget* aCurrWgt = qApp->focusWidget();
997 int aCurWgtId = myStepWgt->currentIndex();
998 if ((aCurrWgt == myShapeTypeCombo) ||
999 (aCurrWgt == myFieldTypeCombo) ||
1000 (aCurrWgt == myNbComponentsSpn) ||
1001 (aCurrWgt == myStampSpnList[aCurWgtId]) ||
1002 (aCurrWgt == myDataTblList[aCurWgtId])) {
1009 //**********************************************************************************
1010 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
1012 if (theNew && (!myIsTabEdit))
1013 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
1016 //**********************************************************************************
1017 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
1019 myMaxLbl->setText(QString::number(theMax));
1020 myRemoveBtn->setEnabled(theMax > 1);
1023 //**********************************************************************************
1024 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
1026 if (myDataTblList.count() < 2)
1028 QObject* aSender = sender();
1029 foreach(QTableWidget* aTable, myDataTblList) {
1030 if (aTable->horizontalHeader() != aSender)
1031 aTable->setColumnWidth(theIndex, theNew);
1035 //**********************************************************************************
1036 QList<std::shared_ptr<ModuleBase_ViewerPrs>>
1037 CollectionPlugin_WidgetField::getAttributeSelection() const
1039 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
1041 DataPtr aData = myFeature->data();
1042 AttributeSelectionListPtr aSelList =
1043 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
1044 AttributeSelectionPtr aAttr;
1046 for (int i = 0; i < aSelList->size(); i++) {
1047 aAttr = aSelList->value(i);
1048 ModuleBase_ViewerPrsPtr
1049 aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));