From 1a113b83d3948d539ab951ae947332f49fd300e6 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Wed, 20 Mar 2013 15:04:28 +0100 Subject: [PATCH] As QModelIndex is stored on clipboard, clear after move/delete operations Added support for multiple delete of reservoir cases p4#: 21007 --- .../ProjectDataModel/RimUiTreeModelPdm.cpp | 52 +++++++++++++++---- .../ProjectDataModel/RimUiTreeModelPdm.h | 4 +- .../ProjectDataModel/RimUiTreeView.cpp | 22 +++++++- cafUserInterface/cafUiTreeModelPdm.h | 2 +- 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.cpp b/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.cpp index 7b09a61663..3385dc91a8 100644 --- a/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.cpp +++ b/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.cpp @@ -140,6 +140,9 @@ bool RimUiTreeModelPdm::deletePropertyFilter(const QModelIndex& itemIndex) { propertyFilterCollection->reservoirView()->createDisplayModelAndRedraw(); } + + clearClipboard(); + return true; } @@ -178,6 +181,8 @@ bool RimUiTreeModelPdm::deleteRangeFilter(const QModelIndex& itemIndex) rangeFilterCollection->reservoirView()->createDisplayModelAndRedraw(); } + clearClipboard(); + return true; } @@ -200,29 +205,36 @@ bool RimUiTreeModelPdm::deleteReservoirView(const QModelIndex& itemIndex) reservoirView->eclipseCase()->removeReservoirView(reservoirView); delete reservoirView; + clearClipboard(); + return true; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RimUiTreeModelPdm::deleteReservoir(const QModelIndex& itemIndex) +void RimUiTreeModelPdm::deleteReservoir(RimReservoir* reservoir) { - CVF_ASSERT(itemIndex.isValid()); + RimCaseCollection* caseCollection = reservoir->parentCaseCollection(); + QModelIndex caseCollectionModelIndex = getModelIndexFromPdmObject(caseCollection); + if (!caseCollectionModelIndex.isValid()) return; - caf::PdmUiTreeItem* uiItem = getTreeItemFromIndex(itemIndex); - CVF_ASSERT(uiItem); + QModelIndex mi = getModelIndexFromPdmObjectRecursive(caseCollectionModelIndex, reservoir); + if (mi.isValid()) + { + caf::PdmUiTreeItem* uiItem = getTreeItemFromIndex(mi); + CVF_ASSERT(uiItem); - RimReservoir* reservoir = dynamic_cast(uiItem->dataObject().p()); - CVF_ASSERT(reservoir); - - // Remove Ui items pointing at the pdm object to delete - removeRow(itemIndex.row(), itemIndex.parent()); + // Remove Ui items pointing at the pdm object to delete + removeRow(mi.row(), mi.parent()); + } RimProject* proj = RIApplication::instance()->project(); proj->removeCaseFromAllGroups(reservoir); delete reservoir; + + clearClipboard(); } //-------------------------------------------------------------------------------------------------- @@ -456,6 +468,8 @@ void RimUiTreeModelPdm::deleteInputProperty(const QModelIndex& itemIndex) inputReservoir->removeProperty(inputProperty); delete inputProperty; + + clearClipboard(); } //-------------------------------------------------------------------------------------------------- @@ -658,6 +672,26 @@ bool RimUiTreeModelPdm::deleteObjectFromPdmPointersField(const QModelIndex& item } } + clearClipboard(); + return true; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimUiTreeModelPdm::clearClipboard() +{ + // We use QModelIndex to identify a selection on the clipboard + // When we delete or move an entity, the clipboard data might be invalid + + QClipboard* clipboard = QApplication::clipboard(); + if (clipboard) + { + if (dynamic_cast(clipboard->mimeData())) + { + clipboard->clear(); + } + } +} + diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.h b/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.h index 4055cdc219..59aabbc789 100644 --- a/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.h +++ b/ApplicationCode/ProjectDataModel/RimUiTreeModelPdm.h @@ -29,6 +29,7 @@ class QFileSystemWatcher; class RimCellPropertyFilter; class RimCellRangeFilter; +class RimReservoir; class RimReservoirView; class RimInputProperty; class RimStatisticalCalculation; @@ -103,7 +104,7 @@ public: bool deletePropertyFilter(const QModelIndex& itemIndex); bool deleteReservoirView(const QModelIndex& itemIndex); void deleteInputProperty(const QModelIndex& itemIndex); - void deleteReservoir(const QModelIndex& itemIndex); + void deleteReservoir(RimReservoir* reservoir); RimCellPropertyFilter* addPropertyFilter(const QModelIndex& itemIndex, QModelIndex& insertedModelIndex); RimCellRangeFilter* addRangeFilter(const QModelIndex& itemIndex, QModelIndex& insertedModelIndex); @@ -120,6 +121,7 @@ public: private slots: void slotRefreshScriptTree(QString path); + void clearClipboard(); private: QFileSystemWatcher* m_scriptChangeDetector; diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp b/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp index 285eb90519..ea6a245a1e 100644 --- a/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp +++ b/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp @@ -34,6 +34,7 @@ #include "RimBinaryExportSettings.h" #include "RigReservoirCellResults.h" #include "RimStatisticalCalculation.h" +#include "RimResultReservoir.h" //-------------------------------------------------------------------------------------------------- /// @@ -783,7 +784,26 @@ void RimUiTreeView::slotCloseCase() RimUiTreeModelPdm* myModel = dynamic_cast(model()); if (myModel) { - myModel->deleteReservoir(currentIndex()); + QItemSelectionModel* m = selectionModel(); + CVF_ASSERT(m); + + caf::PdmObjectGroup group; + + QModelIndexList mil = m->selectedRows(); + for (int i = 0; i < mil.size(); i++) + { + caf::PdmUiTreeItem* uiItem = myModel->getTreeItemFromIndex(mil.at(i)); + group.addObject(uiItem->dataObject().p()); + } + + std::vector > typedObjects; + group.objectsByType(&typedObjects); + + for (size_t i = 0; i < typedObjects.size(); i++) + { + RimReservoir* rimReservoir = typedObjects[i]; + myModel->deleteReservoir(rimReservoir); + } } } diff --git a/cafUserInterface/cafUiTreeModelPdm.h b/cafUserInterface/cafUiTreeModelPdm.h index 77fcf07bd6..0dfbf2593a 100644 --- a/cafUserInterface/cafUiTreeModelPdm.h +++ b/cafUserInterface/cafUiTreeModelPdm.h @@ -65,7 +65,7 @@ public: virtual bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()); virtual bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()); -private: +protected: QModelIndex getModelIndexFromPdmObjectRecursive(const QModelIndex& root, const PdmObject * object) const; private: