From: vsv Date: Wed, 8 Jul 2015 12:12:41 +0000 (+0300) Subject: Make Sketch of Complex boolean as sub-object of the complex Boolean feature X-Git-Tag: V_1.3.0~78 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d9e58981abfe2d72c00c285ba40b63135629aa86;p=modules%2Fshaper.git Make Sketch of Complex boolean as sub-object of the complex Boolean feature --- diff --git a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp index 7d922390e..f8fff4cdd 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp @@ -39,14 +39,14 @@ std::shared_ptr FeaturesPlugin_CompositeBoolean::addFeature(st } //================================================================================================= -int FeaturesPlugin_CompositeBoolean::numberOfSubs() const +int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const { ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value(); return aObj.get()? 1 : 0; } //================================================================================================= -std::shared_ptr FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex) const +std::shared_ptr FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) const { if (theIndex == 0) return std::dynamic_pointer_cast(data()->reference(SKETCH_OBJECT_ID())->value()); diff --git a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.h b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.h index 88a586040..c5ef7f3e6 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.h +++ b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.h @@ -43,10 +43,10 @@ class FeaturesPlugin_CompositeBoolean : public ModelAPI_CompositeFeature FEATURESPLUGIN_EXPORT virtual std::shared_ptr addFeature(std::string theID); /// \return the number of sub-elements. - FEATURESPLUGIN_EXPORT virtual int numberOfSubs() const; + FEATURESPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const; /// \return the sub-feature by zero-base index. - FEATURESPLUGIN_EXPORT virtual std::shared_ptr subFeature(const int theIndex) const; + FEATURESPLUGIN_EXPORT virtual std::shared_ptr subFeature(const int theIndex, bool forTree = false) const; /// \return the sub-feature unique identifier in this composite feature by zero-base index. FEATURESPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const; diff --git a/src/ModelAPI/ModelAPI_CompositeFeature.h b/src/ModelAPI/ModelAPI_CompositeFeature.h index f243cb333..193a73af8 100644 --- a/src/ModelAPI/ModelAPI_CompositeFeature.h +++ b/src/ModelAPI/ModelAPI_CompositeFeature.h @@ -25,10 +25,10 @@ public: virtual std::shared_ptr addFeature(std::string theID) = 0; /// Returns the number of sub-elements - virtual int numberOfSubs() const = 0; + virtual int numberOfSubs(bool forTree = false) const = 0; /// Returns the sub-feature by zero-base index - virtual std::shared_ptr subFeature(const int theIndex) const = 0; + virtual std::shared_ptr subFeature(const int theIndex, bool forTree = false) const = 0; /// Returns the sub-feature unique identifier in this composite feature by zero-base index virtual int subFeatureId(const int theIndex) const = 0; diff --git a/src/ModelAPI/ModelAPI_Tools.cpp b/src/ModelAPI/ModelAPI_Tools.cpp index cbe1290a8..2390d72d5 100644 --- a/src/ModelAPI/ModelAPI_Tools.cpp +++ b/src/ModelAPI/ModelAPI_Tools.cpp @@ -166,4 +166,19 @@ ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub) return ResultPtr(); } +CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature) +{ + if (theFeature.get() && theFeature->data()->isValid()) { + const std::set > aRefs = theFeature->data()->refsToMe(); + std::set >::const_iterator aRefIter = aRefs.begin(); + for(; aRefIter != aRefs.end(); aRefIter++) { + CompositeFeaturePtr aComp = std::dynamic_pointer_cast + ((*aRefIter)->owner()); + if (aComp.get() && aComp->isSub(theFeature)) + return aComp; + } + } + return CompositeFeaturePtr(); // not found +} + } // namespace ModelAPI_Tools diff --git a/src/ModelAPI/ModelAPI_Tools.h b/src/ModelAPI/ModelAPI_Tools.h index 15f4afc1d..67b0427be 100644 --- a/src/ModelAPI/ModelAPI_Tools.h +++ b/src/ModelAPI/ModelAPI_Tools.h @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -42,6 +43,13 @@ MODELAPI_EXPORT void findRandomColor(std::vector& theValues); */ MODELAPI_EXPORT ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub); +/*! + * Returns the cpomposite feature - parent of this feature. + * \param theFeature the sub-element of composite + * \returns null if it is not sub-element of composite + */ +MODELAPI_EXPORT CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature); + } #endif diff --git a/src/PartSet/CMakeLists.txt b/src/PartSet/CMakeLists.txt index 1a9d16288..47ba5390a 100644 --- a/src/PartSet/CMakeLists.txt +++ b/src/PartSet/CMakeLists.txt @@ -30,6 +30,7 @@ SET(PROJECT_HEADERS PartSet_PartDataModel.h PartSet_DataTreeModel.h PartSet_WidgetSketchCreator.h + PartSet_TopDataModel.h ) SET(PROJECT_SOURCES @@ -55,6 +56,7 @@ SET(PROJECT_SOURCES PartSet_DocumentDataModel.cpp PartSet_PartDataModel.cpp PartSet_WidgetSketchCreator.cpp + PartSet_TopDataModel.cpp ) SET(PROJECT_RESOURCES diff --git a/src/PartSet/PartSet_DocumentDataModel.cpp b/src/PartSet/PartSet_DocumentDataModel.cpp index 26668d6e7..3ffc164cb 100644 --- a/src/PartSet/PartSet_DocumentDataModel.cpp +++ b/src/PartSet/PartSet_DocumentDataModel.cpp @@ -2,6 +2,7 @@ #include "PartSet_DocumentDataModel.h" #include "PartSet_PartDataModel.h" +#include "PartSet_TopDataModel.h" #include "PartSet_Module.h" //#include "XGUI_Tools.h" diff --git a/src/PartSet/PartSet_PartDataModel.cpp b/src/PartSet/PartSet_PartDataModel.cpp index 12e7312da..d1a48fdf5 100644 --- a/src/PartSet/PartSet_PartDataModel.cpp +++ b/src/PartSet/PartSet_PartDataModel.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -25,229 +26,6 @@ #include -PartSet_TopDataModel::PartSet_TopDataModel(QObject* theParent) - : PartSet_FeaturesModel(theParent) -{ -} - -PartSet_TopDataModel::~PartSet_TopDataModel() -{ -} - -QVariant PartSet_TopDataModel::data(const QModelIndex& theIndex, int theRole) const -{ - if (theIndex.column() == 1) - return QVariant(); - - switch (theRole) { - case Qt::DisplayRole: - // return a name - switch (theIndex.internalId()) { - case ParamsFolder: - return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex)); - case ParamObject: { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row()); - if (aObject) { - ResultParameterPtr aParam = std::dynamic_pointer_cast(aObject); - AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); - QString aVal = QString::number(aValueAttribute->value()); - QString aTitle = QString(aObject->data()->name().c_str()); - return aTitle + " = " + aVal; - } - } - break; - case ConstructFolder: - return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex)); - case ConstructObject: { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(), - theIndex.row()); - if (aObject) - return aObject->data()->name().c_str(); - } - break; - //case GroupsFolder: - // return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex)); - //case GroupObject: { - // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - // ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultGroup::group(), - // theIndex.row()); - // if (aObject) - // return aObject->data()->name().c_str(); - //} - // break; - } - break; - - case Qt::DecorationRole: - { - // return an Icon - switch (theIndex.internalId()) { - case ParamsFolder: - return QIcon(":pictures/params_folder.png"); - case ConstructFolder: - return QIcon(":pictures/constr_folder.png"); - case ConstructObject: - return QIcon(":pictures/constr_object.png"); - //case GroupsFolder: - // return QIcon(":pictures/constr_folder.png"); - } - } - break; - - case Qt::ToolTipRole: - // return Tooltip - break; - case Qt::ForegroundRole: - return QBrush(myItemsColor); - break; - } - return QVariant(); -} - -QVariant PartSet_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - return QVariant(); -} - -int PartSet_TopDataModel::rowCount(const QModelIndex& theParent) const -{ - if (!theParent.isValid()) - return 2; // In case of groups using it has to be +1 - - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - if (theParent.internalId() == ParamsFolder) - return aRootDoc->size(ModelAPI_ResultParameter::group()); - - if (theParent.internalId() == ConstructFolder) - return aRootDoc->size(ModelAPI_ResultConstruction::group()); - - //if (theParent.internalId() == GroupsFolder) - // return aRootDoc->size(ModelAPI_ResultGroup::group()); - - return 0; -} - -int PartSet_TopDataModel::columnCount(const QModelIndex &parent) const -{ - return 1; -} - -QModelIndex PartSet_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const -{ - if (!theParent.isValid()) { - switch (theRow) { - case 0: - return createIndex(theRow, theColumn, (qint32) ParamsFolder); - case 1: - return createIndex(theRow, theColumn, (qint32) ConstructFolder); - //case 2: - // return createIndex(theRow, theColumn, (qint32) GroupsFolder); - } - } else { - if (theParent.internalId() == ParamsFolder) - return createIndex(theRow, theColumn, (qint32) ParamObject); - - if (theParent.internalId() == ConstructFolder) - return createIndex(theRow, theColumn, (qint32) ConstructObject); - - //if (theParent.internalId() == GroupsFolder) - // return createIndex(theRow, theColumn, (qint32) GroupObject); - } - return QModelIndex(); -} - -QModelIndex PartSet_TopDataModel::parent(const QModelIndex& theIndex) const -{ - int aId = (int) theIndex.internalId(); - switch (aId) { - case ParamsFolder: - case ConstructFolder: - //case GroupsFolder: - return QModelIndex(); - case ParamObject: - return createIndex(0, 0, (qint32) ParamsFolder); - case ConstructObject: - return createIndex(1, 0, (qint32) ConstructFolder); - //case GroupObject: - // return createIndex(2, 0, (qint32) GroupsFolder); - } - return QModelIndex(); -} - -bool PartSet_TopDataModel::hasChildren(const QModelIndex& theParent) const -{ - return rowCount(theParent) > 0; -} - -ObjectPtr PartSet_TopDataModel::object(const QModelIndex& theIndex) const -{ - switch (theIndex.internalId()) { - case ParamsFolder: - case ConstructFolder: - return ObjectPtr(); - case ParamObject: { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - return aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row()); - } - case ConstructObject: { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row()); - } - //case GroupObject: { - // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - // return aRootDoc->object(ModelAPI_ResultGroup::group(), theIndex.row()); - //} - } - return ObjectPtr(); -} - -QModelIndex PartSet_TopDataModel::findParent(const ObjectPtr& theObject) const -{ - return findGroup(theObject->groupName().c_str()); -} - -QModelIndex PartSet_TopDataModel::findGroup(const std::string& theGroup) const -{ - if (theGroup == ModelAPI_ResultParameter::group()) - return createIndex(0, 0, (qint32) ParamsFolder); - if (theGroup == ModelAPI_ResultConstruction::group()) - return createIndex(1, 0, (qint32) ConstructFolder); - //if (theGroup == ModelAPI_ResultGroup::group()) - // return createIndex(2, 0, (qint32) ConstructFolder); - return QModelIndex(); -} - -QModelIndex PartSet_TopDataModel::objectIndex(const ObjectPtr& theObject) const -{ - QModelIndex aIndex; - if (theObject) { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - std::string aGroup = theObject->groupName(); - int aNb = aRootDoc->size(aGroup); - int aRow = -1; - for (int i = 0; i < aNb; i++) { - if (aRootDoc->object(aGroup, i) == theObject) { - aRow = i; - break; - } - } - if (aRow != -1) { - if (aGroup == ModelAPI_ResultParameter::group()) - return createIndex(aRow, 0, (qint32) ParamObject); - if (aGroup == ModelAPI_ResultConstruction::group()) - return createIndex(aRow, 0, (qint32) ConstructObject); - //if (aGroup == ModelAPI_ResultGroup::group()) - // return createIndex(aRow, 0, (qint32) GroupObject); - } - } - return aIndex; -} - -//****************************************************************** -//****************************************************************** -//****************************************************************** PartSet_PartDataModel::PartSet_PartDataModel(QObject* theParent) : PartSet_PartModel(theParent) { @@ -275,6 +53,25 @@ QVariant PartSet_PartDataModel::data(const QModelIndex& theIndex, int theRole) c return QVariant(); } + if (theIndex.internalId() >= 0) { + ObjectPtr aObj = object(theIndex); + switch (theRole) { + case Qt::DisplayRole: + return aObj->data()->name().c_str(); + case Qt::DecorationRole: + { + FeaturePtr aFeature = std::dynamic_pointer_cast(aObj); + if (aFeature) + return PartSet_DocumentDataModel::featureIcon(aFeature); + } + break; + case Qt::ForegroundRole: + if (theIndex.internalId() > lastHistoryRow()) + return QBrush(Qt::lightGray); + return QBrush(myItemsColor); + } + } + switch (theRole) { case Qt::DisplayRole: // return a name @@ -396,6 +193,15 @@ int PartSet_PartDataModel::rowCount(const QModelIndex& parent) const return partDocument()->size(ModelAPI_ResultBody::group()); case GroupsFolder: return partDocument()->size(ModelAPI_ResultGroup::group()); + case HistoryObject: + { + ObjectPtr aObj = object(parent); + CompositeFeaturePtr aCompFeature = + std::dynamic_pointer_cast(aObj); + if (aCompFeature.get()) { + return aCompFeature->numberOfSubs(true); + } + } } return 0; } @@ -437,6 +243,10 @@ QModelIndex PartSet_PartDataModel::index(int theRow, int theColumn, const QModel return createIndex(theRow, theColumn, (qint32) BodiesObject); case GroupsFolder: return createIndex(theRow, theColumn, (qint32) GroupObject); + case HistoryObject: + { + return createIndex(theRow, theColumn, (qint32) theParent.row()); + } } } return QModelIndex(); @@ -444,6 +254,10 @@ QModelIndex PartSet_PartDataModel::index(int theRow, int theColumn, const QModel QModelIndex PartSet_PartDataModel::parent(const QModelIndex& theIndex) const { + if (theIndex.internalId() >= 0) { + int aPRow = theIndex.internalId(); + return createIndex(aPRow, 0, (qint32) HistoryObject); + } switch (theIndex.internalId()) { case ParamsFolder: case ConstructFolder: @@ -479,6 +293,17 @@ DocumentPtr PartSet_PartDataModel::partDocument() const ObjectPtr PartSet_PartDataModel::object(const QModelIndex& theIndex) const { + if (theIndex.internalId() >= 0) { + int aPRow = theIndex.internalId(); + ObjectPtr aObj = + partDocument()->object(ModelAPI_Feature::group(), aPRow - getRowsNumber()); + CompositeFeaturePtr aCompFeature = + std::dynamic_pointer_cast(aObj); + if (aCompFeature.get()) { + return aCompFeature->subFeature(theIndex.row(), true); + } + return ObjectPtr(); + } switch (theIndex.internalId()) { case ParamsFolder: case ConstructFolder: diff --git a/src/PartSet/PartSet_PartDataModel.h b/src/PartSet/PartSet_PartDataModel.h index 9789f79d8..1012f2d6f 100644 --- a/src/PartSet/PartSet_PartDataModel.h +++ b/src/PartSet/PartSet_PartDataModel.h @@ -6,88 +6,6 @@ #include "PartSet.h" #include "PartSet_DataTreeModel.h" -/**\class PartSet_TopDataModel - * \ingroup GUI - * \brief This is a data model for Object Browser (QTreeView). - * It represents only upper part of data tree (non-parts tree items) - */ -class PARTSET_EXPORT PartSet_TopDataModel : public PartSet_FeaturesModel -{ -Q_OBJECT - public: - /// Constructor - /// \param theParent a parent object - PartSet_TopDataModel(QObject* theParent); - virtual ~PartSet_TopDataModel(); - - // Reimpl from QAbstractItemModel - - /// Returns the data stored under the given role for the item referred to by the index. - /// \param theIndex a model index - /// \param theRole a data role (see Qt::ItemDataRole) - virtual QVariant data(const QModelIndex& theIndex, int theRole) const; - - /// Returns the data for the given role and section in the header with the specified orientation. - /// \param theSection a section - /// \param theOrient an orientation - /// \param theRole a data role (see Qt::ItemDataRole) - virtual QVariant headerData(int theSection, Qt::Orientation theOrient, - int theRole = Qt::DisplayRole) const; - - /// Returns the number of rows under the given parent. When the parent is valid it means that - /// rowCount is returning the number of children of parent. - /// \param theParent a parent model index - virtual int rowCount(const QModelIndex &theParent = QModelIndex()) const; - - /// Returns the number of columns for the children of the given parent. - /// It has a one column - /// \param theParent a parent model index - virtual int columnCount(const QModelIndex &theParent = QModelIndex()) const; - - - /// Returns the index of the item in the model specified by the given row, column and parent index. - /// \param theRow a row - /// \param theColumn a column - /// \param theParent a parent model index - virtual QModelIndex index(int theRow, int theColumn, const QModelIndex& theParent = - QModelIndex()) const; - - /// Returns the parent of the model item with the given index. - /// If the item has no parent, an invalid QModelIndex is returned. - /// \param theIndex a model index - virtual QModelIndex parent(const QModelIndex& theIndex) const; - - /// Returns true if parent has any children; otherwise returns false. - /// \param theParent a parent model index - virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; - - //! Returns object by the given Model index. - //! Returns 0 if the given index is not index of a object - virtual ObjectPtr object(const QModelIndex& theIndex) const; - - //! Returns QModelIndex which corresponds to the given object - //! If the object is not found then index is not valid - virtual QModelIndex objectIndex(const ObjectPtr& theObject) const; - - //! Returns parent index of the given object - virtual QModelIndex findParent(const ObjectPtr& theObject) const; - - //! Returns index corresponded to the group - virtual QModelIndex findGroup(const std::string& theGroup) const; - - private: - //! Types of QModelIndexes - enum DataIds - { - ParamsFolder, - ParamObject, - ConstructFolder, - ConstructObject - //GroupsFolder, - //GroupObject - }; - -}; /**\class PartSet_PartDataModel * \ingroup GUI @@ -182,10 +100,11 @@ Q_OBJECT int lastHistoryRow() const; //! Types of QModelIndexes + //! All types have negative Id's. Positive Id means sub-feature and contains row of its parent enum DataIds { //MyRoot, - ParamsFolder, + ParamsFolder = -100, ParamObject, ConstructFolder, ConstructObject, diff --git a/src/PartSet/PartSet_TopDataModel.cpp b/src/PartSet/PartSet_TopDataModel.cpp new file mode 100644 index 000000000..67f8cb8e8 --- /dev/null +++ b/src/PartSet/PartSet_TopDataModel.cpp @@ -0,0 +1,247 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +#include "PartSet_TopDataModel.h" +#include "PartSet_Module.h" +#include "PartSet_DocumentDataModel.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + + +PartSet_TopDataModel::PartSet_TopDataModel(QObject* theParent) + : PartSet_FeaturesModel(theParent) +{ +} + +PartSet_TopDataModel::~PartSet_TopDataModel() +{ +} + +QVariant PartSet_TopDataModel::data(const QModelIndex& theIndex, int theRole) const +{ + if (theIndex.column() == 1) + return QVariant(); + + switch (theRole) { + case Qt::DisplayRole: + // return a name + switch (theIndex.internalId()) { + case ParamsFolder: + return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex)); + case ParamObject: { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row()); + if (aObject) { + ResultParameterPtr aParam = std::dynamic_pointer_cast(aObject); + AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); + QString aVal = QString::number(aValueAttribute->value()); + QString aTitle = QString(aObject->data()->name().c_str()); + return aTitle + " = " + aVal; + } + } + break; + case ConstructFolder: + return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex)); + case ConstructObject: { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(), + theIndex.row()); + if (aObject) + return aObject->data()->name().c_str(); + } + break; + //case GroupsFolder: + // return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex)); + //case GroupObject: { + // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + // ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultGroup::group(), + // theIndex.row()); + // if (aObject) + // return aObject->data()->name().c_str(); + //} + // break; + } + break; + + case Qt::DecorationRole: + { + // return an Icon + switch (theIndex.internalId()) { + case ParamsFolder: + return QIcon(":pictures/params_folder.png"); + case ConstructFolder: + return QIcon(":pictures/constr_folder.png"); + case ConstructObject: + return QIcon(":pictures/constr_object.png"); + //case GroupsFolder: + // return QIcon(":pictures/constr_folder.png"); + } + } + break; + + case Qt::ToolTipRole: + // return Tooltip + break; + case Qt::ForegroundRole: + return QBrush(myItemsColor); + break; + } + return QVariant(); +} + +QVariant PartSet_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + return QVariant(); +} + +int PartSet_TopDataModel::rowCount(const QModelIndex& theParent) const +{ + if (!theParent.isValid()) + return 2; // In case of groups using it has to be +1 + + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + if (theParent.internalId() == ParamsFolder) + return aRootDoc->size(ModelAPI_ResultParameter::group()); + + if (theParent.internalId() == ConstructFolder) + return aRootDoc->size(ModelAPI_ResultConstruction::group()); + + //if (theParent.internalId() == GroupsFolder) + // return aRootDoc->size(ModelAPI_ResultGroup::group()); + + return 0; +} + +int PartSet_TopDataModel::columnCount(const QModelIndex &parent) const +{ + return 1; +} + +QModelIndex PartSet_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const +{ + if (!theParent.isValid()) { + switch (theRow) { + case 0: + return createIndex(theRow, theColumn, (qint32) ParamsFolder); + case 1: + return createIndex(theRow, theColumn, (qint32) ConstructFolder); + //case 2: + // return createIndex(theRow, theColumn, (qint32) GroupsFolder); + } + } else { + if (theParent.internalId() == ParamsFolder) + return createIndex(theRow, theColumn, (qint32) ParamObject); + + if (theParent.internalId() == ConstructFolder) + return createIndex(theRow, theColumn, (qint32) ConstructObject); + + //if (theParent.internalId() == GroupsFolder) + // return createIndex(theRow, theColumn, (qint32) GroupObject); + } + return QModelIndex(); +} + +QModelIndex PartSet_TopDataModel::parent(const QModelIndex& theIndex) const +{ + int aId = (int) theIndex.internalId(); + switch (aId) { + case ParamsFolder: + case ConstructFolder: + //case GroupsFolder: + return QModelIndex(); + case ParamObject: + return createIndex(0, 0, (qint32) ParamsFolder); + case ConstructObject: + return createIndex(1, 0, (qint32) ConstructFolder); + //case GroupObject: + // return createIndex(2, 0, (qint32) GroupsFolder); + } + return QModelIndex(); +} + +bool PartSet_TopDataModel::hasChildren(const QModelIndex& theParent) const +{ + return rowCount(theParent) > 0; +} + +ObjectPtr PartSet_TopDataModel::object(const QModelIndex& theIndex) const +{ + switch (theIndex.internalId()) { + case ParamsFolder: + case ConstructFolder: + return ObjectPtr(); + case ParamObject: { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + return aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row()); + } + case ConstructObject: { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row()); + } + //case GroupObject: { + // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + // return aRootDoc->object(ModelAPI_ResultGroup::group(), theIndex.row()); + //} + } + return ObjectPtr(); +} + +QModelIndex PartSet_TopDataModel::findParent(const ObjectPtr& theObject) const +{ + return findGroup(theObject->groupName().c_str()); +} + +QModelIndex PartSet_TopDataModel::findGroup(const std::string& theGroup) const +{ + if (theGroup == ModelAPI_ResultParameter::group()) + return createIndex(0, 0, (qint32) ParamsFolder); + if (theGroup == ModelAPI_ResultConstruction::group()) + return createIndex(1, 0, (qint32) ConstructFolder); + //if (theGroup == ModelAPI_ResultGroup::group()) + // return createIndex(2, 0, (qint32) ConstructFolder); + return QModelIndex(); +} + +QModelIndex PartSet_TopDataModel::objectIndex(const ObjectPtr& theObject) const +{ + QModelIndex aIndex; + if (theObject) { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + std::string aGroup = theObject->groupName(); + int aNb = aRootDoc->size(aGroup); + int aRow = -1; + for (int i = 0; i < aNb; i++) { + if (aRootDoc->object(aGroup, i) == theObject) { + aRow = i; + break; + } + } + if (aRow != -1) { + if (aGroup == ModelAPI_ResultParameter::group()) + return createIndex(aRow, 0, (qint32) ParamObject); + if (aGroup == ModelAPI_ResultConstruction::group()) + return createIndex(aRow, 0, (qint32) ConstructObject); + //if (aGroup == ModelAPI_ResultGroup::group()) + // return createIndex(aRow, 0, (qint32) GroupObject); + } + } + return aIndex; +} + diff --git a/src/PartSet/PartSet_TopDataModel.h b/src/PartSet/PartSet_TopDataModel.h new file mode 100644 index 000000000..8b72db69e --- /dev/null +++ b/src/PartSet/PartSet_TopDataModel.h @@ -0,0 +1,91 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> +#ifndef PartSet_TopDataModel_H +#define PartSet_TopDataModel_H + +#include "PartSet.h" +#include "PartSet_DataTreeModel.h" + +/**\class PartSet_TopDataModel + * \ingroup GUI + * \brief This is a data model for Object Browser (QTreeView). + * It represents only upper part of data tree (non-parts tree items) + */ +class PARTSET_EXPORT PartSet_TopDataModel : public PartSet_FeaturesModel +{ +Q_OBJECT + public: + /// Constructor + /// \param theParent a parent object + PartSet_TopDataModel(QObject* theParent); + virtual ~PartSet_TopDataModel(); + + // Reimpl from QAbstractItemModel + + /// Returns the data stored under the given role for the item referred to by the index. + /// \param theIndex a model index + /// \param theRole a data role (see Qt::ItemDataRole) + virtual QVariant data(const QModelIndex& theIndex, int theRole) const; + + /// Returns the data for the given role and section in the header with the specified orientation. + /// \param theSection a section + /// \param theOrient an orientation + /// \param theRole a data role (see Qt::ItemDataRole) + virtual QVariant headerData(int theSection, Qt::Orientation theOrient, + int theRole = Qt::DisplayRole) const; + + /// Returns the number of rows under the given parent. When the parent is valid it means that + /// rowCount is returning the number of children of parent. + /// \param theParent a parent model index + virtual int rowCount(const QModelIndex &theParent = QModelIndex()) const; + + /// Returns the number of columns for the children of the given parent. + /// It has a one column + /// \param theParent a parent model index + virtual int columnCount(const QModelIndex &theParent = QModelIndex()) const; + + + /// Returns the index of the item in the model specified by the given row, column and parent index. + /// \param theRow a row + /// \param theColumn a column + /// \param theParent a parent model index + virtual QModelIndex index(int theRow, int theColumn, const QModelIndex& theParent = + QModelIndex()) const; + + /// Returns the parent of the model item with the given index. + /// If the item has no parent, an invalid QModelIndex is returned. + /// \param theIndex a model index + virtual QModelIndex parent(const QModelIndex& theIndex) const; + + /// Returns true if parent has any children; otherwise returns false. + /// \param theParent a parent model index + virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + + //! Returns object by the given Model index. + //! Returns 0 if the given index is not index of a object + virtual ObjectPtr object(const QModelIndex& theIndex) const; + + //! Returns QModelIndex which corresponds to the given object + //! If the object is not found then index is not valid + virtual QModelIndex objectIndex(const ObjectPtr& theObject) const; + + //! Returns parent index of the given object + virtual QModelIndex findParent(const ObjectPtr& theObject) const; + + //! Returns index corresponded to the group + virtual QModelIndex findGroup(const std::string& theGroup) const; + + private: + //! Types of QModelIndexes + enum DataIds + { + ParamsFolder, + ParamObject, + ConstructFolder, + ConstructObject + //GroupsFolder, + //GroupObject + }; + +}; + +#endif \ No newline at end of file diff --git a/src/PartSetPlugin/PartSetPlugin_Part.cpp b/src/PartSetPlugin/PartSetPlugin_Part.cpp index f8b348a37..6691f20fa 100644 --- a/src/PartSetPlugin/PartSetPlugin_Part.cpp +++ b/src/PartSetPlugin/PartSetPlugin_Part.cpp @@ -61,7 +61,7 @@ std::shared_ptr PartSetPlugin_Part::addFeature(std::string the return FeaturePtr(); } -int PartSetPlugin_Part::numberOfSubs() const +int PartSetPlugin_Part::numberOfSubs(bool forTree) const { ResultPartPtr aResult = std::dynamic_pointer_cast(firstResult()); if (aResult.get()) { @@ -72,7 +72,7 @@ int PartSetPlugin_Part::numberOfSubs() const return 0; } -std::shared_ptr PartSetPlugin_Part::subFeature(const int theIndex) const +std::shared_ptr PartSetPlugin_Part::subFeature(const int theIndex, bool forTree) const { ResultPartPtr aResult = std::dynamic_pointer_cast(firstResult()); if (aResult.get()) { diff --git a/src/PartSetPlugin/PartSetPlugin_Part.h b/src/PartSetPlugin/PartSetPlugin_Part.h index a07be67e1..0610a68ab 100644 --- a/src/PartSetPlugin/PartSetPlugin_Part.h +++ b/src/PartSetPlugin/PartSetPlugin_Part.h @@ -53,10 +53,10 @@ class PartSetPlugin_Part : public ModelAPI_CompositeFeature virtual std::shared_ptr addFeature(std::string theID); /// Returns the number of sub-features of the document - virtual int numberOfSubs() const; + virtual int numberOfSubs(bool forTree = false) const; /// Returns the sub-feature by zero-base index - virtual std::shared_ptr subFeature(const int theIndex) const; + virtual std::shared_ptr subFeature(const int theIndex, bool forTree = false) const; /// Returns the sub-feature unique identifier in this composite feature by zero-base index virtual int subFeatureId(const int theIndex) const; diff --git a/src/SketchPlugin/SketchPlugin_Sketch.cpp b/src/SketchPlugin/SketchPlugin_Sketch.cpp index 97cf4e0fd..84170754a 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.cpp +++ b/src/SketchPlugin/SketchPlugin_Sketch.cpp @@ -159,12 +159,12 @@ void SketchPlugin_Sketch::removeFeature(std::shared_ptr theFea data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr()); } -int SketchPlugin_Sketch::numberOfSubs() const +int SketchPlugin_Sketch::numberOfSubs(bool forTree) const { return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size(); } -std::shared_ptr SketchPlugin_Sketch::subFeature(const int theIndex) const +std::shared_ptr SketchPlugin_Sketch::subFeature(const int theIndex, bool forTree) const { ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex); return std::dynamic_pointer_cast(anObj); diff --git a/src/SketchPlugin/SketchPlugin_Sketch.h b/src/SketchPlugin/SketchPlugin_Sketch.h index d4117c093..0512b12e9 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.h +++ b/src/SketchPlugin/SketchPlugin_Sketch.h @@ -180,11 +180,11 @@ class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_ICu virtual void removeFeature(std::shared_ptr theFeature); /// Returns the number of sub-elements - SKETCHPLUGIN_EXPORT virtual int numberOfSubs() const; + SKETCHPLUGIN_EXPORT virtual int numberOfSubs(bool forTree = false) const; /// Returns the sub-feature by zero-base index SKETCHPLUGIN_EXPORT virtual std::shared_ptr - subFeature(const int theIndex) const; + subFeature(const int theIndex, bool forTree = false) const; /// Returns the sub-feature unique identifier in this composite feature by zero-base index SKETCHPLUGIN_EXPORT virtual int subFeatureId(const int theIndex) const;