From: vsv Date: Wed, 23 Nov 2016 12:37:16 +0000 (+0300) Subject: Issue #1865: Create a field X-Git-Tag: V_2.6.0~67 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=4dec4c0346cf40237ad7d9c471d5a25fcd3d12c6;p=modules%2Fshaper.git Issue #1865: Create a field --- diff --git a/src/CollectionPlugin/CollectionPlugin_Field.cpp b/src/CollectionPlugin/CollectionPlugin_Field.cpp index c9228c405..55fefb6a5 100644 --- a/src/CollectionPlugin/CollectionPlugin_Field.cpp +++ b/src/CollectionPlugin/CollectionPlugin_Field.cpp @@ -23,8 +23,8 @@ void CollectionPlugin_Field::initAttributes() { data()->addAttribute(SELECTED_ID(), ModelAPI_AttributeSelectionList::typeId()); data()->addAttribute(COMPONENTS_NAMES_ID(), ModelAPI_AttributeStringArray::typeId()); - data()->addAttribute(VALUES_TYPE_ID(), ModelAPI_AttributeInteger::typeId()); - data()->addAttribute(STEPS_NB_ID(), ModelAPI_AttributeInteger::typeId()); + //data()->addAttribute(VALUES_TYPE_ID(), ModelAPI_AttributeInteger::typeId()); + //data()->addAttribute(STEPS_NB_ID(), ModelAPI_AttributeInteger::typeId()); data()->addAttribute(STAMPS_ID(), ModelAPI_AttributeIntArray::typeId()); data()->addAttribute(VALUES_ID(), ModelAPI_AttributeTables::typeId()); } diff --git a/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp b/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp index a2867f4ee..8dd768c62 100644 --- a/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp +++ b/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp @@ -5,11 +5,17 @@ // Author: Vitaly SMETANNIKOV #include "CollectionPlugin_WidgetField.h" +#include "CollectionPlugin_Field.h" #include #include #include +#include +#include +#include +#include + #include #include #include @@ -24,42 +30,44 @@ #include #include #include +#include +#include +#include +const char* MYFirstCol = "Shape"; +const char* MYTrue = "True"; +const char* MYFalse = "False"; class DataTableItemDelegate : public QStyledItemDelegate { public: - enum DataType { - DoubleType, - IntegerType, - BooleanType, - StringType }; - - DataTableItemDelegate(DataType theType) : QStyledItemDelegate() { myType = theType; } + DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType) : QStyledItemDelegate() { myType = theType; } virtual QWidget* createEditor(QWidget* theParent, const QStyleOptionViewItem & theOption, const QModelIndex& theIndex) const; - virtual void setModelData(QWidget* theEditor, QAbstractItemModel* theModel, - const QModelIndex& theIndex) const; - - DataType dataType() const { return myType; } + ModelAPI_AttributeTables::ValueType dataType() const { return myType; } - void setDataType(DataType theType) { myType = theType; } + void setDataType(ModelAPI_AttributeTables::ValueType theType) { myType = theType; } private: - DataType myType; + ModelAPI_AttributeTables::ValueType myType; }; QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, const QStyleOptionViewItem & theOption, const QModelIndex& theIndex ) const { - if ((theIndex.column() != 0) && (theIndex.row() != 0)) { + if ((theIndex.column() == 0) && (theIndex.row() > 0)) { + QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex); + QLineEdit* aEdt = static_cast(aWgt); + aEdt->setReadOnly(true); + return aEdt; + } else { QLineEdit* aLineEdt = 0; switch (myType) { - case DoubleType: + case ModelAPI_AttributeTables::DOUBLE: aLineEdt = dynamic_cast(QStyledItemDelegate::createEditor(theParent, theOption, theIndex)); @@ -67,7 +75,8 @@ QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, aLineEdt->setValidator(new QDoubleValidator(aLineEdt)); return aLineEdt; } - case IntegerType: + break; + case ModelAPI_AttributeTables::INTEGER: aLineEdt = dynamic_cast(QStyledItemDelegate::createEditor(theParent, theOption, theIndex)); @@ -75,11 +84,12 @@ QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, aLineEdt->setValidator(new QIntValidator(aLineEdt)); return aLineEdt; } - case BooleanType: + break; + case ModelAPI_AttributeTables::BOOLEAN: { QComboBox* aBox = new QComboBox(theParent); - aBox->addItem("True"); - aBox->addItem("False"); + aBox->addItem(MYFalse); + aBox->addItem(MYTrue); return aBox; } } @@ -88,23 +98,14 @@ QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, } -void DataTableItemDelegate::setModelData(QWidget* theEditor, QAbstractItemModel* theModel, - const QModelIndex& theIndex) const -{ - QComboBox* aBox = dynamic_cast(theEditor); - if (aBox) { - theModel->setData(theIndex, aBox->currentText()); - } else - QStyledItemDelegate::setModelData(theEditor, theModel, theIndex); -} - - - +//********************************************************************************** +//********************************************************************************** +//********************************************************************************** CollectionPlugin_WidgetField:: CollectionPlugin_WidgetField(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData): -ModuleBase_WidgetSelector(theParent, theWorkshop, theData) +ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0) { QVBoxLayout* aMainLayout = new QVBoxLayout(this); @@ -118,16 +119,17 @@ ModuleBase_WidgetSelector(theParent, theWorkshop, theData) myShapeTypeCombo = new QComboBox(aTypesWgt); QStringList aShapeTypes; aShapeTypes << tr("Vertices") << tr("Edges") << tr("Faces") - << tr("Solids") << tr("Results") << tr("Parts"); + << tr("Solids") << tr("Objects") << tr("Parts"); myShapeTypeCombo->addItems(aShapeTypes); aTypesLayout->addRow(tr("Type of shapes"), myShapeTypeCombo); // Type of field myFieldTypeCombo = new QComboBox(aTypesWgt); QStringList aFieldTypes; - aFieldTypes << tr("Double") << tr("Integer") << tr("Boolean") + aFieldTypes << tr("Boolean") << tr("Integer") << tr("Double") << tr("String"); myFieldTypeCombo->addItems(aFieldTypes); + myFieldTypeCombo->setCurrentIndex(2); aTypesLayout->addRow(tr("Type of field"), myFieldTypeCombo); // Number of components @@ -193,8 +195,10 @@ ModuleBase_WidgetSelector(theParent, theWorkshop, theData) connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep())); connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int))); connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int))); + connect(myShapeTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onShapeTypeChanged(int))); } +//********************************************************************************** void CollectionPlugin_WidgetField::appendStepControls() { QWidget* aWidget = new QWidget(myStepWgt); @@ -209,84 +213,386 @@ void CollectionPlugin_WidgetField::appendStepControls() myStampSpnList.append(aStampSpn); // Data table - QTableWidget* aDataTbl = new QTableWidget(2, myCompNamesList.count() + 1, aWidget); - aDataTbl->setItemDelegate( - new DataTableItemDelegate((DataTableItemDelegate::DataType) myFieldTypeCombo->currentIndex())); + QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget); + DataTableItemDelegate* aDelegate = 0; + if (myDataTblList.isEmpty()) + aDelegate = new DataTableItemDelegate( + (ModelAPI_AttributeTables::ValueType) myFieldTypeCombo->currentIndex()); + else + aDelegate = dynamic_cast(myDataTblList.first()->itemDelegate()); + + aDataTbl->setItemDelegate(aDelegate); + myDataTblList.append(aDataTbl); + aDataTbl->verticalHeader()->hide(); - aDataTbl->horizontalHeader()->hide(); aDataTbl->setRowHeight(0, 25); - aDataTbl->setRowHeight(1, 25); + aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); - QTableWidgetItem* aItem = new QTableWidgetItem("Shape"); - aItem->setBackgroundColor(Qt::lightGray); - aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled); - aDataTbl->setItem(0, 0, aItem); + updateHeaders(aDataTbl); - aItem = new QTableWidgetItem("Default value"); + QTableWidgetItem* aItem = new QTableWidgetItem("Default value"); aItem->setBackgroundColor(Qt::lightGray); aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled); - aDataTbl->setItem(1, 0, aItem); + aDataTbl->setItem(0, 0, aItem); + // Set default value item for (int i = 0; i < myCompNamesList.count(); i++) { - aItem = new QTableWidgetItem(myCompNamesList[i]); + aItem = createDefaultItem(); aItem->setBackgroundColor(Qt::lightGray); aDataTbl->setItem(0, i + 1, aItem); - - aItem = new QTableWidgetItem("0"); - aItem->setBackgroundColor(Qt::lightGray); - aDataTbl->setItem(1, i + 1, aItem); } aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2); + connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int))); QAbstractItemDelegate* aDel = aDataTbl->itemDelegate(); - myDataTblList.append(aDataTbl); - myStepWgt->addWidget(aWidget); + aDataTbl->horizontalHeader()->viewport()->installEventFilter(this); +} + +//********************************************************************************** +bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent) +{ + QObject* aObject = 0; + foreach(QTableWidget* aTable, myDataTblList) { + if (aTable->horizontalHeader()->viewport() == theObject) { + aObject = theObject; + break; + } + } + if (aObject) { + if (theEvent->type() == QEvent::MouseButtonDblClick) { + if (myHeaderEditor) { //delete previous editor + myHeaderEditor->deleteLater(); + myHeaderEditor = 0; + } + QMouseEvent* aMouseEvent = static_cast(theEvent); + QHeaderView* aHeader = static_cast(aObject->parent()); + QTableWidget* aTable = static_cast(aHeader->parentWidget()); + + int aShift = aTable->horizontalScrollBar()->value(); + int aPos = aMouseEvent->x(); + int aIndex = aHeader->logicalIndex(aHeader->visualIndexAt(aPos)); + if (aIndex > 0) { + QRect aRect; + aRect.setLeft(aHeader->sectionPosition(aIndex)); + aRect.setWidth(aHeader->sectionSize(aIndex)); + aRect.setTop(0); + aRect.setHeight(aHeader->height()); + aRect.adjust(1, 1, -1, -1); + aRect.translate(-aShift, 0); + + myHeaderEditor = new QLineEdit(aHeader->viewport()); + myHeaderEditor->move(aRect.topLeft()); + myHeaderEditor->resize(aRect.size()); + myHeaderEditor->setFrame(false); + QString aText = aHeader->model()-> + headerData(aIndex, aHeader->orientation()).toString(); + myHeaderEditor->setText(aText); + myHeaderEditor->setFocus(); + myEditIndex = aIndex; //save for future use + myHeaderEditor->installEventFilter(this); //catch focus out event + //if user presses Enter it should close editor + connect(myHeaderEditor, SIGNAL(returnPressed()), aTable, SLOT(setFocus())); + myHeaderEditor->show(); + return true; + } + } + } else if ((theObject == myHeaderEditor) && (theEvent->type() == QEvent::FocusOut)) { + QHeaderView* aHeader = + static_cast(myHeaderEditor->parentWidget()->parentWidget()); + QString aNewTitle = myHeaderEditor->text(); + //save item text + aHeader->model()->setHeaderData(myEditIndex, aHeader->orientation(), aNewTitle); + myCompNamesList.replace(myEditIndex - 1, aNewTitle); + myHeaderEditor->deleteLater(); //safely delete editor + myHeaderEditor = 0; + // Store into data model + AttributeStringArrayPtr aStringsAttr = + myFeature->data()->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID()); + aStringsAttr->setValue(myEditIndex - 1, aNewTitle.toStdString()); + } + return ModuleBase_WidgetSelector::eventFilter(theObject, theEvent); } +//********************************************************************************** +QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const +{ + QTableWidgetItem* aItem = new QTableWidgetItem(); + switch (myFieldTypeCombo->currentIndex()) { + case ModelAPI_AttributeTables::DOUBLE: + case ModelAPI_AttributeTables::INTEGER: + aItem->setText("0"); + break; + case ModelAPI_AttributeTables::BOOLEAN: + aItem->setText(MYFalse); + break; + } + return aItem; +} + +//********************************************************************************** +QTableWidgetItem* CollectionPlugin_WidgetField:: + createValueItem(ModelAPI_AttributeTables::Value& theVal) const +{ + QTableWidgetItem* aItem = new QTableWidgetItem(); + switch (myFieldTypeCombo->currentIndex()) { + case ModelAPI_AttributeTables::DOUBLE: + aItem->setText(QString::number(theVal.myDouble)); + break; + case ModelAPI_AttributeTables::INTEGER: + aItem->setText(QString::number(theVal.myInt)); + break; + case ModelAPI_AttributeTables::BOOLEAN: + aItem->setText(theVal.myBool? MYTrue : MYFalse); + break; + case ModelAPI_AttributeTables::STRING: + aItem->setText(theVal.myStr.c_str()); + } + return aItem; +} + +//********************************************************************************** +void CollectionPlugin_WidgetField::updateHeaders(QTableWidget* theDataTbl) const +{ + QStringList aHeaders; + aHeaders << tr(MYFirstCol); + aHeaders << myCompNamesList; + theDataTbl->setHorizontalHeaderLabels(aHeaders); +} + +//********************************************************************************** void CollectionPlugin_WidgetField::removeStepControls() { int aCurWgtId = myStepWgt->currentIndex(); - myStepWgt->removeWidget(myStepWgt->currentWidget()); + QWidget* aWgt = myStepWgt->currentWidget(); + myStepWgt->removeWidget(aWgt); myStampSpnList.removeAt(aCurWgtId); myDataTblList.removeAt(aCurWgtId); + aWgt->deleteLater(); } - +//********************************************************************************** QList CollectionPlugin_WidgetField::getControls() const { QList aControls; // this control will accept focus and will be highlighted in the Property Panel - //aControls.push_back(myShapeTypeCombo); + aControls.push_back(myShapeTypeCombo); //aControls.push_back(myFieldTypeCombo); //aControls.push_back(myNbComponentsSpn); return aControls; } +//********************************************************************************** bool CollectionPlugin_WidgetField::storeValueCustom() { - //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID()); - //AttributeIntegerPtr aValueAttribute = - // std::dynamic_pointer_cast(anAttribute); - //aValueAttribute->setValue(myComboBox->currentIndex()); - //updateObject(myFeature); + DataPtr aData = myFeature->data(); + // Store number of components + AttributeStringArrayPtr aStringsAttr = + aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID()); + int aNbComps = myCompNamesList.size(); + aStringsAttr->setSize(aNbComps); + for ( int i = 0; i < aNbComps; i++) + aStringsAttr->setValue(i, myCompNamesList.at(i).toStdString()); + + AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID()); + // Store number of steps + int aNbSteps = myDataTblList.size(); + + // Store Type of the field values + int aFldType = myFieldTypeCombo->currentIndex(); + + AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID()); + aStampsAttr->setSize(aNbSteps); + // Store data + QTableWidget* aTable = myDataTblList.first(); + int aRows = aTable->rowCount(); + // first column contains selected names which should not be stored + int aColumns = aTable->columnCount() - 1; + + aTablesAttr->setSize(aRows, aColumns, aNbSteps); + aTablesAttr->setType((ModelAPI_AttributeTables::ValueType)aFldType); + for (int i = 0; i < aNbSteps; i++) { + aStampsAttr->setValue(i, myStampSpnList.at(i)->value()); + aTable = myDataTblList.at(i); + for (int j = 0; j < aColumns; j++) { + for (int k = 0; k < aRows; k++) { + QString aTblVal = aTable->item(k, j + 1)->text(); + aTablesAttr->setValue(getValue(aTblVal), k, j, i); + } + } + } + updateObject(myFeature); return true; } +//********************************************************************************** bool CollectionPlugin_WidgetField::restoreValueCustom() { - //AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID()); - //AttributeIntegerPtr aValueAttribute = - // std::dynamic_pointer_cast(anAttribute); + DataPtr aData = myFeature->data(); + + AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID()); + if (!aSelList->isInitialized()) + return false; + std::string aTypeStr = aSelList->selectionType(); + myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr)); - //bool isBlocked = myComboBox->blockSignals(true); - //myComboBox->setCurrentIndex(aValueAttribute->value()); - //myComboBox->blockSignals(isBlocked); + // Get number of components + AttributeStringArrayPtr aStringsAttr = + aData->stringArray(CollectionPlugin_Field::COMPONENTS_NAMES_ID()); + if (!aStringsAttr->isInitialized()) + return true; + + myCompNamesList.clear(); + for (int i = 0; i < aStringsAttr->size(); i++) { + myCompNamesList.append(aStringsAttr->value(i).c_str()); + } + myNbComponentsSpn->setValue(myCompNamesList.size()); + + AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID()); + // Get number of steps + int aNbSteps = aTablesAttr->tables(); + myStepSlider->setMaximum(aNbSteps); + //myStepSlider->setValue(1); + // Clear old tables + myStampSpnList.clear(); + myDataTblList.clear(); + while (myStepWgt->count()) { + QWidget* aWgt = myStepWgt->widget(myStepWgt->count() - 1); + myStepWgt->removeWidget(aWgt); + aWgt->deleteLater(); + } + + while (myStepWgt->count() < aNbSteps) + appendStepControls(); + //myStepWgt->setCurrentIndex(myStepSlider->value() - 1); + + // Get Type of the field values + myFieldTypeCombo->setCurrentIndex(aTablesAttr->type()); + + AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID()); + // Fill data table + int aRows = aTablesAttr->rows(); + int aCols = aTablesAttr->columns(); + + QTableWidgetItem* aItem = 0; + for (int i = 0; i < aNbSteps; i++) { + myStampSpnList.at(i)->setValue(aStampsAttr->value(i)); + QTableWidget* aTable = myDataTblList.at(i); + bool isBlocked = aTable->blockSignals(true); + aTable->setRowCount(aRows); + for (int j = 0; j < aCols + 1; j++) { + for (int k = 0; k < aRows; k++) { + if ((j == 0) && (k > 0)) { + // Add selection names + AttributeSelectionPtr aAttr = aSelList->value(k - 1); + aItem = new QTableWidgetItem(aAttr->namingName().c_str()); + aTable->setItem(k, j, aItem); + } else if (j > 0) { + // Add Values + ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j - 1, i); + aItem = createValueItem(aVal); + if (k == 0) + aItem->setBackgroundColor(Qt::lightGray); + aTable->setItem(k, j, aItem); + + } + } + } + aTable->blockSignals(isBlocked); + } return true; } +//********************************************************************************** +int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const +{ + if (theStr == "vertex") + return 0; + else if (theStr == "edge") + return 1; + else if (theStr == "face") + return 2; + else if (theStr == "solid") + return 3; + else if (theStr == "object") + return 4; + else if (theStr == "part") + return 5; + return -1; +} + + +//********************************************************************************** +std::string CollectionPlugin_WidgetField::getSelectionType(int theType) const +{ + switch (theType) { + case 0: //"Vertices" + return "vertex"; + case 1: // "Edges" + return "edge"; + case 2: // "Faces" + return "face"; + case 3: // "Solids" + return "solid"; + case 4: // "Results" + return "object"; + case 5: // "Parts" + return "part"; + } + return ""; +} + +//********************************************************************************** +QIntList CollectionPlugin_WidgetField::shapeTypes() const +{ + QIntList aRes; + switch (myShapeTypeCombo->currentIndex()) { + case 0: //"Vertices" + aRes.append(ModuleBase_Tools::shapeType("vertex")); + break; + case 1: // "Edges" + aRes.append(ModuleBase_Tools::shapeType("edge")); + break; + case 2: // "Faces" + aRes.append(ModuleBase_Tools::shapeType("face")); + break; + case 3: // "Solids" + aRes.append(ModuleBase_Tools::shapeType("solid")); + break; + case 4: // "Results" + aRes.append(ModuleBase_Tools::shapeType("object")); + break; + case 5: // "Parts" + // TODO: Selection mode for Parts + break; + } + return aRes; +} + +//********************************************************************************** +ModelAPI_AttributeTables::Value CollectionPlugin_WidgetField::getValue(QString theStrVal) const +{ + ModelAPI_AttributeTables::Value aVal; + switch (myFieldTypeCombo->currentIndex()) { + case ModelAPI_AttributeTables::BOOLEAN: + aVal.myBool = (theStrVal == MYTrue)? true : false; + break; + case ModelAPI_AttributeTables::DOUBLE: + aVal.myDouble = theStrVal.toDouble(); + break; + case ModelAPI_AttributeTables::INTEGER: + aVal.myInt = theStrVal.toInt(); + break; + case ModelAPI_AttributeTables::STRING: + aVal.myStr = theStrVal.toStdString(); + } + return aVal; +} + + +//********************************************************************************** void CollectionPlugin_WidgetField::onNbCompChanged(int theVal) { int aOldCol = myCompNamesList.count(); @@ -303,21 +609,20 @@ void CollectionPlugin_WidgetField::onNbCompChanged(int theVal) foreach(QTableWidget* aDataTbl, myDataTblList) { aDataTbl->setColumnCount(theVal + 1); - for (int i = 0; i < myCompNamesList.count(); i++) { + updateHeaders(aDataTbl); + for (int i = aOldCol; i < myCompNamesList.count(); i++) { for (int j = 0; j < aNbRows; j++) { - aItem = new QTableWidgetItem(); + aItem = createDefaultItem(); if (j == 0) - aItem->setText(myCompNamesList.at(i)); - else - aItem->setText("0"); - if (j < 3) aItem->setBackgroundColor(Qt::lightGray); - aDataTbl->setItem(j, i + aOldCol + 1, aItem); + aDataTbl->setItem(j, i + 1, aItem); } } } + emit valuesChanged(); } +//********************************************************************************** void CollectionPlugin_WidgetField::onAddStep() { int aMax = myStepSlider->maximum(); @@ -329,6 +634,7 @@ void CollectionPlugin_WidgetField::onAddStep() myRemoveBtn->setEnabled(aMax > 1); } +//********************************************************************************** void CollectionPlugin_WidgetField::onRemoveStep() { int aMax = myStepSlider->maximum(); @@ -339,78 +645,166 @@ void CollectionPlugin_WidgetField::onRemoveStep() myRemoveBtn->setEnabled(aMax > 1); } +//********************************************************************************** void CollectionPlugin_WidgetField::clearData() { - int aNbRows = myDataTblList.first()->rowCount(); - int aNbCol = myDataTblList.first()->columnCount(); - foreach(QTableWidget* aDataTbl, myDataTblList) { - QString aDefValue; - for (int i = 1; i < aNbCol; i++) { - aDefValue = aDataTbl->item(1, i)->text(); - for (int j = 2; j < aNbRows; j++) { - aDataTbl->item(j, i)->setText(aDefValue); - } - } + aDataTbl->setRowCount(1); } } +//********************************************************************************** void CollectionPlugin_WidgetField::onStepMove(int theStep) { myCurStepLbl->setText(QString::number(theStep)); myStepWgt->setCurrentIndex(theStep - 1); } -QIntList CollectionPlugin_WidgetField::shapeTypes() const +//********************************************************************************** +bool CollectionPlugin_WidgetField:: + isValidSelection(const std::shared_ptr& thePrs) { - QIntList aRes; + return true; +} + +//********************************************************************************** +void CollectionPlugin_WidgetField::onSelectionChanged() +{ + QList aSelected = + myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls); + + clearData(); + AttributeSelectionListPtr aSelList = + myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID()); + aSelList->clear(); + switch (myShapeTypeCombo->currentIndex()) { case 0: //"Vertices" - aRes.append(ModuleBase_Tools::shapeType("vertex")); + aSelList->setSelectionType("vertex"); break; case 1: // "Edges" - aRes.append(ModuleBase_Tools::shapeType("edge")); + aSelList->setSelectionType("edge"); break; case 2: // "Faces" - aRes.append(ModuleBase_Tools::shapeType("face")); + aSelList->setSelectionType("face"); break; case 3: // "Solids" - aRes.append(ModuleBase_Tools::shapeType("solid")); + aSelList->setSelectionType("solid"); break; case 4: // "Results" - aRes.append(ModuleBase_Tools::shapeType("object")); + aSelList->setSelectionType("object"); break; case 5: // "Parts" // TODO: Selection mode for Parts break; } - return aRes; -} - -bool CollectionPlugin_WidgetField:: - isValidSelection(const std::shared_ptr& thePrs) -{ - return true; -} - -void CollectionPlugin_WidgetField::onSelectionChanged() -{ - QList aSelected = - myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls); - - clearData(); + ResultPtr aResult; + GeomShapePtr aShape; + int aNbData = 0; foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) { + aResult = std::dynamic_pointer_cast(aPrs->object()); + aShape = aPrs->shape(); + if (!aResult.get() && !aShape.get()) + continue; + if (!aSelList->isInList(aResult, aShape)) { + aSelList->append(aResult, aShape); + aNbData++; + } + } + int aColumns = myDataTblList.first()->columnCount(); + int aRows = myDataTblList.first()->rowCount(); + AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID()); + aTablesAttr->setSize(aRows + aNbData, aColumns - 1, myDataTblList.size()); + QTableWidgetItem* aItem = 0; + foreach(QTableWidget* aTable, myDataTblList) { + aTable->setRowCount(aRows + aNbData); + for(int i = 0; i < aColumns; i++) { + QString aDefVal = aTable->item(0, i)->text(); + for(int j = 1; j < aRows + aNbData; j++) { + aItem = new QTableWidgetItem(); + if (i == 0) { + AttributeSelectionPtr aAttr = aSelList->value(j - 1); + aItem->setText(aAttr->namingName().c_str()); + aItem->setToolTip(aAttr->namingName().c_str()); + } else { + aItem->setText(aDefVal); + } + aTable->setItem(j, i, aItem); + } + } } + emit valuesChanged(); } +//********************************************************************************** void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx) { DataTableItemDelegate* aDelegate = 0; - foreach(QTableWidget* aTable, myDataTblList) { - aDelegate = dynamic_cast(aTable->itemDelegate()); - if (aDelegate) - aDelegate->setDataType((DataTableItemDelegate::DataType)theIdx); + aDelegate = dynamic_cast(myDataTblList.first()->itemDelegate()); + if (aDelegate) { + ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType(); + if (aOldType != theIdx) { + aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx); + int aColumns = myDataTblList.first()->columnCount(); + int aRows = myDataTblList.first()->rowCount(); + foreach(QTableWidget* aTable, myDataTblList) { + for(int i = 1; i < aColumns; i++) { + for(int j = 0; j < aRows; j++) { + switch (theIdx) { + case ModelAPI_AttributeTables::DOUBLE: + case ModelAPI_AttributeTables::INTEGER: + if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) || + (aOldType == ModelAPI_AttributeTables::STRING)) { + aTable->item(j, i)->setText("0"); + } + break; + case ModelAPI_AttributeTables::BOOLEAN: + aTable->item(j, i)->setText(MYFalse); + break; + } + } + } + } + emit valuesChanged(); + } } } + +void CollectionPlugin_WidgetField::onTableEdited(int theRow, int theCol) +{ + // Do not store here column of names + if (theCol == 0) + return; + if (!myFeature.get()) + return; + QTableWidget* aTable = static_cast(sender()); + int aNb = myDataTblList.indexOf(aTable); + if (aNb == -1) + return; + ModelAPI_AttributeTables::Value aVal = getValue(aTable->item(theRow, theCol)->text()); + + AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID()); + if (aTablesAttr->isInitialized()) + aTablesAttr->setValue(aVal,theRow, theCol - 1, aNb); + else + emit valuesChanged(); +} + +void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType) +{ + AttributeSelectionListPtr aSelList = + myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID()); + if (!aSelList->isInitialized()) + return; + std::string aTypeName = getSelectionType(theType); + if (aTypeName == aSelList->selectionType()) + return; + + //Clear old selection + clearData(); + aSelList->clear(); + AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID()); + aTablesAttr->setSize(1, myNbComponentsSpn->value(), myDataTblList.size()); +} diff --git a/src/CollectionPlugin/CollectionPlugin_WidgetField.h b/src/CollectionPlugin/CollectionPlugin_WidgetField.h index 3e3277b53..2e6d630ff 100644 --- a/src/CollectionPlugin/CollectionPlugin_WidgetField.h +++ b/src/CollectionPlugin/CollectionPlugin_WidgetField.h @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -26,6 +27,8 @@ class QSlider; class QTableWidget; class QStackedWidget; class QPushButton; +class QTableWidgetItem; +class QLineEdit; /*! * \ingroup GUI @@ -63,6 +66,8 @@ protected: /// \return a list of shapes virtual QIntList shapeTypes() const; + virtual bool eventFilter(QObject* theObbject, QEvent* theEvent); + protected slots: /// Slot which is called on selection event @@ -79,12 +84,28 @@ private slots: void onFieldTypeChanged(int theIdx); + void onTableEdited(int theRow, int theCol); + + void onShapeTypeChanged(int theType); + private: void clearData(); void appendStepControls(); void removeStepControls(); + void updateHeaders(QTableWidget* theDataTbl) const; + + int getSelectionType(const std::string& theStr) const; + + std::string getSelectionType(int theType) const; + + QTableWidgetItem* createDefaultItem() const; + + QTableWidgetItem* createValueItem(ModelAPI_AttributeTables::Value& theVal) const; + + ModelAPI_AttributeTables::Value getValue(QString theStrVal) const; + /// Types of shapes selection QComboBox* myShapeTypeCombo; @@ -111,9 +132,10 @@ private: QStringList myCompNamesList; - QList mySelection; - QPushButton* myRemoveBtn; + + QLineEdit* myHeaderEditor; + int myEditIndex; }; #endif \ No newline at end of file diff --git a/src/Model/Model_AttributeTables.cpp b/src/Model/Model_AttributeTables.cpp index 923caa3d7..77930e5a2 100644 --- a/src/Model/Model_AttributeTables.cpp +++ b/src/Model/Model_AttributeTables.cpp @@ -155,7 +155,7 @@ void Model_AttributeTables::setType(ModelAPI_AttributeTables::ValueType theType) myType = theType; int aTables = myTables; myTables = 0; // to let setSize know that there is no old array - setSize(myRows, myCols, myTables); + setSize(myRows, myCols, aTables); } else { myType = theType; owner()->data()->sendAttributeUpdated(this); diff --git a/src/ModuleBase/ModuleBase_WidgetSelector.cpp b/src/ModuleBase/ModuleBase_WidgetSelector.cpp index a7b536db1..11811c422 100755 --- a/src/ModuleBase/ModuleBase_WidgetSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetSelector.cpp @@ -211,6 +211,8 @@ void ModuleBase_WidgetSelector::deactivate() /// clear temporary cash AttributePtr anAttribute = attribute(); + if (!anAttribute.get()) + return; std::string aType = anAttribute->attributeType(); if (anAttribute->attributeType() == ModelAPI_AttributeSelection::typeId()) { AttributeSelectionPtr aSelectAttr =