1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: CollectionPlugin_WidgetField.cpp
4 // Created: 16 Nov 2016
5 // Author: Vitaly SMETANNIKOV
7 #include "CollectionPlugin_WidgetField.h"
8 #include "CollectionPlugin_Field.h"
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_ISelection.h>
14 #include <XGUI_Tools.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_PropertyPanel.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_AttributeStringArray.h>
20 #include <ModelAPI_AttributeInteger.h>
21 #include <ModelAPI_AttributeIntArray.h>
25 #include <QFormLayout>
30 #include <QTableWidget>
31 #include <QPushButton>
32 #include <QHeaderView>
33 #include <QStackedWidget>
35 #include <QStyledItemDelegate>
38 #include <QMouseEvent>
40 #include <QApplication>
42 const char* MYFirstCol = "Shape";
43 const char* MYTrue = "True";
44 const char* MYFalse = "False";
46 class DataTableItemDelegate : public QStyledItemDelegate
49 DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType) :
50 QStyledItemDelegate() { myType = theType; }
52 virtual QWidget* createEditor(QWidget* theParent,
53 const QStyleOptionViewItem & theOption,
54 const QModelIndex& theIndex) const;
56 ModelAPI_AttributeTables::ValueType dataType() const { return myType; }
58 void setDataType(ModelAPI_AttributeTables::ValueType theType) { myType = theType; }
64 ModelAPI_AttributeTables::ValueType myType;
67 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
68 const QStyleOptionViewItem & theOption,
69 const QModelIndex& theIndex ) const
71 if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
72 QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
73 QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
74 aEdt->setReadOnly(true);
77 QLineEdit* aLineEdt = 0;
79 case ModelAPI_AttributeTables::DOUBLE:
80 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
84 aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
88 case ModelAPI_AttributeTables::INTEGER:
89 aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent,
93 aLineEdt->setValidator(new QIntValidator(aLineEdt));
97 case ModelAPI_AttributeTables::BOOLEAN:
99 QComboBox* aBox = new QComboBox(theParent);
100 aBox->addItem(MYFalse);
101 aBox->addItem(MYTrue);
106 return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
111 //**********************************************************************************
112 //**********************************************************************************
113 //**********************************************************************************
114 CollectionPlugin_WidgetField::
115 CollectionPlugin_WidgetField(QWidget* theParent,
116 ModuleBase_IWorkshop* theWorkshop,
117 const Config_WidgetAPI* theData):
118 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
119 myIsEditing(false), myActivation(false)
121 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
123 // Types definition controls
124 QWidget* aTypesWgt = new QWidget(this);
125 QFormLayout* aTypesLayout = new QFormLayout(aTypesWgt);
126 aTypesLayout->setContentsMargins(0, 0, 0, 0);
127 aMainLayout->addWidget(aTypesWgt);
130 myShapeTypeCombo = new QComboBox(aTypesWgt);
131 QStringList aShapeTypes;
132 aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces")
133 << tr("Solids") << tr("Objects") << tr("Parts");
134 myShapeTypeCombo->addItems(aShapeTypes);
135 aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo);
138 myFieldTypeCombo = new QComboBox(aTypesWgt);
139 QStringList aFieldTypes;
140 aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double")
142 myFieldTypeCombo->addItems(aFieldTypes);
143 myFieldTypeCombo->setCurrentIndex(2);
144 aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo);
146 // Number of components
147 myNbComponentsSpn = new QSpinBox(aTypesWgt);
148 myNbComponentsSpn->setMinimum(1);
149 aTypesLayout->addRow(tr("Nb. of components"), myNbComponentsSpn);
152 QFrame* aStepFrame = new QFrame(this);
153 aStepFrame->setFrameShape(QFrame::Box);
154 aStepFrame->setFrameStyle(QFrame::StyledPanel);
155 QGridLayout* aStepLayout = new QGridLayout(aStepFrame);
156 aMainLayout->addWidget(aStepFrame);
158 // Current step label
159 aStepLayout->addWidget(new QLabel(tr("Current step"), aStepFrame), 0, 0);
160 myCurStepLbl = new QLabel("1", aStepFrame);
161 QFont aFont = myCurStepLbl->font();
163 myCurStepLbl->setFont(aFont);
164 aStepLayout->addWidget(myCurStepLbl, 0, 1);
167 QWidget* aSliderWidget = new QWidget(aStepFrame);
168 aStepLayout->addWidget(aSliderWidget, 1, 0, 1, 2);
169 QHBoxLayout* aSliderLayout = new QHBoxLayout(aSliderWidget);
170 aSliderLayout->setContentsMargins(0, 0, 0, 0);
172 aSliderLayout->addWidget(new QLabel("1", aSliderWidget));
174 myStepSlider = new QSlider(Qt::Horizontal, aSliderWidget);
175 myStepSlider->setTickPosition(QSlider::TicksBelow);
176 myStepSlider->setRange(1, 1);
177 myStepSlider->setPageStep(myStepSlider->singleStep());
178 aSliderLayout->addWidget(myStepSlider, 1);
180 myMaxLbl = new QLabel("1", aSliderWidget);
181 aSliderLayout->addWidget(myMaxLbl);
184 myCompNamesList << "Comp 1";
185 myStepWgt = new QStackedWidget(aStepFrame);
186 aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
187 appendStepControls();
190 QWidget* aBtnWgt = new QWidget(this);
191 aMainLayout->addWidget(aBtnWgt);
192 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
193 aBtnLayout->setContentsMargins(0, 0, 0, 0);
195 QPushButton* aAddBtn = new QPushButton(tr("Add step"), aBtnWgt);
196 aBtnLayout->addWidget(aAddBtn);
198 aBtnLayout->addStretch(1);
200 myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
201 aBtnLayout->addWidget(myRemoveBtn);
202 myRemoveBtn->setEnabled(false);
204 connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
205 connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
206 connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
207 connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
208 connect(myStepSlider, SIGNAL(rangeChanged(int, int)), SLOT(onRangeChanged(int, int)));
209 connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
210 connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int)));
211 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(onFocusChanged(QWidget*, QWidget*)));
214 //**********************************************************************************
215 void CollectionPlugin_WidgetField::appendStepControls()
217 QWidget* aWidget = new QWidget(myStepWgt);
218 QGridLayout* aStepLayout = new QGridLayout(aWidget);
219 aStepLayout->setContentsMargins(0, 0, 0, 0);
221 aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
223 QSpinBox* aStampSpn = new QSpinBox(aWidget);
224 aStepLayout->addWidget(aStampSpn, 0, 1);
226 myStampSpnList.append(aStampSpn);
229 QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
230 aDataTbl->installEventFilter(this);
231 DataTableItemDelegate* aDelegate = 0;
232 if (myDataTblList.isEmpty())
233 aDelegate = new DataTableItemDelegate(
234 (ModelAPI_AttributeTables::ValueType) myFieldTypeCombo->currentIndex());
236 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
238 aDataTbl->setItemDelegate(aDelegate);
239 myDataTblList.append(aDataTbl);
241 aDataTbl->verticalHeader()->hide();
242 aDataTbl->setRowHeight(0, 25);
243 aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
245 updateHeaders(aDataTbl);
247 QTableWidgetItem* aItem = new QTableWidgetItem("Default value");
248 aItem->setBackgroundColor(Qt::lightGray);
249 aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
250 aDataTbl->setItem(0, 0, aItem);
252 // Set default value item
253 for (int i = 0; i < myCompNamesList.count(); i++) {
254 aItem = createDefaultItem();
255 aItem->setBackgroundColor(Qt::lightGray);
256 aDataTbl->setItem(0, i + 1, aItem);
258 aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
259 connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
261 QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
262 myStepWgt->addWidget(aWidget);
263 aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
266 //**********************************************************************************
267 void CollectionPlugin_WidgetField::deactivate()
269 ModuleBase_WidgetSelector::deactivate();
274 //**********************************************************************************
275 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
277 QObject* aObject = 0;
278 foreach(QTableWidget* aTable, myDataTblList) {
279 if (aTable->horizontalHeader()->viewport() == theObject) {
285 if (theEvent->type() == QEvent::MouseButtonDblClick) {
286 if (myHeaderEditor) { //delete previous editor
287 myHeaderEditor->deleteLater();
290 QMouseEvent* aMouseEvent = static_cast<QMouseEvent*>(theEvent);
291 QHeaderView* aHeader = static_cast<QHeaderView*>(aObject->parent());
292 QTableWidget* aTable = static_cast<QTableWidget*>(aHeader->parentWidget());
294 int aShift = aTable->horizontalScrollBar()->value();
295 int aPos = aMouseEvent->x();
296 int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos));
299 aRect.setLeft(aHeader->sectionPosition(aIndex));
300 aRect.setWidth(aHeader->sectionSize(aIndex));
302 aRect.setHeight(aHeader->height());
303 aRect.adjust(1, 1, -1, -1);
304 aRect.translate(-aShift, 0);
306 myHeaderEditor = new QLineEdit(aHeader->viewport());
307 myHeaderEditor->move(aRect.topLeft());
308 myHeaderEditor->resize(aRect.size());
309 myHeaderEditor->setFrame(false);
310 QString aText = aHeader->model()->
311 headerData(aIndex, aHeader->orientation()).toString();
312 myHeaderEditor->setText(aText);
313 myHeaderEditor->setFocus();
314 myEditIndex = aIndex; //save for future use
315 myHeaderEditor->installEventFilter(this); //catch focus out event
316 //if user presses Enter it should close editor
317 connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus()));
318 myHeaderEditor->show();
322 } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) {
323 //QHeaderView* aHeader =
324 // static_cast<QHeaderView*>(myHeaderEditor->parentWidget()->parentWidget());
325 QString aNewTitle = myHeaderEditor->text();
327 //aHeader->model()->setHeaderData(myEditIndex, aHeader->orientation(), aNewTitle);
328 myCompNamesList.replace(myEditIndex - 1, aNewTitle);
329 myHeaderEditor->deleteLater(); //safely delete editor
331 // Store into data model
332 AttributeStringArrayPtr aStringsAttr =
333 myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
334 aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString());
335 foreach(QTableWidget* aTable, myDataTblList) {
336 updateHeaders(aTable);
338 } else if (theEvent->type() == QEvent::FocusIn) {
339 QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
341 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
342 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
343 if (aPanel->activeWidget() != this) {
345 aPanel->activateWidget(this, false);
349 return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent);
352 //**********************************************************************************
353 QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
355 QTableWidgetItem* aItem = new QTableWidgetItem();
356 switch (myFieldTypeCombo->currentIndex()) {
357 case ModelAPI_AttributeTables::DOUBLE:
358 case ModelAPI_AttributeTables::INTEGER:
361 case ModelAPI_AttributeTables::BOOLEAN:
362 aItem->setText(MYFalse);
368 //**********************************************************************************
369 QTableWidgetItem* CollectionPlugin_WidgetField::
370 createValueItem(ModelAPI_AttributeTables::Value& theVal) const
372 QTableWidgetItem* aItem = new QTableWidgetItem();
373 switch (myFieldTypeCombo->currentIndex()) {
374 case ModelAPI_AttributeTables::DOUBLE:
375 aItem->setText(QString::number(theVal.myDouble));
377 case ModelAPI_AttributeTables::INTEGER:
378 aItem->setText(QString::number(theVal.myInt));
380 case ModelAPI_AttributeTables::BOOLEAN:
381 aItem->setText(theVal.myBool? MYTrue : MYFalse);
383 case ModelAPI_AttributeTables::STRING:
384 aItem->setText(theVal.myStr.c_str());
389 //**********************************************************************************
390 void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const
392 QStringList aHeaders;
393 aHeaders << tr(MYFirstCol);
394 aHeaders << myCompNamesList;
395 theDataTbl->setHorizontalHeaderLabels(aHeaders);
398 //**********************************************************************************
399 void CollectionPlugin_WidgetField::removeStepControls()
401 int aCurWgtId = myStepWgt->currentIndex();
402 QWidget* aWgt = myStepWgt->currentWidget();
403 myStepWgt->removeWidget(aWgt);
405 myStampSpnList.removeAt(aCurWgtId);
406 myDataTblList.removeAt(aCurWgtId);
410 //**********************************************************************************
411 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
413 QList<QWidget*> aControls;
414 // this control will accept focus and will be highlighted in the Property Panel
415 aControls.push_back(myShapeTypeCombo);
416 aControls.push_back(myFieldTypeCombo);
417 aControls.push_back(myNbComponentsSpn);
421 //**********************************************************************************
422 bool CollectionPlugin_WidgetField::storeValueCustom()
424 DataPtr aData = myFeature->data();
425 // Store number of components
426 AttributeStringArrayPtr aStringsAttr =
427 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
428 int aNbComps = myCompNamesList.size();
429 aStringsAttr->setSize(aNbComps);
430 for ( int i = 0; i < aNbComps; i++)
431 aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString());
433 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
434 // Store number of steps
435 int aNbSteps = myDataTblList.size();
437 // Store Type of the field values
438 int aFldType = myFieldTypeCombo->currentIndex();
440 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
441 aStampsAttr->setSize(aNbSteps);
443 QTableWidget* aTable = myDataTblList.first();
444 int aRows = aTable->rowCount();
445 // first column contains selected names which should not be stored
446 int aColumns = aTable->columnCount() - 1;
448 aTablesAttr->setSize(aRows, aColumns, aNbSteps);
449 aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType);
450 for (int i = 0; i < aNbSteps; i++) {
451 aStampsAttr->setValue(i, myStampSpnList.at(i)->value());
452 aTable = myDataTblList.at(i);
453 for (int j = 0; j < aColumns; j++) {
454 for (int k = 0; k < aRows; k++) {
455 QString aTblVal = aTable->item(k, j + 1)->text();
456 aTablesAttr->setValue(getValue(aTblVal), k, j, i);
460 updateObject(myFeature);
464 //**********************************************************************************
465 bool CollectionPlugin_WidgetField::restoreValueCustom()
468 DataPtr aData = myFeature->data();
470 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
471 if (!aSelList->isInitialized())
473 std::string aTypeStr = aSelList->selectionType();
474 myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
476 // Get number of components
477 AttributeStringArrayPtr aStringsAttr =
478 aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID());
480 myCompNamesList.clear();
481 for (int i = 0; i < aStringsAttr->size(); i++) {
482 myCompNamesList.append(aStringsAttr->value(i).c_str());
484 isBlocked = myNbComponentsSpn->blockSignals(true);
485 myNbComponentsSpn->setValue(myCompNamesList.size());
486 myNbComponentsSpn->blockSignals(isBlocked);
488 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
489 // Get number of steps
490 int aNbSteps = aTablesAttr->tables();
491 myStepSlider->setMaximum(aNbSteps);
492 //myStepSlider->setValue(1);
494 myStampSpnList.clear();
495 myDataTblList.clear();
496 while (myStepWgt->count()) {
497 QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1);
498 myStepWgt->removeWidget(aWgt);
502 while (myStepWgt->count() < aNbSteps)
503 appendStepControls();
504 //myStepWgt->setCurrentIndex(myStepSlider->value() - 1);
506 // Get Type of the field values
507 myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
509 AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
511 int aRows = aTablesAttr->rows();
512 int aCols = aTablesAttr->columns();
514 QTableWidgetItem* aItem = 0;
515 for (int i = 0; i < aNbSteps; i++) {
516 myStampSpnList.at(i)->setValue(aStampsAttr->value(i));
517 QTableWidget* aTable = myDataTblList.at(i);
518 isBlocked = aTable->blockSignals(true);
519 aTable->setRowCount(aRows);
520 for (int j = 0; j < aCols + 1; j++) {
521 for (int k = 0; k < aRows; k++) {
522 if ((j == 0) && (k > 0)) {
523 // Add selection names
524 AttributeSelectionPtr aAttr = aSelList->value(k - 1);
525 aItem = new QTableWidgetItem(aAttr->namingName().c_str());
526 aTable->setItem(k, j, aItem);
529 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i);
530 aItem = createValueItem(aVal);
532 aItem->setBackgroundColor(Qt::lightGray);
533 aTable->setItem(k, j, aItem);
538 aTable->blockSignals(isBlocked);
543 //**********************************************************************************
544 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
546 if (theStr == "vertex")
548 else if (theStr == "edge")
550 else if (theStr == "face")
552 else if (theStr == "solid")
554 else if (theStr == "object")
556 else if (theStr == "part")
562 //**********************************************************************************
563 std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const
582 //**********************************************************************************
583 QIntList CollectionPlugin_WidgetField::shapeTypes() const
586 switch (myShapeTypeCombo->currentIndex()) {
588 aRes.append(ModuleBase_Tools::shapeType("vertex"));
591 aRes.append(ModuleBase_Tools::shapeType("edge"));
594 aRes.append(ModuleBase_Tools::shapeType("face"));
597 aRes.append(ModuleBase_Tools::shapeType("solid"));
600 aRes.append(ModuleBase_Tools::shapeType("object"));
603 // TODO: Selection mode for Parts
609 //**********************************************************************************
610 ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const
612 ModelAPI_AttributeTables::Value aVal;
613 switch (myFieldTypeCombo->currentIndex()) {
614 case ModelAPI_AttributeTables::BOOLEAN:
615 aVal.myBool = (theStrVal == MYTrue)? true : false;
617 case ModelAPI_AttributeTables::DOUBLE:
618 aVal.myDouble = theStrVal.toDouble();
620 case ModelAPI_AttributeTables::INTEGER:
621 aVal.myInt = theStrVal.toInt();
623 case ModelAPI_AttributeTables::STRING:
624 aVal.myStr = theStrVal.toStdString();
630 //**********************************************************************************
631 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
633 int aOldCol = myCompNamesList.count();
634 int aNbRows = myDataTblList.first()->rowCount();
635 int aDif = theVal - aOldCol;
636 QTableWidgetItem* aItem = 0;
638 while (myCompNamesList.count() != theVal) {
640 myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
642 myCompNamesList.removeLast();
645 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
646 aTablesAttr->setSize(aNbRows, myCompNamesList.size(), myDataTblList.size());
648 foreach(QTableWidget* aDataTbl, myDataTblList) {
649 aDataTbl->setColumnCount(theVal + 1);
650 updateHeaders(aDataTbl);
651 for (int i = aOldCol; i < myCompNamesList.count(); i++) {
652 for (int j = 0; j < aNbRows; j++) {
653 aItem = createDefaultItem();
655 aItem->setBackgroundColor(Qt::lightGray);
656 aDataTbl->setItem(j, i + 1, aItem);
660 emit valuesChanged();
663 //**********************************************************************************
664 void CollectionPlugin_WidgetField::onAddStep()
666 int aMax = myStepSlider->maximum();
668 myStepSlider->setMaximum(aMax);
669 myMaxLbl->setText(QString::number(aMax));
670 appendStepControls();
671 myStepSlider->setValue(aMax);
673 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
674 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
677 AttributeSelectionListPtr aSelList =
678 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
679 if (!aSelList->isInitialized())
681 int aSelNb = aSelList->size();
685 int aColumns = myNbComponentsSpn->value() + 1;
686 int aRows = aSelNb + 1;
687 QTableWidget* aTable = myDataTblList.last();
688 aTable->setRowCount(aRows);
689 QTableWidgetItem* aItem = 0;
690 for(int i = 0; i < aColumns; i++) {
692 for(int j = 1; j < aRows; j++) {
693 aItem = new QTableWidgetItem();
694 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
695 aItem->setText(aAttr->namingName().c_str());
696 aItem->setToolTip(aAttr->namingName().c_str());
697 aTable->setItem(j, i, aItem);
700 QString aDefVal = aTable->item(0, i)->text();
701 for(int j = 1; j < aRows; j++) {
702 aItem = new QTableWidgetItem();
703 aItem->setText(aDefVal);
704 aTable->setItem(j, i, aItem);
710 //**********************************************************************************
711 void CollectionPlugin_WidgetField::onRemoveStep()
713 int aMax = myStepSlider->maximum();
715 myMaxLbl->setText(QString::number(aMax));
716 removeStepControls();
717 myStepSlider->setMaximum(aMax);
719 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
720 aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
723 //**********************************************************************************
724 void CollectionPlugin_WidgetField::clearData()
726 foreach(QTableWidget* aDataTbl, myDataTblList) {
727 aDataTbl->setRowCount(1);
731 //**********************************************************************************
732 void CollectionPlugin_WidgetField::onStepMove(int theStep)
734 myCurStepLbl->setText(QString::number(theStep));
735 myStepWgt->setCurrentIndex(theStep - 1);
738 //**********************************************************************************
739 bool CollectionPlugin_WidgetField::
740 isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
742 return (myShapeTypeCombo->currentIndex() == 5)? false : true;
745 //**********************************************************************************
746 void CollectionPlugin_WidgetField::onSelectionChanged()
749 myActivation = false;
752 // Ignore selection for Parts mode
753 if (myShapeTypeCombo->currentIndex() == 5)
756 QList<ModuleBase_ViewerPrsPtr> aSelected =
757 myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
759 AttributeSelectionListPtr aSelList =
760 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
761 aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
767 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
768 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
769 aShape = aPrs->shape();
770 if (!aResult.get() && !aShape.get())
772 if (!aSelList->isInList(aResult, aShape)) {
773 aSelList->append(aResult, aShape);
777 int aColumns = myDataTblList.first()->columnCount();
778 int aRows = myDataTblList.first()->rowCount();
779 int aNewRows = aNbData + 1;
780 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
781 aTablesAttr->setSize(aNewRows, aColumns - 1, myDataTblList.size());
783 QTableWidgetItem* aItem = 0;
784 foreach(QTableWidget* aTable, myDataTblList) {
785 aTable->setRowCount(aNewRows);
786 if (aNewRows > aRows) {
788 for(int i = 0; i < aColumns; i++) {
790 for(int j = 1; j < aNewRows; j++) {
791 aItem = new QTableWidgetItem();
792 AttributeSelectionPtr aAttr = aSelList->value(j - 1);
793 aItem->setText(aAttr->namingName().c_str());
794 aItem->setToolTip(aAttr->namingName().c_str());
795 aTable->setItem(j, i, aItem);
798 QString aDefVal = aTable->item(0, i)->text();
799 for(int j = aRows; j < aNewRows; j++) {
800 aItem = new QTableWidgetItem();
801 aItem->setText(aDefVal);
802 aTable->setItem(j, i, aItem);
807 // Update only selection name
808 for(int j = 1; j < aNewRows - 1; j++) {
809 AttributeSelectionPtr aAttr = aSelList->value(j);
810 aTable->item(j, 0)->setText(aAttr->namingName().c_str());
811 aTable->item(j, 0)->setToolTip(aAttr->namingName().c_str());
815 emit valuesChanged();
818 //**********************************************************************************
819 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
821 DataTableItemDelegate* aDelegate = 0;
822 aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
824 ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
825 if (aOldType != theIdx) {
826 aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
827 int aColumns = myDataTblList.first()->columnCount();
828 int aRows = myDataTblList.first()->rowCount();
829 foreach(QTableWidget* aTable, myDataTblList) {
830 for(int i = 1; i < aColumns; i++) {
831 for(int j = 0; j < aRows; j++) {
833 case ModelAPI_AttributeTables::DOUBLE:
834 case ModelAPI_AttributeTables::INTEGER:
835 if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
836 (aOldType == ModelAPI_AttributeTables::STRING)) {
837 aTable->item(j, i)->setText("0");
840 case ModelAPI_AttributeTables::BOOLEAN:
841 aTable->item(j, i)->setText(MYFalse);
847 emit valuesChanged();
852 //**********************************************************************************
853 void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol)
855 // Do not store here column of names
858 if (!myFeature.get())
860 QTableWidget* aTable = static_cast<QTableWidget*>(sender());
861 int aNb = myDataTblList.indexOf(aTable);
864 ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text());
866 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
867 if (aTablesAttr->isInitialized())
868 aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb);
870 emit valuesChanged();
873 //**********************************************************************************
874 void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
876 activateSelectionAndFilters(theType == 5? false:true);
878 AttributeSelectionListPtr aSelList =
879 myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
881 std::string aTypeName = getSelectionType(theType);
882 if (aTypeName == aSelList->selectionType())
884 aSelList->setSelectionType(aTypeName);
886 //Clear old selection
889 AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
890 aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size());
891 emit valuesChanged();
894 //**********************************************************************************
895 bool CollectionPlugin_WidgetField::processEnter()
904 //**********************************************************************************
905 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
907 if (theNew && (!myIsEditing))
908 myIsEditing = dynamic_cast<QLineEdit*>(theNew);
911 //**********************************************************************************
912 void CollectionPlugin_WidgetField::onRangeChanged(int theMin, int theMax)
914 myMaxLbl->setText(QString::number(theMax));
915 myRemoveBtn->setEnabled(theMax > 1);