From 1625826f5a48b78d95fab31e365d1e60e5141b23 Mon Sep 17 00:00:00 2001
From: Mikhail Svechnikov <svechnikovmv@gmail.com>
Date: Fri, 10 Feb 2023 14:56:27 +0100
Subject: [PATCH] MaskContainerModel --> unique ptr

---
 GUI/Model/Data/IntensityDataItem.cpp          | 10 ++++------
 GUI/Model/Device/MaskItems.cpp                | 19 ++++++-------------
 GUI/Model/Device/MaskItems.h                  | 15 +++------------
 GUI/View/Import/RealDataMaskWidget.cpp        | 11 +++--------
 GUI/View/Mask/MaskGraphicsScene.cpp           |  3 ++-
 .../IntensityDataProjectionsWidget.cpp        |  9 ++-------
 .../Projection/ProjectionsEditorCanvas.cpp    |  8 +-------
 7 files changed, 21 insertions(+), 54 deletions(-)

diff --git a/GUI/Model/Data/IntensityDataItem.cpp b/GUI/Model/Data/IntensityDataItem.cpp
index cb8b01926c6..5127ae125fd 100644
--- a/GUI/Model/Data/IntensityDataItem.cpp
+++ b/GUI/Model/Data/IntensityDataItem.cpp
@@ -418,10 +418,9 @@ const MaskContainerItem* IntensityDataItem::maskContainerItem() const
 
 MaskContainerItem* IntensityDataItem::getOrCreateMaskContainerItem()
 {
-    if (!m_maskContainerItem) {
+    if (!m_maskContainerItem)
         m_maskContainerItem.reset(new MaskContainerItem);
-        m_maskContainerItem->setParent(this);
-    }
+
     return m_maskContainerItem.get();
 }
 
@@ -437,10 +436,9 @@ const ProjectionContainerItem* IntensityDataItem::projectionContainerItem() cons
 
 ProjectionContainerItem* IntensityDataItem::getOrCreateProjectionContainerItem()
 {
-    if (!m_projectionContainerItem) {
+    if (!m_projectionContainerItem)
         m_projectionContainerItem.reset(new ProjectionContainerItem);
-        m_projectionContainerItem->setParent(this);
-    }
+
     return m_projectionContainerItem.get();
 }
 
diff --git a/GUI/Model/Device/MaskItems.cpp b/GUI/Model/Device/MaskItems.cpp
index 69958f0b4ea..755afb079ad 100644
--- a/GUI/Model/Device/MaskItems.cpp
+++ b/GUI/Model/Device/MaskItems.cpp
@@ -49,6 +49,7 @@ const QString Mask("Mask");
 
 MaskContainerItem::MaskContainerItem()
     : MaskItemObject()
+    , m_model(new MaskContainerModel(this))
 {
 }
 
@@ -126,13 +127,7 @@ void MaskContainerItem::copyFrom(const MaskContainerItem* maskContainer)
 
 MaskContainerModel* MaskContainerItem::model()
 {
-    return m_model;
-}
-
-void MaskContainerItem::setModel(MaskContainerModel* model)
-{
-    m_model = model;
-    model->maskContainer = this;
+    return m_model.get();
 }
 
 void MaskContainerItem::updateMaskNames()
@@ -896,7 +891,10 @@ MaskItemObject::~MaskItemObject()
 
 // Implementation of MaskContainerModel is based on the Qt source code for QStringListModel
 
-MaskContainerModel::MaskContainerModel() {}
+MaskContainerModel::MaskContainerModel(MaskContainerItem *container)
+    : maskContainer(container)
+{
+}
 
 MaskContainerModel::~MaskContainerModel() {}
 
@@ -925,11 +923,6 @@ QVariant MaskContainerModel::data(const QModelIndex& index, int role) const
 
 // End overridden methods from QAbstractListModel
 
-void MaskContainerModel::setParent(QObject* parent)
-{
-    QAbstractListModel::setParent(parent);
-}
-
 void MaskContainerModel::insertMask(int row, MaskItem* maskItem)
 {
     QAbstractListModel::beginInsertRows(maskContainer->rootIndex, row, row);
diff --git a/GUI/Model/Device/MaskItems.h b/GUI/Model/Device/MaskItems.h
index 8bed4c218a9..3f084cd1d9c 100644
--- a/GUI/Model/Device/MaskItems.h
+++ b/GUI/Model/Device/MaskItems.h
@@ -253,9 +253,6 @@ public:
     //! Return the corresponding MaskContainerModel
     MaskContainerModel* model();
 
-    //! Set the corresponding MaskContainerModel
-    void setModel(MaskContainerModel* model);
-
     //! Update numbers in mask names
     void updateMaskNames();
 
@@ -264,19 +261,15 @@ public:
 
 protected:
     SelectionVector<MaskItemCatalog> m_maskItems;
-    MaskContainerModel* m_model = nullptr;
+    std::unique_ptr<MaskContainerModel> m_model;
 };
 
-//! MaskContainerModel
-//! This class is only for migration away from `SessionModel`, to keep the list `QIndex`es
-//! refering to `MaskItems`, needed to cooperate with `QItemSelectionModel`.
-//! After removal of `SessionModel`, if this class is still needed, it should be derived
-//! directly from `QAbstractItemModel`.
+//! Provides interfaces to a MaskContainerItem, allowing its contents to be displayed and modified using the Qt mechanisms.
 
 class MaskContainerModel : public QAbstractListModel {
 
 public:
-    MaskContainerModel();
+    MaskContainerModel(MaskContainerItem* container);
     ~MaskContainerModel();
 
     //! Insert mask at given row.
@@ -306,8 +299,6 @@ public:
 
     RegionOfInterestItem* regionOfInterestItem() const;
 
-    void setParent(QObject* parent);
-
 public:
     MaskContainerItem* maskContainer = nullptr;
 };
diff --git a/GUI/View/Import/RealDataMaskWidget.cpp b/GUI/View/Import/RealDataMaskWidget.cpp
index 552ac10e28f..d62c48199c1 100644
--- a/GUI/View/Import/RealDataMaskWidget.cpp
+++ b/GUI/View/Import/RealDataMaskWidget.cpp
@@ -80,18 +80,13 @@ void RealDataMaskWidget::setContext()
     ASSERT(currentIntensityDataItem());
 
     MaskContainerItem* containerItem = currentIntensityDataItem()->getOrCreateMaskContainerItem();
-    MaskContainerModel* containerModel = containerItem->model();
+    ASSERT(containerItem);
 
-    // TODO: Proper memory release of the MaskContainerModel instance
-    if (!containerModel) {
-        containerModel = new MaskContainerModel;
-        containerModel->setParent(containerItem);
-        containerItem->setModel(containerModel);
-    }
+    MaskContainerModel* containerModel = containerItem->model();
+    ASSERT(containerModel);
 
     m_editorPropertyPanel->setMaskContext(containerModel);
 
-    ASSERT(dynamic_cast<MaskContainerItem*>(containerItem));
     m_editorCanvas->setSelectionModel(m_editorPropertyPanel->selectionModel());
     m_editorCanvas->setMaskContext(currentIntensityDataItem());
 
diff --git a/GUI/View/Mask/MaskGraphicsScene.cpp b/GUI/View/Mask/MaskGraphicsScene.cpp
index 4f0432967fb..581c61e5d53 100644
--- a/GUI/View/Mask/MaskGraphicsScene.cpp
+++ b/GUI/View/Mask/MaskGraphicsScene.cpp
@@ -61,8 +61,9 @@ void MaskGraphicsScene::setMaskContext(IntensityDataItem* intensityItem, MaskCon
         return;
 
     MaskContainerModel* maskContainerModel = maskContainerItem->model();
+    ASSERT(maskContainerModel);
 
-    if (maskContainerModel && maskContainerModel != m_maskContainerModel) {
+    if (maskContainerModel != m_maskContainerModel) {
         disconnectMaskContainer(m_maskContainerModel);
         m_maskContainerItem = maskContainerItem;
         m_maskContainerModel = maskContainerModel;
diff --git a/GUI/View/Projection/IntensityDataProjectionsWidget.cpp b/GUI/View/Projection/IntensityDataProjectionsWidget.cpp
index 897a0787fcd..e7de5e852da 100644
--- a/GUI/View/Projection/IntensityDataProjectionsWidget.cpp
+++ b/GUI/View/Projection/IntensityDataProjectionsWidget.cpp
@@ -66,15 +66,10 @@ void IntensityDataProjectionsWidget::setContext()
 
     ProjectionContainerItem* containerItem =
         currentIntensityDataItem()->getOrCreateProjectionContainerItem();
+    ASSERT(containerItem);
 
     MaskContainerModel* containerModel = containerItem->model();
-
-    // TODO: Proper memory release of the MaskContainerModel instance
-    if (!containerModel) {
-        containerModel = new MaskContainerModel;
-        containerModel->setParent(containerItem);
-        containerItem->setModel(containerModel);
-    }
+    ASSERT(containerModel);
 
     m_selectionModel.reset(new QItemSelectionModel(containerModel));
 
diff --git a/GUI/View/Projection/ProjectionsEditorCanvas.cpp b/GUI/View/Projection/ProjectionsEditorCanvas.cpp
index 4782eec59be..7e4f31b727a 100644
--- a/GUI/View/Projection/ProjectionsEditorCanvas.cpp
+++ b/GUI/View/Projection/ProjectionsEditorCanvas.cpp
@@ -56,13 +56,7 @@ ProjectionsEditorCanvas::ProjectionsEditorCanvas(QWidget* parent)
 void ProjectionsEditorCanvas::setContext(IntensityDataItem* intensityItem)
 {
     ProjectionContainerItem* containerItem = intensityItem->getOrCreateProjectionContainerItem();
-
-    // TODO: Proper memory release of the MaskContainerModel instance
-    if (!containerItem->model()) {
-        MaskContainerModel* containerModel = new MaskContainerModel;
-        containerModel->setParent(containerItem);
-        containerItem->setModel(containerModel);
-    }
+    ASSERT(containerItem);
 
     m_scene->setMaskContext(intensityItem, containerItem);
 
-- 
GitLab