From 6b388da7984afa96f7973bddbc5c20cc52414cd3 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Fri, 8 Apr 2016 15:43:30 +0200
Subject: [PATCH] Refactoring in MainWindow

---
 GUI/coregui/Models/ApplicationModels.cpp  | 36 +++++++---
 GUI/coregui/Models/ApplicationModels.h    | 14 ++--
 GUI/coregui/Views/TestView.cpp            | 14 ----
 GUI/coregui/mainwindow/mainwindow.cpp     | 87 +++++++++++------------
 GUI/coregui/mainwindow/mainwindow.h       | 50 +++++--------
 GUI/coregui/mainwindow/projectmanager.cpp |  3 +-
 6 files changed, 95 insertions(+), 109 deletions(-)

diff --git a/GUI/coregui/Models/ApplicationModels.cpp b/GUI/coregui/Models/ApplicationModels.cpp
index 277732a59d6..3879c1bea95 100644
--- a/GUI/coregui/Models/ApplicationModels.cpp
+++ b/GUI/coregui/Models/ApplicationModels.cpp
@@ -27,7 +27,7 @@
 ApplicationModels::ApplicationModels(QObject *parent)
     : QObject(parent)
     , m_materialModel(0)
-    , m_materialEditor(0)
+    , m_materialSvc(0)
     , m_instrumentModel(0)
     , m_sampleModel(0)
     , m_jobModel(0)
@@ -35,12 +35,36 @@ ApplicationModels::ApplicationModels(QObject *parent)
 {
     createModels();
     testGUIObjectBuilder();
-
 }
 
 ApplicationModels::~ApplicationModels()
 {
-    delete m_materialEditor;
+    delete m_materialSvc;
+}
+
+MaterialModel *ApplicationModels::materialModel()
+{
+    return m_materialModel;
+}
+
+InstrumentModel *ApplicationModels::instrumentModel()
+{
+    return m_instrumentModel;
+}
+
+SampleModel *ApplicationModels::sampleModel()
+{
+    return m_sampleModel;
+}
+
+JobModel *ApplicationModels::jobModel()
+{
+    return m_jobModel;
+}
+
+FitModel *ApplicationModels::fitModel()
+{
+    return m_fitModel;
 }
 
 //! creates and initializes models
@@ -94,11 +118,7 @@ void ApplicationModels::createMaterialModel()
 {
     delete m_materialModel;
     m_materialModel = new MaterialModel(this);
-//    m_materialModel->addMaterial("Default", 1e-3, 1e-5);
-//    m_materialModel->addMaterial("Air", 0.0, 0.0);
-//    m_materialModel->addMaterial("Particle", 6e-4, 2e-8);
-//    m_materialModel->addMaterial("Substrate", 6e-6, 2e-8);
-    m_materialEditor = new MaterialSvc(m_materialModel);
+    m_materialSvc = new MaterialSvc(m_materialModel);
 }
 
 void ApplicationModels::createSampleModel()
diff --git a/GUI/coregui/Models/ApplicationModels.h b/GUI/coregui/Models/ApplicationModels.h
index db805297001..55ee0ca5f75 100644
--- a/GUI/coregui/Models/ApplicationModels.h
+++ b/GUI/coregui/Models/ApplicationModels.h
@@ -34,11 +34,11 @@ public:
     ApplicationModels(QObject *parent);
     ~ApplicationModels();
 
-    MaterialModel *materialModel() { return m_materialModel; }
-    InstrumentModel *instrumentModel() { return m_instrumentModel; }
-    SampleModel *sampleModel() { return m_sampleModel; }
-    JobModel *jobModel() { return m_jobModel; }
-    FitModel *fitModel() { return m_fitModel; }
+    MaterialModel *materialModel();
+    InstrumentModel *instrumentModel();
+    SampleModel *sampleModel();
+    JobModel *jobModel();
+    FitModel *fitModel();
 
     void resetModels();
 
@@ -52,13 +52,11 @@ private:
     void testGUIObjectBuilder();
 
     MaterialModel *m_materialModel;
-    MaterialSvc *m_materialEditor;
+    MaterialSvc *m_materialSvc;
     InstrumentModel *m_instrumentModel;
     SampleModel *m_sampleModel;
     JobModel *m_jobModel;
     FitModel *m_fitModel;
 };
 
-
-
 #endif
diff --git a/GUI/coregui/Views/TestView.cpp b/GUI/coregui/Views/TestView.cpp
index 3c69bc78f28..dca102dfc74 100644
--- a/GUI/coregui/Views/TestView.cpp
+++ b/GUI/coregui/Views/TestView.cpp
@@ -38,20 +38,6 @@
 #include "ModelMapper.h"
 #include "DetectorItems.h"
 
-
-
-// FIXME_DAVID Rename Ivona's FitView into ObsoleteFitView. And use nice name FitView for own purpose.
-//  -- suggestion: for consistency use prefix Obsolete for all classes you are goind to throw soon
-//  -- you may want to add to all Ivona's classes prefix Obsolete. Don't forget about 'ifndef' header guards
-
-// FIXME_DAVID Move your activity from TestView to FitView.
-// - FitView should contain QTabWidget with 3 tabs:
-// - 1) ImportDataWidget (empty for the moment) 2) FitSettingsWidget 3) RunFitWidget
-
-// FIXME_DAVID FitSettingsWidget should contain
-// - FitParametersWidget (for the moment), and later sample/instrument selector + MinimizerSettingsWidgert
-
-
 TestView::TestView(MainWindow *mainWindow)
     : QWidget(mainWindow)
     , m_mainWindow(mainWindow)
diff --git a/GUI/coregui/mainwindow/mainwindow.cpp b/GUI/coregui/mainwindow/mainwindow.cpp
index cb5020ec181..096f02849c7 100644
--- a/GUI/coregui/mainwindow/mainwindow.cpp
+++ b/GUI/coregui/mainwindow/mainwindow.cpp
@@ -50,6 +50,9 @@
 
 MainWindow::MainWindow(QWidget *parent)
     : Manhattan::FancyMainWindow(parent)
+    , m_applicationModels(new ApplicationModels(this))
+    , m_projectManager(new ProjectManager(this))
+    , m_actionManager(new ActionManager(this))
     , m_tabWidget(0)
     , m_welcomeView(0)
     , m_instrumentView(0)
@@ -58,9 +61,6 @@ MainWindow::MainWindow(QWidget *parent)
     , m_jobView(0)
     , m_fitView(0)
     , m_progressBar(0)
-    , m_actionManager(0)
-    , m_projectManager(0)
-    , m_applicationModels(new ApplicationModels(this))
     , m_toolTipDataBase(new ToolTipDataBase(this))
     , m_updateNotifier(new UpdateNotifier(this))
 {
@@ -76,12 +76,11 @@ MainWindow::MainWindow(QWidget *parent)
     Manhattan::Utils::StyleHelper::setBaseColor(QColor(Constants::MAIN_THEME_COLOR));
 
     setDockNestingEnabled(true);
+    setAcceptDrops(true);
 
     setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
     setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
 
-    m_projectManager = new ProjectManager(this);
-    m_actionManager = new ActionManager(this);
     readSettings();
 
     m_tabWidget = new Manhattan::FancyTabWidget(this);
@@ -113,8 +112,6 @@ MainWindow::MainWindow(QWidget *parent)
 
     setCentralWidget(m_tabWidget);
 
-    setAcceptDrops(true);
-
     // signals/slots
     connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onChangeTabWidget(int)));
     connect(m_jobView, SIGNAL(focusRequest(int)), this, SLOT(onFocusRequest(int)));
@@ -149,6 +146,40 @@ FitModel *MainWindow::getFitModel()
     return models()->fitModel();
 }
 
+ApplicationModels *MainWindow::models()
+{
+    return m_applicationModels;
+}
+
+//! updates views which depend on others
+void MainWindow::onChangeTabWidget(int index)
+{
+    if(index == WELCOME)
+    {
+        m_welcomeView->updateRecentProjectPanel();
+    }
+    else if (index == INSTRUMENT) {
+        m_instrumentView->updateView();
+    }
+    else if(index == SIMULATION) {
+        m_simulationView->updateSimulationViewElements();
+    }
+}
+
+void MainWindow::onFocusRequest(int index)
+{
+    m_tabWidget->setCurrentIndex(index);
+}
+
+void MainWindow::openRecentProject()
+{
+    if (const QAction *action = qobject_cast<const QAction*>(sender())) {
+        QString file = action->data().value<QString>();
+        qDebug() << "MainWindow::openRecentProject() -> " << file;
+        m_projectManager->openProject(file);
+    }
+}
+
 void MainWindow::readSettings()
 {
     QSettings settings;
@@ -180,35 +211,10 @@ void MainWindow::onRunSimulationShortcut()
     m_simulationView->onRunSimulationShortcut();
 }
 
-void MainWindow::openRecentProject()
-{
-    if (const QAction *action = qobject_cast<const QAction*>(sender())) {
-        QString file = action->data().value<QString>();
-        qDebug() << "MainWindow::openRecentProject() -> " << file;
-        m_projectManager->openProject(file);
-    }
-}
-
-void MainWindow::onChangeTabWidget(int index)
-{
-    // update views which depend on others
-    (void)index;
-
-    if(index == WELCOME)
-    {
-        m_welcomeView->updateRecentProjectPanel();
-    }
-    else if (index == INSTRUMENT) {
-        m_instrumentView->updateView();
-    }
-    else if(index == SIMULATION) {
-        m_simulationView->updateSimulationViewElements();
-    }
-}
-
-void MainWindow::onFocusRequest(int index)
+void MainWindow::onAboutApplication()
 {
-    m_tabWidget->setCurrentIndex(index);
+    AboutApplicationDialog dialog(this);
+    dialog.exec();
 }
 
 void MainWindow::closeEvent(QCloseEvent *event)
@@ -232,17 +238,6 @@ void MainWindow::closeEvent(QCloseEvent *event)
     }
 }
 
-void MainWindow::onAboutApplication()
-{
-    AboutApplicationDialog dialog(this);
-    dialog.exec();
-}
-
-void MainWindow::resetModels()
-{
-    models()->resetModels();
-}
-
 void MainWindow::showEvent(QShowEvent *event)
 {
     QWidget::showEvent( event );
diff --git a/GUI/coregui/mainwindow/mainwindow.h b/GUI/coregui/mainwindow/mainwindow.h
index e8bb757a018..13094a39ab3 100644
--- a/GUI/coregui/mainwindow/mainwindow.h
+++ b/GUI/coregui/mainwindow/mainwindow.h
@@ -26,58 +26,49 @@ namespace Manhattan {
     class ProgressBar;
 }
 
-class TaskSelectorWidget;
 class WelcomeView;
 class InstrumentView;
 class SampleView;
-class PyScriptView;
 class SimulationView;
 class JobView;
-class Instrument;
-class ISample;
-class ActionManager;
-class ProjectManager;
-class QCloseEvent;
-class QSettings;
-class InstrumentModel;
-class MaterialSvc;
-class ToolTipDataBase;
+class FitView;
+
 class MaterialModel;
+class MaterialSvc;
 class SampleModel;
-class FitView;
+class InstrumentModel;
 class JobModel;
-class UpdateNotifier;
 class FitModel;
 class ApplicationModels;
 
+class ProjectManager;
+class ActionManager;
+class QSettings;
+class ToolTipDataBase;
+class UpdateNotifier;
+
+
 class BA_CORE_API_ MainWindow : public Manhattan::FancyMainWindow
 {
     Q_OBJECT
 
 public:
-    enum ETabViewId { WELCOME, INSTRUMENT, SAMPLE, SIMULATION, JOB, FIT, TESTVIEW};
+    enum ETabViewId {WELCOME, INSTRUMENT, SAMPLE, SIMULATION, JOB, FIT, TESTVIEW};
 
     explicit MainWindow(QWidget *parent = 0);
 
-//    MaterialModel *getMaterialModel() { return m_materialModel; }
-//    InstrumentModel *instrumentModel() { return m_instrumentModel; }
-//    SampleModel *sampleModel() { return m_sampleModel; }
-//    JobModel *jobModel() { return m_jobModel; }
-//    FitModel *getFitModel() { return m_fitModel; }
-
     MaterialModel *getMaterialModel();
     InstrumentModel *instrumentModel();
     SampleModel *sampleModel();
     JobModel *jobModel();
     FitModel *getFitModel();
-
+    ApplicationModels *models();
 
     Manhattan::ProgressBar *getProgressBar() { return m_progressBar; }
     ActionManager *getActionManager() { return m_actionManager; }
     ProjectManager *getProjectManager() { return m_projectManager; }
     UpdateNotifier *getUpdateNotifier() { return m_updateNotifier; }
 
-    ApplicationModels *models() { return m_applicationModels; }
 
 public slots:
     void onChangeTabWidget(int index);
@@ -87,31 +78,26 @@ public slots:
     void writeSettings();
     void onRunSimulationShortcut();
     void onAboutApplication();
-    void resetModels();
 
 protected:
     virtual void closeEvent(QCloseEvent *event);
+    virtual void showEvent(QShowEvent *event);
 
 private:
+    ApplicationModels *m_applicationModels;
+    ProjectManager *m_projectManager;
+    ActionManager *m_actionManager;
+
     Manhattan::FancyTabWidget  *m_tabWidget;
     WelcomeView *m_welcomeView;
     InstrumentView *m_instrumentView;
     SampleView *m_sampleView;
     SimulationView *m_simulationView;
-
     JobView *m_jobView;
     FitView *m_fitView;
-
     Manhattan::ProgressBar *m_progressBar;
-
-    ActionManager *m_actionManager; //!< responsible for menus and actions
-    ProjectManager *m_projectManager; //!< handles activity related to opening/saving projects
-    ApplicationModels *m_applicationModels;
-
     ToolTipDataBase *m_toolTipDataBase;
     UpdateNotifier *m_updateNotifier;
-
-    virtual void showEvent(QShowEvent *event);
 };
 
 #endif // MAINWINDOW_H
diff --git a/GUI/coregui/mainwindow/projectmanager.cpp b/GUI/coregui/mainwindow/projectmanager.cpp
index f9fe7e9aa59..4f0c43aa9ba 100644
--- a/GUI/coregui/mainwindow/projectmanager.cpp
+++ b/GUI/coregui/mainwindow/projectmanager.cpp
@@ -25,6 +25,7 @@
 #include "WarningMessageService.h"
 #include "MessageContainer.h"
 #include "GUIMessage.h"
+#include "ApplicationModels.h"
 #include <QDir>
 #include <QFileDialog>
 #include <QSettings>
@@ -314,7 +315,7 @@ void ProjectManager::deleteCurrentProject()
 {
     delete m_project_document;
     m_project_document = 0;
-    m_mainWindow->resetModels();
+    m_mainWindow->models()->resetModels();
 }
 
 
-- 
GitLab