1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: CollectionPlugin_WidgetField.cpp
4 // Created: 16 Nov 2016
5 // Author: Vitaly SMETANNIKOV
7 #include "CollectionPlugin_WidgetField.h"
8 #include "CollectionPlugin_Field.h"
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_IModule.h>
13 #include <ModuleBase_ISelection.h>
14 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_AttributeStringArray.h>
18 #include <ModelAPI_AttributeInteger.h>
19 #include <ModelAPI_AttributeIntArray.h>
23 #include <QFormLayout>
28 #include <QTableWidget>
29 #include <QPushButton>
30 #include <QHeaderView>
31 #include <QStackedWidget>
35 #include <QMouseEvent>
37 #include <QApplication>
39 const char* MYFirstCol = "Shape";
40 const char* MYTrue = "True";
41 const char* MYFalse = "False";
43 DataTableItemDelegate::DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType)
44 : QStyledItemDelegate(), myType(theType)
49 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
50 const QStyleOptionViewItem & theOption,
51 const QModelIndex& theIndex ) const
54 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
55 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
56 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
57 aEdt->setReadOnly(true);
60 QLineEdit* aLineEdt = 0;
62 case ModelAPI_AttributeTables::DOUBLE:
63 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
67 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
71 case ModelAPI_AttributeTables::INTEGER:
72 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
76 aLineEdt->setValidator(new QIntValidator(aLineEdt));
80 case ModelAPI_AttributeTables::BOOLEAN:
82 QComboBox* aBox = new QComboBox(theParent);
83 aBox->addItem(MYFalse);
84 aBox->addItem(MYTrue);
89 aEditor = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
92 QObject* aThat = (QObject*) this;
93 aEditor->installEventFilter(aThat);
97 //bool DataTableItemDelegate::eventFilter(QObject* theObj, QEvent* theEvent)
99 // qDebug("### Type = %i", theEvent->type());
100 // if (theEvent->type() == QEvent::Close) {
101 // QWidget* aWgt = dynamic_cast<QWidget*>(theObj);
104 // return QStyledItemDelegate::eventFilter(theObj, theEvent);
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);
186 new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
187 myFieldTypeCombo->currentIndex());
189 appendStepControls();
192 QWidget* aBtnWgt = new QWidget(this);
193 aMainLayout->addWidget(aBtnWgt);
194 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
195 aBtnLayout->setContentsMargins(0, 0, 0, 0);
197 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
198 aBtnLayout->addWidget(aAddBtn);
200 aBtnLayout->addStretch(1);
202 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
203 aBtnLayout->addWidget(myRemoveBtn);
204 myRemoveBtn->setEnabled(false);
206 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
207 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
208 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
209 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
210 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
211 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
212 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
213 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
216 //**********************************************************************************
217 void CollectionPlugin_WidgetField::appendStepControls()
219 QWidget* aWidget = new QWidget(myStepWgt);
220 QGridLayout* aStepLayout = new QGridLayout(aWidget);
221 aStepLayout->setContentsMargins(0, 0, 0, 0);
223 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
225 QSpinBox* aStampSpn = new QSpinBox(aWidget);
226 aStepLayout->addWidget(aStampSpn, 0, 1);
228 myStampSpnList.append(aStampSpn);
231 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
232 aDataTbl->installEventFilter(this);
233 aDataTbl->setItemDelegate(myDelegate);
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 myDataTblList.append(aDataTbl);
243 aDataTbl->verticalHeader()->hide();
244 aDataTbl->setRowHeight(0, 25);
245 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
247 connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
248 SLOT(onColumnResize(int, int, int)));
250 updateHeaders(aDataTbl);
252 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
253 aItem->setBackgroundColor(Qt::lightGray);
254 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
255 aDataTbl->setItem(0, 0, aItem);
257 // Set default value item
258 for (int i = 0; i < myCompNamesList.count(); i++) {
259 aItem = createDefaultItem();
260 aItem->setBackgroundColor(Qt::lightGray);
261 aDataTbl->setItem(0, i + 1, aItem);
264 if (aColWidth.length() > 0) {
265 for (int i = 0; i < aDataTbl->columnCount(); i++) {
266 if (i < aColWidth.size())
267 aDataTbl->setColumnWidth(i, aColWidth.at(i));
270 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
271 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
273 myStepWgt->addWidget(aWidget);
274 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
277 //**********************************************************************************
278 void CollectionPlugin_WidgetField::deactivate()
280 ModuleBase_WidgetSelector::deactivate();
284 //**********************************************************************************
285 //void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
287 // myShapeTypeCombo->setEnabled(!isEditingMode());
288 // myFieldTypeCombo->setEnabled(!isEditingMode());
289 // myNbComponentsSpn->setEnabled(!isEditingMode());
292 //**********************************************************************************
293 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
295 QObject* aObject = 0;
296 foreach(QTableWidget* aTable, myDataTblList) {
297 if (aTable->horizontalHeader()->viewport() == theObject) {
303 if (theEvent->type() == QEvent::MouseButtonDblClick) {
304 if (myHeaderEditor) { //delete previous editor
305 myHeaderEditor->deleteLater();
308 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
309 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
310 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
312 int aShift = aTable->horizontalScrollBar()->value();
313 int aPos = aMouseEvent->x();
314 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
317 aRect.setLeft(aHeader->sectionPosition(aIndex));
318 aRect.setWidth(aHeader->sectionSize(aIndex));
320 aRect.setHeight(aHeader->height());
321 aRect.adjust(1, 1, -1, -1);
322 aRect.translate(-aShift, 0);
324 myHeaderEditor = new QLineEdit(aHeader->viewport());
325 myHeaderEditor->move(aRect.topLeft());
326 myHeaderEditor->resize(aRect.size());
327 myHeaderEditor->setFrame(false);
328 QString aText = aHeader->model()->
329 headerData(aIndex, aHeader->orientation()).toString();
330 myHeaderEditor->setText(aText);
331 myHeaderEditor->setFocus();
332 myEditIndex = aIndex; //save for future use
333 myHeaderEditor->installEventFilter(this); //catch focus out event
334 //if user presses Enter it should close editor
335 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
336 myHeaderEditor->show();
340 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
341 QString aNewTitle = myHeaderEditor->text();
343 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
344 myHeaderEditor->deleteLater(); //safely delete editor
346 // Store into data model
347 AttributeStringArrayPtr aStringsAttr =
348 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
349 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
350 foreach(QTableWidget* aTable, myDataTblList) {
351 updateHeaders(aTable);
353 } else if (theEvent->type() == QEvent::FocusIn) {
354 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
356 ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
357 if (aPanel->activeWidget() != this) {
359 aPanel->activateWidget(this, false);
363 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
366 //**********************************************************************************
367 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
369 QTableWidgetItem* aItem = new QTableWidgetItem();
370 switch (myFieldTypeCombo->currentIndex()) {
371 case ModelAPI_AttributeTables::DOUBLE:
372 case ModelAPI_AttributeTables::INTEGER:
375 case ModelAPI_AttributeTables::BOOLEAN:
376 aItem->setText(MYFalse);
378 case ModelAPI_AttributeTables::STRING:
385 //**********************************************************************************
386 QTableWidgetItem* CollectionPlugin_WidgetField::
387 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
389 QTableWidgetItem* aItem = new QTableWidgetItem();
390 aItem->setText(getValueText(theVal));
394 //**********************************************************************************
395 QString CollectionPlugin_WidgetField::getValueText(ModelAPI_AttributeTables::Value& theVal) const
397 switch (myFieldTypeCombo->currentIndex()) {
398 case ModelAPI_AttributeTables::DOUBLE:
399 return QString::number(theVal.myDouble);
400 case ModelAPI_AttributeTables::INTEGER:
401 return QString::number(theVal.myInt);
402 case ModelAPI_AttributeTables::BOOLEAN:
403 return theVal.myBool? MYTrue : MYFalse;
404 case ModelAPI_AttributeTables::STRING:
405 return theVal.myStr.c_str();
411 //**********************************************************************************
412 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
414 QStringList aHeaders;
415 aHeaders << tr(MYFirstCol);
416 aHeaders << myCompNamesList;
417 theDataTbl->setHorizontalHeaderLabels(aHeaders);
420 //**********************************************************************************
421 void CollectionPlugin_WidgetField::removeStepControls()
423 int aCurWgtId = myStepWgt->currentIndex();
424 QWidget* aWgt = myStepWgt->currentWidget();
425 myStepWgt->removeWidget(aWgt);
427 myStampSpnList.removeAt(aCurWgtId);
428 myDataTblList.removeAt(aCurWgtId);
432 //**********************************************************************************
433 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
435 QList<QWidget*> aControls;
436 // this control will accept focus and will be highlighted in the Property Panel
437 aControls.append(myShapeTypeCombo);
438 aControls.append(myFieldTypeCombo);
439 aControls.append(myNbComponentsSpn);
440 if (myStampSpnList.size() > 0)
441 aControls.append(myStampSpnList.first());
442 if (myDataTblList.size() > 0)
443 aControls.append(myDataTblList.first());
448 //**********************************************************************************
449 bool CollectionPlugin_WidgetField::storeValueCustom()
451 DataPtr aData = myFeature->data();
452 // Store number of components
453 AttributeStringArrayPtr aStringsAttr =
454 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
455 int aNbComps = myCompNamesList.size();
456 aStringsAttr->setSize(aNbComps);
457 for ( int i = 0; i < aNbComps; i++)
458 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
460 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
461 // Store number of steps
462 int aNbSteps = myDataTblList.size();
464 // Store Type of the field values
465 int aFldType = myFieldTypeCombo->currentIndex();
467 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
468 aStampsAttr->setSize(aNbSteps);
470 QTableWidget* aTable = myDataTblList.first();
471 int aRows = aTable->rowCount();
472 // first column contains selected names which should not be stored
473 int aColumns = aTable->columnCount() - 1;
475 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
476 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
477 for (int i = 0; i < aNbSteps; i++) {
478 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
479 aTable = myDataTblList.at(i);
480 for (int j = 0; j < aColumns; j++) {
481 for (int k = 0; k < aRows; k++) {
482 QString aTblVal = aTable->item(k, j + 1)->text();
483 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
487 updateObject(myFeature);
491 //**********************************************************************************
492 bool CollectionPlugin_WidgetField::restoreValueCustom()
495 DataPtr aData = myFeature->data();
497 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
498 std::string aTypeStr = aSelList->selectionType();
500 return false; // The attribute is not initialized
501 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
503 // Get number of components
504 AttributeStringArrayPtr aStringsAttr =
505 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
507 myCompNamesList.clear();
508 for (int i = 0; i < aStringsAttr->size(); i++) {
509 myCompNamesList.append(aStringsAttr->value(i).c_str());
511 isBlocked = myNbComponentsSpn->blockSignals(true);
512 myNbComponentsSpn->setValue(myCompNamesList.size());
513 myNbComponentsSpn->blockSignals(isBlocked);
515 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
516 // Get number of steps
517 int aNbSteps = aTablesAttr->tables();
518 myStepSlider->setMaximum(aNbSteps);
519 //myStepSlider->setValue(1);
521 while (myDataTblList.count() > aNbSteps) {
522 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
523 myStepWgt->removeWidget(aWgt);
526 myStampSpnList.removeLast();
527 myDataTblList.removeLast();
529 while (myDataTblList.count() < aNbSteps)
530 appendStepControls();
531 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
534 // Get Type of the field values
535 isBlocked = myFieldTypeCombo->blockSignals(true);
536 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
537 myFieldTypeCombo->blockSignals(isBlocked);
538 myDelegate->setDataType(aTablesAttr->type());
540 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
542 int aRows = aTablesAttr->rows();
543 int aCols = aTablesAttr->columns();
545 // Get width of columns
547 QTableWidget* aFirstTable = myDataTblList.first();
548 for (int i = 0; i < aFirstTable->columnCount(); i++)
549 aColWidth.append(aFirstTable->columnWidth(i));
551 QTableWidgetItem* aItem = 0;
552 for (int i = 0; i < aNbSteps; i++) {
553 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
554 QTableWidget* aTable = myDataTblList.at(i);
555 isBlocked = aTable->blockSignals(true);
556 aTable->setRowCount(aRows);
557 aTable->setColumnCount(aCols + 1);
558 updateHeaders(aTable);
559 for (int j = 0; j < aCols + 1; j++) {
560 for (int k = 0; k < aRows; k++) {
561 aItem = aTable->item(k, j);
562 if ((j == 0) && (k > 0)) {
563 // Add selection names
564 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
566 aItem->setText(aAttr->namingName().c_str());
568 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
569 aTable->setItem(k, j, aItem);
573 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
575 aItem->setText(getValueText(aVal));
577 aItem = createValueItem(aVal);
579 aItem->setBackgroundColor(Qt::lightGray);
580 aTable->setItem(k, j, aItem);
585 // Restore columns width
586 for (int i = 0; i < aTable->columnCount(); i++) {
587 if (i < aColWidth.size())
588 aTable->setColumnWidth(i, aColWidth.at(i));
591 aTable->blockSignals(isBlocked);
596 //**********************************************************************************
597 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
599 QString aType(theStr.c_str());
600 aType = aType.toLower();
601 if (aType == "vertex")
603 else if (aType == "edge")
605 else if (aType == "face")
607 else if (aType == "solid")
609 else if (aType == "object")
611 else if (aType == "part")
617 //**********************************************************************************
618 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
637 //**********************************************************************************
638 QIntList CollectionPlugin_WidgetField::shapeTypes() const
641 switch (myShapeTypeCombo->currentIndex()) {
643 aRes.append(ModuleBase_Tools::shapeType("vertex"));
646 aRes.append(ModuleBase_Tools::shapeType("edge"));
649 aRes.append(ModuleBase_Tools::shapeType("face"));
652 aRes.append(ModuleBase_Tools::shapeType("solid"));
655 aRes.append(ModuleBase_Tools::shapeType("object"));
658 // TODO: Selection mode for Parts
664 //**********************************************************************************
665 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
667 ModelAPI_AttributeTables::Value aVal;
668 switch (myFieldTypeCombo->currentIndex()) {
669 case ModelAPI_AttributeTables::BOOLEAN:
670 aVal.myBool = (theStrVal == MYTrue)? true : false;
672 case ModelAPI_AttributeTables::DOUBLE:
673 aVal.myDouble = theStrVal.toDouble();
675 case ModelAPI_AttributeTables::INTEGER:
676 aVal.myInt = theStrVal.toInt();
678 case ModelAPI_AttributeTables::STRING:
679 aVal.myStr = theStrVal.toStdString();
685 //**********************************************************************************
686 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
688 int aOldCol = myCompNamesList.count();
689 int aNbRows = myDataTblList.first()->rowCount();
690 int aDif = theVal - aOldCol;
691 QTableWidgetItem* aItem = 0;
693 while (myCompNamesList.count() != theVal) {
695 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
697 myCompNamesList.removeLast();
700 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
701 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
703 foreach(QTableWidget* aDataTbl, myDataTblList) {
704 aDataTbl->setColumnCount(theVal + 1);
705 updateHeaders(aDataTbl);
706 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
707 for (int j = 0; j < aNbRows; j++) {
708 aItem = aDataTbl->item(j, i + 1);
710 aItem = createDefaultItem();
712 aItem->setBackgroundColor(Qt::lightGray);
713 aDataTbl->setItem(j, i + 1, aItem);
718 emit valuesChanged();
721 //**********************************************************************************
722 void CollectionPlugin_WidgetField::onAddStep()
724 int aMax = myStepSlider->maximum();
726 myStepSlider->setMaximum(aMax);
727 myMaxLbl->setText(QString::number(aMax));
728 appendStepControls();
729 myStepSlider->setValue(aMax);
731 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
732 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
735 AttributeSelectionListPtr aSelList =
736 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
737 if (!aSelList->isInitialized())
739 int aSelNb = aSelList->size();
743 int aColumns = myNbComponentsSpn->value() + 1;
744 int aRows = aSelNb + 1;
745 QTableWidget* aTable = myDataTblList.last();
746 aTable->setRowCount(aRows);
747 QTableWidgetItem* aItem = 0;
748 for(int i = 0; i < aColumns; i++) {
750 for(int j = 1; j < aRows; j++) {
751 aItem = aTable->item(j, i);
753 aItem = new QTableWidgetItem();
754 aTable->setItem(j, i, aItem);
756 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
757 aItem->setText(aAttr->namingName().c_str());
758 aItem->setToolTip(aAttr->namingName().c_str());
761 QString aDefVal = aTable->item(0, i)->text();
762 for(int j = 1; j < aRows; j++) {
763 aItem = aTable->item(j, i);
765 aItem = new QTableWidgetItem();
766 aTable->setItem(j, i, aItem);
768 aItem->setText(aDefVal);
772 emit valuesChanged();
775 //**********************************************************************************
776 void CollectionPlugin_WidgetField::onRemoveStep()
778 int aMax = myStepSlider->maximum();
780 myMaxLbl->setText(QString::number(aMax));
781 removeStepControls();
782 myStepSlider->setMaximum(aMax);
784 //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
785 //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
786 emit valuesChanged();
789 //**********************************************************************************
790 void CollectionPlugin_WidgetField::clearData()
792 foreach(QTableWidget* aDataTbl, myDataTblList) {
793 aDataTbl->setRowCount(1);
797 //**********************************************************************************
798 void CollectionPlugin_WidgetField::onStepMove(int theStep)
800 myCurStepLbl->setText(QString::number(theStep));
801 myStepWgt->setCurrentIndex(theStep - 1);
804 //**********************************************************************************
805 bool CollectionPlugin_WidgetField::
806 isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
808 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
811 //**********************************************************************************
812 bool CollectionPlugin_WidgetField::
813 setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
816 myActivation = false;
819 // Ignore selection for Parts mode
820 if (myShapeTypeCombo->currentIndex() == 5)
823 QList<ModuleBase_ViewerPrsPtr> aSelected;
824 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
825 for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
826 ModuleBase_ViewerPrsPtr aValue = *anIt;
827 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
828 if (theToValidate && aRes.get() && acceptSubShape(aValue->shape(), aRes))
829 aSelected.append(aValue);
831 AttributeSelectionListPtr aSelList =
832 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
833 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
839 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
840 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
841 aShape = aPrs->shape();
842 if (!aResult.get() && !aShape.get())
844 if (!aSelList->isInList(aResult, aShape)) {
845 aSelList->append(aResult, aShape);
849 int aColumns = myDataTblList.first()->columnCount();
850 int aRows = myDataTblList.first()->rowCount();
851 int aNewRows = aNbData + 1;
852 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
853 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
855 QTableWidgetItem* aItem = 0;
856 foreach(QTableWidget* aTable, myDataTblList) {
857 aTable->setRowCount(aNewRows);
858 if (aNewRows > aRows) {
860 for(int i = 0; i < aColumns; i++) {
862 for(int j = 1; j < aNewRows; j++) {
863 aItem = aTable->item(j, i);
865 aItem = new QTableWidgetItem();
866 aTable->setItem(j, i, aItem);
868 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
869 aItem->setText(aAttr->namingName().c_str());
870 aItem->setToolTip(aAttr->namingName().c_str());
873 QString aDefVal = aTable->item(0, i)->text();
874 for(int j = aRows; j < aNewRows; j++) {
875 aItem = aTable->item(j, i);
877 aItem = new QTableWidgetItem();
878 aTable->setItem(j, i, aItem);
880 aItem->setText(aDefVal);
885 // Update only selection name
886 for(int j = 1; j < aNewRows - 1; j++) {
887 AttributeSelectionPtr aAttr = aSelList->value(j);
888 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
889 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
896 //**********************************************************************************
897 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
899 ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
900 if (aOldType != theIdx) {
901 myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
902 int aColumns = myDataTblList.first()->columnCount();
903 int aRows = myDataTblList.first()->rowCount();
904 foreach(QTableWidget* aTable, myDataTblList) {
905 for(int i = 1; i < aColumns; i++) {
906 for(int j = 0; j < aRows; j++) {
908 case ModelAPI_AttributeTables::DOUBLE:
909 case ModelAPI_AttributeTables::INTEGER:
910 aTable->item(j, i)->setText("0");
912 case ModelAPI_AttributeTables::BOOLEAN:
913 aTable->item(j, i)->setText(MYFalse);
915 case ModelAPI_AttributeTables::STRING:
916 aTable->item(j, i)->setText("");
922 emit valuesChanged();
926 //**********************************************************************************
927 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
929 // Do not store here column of names
932 if (!myFeature.get())
934 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
935 int aNb = myDataTblList.indexOf(aTable);
938 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
940 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
941 if (aTablesAttr->isInitialized())
942 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
944 emit valuesChanged();
947 //**********************************************************************************
948 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
950 activateSelectionAndFilters(theType == 5? false:true);
952 AttributeSelectionListPtr aSelList =
953 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
955 std::string aTypeName = getSelectionType(theType);
956 if (aTypeName == aSelList->selectionType())
958 aSelList->setSelectionType(aTypeName);
960 //Clear old selection
963 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
964 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
965 emit valuesChanged();
968 //**********************************************************************************
969 bool CollectionPlugin_WidgetField::processEnter()
975 QWidget* aCurrWgt = qApp->focusWidget();
976 int aCurWgtId = myStepWgt->currentIndex();
977 if ((aCurrWgt == myShapeTypeCombo) ||
978 (aCurrWgt == myFieldTypeCombo) ||
979 (aCurrWgt == myNbComponentsSpn) ||
980 (aCurrWgt == myStampSpnList[aCurWgtId]) ||
981 (aCurrWgt == myDataTblList[aCurWgtId])) {
988 //**********************************************************************************
989 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
991 if (theNew && (!myIsTabEdit))
992 myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
995 //**********************************************************************************
996 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
998 myMaxLbl->setText(QString::number(theMax));
999 myRemoveBtn->setEnabled(theMax > 1);
1002 //**********************************************************************************
1003 void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int theNew)
1005 if (myDataTblList.count() < 2)
1007 QObject* aSender = sender();
1008 foreach(QTableWidget* aTable, myDataTblList) {
1009 if (aTable->horizontalHeader() != aSender)
1010 aTable->setColumnWidth(theIndex, theNew);
1014 //**********************************************************************************
1015 QList<std::shared_ptr<ModuleBase_ViewerPrs>>
1016 CollectionPlugin_WidgetField::getAttributeSelection() const
1018 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
1020 DataPtr aData = myFeature->data();
1021 AttributeSelectionListPtr aSelList =
1022 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
1023 AttributeSelectionPtr aAttr;
1025 for (int i = 0; i < aSelList->size(); i++) {
1026 aAttr = aSelList->value(i);
1027 ModuleBase_ViewerPrsPtr
1028 aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));