+void XGUI_DocumentDataModel::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
+{
+ DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+
+ // Created object event *******************
+ if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
+ boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
+ boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+ std::set<ObjectPtr> aObjects = aUpdMsg->objects();
+
+ std::set<ObjectPtr>::const_iterator aIt;
+ for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
+ ObjectPtr aObject = (*aIt);
+ FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
+ if (aFeature && (!aFeature->isInHistory()))
+ continue;
+
+ DocumentPtr aDoc = aObject->document();
+ if (aDoc == aRootDoc) { // If root objects
+ if (aObject->groupName() == ModelAPI_ResultPart::group()) { // Update only Parts group
+ // Add a new part
+ int aStart = myPartModels.size();
+ XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
+ aModel->setPartId(myPartModels.count());
+ myPartModels.append(aModel);
+ insertRow(aStart, partFolderNode());
+ } else { // Update top groups (other except parts
+ QModelIndex aIndex = myModel->findParent(aObject);
+ int aStart = myModel->rowCount(aIndex) - 1;
+ if (aStart < 0)
+ aStart = 0;
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
+ insertRow(aStart, aIndex);
+ }
+ } else { // if sub-objects of first level nodes
+ 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->findParent(aObject);
+ int aStart = aPartModel->rowCount(aIndex); // check this index
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
+ insertRow(aStart, aIndex);
+ }
+ }
+ }
+ // Deleted object event ***********************
+ } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
+ boost::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
+ boost::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
+ DocumentPtr aDoc = aUpdMsg->document();
+ std::set<std::string> aGroups = aUpdMsg->groups();
+
+ std::set<std::string>::const_iterator aIt;
+ for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
+ std::string aGroup = (*aIt);
+ if (aDoc == aRootDoc) { // If root objects
+ if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
+ int aStart = myPartModels.size() - 1;
+ removeSubModel(aStart);
+ removeRow(aStart, partFolderNode());
+ if (myActivePart && (!isPartSubModel(myActivePart))) {
+ myActivePart = 0;
+ myActivePartIndex = QModelIndex();
+ myModel->setItemsColor(ACTIVE_COLOR);
+ }
+ } else { // Update top groups (other except parts
+ QModelIndex aIndex = myModel->findGroup(aGroup);
+ int aStart = myModel->rowCount(aIndex);
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
+ removeRow(aStart, aIndex);
+ }
+ } 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(aGroup);
+ int aStart = aPartModel->rowCount(aIndex);
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
+ removeRow(aStart, aIndex);
+ }
+ }
+ }
+ // Deleted object event ***********************
+ } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
+ //boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg = boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+ //ObjectPtr aFeature = aUpdMsg->feature();
+ //DocumentPtr aDoc = aFeature->document();
+
+ // TODO: Identify the necessary index by the modified feature
+ QModelIndex aIndex;
+ emit dataChanged(aIndex, aIndex);
+
+ // Reset whole tree **************************
+ } else {
+ rebuildDataTree();
+ }
+}