+ if (theIndex.internalId() == PartsFolder)
+ return ObjectPtr();
+ if (theIndex.internalId() == HistoryNode) {
+ DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ int aOffset = historyOffset();
+ return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
+ }
+ QModelIndex* aIndex = toSourceModelIndex(theIndex);
+ if (!isSubModel(aIndex->model()))
+ return ObjectPtr();
+
+ const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
+ return aModel->object(*aIndex);
+}
+
+bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
+{
+ beginInsertRows(theParent, theRow, theRow + theCount - 1);
+ //endInsertRows();
+
+ // Update history
+ QModelIndex aRoot;
+ int aRow = rowCount(aRoot);
+ beginInsertRows(aRoot, aRow, aRow);
+ endInsertRows();
+
+ return true;
+}
+
+bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
+{
+ beginRemoveRows(theParent, theRow, theRow + theCount - 1);
+ endRemoveRows();
+ return true;
+}
+
+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::isSubModel(const QAbstractItemModel* theModel) const
+{
+ if (theModel == myModel)
+ return true;
+ return isPartSubModel(theModel);
+}
+
+bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
+{
+ return myPartModels.contains((XGUI_PartModel*) theModel);
+}
+
+QModelIndex XGUI_DocumentDataModel::partFolderNode() const
+{
+ int aPos = myModel->rowCount(QModelIndex());
+ return createIndex(aPos, columnCount() - 1, PartsFolder);
+}
+
+int XGUI_DocumentDataModel::historyOffset() const
+{
+ // Nb of rows of top model + Parts folder
+ return myModel->rowCount(QModelIndex()) + 1;
+}
+
+bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
+{
+ if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
+ return false;
+
+ QModelIndex* aIndex = toSourceModelIndex(theIndex);
+ if (!aIndex)
+ return false;
+
+ const QAbstractItemModel* aModel = aIndex->model();
+
+ if (isPartSubModel(aModel)) {
+ // if this is root node (Part item index)
+ if (!aIndex->parent().isValid()) {
+ if (myActivePart)
+ myActivePart->setItemsColor(PASSIVE_COLOR);
+
+ if (myActivePart == aModel) {
+ myActivePart = 0;
+ myActivePartIndex = QModelIndex();
+ } else {
+ myActivePart = (XGUI_PartModel*)aModel;
+ myActivePartIndex = theIndex;
+ }
+
+ if (myActivePart) {
+ myActivePart->setItemsColor(ACTIVE_COLOR);
+ myModel->setItemsColor(PASSIVE_COLOR);
+ } else
+ myModel->setItemsColor(ACTIVE_COLOR);
+ return true;
+ }
+ }
+ return false;
+}
+
+ResultPartPtr XGUI_DocumentDataModel::activePart() const
+{
+ if (myActivePart)
+ return myActivePart->part();
+ return ResultPartPtr();
+}
+
+void XGUI_DocumentDataModel::deactivatePart()
+{
+ if (myActivePart)
+ myActivePart->setItemsColor(PASSIVE_COLOR);
+ myActivePart = 0;
+ myActivePartIndex = QModelIndex();
+ myModel->setItemsColor(ACTIVE_COLOR);
+ }
+
+Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
+{
+ Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
+ if (object(theIndex)) {
+ aFlags |= Qt::ItemIsEditable;
+ }
+ return aFlags;
+}
+
+QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
+{
+ int aRow = -1;
+ XGUI_PartModel* aModel = 0;
+ foreach (XGUI_PartModel* aPartModel, myPartModels)
+ {
+ aRow++;
+ if (aPartModel->part() == theObject) {
+ aModel = aPartModel;
+ break;
+ }
+ }
+ if (aModel) {
+ return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
+ }
+ return QModelIndex();
+}
+
+QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
+{
+ // Check that this feature belongs to root document
+ DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ DocumentPtr aDoc = theObject->document();
+ if (aDoc == aRootDoc) {
+ // This feature belongs to histrory or top model
+ FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+ if (aFeature) {
+ int aId;
+ int aNb = aRootDoc->size(ModelAPI_Feature::group());
+ for (aId = 0; aId < aNb; aId++) {
+ if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
+ break;
+ }
+ if (aId < aNb)
+ return index(aId + historyOffset(), 0, QModelIndex());
+ } else {
+ QModelIndex aIndex = myModel->objectIndex(theObject);
+ return
+ aIndex.isValid() ?
+ createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
+ QModelIndex();
+ }
+ } else {
+ XGUI_PartModel* aPartModel = 0;
+ foreach(XGUI_PartModel* aModel, myPartModels)
+ {
+ if (aModel->hasDocument(aDoc)) {
+ aPartModel = aModel;
+ break;
+ }
+ }
+ if (aPartModel) {
+ QModelIndex aIndex = aPartModel->objectIndex(theObject);
+ return
+ aIndex.isValid() ?
+ createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
+ QModelIndex();
+ }
+ }
+ return QModelIndex();
+}