From: vsv Date: Wed, 9 Apr 2014 06:53:48 +0000 (+0400) Subject: Data model preliminary version X-Git-Tag: V_0.1~31^2~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c5a608bb7f2f754085388cd0fa4e3971239f95c5;p=modules%2Fshaper.git Data model preliminary version --- diff --git a/src/Model/Model_Document.cxx b/src/Model/Model_Document.cxx index 7c2077cb9..b7f195242 100644 --- a/src/Model/Model_Document.cxx +++ b/src/Model/Model_Document.cxx @@ -226,8 +226,8 @@ shared_ptr Model_Document::feature(const string& theGroupID, c { // TODO: optimize this method shared_ptr anIter = featuresIterator(theGroupID); - for(int a = 0; a != theIndex; anIter->next()) a++; - return anIter->current(); + for(int a = 0; a != theIndex && anIter->more(); anIter->next()) a++; + return anIter->more() ? anIter->current() : shared_ptr(); } const vector& Model_Document::getGroups() const diff --git a/src/XGUI/CMakeLists.txt b/src/XGUI/CMakeLists.txt index 713f9b905..f7becf207 100644 --- a/src/XGUI/CMakeLists.txt +++ b/src/XGUI/CMakeLists.txt @@ -20,7 +20,7 @@ SET(PROJECT_HEADERS XGUI_ViewBackground.h XGUI_WidgetFactory.h XGUI_DocumentDataModel.h - XGUI_DataModel.h + XGUI_PartDataModel.h XGUI_ObjectsBrowser.h ) @@ -44,7 +44,7 @@ SET(PROJECT_SOURCES XGUI_ViewBackground.cpp XGUI_WidgetFactory.cpp XGUI_DocumentDataModel.cpp - XGUI_DataModel.cpp + XGUI_PartDataModel.cpp XGUI_ObjectsBrowser.cpp ) @@ -63,6 +63,7 @@ SET(PROJECT_LIBRARIES ${Qt5Widgets_LIBRARIES} ${CAS_VIEWER} ${CAS_KERNEL} + ${CAS_OCAF} PyConsole PyInterp PyEvent @@ -83,6 +84,7 @@ INCLUDE_DIRECTORIES (${PROJECT_SOURCE_DIR}/src/Event ${PROJECT_SOURCE_DIR}/src/PyInterp ${PROJECT_SOURCE_DIR}/src/PyConsole ${PROJECT_SOURCE_DIR}/src/ModelAPI + ${PROJECT_SOURCE_DIR}/src/Model ${CAS_INCLUDE_DIRS}) LINK_DIRECTORIES($ENV{PYTHON_LIB_DIR}) diff --git a/src/XGUI/XGUI_DataModel.cpp b/src/XGUI/XGUI_DataModel.cpp deleted file mode 100644 index 7dc78aa87..000000000 --- a/src/XGUI/XGUI_DataModel.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "XGUI_DataModel.h" - - -XGUI_DataModel::XGUI_DataModel(QObject* theParent) - : QAbstractItemModel(theParent) -{ -} - - -XGUI_DataModel::~XGUI_DataModel() -{ -} diff --git a/src/XGUI/XGUI_DataModel.h b/src/XGUI/XGUI_DataModel.h deleted file mode 100644 index 106a7f0cc..000000000 --- a/src/XGUI/XGUI_DataModel.h +++ /dev/null @@ -1,15 +0,0 @@ - -#ifndef XGUI_DataModel_H -#define XGUI_DataModel_H - -#include - -class XGUI_DataModel : public QAbstractItemModel -{ - Q_OBJECT -public: - XGUI_DataModel(QObject* theParent); - virtual ~XGUI_DataModel(); -}; - -#endif \ No newline at end of file diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index ccb90aae2..b2376b9aa 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -1,20 +1,29 @@ #include "XGUI_DocumentDataModel.h" +#include "XGUI_PartDataModel.h" #include #include #include +#include +#include +#include +#include +#include + XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent) - : QAbstractItemModel(theParent), - myParamsFolder(0), - myConstructFolder(0) + : QAbstractItemModel(theParent) { - //std::shared_ptr myRoot = aMgr->createFeature("Point"); std::shared_ptr aMgr = ModelAPI_PluginManager::get(); myDocument = aMgr->currentDocument(); + + Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED)); + + myModel = new XGUI_TopDataModel(this); + myModel->setDocument(myDocument); } @@ -23,79 +32,91 @@ XGUI_DocumentDataModel::~XGUI_DocumentDataModel() } -QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const +void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage) { - switch (theRole) { - case Qt::DisplayRole: - // return a name - if (theIndex.internalId() == quintptr(&myParamsFolder)) - return "Parameters"; - else if (theIndex.internalId() == quintptr(&myConstructFolder)) - return "Constructions"; - else if (theIndex.internalId() == 0) { - return "Part"; + beginResetModel(); + int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft(); + if (myPartModels.size() != aNbParts) { // resize internal models + while (myPartModels.size() > aNbParts) { + delete myPartModels.last(); + myPartModels.removeLast(); + } + while (myPartModels.size() < aNbParts) { + myPartModels.append(new XGUI_PartDataModel(this)); } - break; - case Qt::DecorationRole: - // return an Icon - break; - case Qt::ToolTipRole: - // return Tooltip - break; + for (int i = 0; i < myPartModels.size(); i++) + myPartModels.at(i)->setDocument(myDocument, i); } - return QVariant(); + endResetModel(); } +QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const +{ + if (!theIndex.isValid()) + return QVariant(); + return toSourceModel(theIndex).data(theRole); +} -QVariant XGUI_DocumentDataModel::headerData(int section, Qt::Orientation orientation, int role) const + +QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const { return QVariant(); } -int XGUI_DocumentDataModel::rowCount(const QModelIndex &parent) const +int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const { - std::shared_ptr aIt = myDocument->featuresIterator(PARTS_GROUP); - int a = aIt->numIterationsLeft(); - return aIt->numIterationsLeft() + 2; + if (!theParent.isValid()) + return myModel->rowCount(theParent) + myPartModels.size(); + + return 0; } -int XGUI_DocumentDataModel::columnCount(const QModelIndex &parent) const +int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const { return 1; } QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const { - switch (theRow) { - case 0: - return createIndex(theRow, theColumn, (quintptr) &myParamsFolder); - case 1: - return createIndex(theRow, theColumn, (quintptr) &myConstructFolder); - default: - { - std::shared_ptr aIt = myDocument->featuresIterator(PARTS_GROUP); - if (aIt->numIterationsLeft() <= (theRow - 1)) { - return createIndex(theRow, theColumn, (quintptr) 0); - } - } + QModelIndex aIndex; + if (!theParent.isValid()) { + int aOffs = myModel->rowCount(); + if (theRow < aOffs) + aIndex = myModel->index(theRow, theColumn, theParent); + else + aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent); + aIndex = createIndex(theRow, theColumn, aIndex.internalId()); } - return QModelIndex(); + return aIndex; } -QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex &index) const +QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const { return QModelIndex(); } + bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const { if (!theParent.isValid()) return true; - - if (theParent.internalId() == quintptr(&myParamsFolder)) - return myDocument->featuresIterator(PARAMETERS_GROUP)->more(); - if (theParent.internalId() == quintptr(&myConstructFolder)) - return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->more(); return false; -} \ No newline at end of file +} + + +QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const +{ + int aRow = theProxy.row(); + if (!theProxy.parent().isValid()) { + if (aRow < myModel->rowCount()) { + return myModel->index(aRow, 0); + } else { + int aOffs = aRow - myModel->rowCount(); + return myPartModels.at(aOffs)->index(aOffs, 0); + } + } + return QModelIndex(); +} + + diff --git a/src/XGUI/XGUI_DocumentDataModel.h b/src/XGUI/XGUI_DocumentDataModel.h index e6f8a787e..796a8e51a 100644 --- a/src/XGUI/XGUI_DocumentDataModel.h +++ b/src/XGUI/XGUI_DocumentDataModel.h @@ -3,37 +3,50 @@ #define XGUI_DocumentDataModel_H #include +#include + +#include class ModelAPI_Document; +class XGUI_PartDataModel; +class XGUI_TopDataModel; + -class XGUI_DocumentDataModel : public QAbstractItemModel +class XGUI_DocumentDataModel : public QAbstractItemModel, public Event_Listener { Q_OBJECT public: XGUI_DocumentDataModel(QObject* theParent); virtual ~XGUI_DocumentDataModel(); + // Event Listener method + virtual void processEvent(const Event_Message* theMessage); + virtual QVariant data(const QModelIndex& theIndex, int theRole) const; - virtual QVariant headerData(int section, Qt::Orientation orientation, - int role = Qt::DisplayRole) const; + virtual QVariant headerData(int theSection, Qt::Orientation theOrient, + int theRole = Qt::DisplayRole) const; - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; + virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const; + virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const; virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &parent = QModelIndex()) const; - virtual QModelIndex parent(const QModelIndex &index) const; + virtual QModelIndex parent(const QModelIndex& theIndex) const; virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; private: - //std::shared_ptr myRoot; - short myParamsFolder; - short myConstructFolder; + + QModelIndex toSourceModel(const QModelIndex& theProxy) const; + QModelIndex fromSourceModel(const QModelIndex& theSource) const; + std::shared_ptr myDocument; + + XGUI_TopDataModel* myModel; + QList myPartModels; }; #endif \ No newline at end of file diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 926929063..5b6547c63 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -1,13 +1,23 @@ #include "XGUI_ObjectsBrowser.h" #include "XGUI_DocumentDataModel.h" +//#include "XGUI_PartDataModel.h" +//#include +//#include +//#include +//#include +//#include +//#include XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent) : QTreeView(theParent) { setHeaderHidden(true); - myDocModel = new XGUI_DocumentDataModel(this); - setModel(myDocModel); + XGUI_DocumentDataModel* aDocModel = new XGUI_DocumentDataModel(this); + //std::shared_ptr aMgr = ModelAPI_PluginManager::get(); + //std::shared_ptr aDocument = aMgr->currentDocument(); + //aDocModel->setDocument(aDocument, 0); + setModel(aDocModel); } diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index 75d12e5b6..c2d56ee38 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -13,11 +13,11 @@ public: XGUI_ObjectsBrowser(QWidget* theParent); virtual ~XGUI_ObjectsBrowser(); - XGUI_DocumentDataModel* dataModel() const { return myDocModel; } + QAbstractItemModel* dataModel() const { return myDocModel; } private: - XGUI_DocumentDataModel* myDocModel; + QAbstractItemModel* myDocModel; }; #endif \ No newline at end of file diff --git a/src/XGUI/XGUI_PartDataModel.cpp b/src/XGUI/XGUI_PartDataModel.cpp new file mode 100644 index 000000000..61e56130c --- /dev/null +++ b/src/XGUI/XGUI_PartDataModel.cpp @@ -0,0 +1,261 @@ +#include "XGUI_PartDataModel.h" + +#include +#include +#include +#include +#include +#include + +#include + +XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent) + : QAbstractItemModel(theParent) +{ +} + +XGUI_TopDataModel::~XGUI_TopDataModel() +{ +} + + +QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const +{ + switch (theRole) { + case Qt::DisplayRole: + // return a name + if (theIndex.model() == this) { + if (theIndex.internalId() == ParamsFolder) + return tr("Parameters"); + + if (theIndex.internalId() == ParamObject) { + std::shared_ptr aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row()); + return aFeature->data()->getName().c_str(); + } + if (theIndex.internalId() == ConstructFolder) + return tr("Constructions"); + + if (theIndex.internalId() == ConstructObject) { + std::shared_ptr aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()); + return aFeature->data()->getName().c_str(); + } + } + break; + + case Qt::DecorationRole: + // return an Icon + if (theIndex.model() == this) { + if (theIndex.internalId() == ParamsFolder) + return QIcon(":pictures/params_folder.png"); + else if (theIndex.internalId() == ConstructFolder) + return QIcon(":pictures/constr_folder.png"); + } + break; + + case Qt::ToolTipRole: + // return Tooltip + break; + } + return QVariant(); +} + +QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + return QVariant(); +} + +int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const +{ + if (!theParent.isValid()) + return 2; + + if (theParent.internalId() == ParamsFolder) + return myDocument->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft(); + + if (theParent.internalId() == ConstructFolder) + return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft(); + + return 0; +} + +int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const +{ + return 1; +} + +QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const +{ + if (!theParent.isValid()) { + switch (theRow) { + case 0: + return createIndex(theRow, theColumn, (quintptr) ParamsFolder); + case 1: + return createIndex(theRow, theColumn, (quintptr) ConstructFolder); + } + } else { + if (theParent.internalId() == ParamsFolder) + return createIndex(theRow, theColumn, (quintptr) ParamObject); + + if (theParent.internalId() == ConstructFolder) + return createIndex(theRow, theColumn, (quintptr) ConstructObject); + } + return QModelIndex(); +} + +QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const +{ + int aId = (int)theIndex.internalId(); + switch (aId) { + case ParamsFolder: + case ConstructFolder: + return QModelIndex(); + case ParamObject: + return createIndex(0, 0, (quintptr) ParamsFolder); + case ConstructObject: + return createIndex(1, 0, (quintptr) ConstructFolder); + } + return QModelIndex(); +} + +bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const +{ + if (!theParent.isValid()) + return true; + + int aId = (int)theParent.internalId(); + switch (aId) { + case ParamsFolder: + return myDocument->featuresIterator(PARAMETERS_GROUP)->more(); + case ConstructFolder: + return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->more(); + case ParamObject: + case ConstructObject: + return false; + } + return false; +} + + +//****************************************************************** +//****************************************************************** +//****************************************************************** +XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent) + : QAbstractItemModel(theParent) +{ +} + + +XGUI_PartDataModel::~XGUI_PartDataModel() +{ +} + +QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const +{ + switch (theRole) { + case Qt::DisplayRole: + // return a name + if (theIndex.internalId() == MyRoot) { + std::shared_ptr aFeature = myDocument->feature(PARTS_GROUP, myId); + return aFeature->data()->getName().c_str(); + } + if (theIndex.internalId() == ParamsFolder) + return tr("Parameters"); + if (theIndex.internalId() == ConstructFolder) + return tr("Constructions"); + break; + case Qt::DecorationRole: + // return an Icon + if (theIndex.internalId() == MyRoot) + return QIcon(":pictures/part_ico.png"); + if (theIndex.internalId() == ParamsFolder) + return QIcon(":pictures/params_folder.png"); + if (theIndex.internalId() == ConstructFolder) + return QIcon(":pictures/constr_folder.png"); + break; + case Qt::ToolTipRole: + // return Tooltip + break; + } + return QVariant(); +} + +QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + return QVariant(); +} + +int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const +{ + if (!parent.isValid()) + if (myDocument->feature(PARTS_GROUP, myId)) + return 1; + else + return 0; + if (parent.internalId() == MyRoot) + return 2; + return 0; +} + +int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const +{ + return 1; +} + +QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const +{ + if (!theParent.isValid()) + return createIndex(theRow, 0, (quintptr) MyRoot); + + int aId = (int)theParent.internalId(); + switch (aId) { + case MyRoot: + switch (theRow) { + case 0: + return createIndex(0, 0, (quintptr) ParamsFolder); + case 1: + return createIndex(1, 0, (quintptr) ConstructFolder); + } + case ParamsFolder: + return createIndex(theRow, 0, (quintptr) ParamObject); + case ConstructFolder: + return createIndex(theRow, 0, (quintptr) ConstructObject); + } + return QModelIndex(); +} + +QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const +{ + int aId = (int)theIndex.internalId(); + switch (aId) { + case MyRoot: + return QModelIndex(); + case ParamsFolder: + case ConstructFolder: + return createIndex(0, 0, (quintptr) MyRoot); + case ParamObject: + return createIndex(0, 0, (quintptr) ParamsFolder); + case ConstructObject: + return createIndex(1, 0, (quintptr) ConstructFolder); + } + return QModelIndex(); +} + +bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const +{ + if (!theParent.isValid()) + return myDocument->feature(PARTS_GROUP, myId); + + int aId = (int)theParent.internalId(); + switch (aId) { + case MyRoot: + return true; + case ParamsFolder: + return false; // TODO + case ConstructFolder: + return false; // TODO + case ParamObject: + case ConstructObject: + return false; + } + return false; +} diff --git a/src/XGUI/XGUI_PartDataModel.h b/src/XGUI/XGUI_PartDataModel.h new file mode 100644 index 000000000..fc597f034 --- /dev/null +++ b/src/XGUI/XGUI_PartDataModel.h @@ -0,0 +1,91 @@ + +#ifndef XGUI_PartDataModel_H +#define XGUI_PartDataModel_H + +#include + +class ModelAPI_Feature; +class ModelAPI_Document; + +class XGUI_TopDataModel : public QAbstractItemModel +{ + Q_OBJECT +public: + XGUI_TopDataModel(QObject* theParent); + virtual ~XGUI_TopDataModel(); + + virtual void setDocument(const std::shared_ptr& theDoc) + { + myDocument = theDoc; + } + + // Reimplementation from QAbstractItemModel + virtual QVariant data(const QModelIndex& theIndex, int theRole) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; + + virtual QModelIndex index(int theRow, int theColumn, + const QModelIndex& theParent = QModelIndex()) const; + + virtual QModelIndex parent(const QModelIndex& theIndex) const; + + virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + +private: + enum DataIds { + ParamsFolder, + ParamObject, + ConstructFolder, + ConstructObject + }; + + std::shared_ptr myDocument; +}; + + + +class XGUI_PartDataModel : public QAbstractItemModel +{ + Q_OBJECT +public: + XGUI_PartDataModel(QObject* theParent); + virtual ~XGUI_PartDataModel(); + + virtual void setDocument(const std::shared_ptr& theDoc, int theId) + { + myDocument = theDoc; + myId = theId; + } + + // Reimplementation from QAbstractItemModel + virtual QVariant data(const QModelIndex& theIndex, int theRole) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; + + virtual QModelIndex index(int theRow, int theColumn, + const QModelIndex& theParent = QModelIndex()) const; + + virtual QModelIndex parent(const QModelIndex& theIndex) const; + + virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const; + +private: + enum DataIds { + MyRoot, + ParamsFolder, + ParamObject, + ConstructFolder, + ConstructObject + }; + + std::shared_ptr myDocument; + int myId; +}; + +#endif \ No newline at end of file diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 8b3fd9ac3..0aef70027 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include @@ -61,7 +63,11 @@ void XGUI_Workshop::startApplication() myMainWindow->showPythonConsole(); std::shared_ptr aMgr = ModelAPI_PluginManager::get(); - std::shared_ptr myRoot = aMgr->createFeature("Point"); + //std::shared_ptr aPoint1 = aMgr->rootDocument()->addFeature("Point"); + std::shared_ptr aPart = aMgr->rootDocument()->addFeature("Part"); + aPart->execute(); + //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value()); + //std::shared_ptr aPoint2 = aMgr->rootDocument()->addFeature("Point"); } //******************************************************