#include <ModelAPI_Data.h>
#include <Model_Events.h>
-#include <Event_Loop.h>
+#include <Events_Loop.h>
#include <QIcon>
: QAbstractItemModel(theParent)
{
// Find Document object
- std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
+ boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
myDocument = aMgr->currentDocument();
// Register in event loop
- Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_CREATED));
- Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
- Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_DELETED));
+ 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);
}
-void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
+void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
{
// Created object event *******************
if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
- const ModelAPI_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureUpdatedMessage*>(theMessage);
- std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
- std::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
+ const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
+ boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
+ boost::shared_ptr<ModelAPI_Document> 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();
+ 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);
+ int aStart = myModel->rowCount(aIndex) - 1;
aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- insertRows(aIndex, aStart-1, aStart);
+ insertRows(aIndex, aStart, aStart);
}
} else { // if sub-objects of first level nodes
XGUI_PartModel* aPartModel = 0;
}
if (aPartModel) {
QModelIndex aIndex = aPartModel->findParent(aFeature);
- int aStart = aPartModel->rowCount(aIndex);
+ int aStart = aPartModel->rowCount(aIndex) - 1;
aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- insertRows(aIndex, aStart-1, aStart);
+ insertRows(aIndex, aStart, aStart);
}
}
// Deteted object event ***********************
- } if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
- const ModelAPI_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureDeletedMessage*>(theMessage);
- std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
+ } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
+ const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
+ boost::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
if (aDoc == myDocument) { // If root objects
- int aStart = myPartModels.count() - 1;
- delete myPartModels.last();
- myPartModels.removeLast();
- beginRemoveRows(QModelIndex(), aStart, aStart);
- endRemoveRows();
+ 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<XGUI_PartModel*>::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 XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
{
- if (!theParent.isValid())
+ 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 aOffs = myModel->rowCount();
if (theRow < aOffs)
aIndex = myModel->index(theRow, theColumn, theParent);
- else
- aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, 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();
QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
{
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));
FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
{
QModelIndex aIndex = toSourceModelIndex(theIndex);
+ if (!hasSubModel(aIndex.model()))
+ return FeaturePtr();
+
const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
return aModel->feature(aIndex);
}
{
beginInsertRows(theParent, theStart, theEnd);
endInsertRows();
- if (theStart == 1) // Update parent if this is a first child in order to update node decoration
+ if (theStart == 0) // Update parent if this is a first child in order to update node decoration
emit dataChanged(theParent, theParent);
-}
\ No newline at end of file
+}
+
+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<XGUI_PartModel*>::const_iterator aIt;
+ for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt)
+ if ((*aIt) == theModel)
+ return true;
+ return false;
+}