X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FXGUI%2FXGUI_DocumentDataModel.cpp;h=1ee46e331e9d07ca4d2c47980bf40cdea6af65a0;hb=d86c77d1c6210bbe04fbc3e5b00f9e212e1ec930;hp=ccb90aae2a3e3afe9393656009888dc288ce2169;hpb=b59f7dcc2d937516200929ad5bb183085febd459;p=modules%2Fshaper.git diff --git a/src/XGUI/XGUI_DocumentDataModel.cpp b/src/XGUI/XGUI_DocumentDataModel.cpp index ccb90aae2..1ee46e331 100644 --- a/src/XGUI/XGUI_DocumentDataModel.cpp +++ b/src/XGUI/XGUI_DocumentDataModel.cpp @@ -1,101 +1,298 @@ #include "XGUI_DocumentDataModel.h" +#include "XGUI_PartDataModel.h" #include #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(); + // Find Document object + boost::shared_ptr aMgr = ModelAPI_PluginManager::get(); myDocument = aMgr->currentDocument(); + + // Register in event loop + Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED)); + Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED)); + Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED)); + + // Create a top part of data tree model + myModel = new XGUI_TopDataModel(myDocument, this); } XGUI_DocumentDataModel::~XGUI_DocumentDataModel() { + clearModelIndexes(); } -QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const +void XGUI_DocumentDataModel::processEvent(const Events_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"; + // Created object event ******************* + if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) { + const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast(theMessage); + boost::shared_ptr aFeature = aUpdMsg->feature(); + boost::shared_ptr aDoc = aFeature->document(); + + if (aDoc == myDocument) { // If root objects + if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Updsate only Parts group + // Add a new part + int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size() + 1; + XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this); + aModel->setPartId(myPartModels.count()); + myPartModels.append(aModel); + insertRows(QModelIndex(), aStart, aStart); + } else { // Update top groups (other except parts + QModelIndex aIndex = myModel->findParent(aFeature); + int aStart = myModel->rowCount(aIndex) - 1; + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)); + insertRows(aIndex, aStart, aStart); + } + } else { // if sub-objects of first level nodes + XGUI_PartModel* aPartModel = 0; + QList::const_iterator aIt; + for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) { + if ((*aIt)->hasDocument(aDoc)) { + aPartModel = (*aIt); + break; + } + } + if (aPartModel) { + QModelIndex aIndex = aPartModel->findParent(aFeature); + int aStart = aPartModel->rowCount(aIndex) - 1; + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)); + insertRows(aIndex, aStart, aStart); + } } - break; - case Qt::DecorationRole: - // return an Icon - break; - case Qt::ToolTipRole: - // return Tooltip - break; + + // Deteted object event *********************** + } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) { + const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast(theMessage); + boost::shared_ptr aDoc = aUpdMsg->document(); + + if (aDoc == myDocument) { // If root objects + if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group + int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size() - 1; + beginRemoveRows(QModelIndex(), aStart, aStart); + removeSubModel(myPartModels.size() - 1); + endRemoveRows(); + } else { // Update top groups (other except parts + QModelIndex aIndex = myModel->findGroup(aUpdMsg->group()); + int aStart = myModel->rowCount(aIndex); + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)); + beginRemoveRows(aIndex, aStart, aStart); + endRemoveRows(); + } + } else { + XGUI_PartModel* aPartModel = 0; + QList::const_iterator aIt; + for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) { + if ((*aIt)->hasDocument(aDoc)) { + aPartModel = (*aIt); + break; + } + } + if (aPartModel) { + QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group()); + int aStart = aPartModel->rowCount(aIndex); + aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)); + beginRemoveRows(aIndex, aStart, aStart); + endRemoveRows(); + } + } + + // Reset whole tree ************************** + } else { + 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(myDocument, this)); + } + for (int i = 0; i < myPartModels.size(); i++) + myPartModels.at(i)->setPartId(i); + } + clearModelIndexes(); + endResetModel(); } - return QVariant(); +} + +QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const +{ + if (!theIndex.isValid()) + return QVariant(); + return toSourceModelIndex(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()) { + int aVal = myModel->rowCount(theParent) + myPartModels.size(); + return myModel->rowCount(theParent) + myPartModels.size(); + } + QModelIndex aParent = toSourceModelIndex(theParent); + if (!hasSubModel(aParent.model())) + return 0; + + return aParent.model()->rowCount(aParent); } -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 { + if (myPartModels.size() > 0) { + int aPos = theRow - aOffs; + if (aPos >= myPartModels.size()) + aPos = 0; + aIndex = myPartModels.at(aPos)->index(aPos, theColumn, theParent); + } else + return aIndex; } + aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex)); + } else { + QModelIndex* aParent = (QModelIndex*)theParent.internalPointer(); + aIndex = aParent->model()->index(theRow, theColumn, (*aParent)); + + aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex)); } - return QModelIndex(); + return aIndex; } -QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex &index) const +QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const { - return QModelIndex(); + QModelIndex aParent = toSourceModelIndex(theIndex); + if (!hasSubModel(aParent.model())) + return QModelIndex(); + + aParent = aParent.model()->parent(aParent); + if (aParent.isValid()) + return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent)); + return aParent; } + bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const { if (!theParent.isValid()) return true; + return rowCount(theParent) > 0; +} + - if (theParent.internalId() == quintptr(&myParamsFolder)) - return myDocument->featuresIterator(PARAMETERS_GROUP)->more(); - if (theParent.internalId() == quintptr(&myConstructFolder)) - return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->more(); +QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const +{ + QModelIndex* aIndexPtr = static_cast(theProxy.internalPointer()); + return (*aIndexPtr); +} + + +QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const +{ + QList::const_iterator aIt; + for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) { + QModelIndex* aIndex = (*aIt); + if ((*aIndex) == theIndex) + return aIndex; + } + return 0; +} + +QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const +{ + QModelIndex* aIndexPtr = findModelIndex(theIndex); + if (!aIndexPtr) { + aIndexPtr = new QModelIndex(theIndex); + XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this; + that->myIndexes.append(aIndexPtr); + } + return aIndexPtr; +} + +void XGUI_DocumentDataModel::clearModelIndexes() +{ + QList::const_iterator aIt; + for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) + delete (*aIt); + myIndexes.clear(); +} + +FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const +{ + QModelIndex aIndex = toSourceModelIndex(theIndex); + if (!hasSubModel(aIndex.model())) + return FeaturePtr(); + + const XGUI_FeaturesModel* aModel = dynamic_cast(aIndex.model()); + return aModel->feature(aIndex); +} + +void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd) +{ + beginInsertRows(theParent, theStart, theEnd); + endInsertRows(); + if (theStart == 0) // Update parent if this is a first child in order to update node decoration + emit dataChanged(theParent, theParent); +} + +void XGUI_DocumentDataModel::removeSubModel(int theModelId) +{ + XGUI_PartModel* aModel = myPartModels.at(theModelId); + QIntList aToRemove; + for (int i = 0; i < myIndexes.size(); i++) { + if (myIndexes.at(i)->model() == aModel) + aToRemove.append(i); + } + int aId; + while(aToRemove.size() > 0) { + aId = aToRemove.last(); + delete myIndexes.at(aId); + myIndexes.removeAt(aId); + aToRemove.removeLast(); + } + delete aModel; + myPartModels.removeAt(theModelId); +} + +bool XGUI_DocumentDataModel::hasSubModel(const QAbstractItemModel* theModel) const +{ + if (theModel == myModel) + return true; + QList::const_iterator aIt; + for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) + if ((*aIt) == theModel) + return true; return false; -} \ No newline at end of file +}