From ab56e0efa5ca99501d78631ec31ef93deb8e70e6 Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 18 Nov 2016 13:01:17 +0300 Subject: [PATCH] Issue #1834: Fix length of lines --- src/CollectionPlugin/CMakeLists.txt | 4 +- .../CollectionPlugin_Field.cpp | 2 +- .../CollectionPlugin_Group.cpp | 4 +- .../CollectionPlugin_WidgetCreator.cpp | 4 +- .../CollectionPlugin_WidgetField.cpp | 310 ++++++++++++++---- .../CollectionPlugin_WidgetField.h | 47 ++- 6 files changed, 301 insertions(+), 70 deletions(-) diff --git a/src/CollectionPlugin/CMakeLists.txt b/src/CollectionPlugin/CMakeLists.txt index a32fb8e99..c1b22136e 100644 --- a/src/CollectionPlugin/CMakeLists.txt +++ b/src/CollectionPlugin/CMakeLists.txt @@ -40,6 +40,8 @@ INCLUDE_DIRECTORIES( ../GeomValidators ../Events ../ModuleBase + ../Config + ${CAS_INCLUDE_DIRS} ) SET(PROJECT_LIBRARIES @@ -51,7 +53,7 @@ SET(PROJECT_LIBRARIES ModuleBase ) -ADD_DEFINITIONS(-DCOLLECTIONPLUGIN_EXPORTS) +ADD_DEFINITIONS(-DCOLLECTIONPLUGIN_EXPORTS ${CAS_DEFINITIONS}) ADD_LIBRARY(CollectionPlugin MODULE ${PROJECT_SOURCES} ${PROJECT_HEADERS} ${XML_RESOURCES} ${TEXT_RESOURCES}) TARGET_LINK_LIBRARIES(CollectionPlugin ${PROJECT_LIBRARIES}) diff --git a/src/CollectionPlugin/CollectionPlugin_Field.cpp b/src/CollectionPlugin/CollectionPlugin_Field.cpp index bc2daf57e..863b1c90a 100644 --- a/src/CollectionPlugin/CollectionPlugin_Field.cpp +++ b/src/CollectionPlugin/CollectionPlugin_Field.cpp @@ -22,7 +22,7 @@ CollectionPlugin_Field::CollectionPlugin_Field() void CollectionPlugin_Field::initAttributes() { data()->addAttribute(SELECTED_ID(), ModelAPI_AttributeSelectionList::typeId()); - data()->addAttribute(COMPONENTS_NB_ID(), ModelAPI_AttributeInteger::typeId()); + //data()->addAttribute(COMPONENTS_NB_ID(), ModelAPI_AttributeInteger::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()); diff --git a/src/CollectionPlugin/CollectionPlugin_Group.cpp b/src/CollectionPlugin/CollectionPlugin_Group.cpp index c62b7d0d9..ede838a3e 100644 --- a/src/CollectionPlugin/CollectionPlugin_Group.cpp +++ b/src/CollectionPlugin/CollectionPlugin_Group.cpp @@ -19,8 +19,8 @@ CollectionPlugin_Group::CollectionPlugin_Group() void CollectionPlugin_Group::initAttributes() { - //data()->addAttribute(CollectionPlugin_Group::NAME_ID(), ModelAPI_AttributeString::typeId()); - data()->addAttribute(CollectionPlugin_Group::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()); + data()->addAttribute(CollectionPlugin_Group::LIST_ID(), + ModelAPI_AttributeSelectionList::typeId()); } void CollectionPlugin_Group::execute() diff --git a/src/CollectionPlugin/CollectionPlugin_WidgetCreator.cpp b/src/CollectionPlugin/CollectionPlugin_WidgetCreator.cpp index a07562235..a369cf36e 100644 --- a/src/CollectionPlugin/CollectionPlugin_WidgetCreator.cpp +++ b/src/CollectionPlugin/CollectionPlugin_WidgetCreator.cpp @@ -24,14 +24,14 @@ ModuleBase_ModelWidget* CollectionPlugin_WidgetCreator::createWidgetByType( const std::string& theType, QWidget* theParent, Config_WidgetAPI* theWidgetApi, - ModuleBase_IWorkshop* /*theWorkshop*/) + ModuleBase_IWorkshop* theWorkshop) { ModuleBase_ModelWidget* aWidget = 0; if (myPanelTypes.find(theType) == myPanelTypes.end()) return aWidget; if (theType == "field-panel") { - aWidget = new CollectionPlugin_WidgetField(theParent, theWidgetApi); + aWidget = new CollectionPlugin_WidgetField(theParent, theWorkshop, theWidgetApi); } return aWidget; diff --git a/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp b/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp index d0500355e..a2867f4ee 100644 --- a/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp +++ b/src/CollectionPlugin/CollectionPlugin_WidgetField.cpp @@ -6,6 +6,10 @@ #include "CollectionPlugin_WidgetField.h" +#include +#include +#include + #include #include #include @@ -16,10 +20,91 @@ #include #include #include +#include +#include +#include +#include + + +class DataTableItemDelegate : public QStyledItemDelegate +{ +public: + enum DataType { + DoubleType, + IntegerType, + BooleanType, + StringType }; + + DataTableItemDelegate(DataType 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; } + + void setDataType(DataType theType) { myType = theType; } + +private: + DataType myType; +}; + +QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, + const QStyleOptionViewItem & theOption, + const QModelIndex& theIndex ) const +{ + if ((theIndex.column() != 0) && (theIndex.row() != 0)) { + QLineEdit* aLineEdt = 0; + switch (myType) { + case DoubleType: + aLineEdt = dynamic_cast(QStyledItemDelegate::createEditor(theParent, + theOption, + theIndex)); + if (aLineEdt) { + aLineEdt->setValidator(new QDoubleValidator(aLineEdt)); + return aLineEdt; + } + case IntegerType: + aLineEdt = dynamic_cast(QStyledItemDelegate::createEditor(theParent, + theOption, + theIndex)); + if (aLineEdt) { + aLineEdt->setValidator(new QIntValidator(aLineEdt)); + return aLineEdt; + } + case BooleanType: + { + QComboBox* aBox = new QComboBox(theParent); + aBox->addItem("True"); + aBox->addItem("False"); + return aBox; + } + } + } + return QStyledItemDelegate::createEditor(theParent, theOption, theIndex); +} + + +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, const Config_WidgetAPI* theData): -ModuleBase_ModelWidget(theParent, theData) + CollectionPlugin_WidgetField(QWidget* theParent, + ModuleBase_IWorkshop* theWorkshop, + const Config_WidgetAPI* theData): +ModuleBase_WidgetSelector(theParent, theWorkshop, theData) { QVBoxLayout* aMainLayout = new QVBoxLayout(this); @@ -83,36 +168,10 @@ ModuleBase_ModelWidget(theParent, theData) aSliderLayout->addWidget(myMaxLbl); // Stamp value - aStepLayout->addWidget(new QLabel(tr("Stamp"), aStepFrame), 2, 0); - myStampSpn = new QSpinBox(aStepFrame); - aStepLayout->addWidget(myStampSpn, 2, 1); - - // Data table - myDataTbl = new QTableWidget(2, 2, aStepFrame); - myDataTbl->verticalHeader()->hide(); - myDataTbl->horizontalHeader()->hide(); - myDataTbl->setRowHeight(0, 25); - myDataTbl->setRowHeight(1, 25); - - QTableWidgetItem* aItem = new QTableWidgetItem("Shape"); - aItem->setBackgroundColor(Qt::lightGray); - aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled); - myDataTbl->setItem(0, 0, aItem); - - aItem = new QTableWidgetItem("Comp 1"); - aItem->setBackgroundColor(Qt::lightGray); - myDataTbl->setItem(0, 1, aItem); - - aItem = new QTableWidgetItem("Default value"); - aItem->setBackgroundColor(Qt::lightGray); - aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled); - myDataTbl->setItem(1, 0, aItem); - - aItem = new QTableWidgetItem("0"); - aItem->setBackgroundColor(Qt::lightGray); - myDataTbl->setItem(1, 1, aItem); - - aStepLayout->addWidget(myDataTbl, 3, 0, 1, 2); + myCompNamesList << "Comp 1"; + myStepWgt = new QStackedWidget(aStepFrame); + aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2); + appendStepControls(); // Buttons below QWidget* aBtnWgt = new QWidget(this); @@ -125,20 +184,83 @@ ModuleBase_ModelWidget(theParent, theData) aBtnLayout->addStretch(1); - QPushButton* aRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt); - aBtnLayout->addWidget(aRemoveBtn); + myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt); + aBtnLayout->addWidget(myRemoveBtn); + myRemoveBtn->setEnabled(false); connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int))); connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep())); - connect(aRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep())); + connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep())); connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int))); + connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int))); +} + +void CollectionPlugin_WidgetField::appendStepControls() +{ + QWidget* aWidget = new QWidget(myStepWgt); + QGridLayout* aStepLayout = new QGridLayout(aWidget); + aStepLayout->setContentsMargins(0, 0, 0, 0); + + aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0); + + QSpinBox* aStampSpn = new QSpinBox(aWidget); + aStepLayout->addWidget(aStampSpn, 0, 1); + + myStampSpnList.append(aStampSpn); + + // Data table + QTableWidget* aDataTbl = new QTableWidget(2, myCompNamesList.count() + 1, aWidget); + aDataTbl->setItemDelegate( + new DataTableItemDelegate((DataTableItemDelegate::DataType) myFieldTypeCombo->currentIndex())); + aDataTbl->verticalHeader()->hide(); + aDataTbl->horizontalHeader()->hide(); + aDataTbl->setRowHeight(0, 25); + aDataTbl->setRowHeight(1, 25); + + QTableWidgetItem* aItem = new QTableWidgetItem("Shape"); + aItem->setBackgroundColor(Qt::lightGray); + aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled); + aDataTbl->setItem(0, 0, aItem); + + aItem = new QTableWidgetItem("Default value"); + aItem->setBackgroundColor(Qt::lightGray); + aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled); + aDataTbl->setItem(1, 0, aItem); + + for (int i = 0; i < myCompNamesList.count(); i++) { + aItem = new QTableWidgetItem(myCompNamesList[i]); + 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); + + QAbstractItemDelegate* aDel = aDataTbl->itemDelegate(); + myDataTblList.append(aDataTbl); + + myStepWgt->addWidget(aWidget); +} + +void CollectionPlugin_WidgetField::removeStepControls() +{ + int aCurWgtId = myStepWgt->currentIndex(); + myStepWgt->removeWidget(myStepWgt->currentWidget()); + + myStampSpnList.removeAt(aCurWgtId); + myDataTblList.removeAt(aCurWgtId); } + QList CollectionPlugin_WidgetField::getControls() const { QList aControls; // this control will accept focus and will be highlighted in the Property Panel - //aControls.push_back(myComboBox); + //aControls.push_back(myShapeTypeCombo); + //aControls.push_back(myFieldTypeCombo); + //aControls.push_back(myNbComponentsSpn); return aControls; } @@ -167,21 +289,31 @@ bool CollectionPlugin_WidgetField::restoreValueCustom() void CollectionPlugin_WidgetField::onNbCompChanged(int theVal) { - int aOldCol = myDataTbl->columnCount() - 1; - int aNbRows = myDataTbl->rowCount(); - myDataTbl->setColumnCount(theVal + 1); + int aOldCol = myCompNamesList.count(); + int aNbRows = myDataTblList.first()->rowCount(); int aDif = theVal - aOldCol; QTableWidgetItem* aItem = 0; - for (int i = 0; i < aDif; i++) { - for (int j = 0; j < aNbRows; j++) { - aItem = new QTableWidgetItem(); - if (j == 0) - aItem->setText(QString("Comp %1").arg(i + aOldCol + 1)); - else - aItem->setText("0"); - if (j < 3) - aItem->setBackgroundColor(Qt::lightGray); - myDataTbl->setItem(j, i + aOldCol + 1, aItem); + + while (myCompNamesList.count() != theVal) { + if (aDif > 0) + myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1)); + else + myCompNamesList.removeLast(); + } + + foreach(QTableWidget* aDataTbl, myDataTblList) { + aDataTbl->setColumnCount(theVal + 1); + for (int i = 0; i < myCompNamesList.count(); i++) { + for (int j = 0; j < aNbRows; j++) { + aItem = new QTableWidgetItem(); + 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); + } } } } @@ -192,28 +324,33 @@ void CollectionPlugin_WidgetField::onAddStep() aMax++; myStepSlider->setMaximum(aMax); myMaxLbl->setText(QString::number(aMax)); - clearData(); + appendStepControls(); myStepSlider->setValue(aMax); + myRemoveBtn->setEnabled(aMax > 1); } void CollectionPlugin_WidgetField::onRemoveStep() { int aMax = myStepSlider->maximum(); aMax--; - myStepSlider->setMaximum(aMax); myMaxLbl->setText(QString::number(aMax)); + removeStepControls(); + myStepSlider->setMaximum(aMax); + myRemoveBtn->setEnabled(aMax > 1); } void CollectionPlugin_WidgetField::clearData() { - int aNbRows = myDataTbl->rowCount(); - int aNbCol = myDataTbl->columnCount(); - - QString aDefValue; - for (int i = 1; i < aNbCol; i++) { - aDefValue = myDataTbl->item(1, i)->text(); - for (int j = 2; j < aNbRows; j++) { - myDataTbl->item(j, i)->setText(aDefValue); + 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); + } } } } @@ -221,4 +358,59 @@ void CollectionPlugin_WidgetField::clearData() void CollectionPlugin_WidgetField::onStepMove(int theStep) { myCurStepLbl->setText(QString::number(theStep)); + myStepWgt->setCurrentIndex(theStep - 1); +} + +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; +} + +bool CollectionPlugin_WidgetField:: + isValidSelection(const std::shared_ptr& thePrs) +{ + return true; +} + +void CollectionPlugin_WidgetField::onSelectionChanged() +{ + QList aSelected = + myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls); + + clearData(); + + foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) { + + } +} + +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); + } } diff --git a/src/CollectionPlugin/CollectionPlugin_WidgetField.h b/src/CollectionPlugin/CollectionPlugin_WidgetField.h index 009815739..3e3277b53 100644 --- a/src/CollectionPlugin/CollectionPlugin_WidgetField.h +++ b/src/CollectionPlugin/CollectionPlugin_WidgetField.h @@ -11,7 +11,12 @@ #include "CollectionPlugin.h" -#include +#include +#include + +#include +#include + class QWidget; class QComboBox; @@ -19,16 +24,20 @@ class QSpinBox; class QLabel; class QSlider; class QTableWidget; +class QStackedWidget; +class QPushButton; /*! * \ingroup GUI * Represent a content of the property panel to show/modify parameters of a Field feature. */ -class CollectionPlugin_WidgetField : public ModuleBase_ModelWidget +class CollectionPlugin_WidgetField : public ModuleBase_WidgetSelector { Q_OBJECT public: - CollectionPlugin_WidgetField(QWidget* theParent, const Config_WidgetAPI* theData); + CollectionPlugin_WidgetField(QWidget* theParent, + ModuleBase_IWorkshop* theWorkshop, + const Config_WidgetAPI* theData); virtual ~CollectionPlugin_WidgetField() {} @@ -36,6 +45,12 @@ public: /// \return a control list virtual QList getControls() const; + /// Checks the widget validity. By default, it returns true. + /// \param thePrs a selected presentation in the view + /// \return a boolean value + virtual bool isValidSelection(const std::shared_ptr& theValue); + + protected: /// Saves the internal parameters to the given feature /// \return True in success @@ -44,6 +59,15 @@ protected: /// Restore value from attribute data to the widget's control virtual bool restoreValueCustom(); + /// Retunrs a list of possible shape types + /// \return a list of shapes + virtual QIntList shapeTypes() const; + + +protected slots: + /// Slot which is called on selection event + virtual void onSelectionChanged(); + private slots: void onNbCompChanged(int theVal); @@ -53,9 +77,14 @@ private slots: void onStepMove(int theStep); + void onFieldTypeChanged(int theIdx); + private: void clearData(); + void appendStepControls(); + void removeStepControls(); + /// Types of shapes selection QComboBox* myShapeTypeCombo; @@ -72,11 +101,19 @@ private: QSlider* myStepSlider; /// Stamp value - QSpinBox* myStampSpn; + QList myStampSpnList; - QTableWidget* myDataTbl; + QList myDataTblList; QLabel* myMaxLbl; + + QStackedWidget* myStepWgt; + + QStringList myCompNamesList; + + QList mySelection; + + QPushButton* myRemoveBtn; }; #endif \ No newline at end of file -- 2.39.2