diff --git a/GUI/Models/ApplicationModels.cpp b/GUI/Models/ApplicationModels.cpp index 24c9b4d7ed8bc1673428436ac0dc20772d9db551..e73f82822e303c7252256e1ace619e2507a3fb95 100644 --- a/GUI/Models/ApplicationModels.cpp +++ b/GUI/Models/ApplicationModels.cpp @@ -53,32 +53,32 @@ ApplicationModels::ApplicationModels(QObject* parent) ApplicationModels::~ApplicationModels() = default; -DocumentModel* ApplicationModels::documentModel() +DocumentModel* ApplicationModels::documentModel() const { return m_documentModel; } -MaterialModel* ApplicationModels::materialModel() +MaterialModel* ApplicationModels::materialModel() const { return m_materialModel; } -InstrumentModel* ApplicationModels::instrumentModel() +InstrumentModel* ApplicationModels::instrumentModel() const { return m_instrumentModel; } -SampleModel* ApplicationModels::sampleModel() +SampleModel* ApplicationModels::sampleModel() const { return m_sampleModel; } -RealDataModel* ApplicationModels::realDataModel() +RealDataModel* ApplicationModels::realDataModel() const { return m_realDataModel; } -JobModel* ApplicationModels::jobModel() +JobModel* ApplicationModels::jobModel() const { return m_jobModel; } diff --git a/GUI/Models/ApplicationModels.h b/GUI/Models/ApplicationModels.h index a804f4782976887dc2b52e679e5209f972350239..78759a648d4435b35a9878ef0db296b16cee7d80 100644 --- a/GUI/Models/ApplicationModels.h +++ b/GUI/Models/ApplicationModels.h @@ -33,12 +33,12 @@ public: explicit ApplicationModels(QObject* parent = nullptr); ~ApplicationModels(); - DocumentModel* documentModel(); - MaterialModel* materialModel(); - InstrumentModel* instrumentModel(); - SampleModel* sampleModel(); - RealDataModel* realDataModel(); - JobModel* jobModel(); + DocumentModel* documentModel() const; + MaterialModel* materialModel() const; + InstrumentModel* instrumentModel() const; + SampleModel* sampleModel() const; + RealDataModel* realDataModel() const; + JobModel* jobModel() const; void resetModels(); diff --git a/GUI/mainwindow/mainwindow.cpp b/GUI/mainwindow/mainwindow.cpp index 8641e8ddf0ae05e91b9e7bdf3a1aac633bd465b8..759405e539f4ea3bc6b1a50cb3bb61f41343bb57 100644 --- a/GUI/mainwindow/mainwindow.cpp +++ b/GUI/mainwindow/mainwindow.cpp @@ -50,7 +50,6 @@ MainWindow::MainWindow() , m_viewsStack(new QStackedLayout) , m_viewSelectionButtonsLayout(new QVBoxLayout) , m_statusBar(new QStatusBar) - , m_applicationModels(new ApplicationModels(this)) , m_projectManager(new ProjectManager(this)) , m_actionManager(new ActionManager(this)) , m_welcomeView(0) @@ -115,10 +114,6 @@ MainWindow::MainWindow() ASSERT(m_viewSelectionButtons->button(ViewId::WELCOME) != nullptr); m_viewSelectionButtons->button(ViewId::WELCOME)->setChecked(true); - // m_applicationModels->createTestSample(); - // m_applicationModels->createTestJob(); - // m_applicationModels->createTestRealData(); - updateTitle(); if (baApp->settings().createNewProjectOnStartup()) @@ -135,11 +130,6 @@ MainWindow* MainWindow::instance() return s_instance; } -ApplicationModels* MainWindow::models() -{ - return m_applicationModels; -} - QProgressBar* MainWindow::progressBar() { return m_progressBar; diff --git a/GUI/mainwindow/mainwindow.h b/GUI/mainwindow/mainwindow.h index ef13108d8d6713a194c71c6b301fea75638a673d..3b3cc7a46bf3e2266efe1abcffe454caffe905f5 100644 --- a/GUI/mainwindow/mainwindow.h +++ b/GUI/mainwindow/mainwindow.h @@ -25,12 +25,6 @@ class SimulationView; class JobView; class SessionModelView; -class MaterialModel; -class SampleModel; -class InstrumentModel; -class RealDataModel; -class JobModel; -class ApplicationModels; class ProjectManager; class ActionManager; class UpdateNotifier; @@ -54,9 +48,6 @@ public: //! Returns the one and only instance of this class static MainWindow* instance(); - JobModel* jobModel(); - ApplicationModels* models(); - QProgressBar* progressBar(); QStatusBar* statusBar(); @@ -105,7 +96,6 @@ private: QVBoxLayout* m_viewSelectionButtonsLayout; QStatusBar* m_statusBar; - ApplicationModels* m_applicationModels; ProjectManager* m_projectManager; ActionManager* m_actionManager; UpdateNotifier* m_updateNotifier; diff --git a/GUI/mainwindow/projectdocument.cpp b/GUI/mainwindow/projectdocument.cpp index 695814e2f43ab90f0a554c5ebc02f87d787075d6..5d8a122dec64416c94d6c8830ae33db6530846a8 100644 --- a/GUI/mainwindow/projectdocument.cpp +++ b/GUI/mainwindow/projectdocument.cpp @@ -37,14 +37,17 @@ const QString InfoNameAttribute("ProjectName"); ProjectDocument::ProjectDocument(const QString& projectFileName) - : m_applicationModels(nullptr) - , m_modified(false) + : m_modified(false) , m_dataService(new OutputDataIOService(this)) , m_linkManager(new LinkInstrumentManager(this)) { setObjectName("ProjectDocument"); if (!projectFileName.isEmpty()) setProjectFileName(projectFileName); + + m_dataService->setApplicationModels(&m_applicationModels); + connectModels(); + m_linkManager->setModels(instrumentModel(), realDataModel()); } QString ProjectDocument::projectName() const @@ -89,46 +92,34 @@ QString ProjectDocument::projectFileExtension() return ".pro"; } -void ProjectDocument::setApplicationModels(ApplicationModels* applicationModels) -{ - if (applicationModels != m_applicationModels) { - disconnectModels(); - m_applicationModels = applicationModels; - m_dataService->setApplicationModels(m_applicationModels); - connectModels(); - } - - m_linkManager->setModels(instrumentModel(), realDataModel()); -} - ApplicationModels* ProjectDocument::applicationModels() { - return m_applicationModels; + return &m_applicationModels; } InstrumentModel* ProjectDocument::instrumentModel() const { - return m_applicationModels != nullptr ? m_applicationModels->instrumentModel() : nullptr; + return m_applicationModels.instrumentModel(); } SampleModel* ProjectDocument::sampleModel() const { - return m_applicationModels != nullptr ? m_applicationModels->sampleModel() : nullptr; + return m_applicationModels.sampleModel(); } MaterialModel* ProjectDocument::materialModel() const { - return m_applicationModels != nullptr ? m_applicationModels->materialModel() : nullptr; + return m_applicationModels.materialModel(); } RealDataModel* ProjectDocument::realDataModel() const { - return m_applicationModels != nullptr ? m_applicationModels->realDataModel() : nullptr; + return m_applicationModels.realDataModel(); } JobModel* ProjectDocument::jobModel() const { - return m_applicationModels != nullptr ? m_applicationModels->jobModel() : nullptr; + return m_applicationModels.jobModel(); } LinkInstrumentManager* ProjectDocument::linkInstrumentManager() @@ -267,7 +258,7 @@ ProjectDocument::ReadResult ProjectDocument::readProject(QIODevice* device, if (reader.name() == InfoTag) reader.skipCurrentElement(); else - m_applicationModels->readFrom(&reader, &messageService); + m_applicationModels.readFrom(&reader, &messageService); } } } @@ -296,7 +287,7 @@ void ProjectDocument::writeTo(QIODevice* device) writer.writeAttribute(InfoNameAttribute, projectName()); writer.writeEndElement(); // InfoTag - m_applicationModels->writeTo(&writer); + m_applicationModels.writeTo(&writer); writer.writeEndElement(); // BornAgain tag writer.writeEndDocument(); @@ -304,14 +295,12 @@ void ProjectDocument::writeTo(QIODevice* device) void ProjectDocument::disconnectModels() { - if (m_applicationModels) - disconnect(m_applicationModels, &ApplicationModels::modelChanged, this, - &ProjectDocument::onModelChanged); + disconnect(&m_applicationModels, &ApplicationModels::modelChanged, this, + &ProjectDocument::onModelChanged); } void ProjectDocument::connectModels() { - if (m_applicationModels) - connect(m_applicationModels, &ApplicationModels::modelChanged, this, - &ProjectDocument::onModelChanged, Qt::UniqueConnection); + connect(&m_applicationModels, &ApplicationModels::modelChanged, this, + &ProjectDocument::onModelChanged, Qt::UniqueConnection); } diff --git a/GUI/mainwindow/projectdocument.h b/GUI/mainwindow/projectdocument.h index 865990a8d013b66acd62dcd9853656dc751cfeac..5891de6d5e144077ecd1f79bf9e54bcf6d0705a1 100644 --- a/GUI/mainwindow/projectdocument.h +++ b/GUI/mainwindow/projectdocument.h @@ -15,6 +15,7 @@ #ifndef BORNAGAIN_GUI_MAINWINDOW_PROJECTDOCUMENT_H #define BORNAGAIN_GUI_MAINWINDOW_PROJECTDOCUMENT_H +#include "GUI/Models/ApplicationModels.h" #include <QObject> class QIODevice; @@ -54,7 +55,6 @@ public: static QString projectFileExtension(); - void setApplicationModels(ApplicationModels* applicationModels); ApplicationModels* applicationModels(); InstrumentModel* instrumentModel() const; SampleModel* sampleModel() const; @@ -92,7 +92,7 @@ private: QString m_project_dir; QString m_project_name; - ApplicationModels* m_applicationModels; + ApplicationModels m_applicationModels; bool m_modified; QString m_currentVersion; OutputDataIOService* m_dataService; diff --git a/GUI/mainwindow/projectmanager.cpp b/GUI/mainwindow/projectmanager.cpp index 570c32f143b7de9a81469eb4bbc57cab27892e19..2dd79ce5c4b75af6e23eac2f75756673f5e7d2c7 100644 --- a/GUI/mainwindow/projectmanager.cpp +++ b/GUI/mainwindow/projectmanager.cpp @@ -361,7 +361,6 @@ void ProjectManager::createNewProject() connect(m_project_document, &ProjectDocument::modified, this, &ProjectManager::onDocumentModified); m_project_document->setProjectName("Untitled"); - m_project_document->setApplicationModels(m_mainWindow->models()); m_saveService->setDocument(m_project_document); } @@ -373,7 +372,6 @@ void ProjectManager::deleteCurrentProject() delete m_project_document; m_project_document = 0; - m_mainWindow->models()->resetModels(); } //! Load project data from file name. If autosave info exists, opens dialog for project restore. diff --git a/Tests/Unit/GUI/TestDataItemViews.cpp b/Tests/Unit/GUI/TestDataItemViews.cpp index f6fd4dd74770da6c59b3679133c2ee262455ed1b..0b3c91ca271807fd51981688eb4ad742a749f5fa 100644 --- a/Tests/Unit/GUI/TestDataItemViews.cpp +++ b/Tests/Unit/GUI/TestDataItemViews.cpp @@ -123,27 +123,24 @@ TEST_F(TestDataItemViews, testSavingLinkedData) GuiUnittestUtils::create_dir(projectDir); const QString projectFileName(projectDir + "/document.pro"); { - ApplicationModels models; + ProjectDocument document; - SessionModel* real_data_model = models.realDataModel(); + SessionModel* real_data_model = document.realDataModel(); DataItem* item = insertNewDataItem(*real_data_model, 0.0); DataItem* item2 = insertNewDataItem(*real_data_model, 1.0); auto view_item = real_data_model->insertItem<DataPropertyContainer>(); view_item->addItem(item); view_item->addItem(item2); - document.setApplicationModels(&models); document.save(projectFileName); } - ApplicationModels models; MessageService message_service; ProjectDocument document; - document.setApplicationModels(&models); document.loadProjectFile(projectFileName, message_service); - SessionModel* real_data_model = models.realDataModel(); + SessionModel* real_data_model = document.realDataModel(); auto view_item = real_data_model->topItem<DataPropertyContainer>(); EXPECT_TRUE(view_item); diff --git a/Tests/Unit/GUI/TestProjectDocument.cpp b/Tests/Unit/GUI/TestProjectDocument.cpp index 22be47c6ee125467bcbcbffb8fe61a74662b5589..221f3c2f34fd6ca2660c0c71efd62709810cd2df 100644 --- a/Tests/Unit/GUI/TestProjectDocument.cpp +++ b/Tests/Unit/GUI/TestProjectDocument.cpp @@ -29,13 +29,7 @@ TEST_F(TestProjectDocument, test_projectDocument) GuiUnittestUtils::create_dir(projectDir); const QString projectFileName(projectDir + "/document.pro"); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); - instrument->setItemName("GISAS"); - ProjectDocument document; - document.setApplicationModels(&models); - // Checking initial document status EXPECT_FALSE(document.isModified()); EXPECT_FALSE(document.hasValidNameAndPath()); @@ -43,6 +37,9 @@ TEST_F(TestProjectDocument, test_projectDocument) EXPECT_EQ(document.projectName(), QString()); EXPECT_EQ(document.projectFileName(), QString()); + auto instrument = document.instrumentModel()->insertItem<GISASInstrumentItem>(); + instrument->setItemName("GISAS"); + // Checking document name and isModified status after project save document.save(projectFileName); EXPECT_TRUE(document.hasValidNameAndPath()); @@ -55,7 +52,7 @@ TEST_F(TestProjectDocument, test_projectDocument) EXPECT_EQ(spyDocument.count(), 0); // Changing document and checking its status - modify_models(&models); + modify_models(document.applicationModels()); EXPECT_TRUE(document.isModified()); EXPECT_EQ(spyDocument.count(), 1); @@ -73,18 +70,17 @@ TEST_F(TestProjectDocument, test_projectDocumentWithData) const QString projectDir("test_projectDocumentWithData"); GuiUnittestUtils::create_dir(projectDir); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); + ProjectDocument document; + auto instrument = document.instrumentModel()->insertItem<GISASInstrumentItem>(); instrument->setItemName("GISAS"); - RealDataItem* realData = GuiUnittestUtils::createRealData("RealData", *models.realDataModel()); + RealDataItem* realData = + GuiUnittestUtils::createRealData("RealData", *document.realDataModel()); ASSERT(realData); DataItem* intensityItem = realData->dataItem(); GUI::Model::JobItemUtils::createDefaultDetectorMap( - intensityItem, models.instrumentModel()->instrument2DItems().front()); + intensityItem, document.instrumentModel()->instrument2DItems().front()); intensityItem->setFileName("realdata.int.gz"); - ProjectDocument document; - document.setApplicationModels(&models); document.save(projectDir + "/untitled.pro"); QFileInfo info(projectDir + "/untitled.pro"); diff --git a/Tests/Unit/GUI/TestSaveService.cpp b/Tests/Unit/GUI/TestSaveService.cpp index 97ed8e4c4a6425585b2dd75d5800a35ecdcd7cb7..db0edd0e242c38aa5d93ebf3e27905c7e4419067 100644 --- a/Tests/Unit/GUI/TestSaveService.cpp +++ b/Tests/Unit/GUI/TestSaveService.cpp @@ -38,12 +38,9 @@ TEST_F(TestSaveService, test_autoSaveController) const int autosave_time(100); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); - instrument->setItemName("GISAS"); - std::unique_ptr<ProjectDocument> document(new ProjectDocument); - document->setApplicationModels(&models); + auto instrument = document->instrumentModel()->insertItem<GISASInstrumentItem>(); + instrument->setItemName("GISAS"); document->save(projectFileName); // setting up autosave @@ -58,7 +55,7 @@ TEST_F(TestSaveService, test_autoSaveController) QSignalSpy spyAutosave(&autosave, SIGNAL(autosaveRequest())); // modify document once and check that autosave directory was created - modify_models(&models); + modify_models(document->applicationModels()); EXPECT_TRUE(document->isModified()); EXPECT_TRUE(spyAutosave.wait(m_save_wait)); EXPECT_EQ(spyAutosave.count(), 1); @@ -71,7 +68,7 @@ TEST_F(TestSaveService, test_autoSaveController) // modify several times and check than autosave was triggered only once for (size_t i = 0; i < 10; ++i) - modify_models(&models); + modify_models(document->applicationModels()); EXPECT_TRUE(spyAutosave.wait(m_save_wait)); EXPECT_EQ(spyAutosave.count(), 2); @@ -85,12 +82,9 @@ TEST_F(TestSaveService, test_autoSaveController) TEST_F(TestSaveService, test_autoSaveControllerNewDocument) { - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); - instrument->setItemName("GISAS"); - std::unique_ptr<ProjectDocument> document(new ProjectDocument); - document->setApplicationModels(&models); + auto instrument = document->instrumentModel()->insertItem<GISASInstrumentItem>(); + instrument->setItemName("GISAS"); const int autosave_time(100); @@ -100,7 +94,7 @@ TEST_F(TestSaveService, test_autoSaveControllerNewDocument) QSignalSpy spyAutosave(&autosave, SIGNAL(autosaveRequest())); - modify_models(&models); + modify_models(document->applicationModels()); EXPECT_FALSE(spyAutosave.wait(autosave_time * 2)); EXPECT_EQ(spyAutosave.count(), 0); } @@ -114,13 +108,11 @@ TEST_F(TestSaveService, test_saveService) GuiUnittestUtils::create_dir(projectDir); const QString projectFileName(projectDir + "/document.pro"); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); - instrument->setItemName("GISAS"); std::unique_ptr<ProjectDocument> document(new ProjectDocument); - document->setApplicationModels(&models); - modify_models(&models); + auto instrument = document->instrumentModel()->insertItem<GISASInstrumentItem>(); + instrument->setItemName("GISAS"); + modify_models(document->applicationModels()); EXPECT_FALSE(QFile::exists(projectFileName)); @@ -150,13 +142,10 @@ TEST_F(TestSaveService, test_failingSaveService) // do NOT create dir in order to force saving to fail const QString projectFileName(projectDir + "/document.pro"); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); - instrument->setItemName("GISAS"); - std::unique_ptr<ProjectDocument> document(new ProjectDocument); - document->setApplicationModels(&models); - modify_models(&models); + auto instrument = document->instrumentModel()->insertItem<GISASInstrumentItem>(); + instrument->setItemName("GISAS"); + modify_models(document->applicationModels()); EXPECT_FALSE(QFile::exists(projectFileName)); @@ -184,20 +173,18 @@ TEST_F(TestSaveService, test_saveServiceWithData) GuiUnittestUtils::create_dir(projectDir); const QString projectFileName(projectDir + "/document.pro"); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); + std::unique_ptr<ProjectDocument> document(new ProjectDocument); + auto instrument = document->instrumentModel()->insertItem<GISASInstrumentItem>(); instrument->setItemName("GISAS"); - RealDataItem* realData = GuiUnittestUtils::createRealData("RealData", *models.realDataModel()); + RealDataItem* realData = + GuiUnittestUtils::createRealData("RealData", *document->realDataModel()); ASSERT(realData); DataItem* intensityItem = realData->dataItem(); GUI::Model::JobItemUtils::createDefaultDetectorMap( - intensityItem, models.instrumentModel()->instrument2DItems().front()); + intensityItem, document->instrumentModel()->instrument2DItems().front()); intensityItem->setFileName("realdata.int.gz"); - std::unique_ptr<ProjectDocument> document(new ProjectDocument); - document->setApplicationModels(&models); - EXPECT_FALSE(QFile::exists(projectFileName)); SaveService service; @@ -222,19 +209,17 @@ TEST_F(TestSaveService, test_autosaveEnabled) GuiUnittestUtils::create_dir(projectDir); const QString projectFileName(projectDir + "/document.pro"); - ApplicationModels models; - auto instrument = models.instrumentModel()->insertItem<GISASInstrumentItem>(); + std::unique_ptr<ProjectDocument> document(new ProjectDocument(projectFileName)); + auto instrument = document->instrumentModel()->insertItem<GISASInstrumentItem>(); instrument->setItemName("GISAS"); - RealDataItem* realData = GuiUnittestUtils::createRealData("RealData", *models.realDataModel()); + RealDataItem* realData = + GuiUnittestUtils::createRealData("RealData", *document->realDataModel()); DataItem* intensityItem = realData->dataItem(); GUI::Model::JobItemUtils::createDefaultDetectorMap( - intensityItem, models.instrumentModel()->instrument2DItems().front()); + intensityItem, document->instrumentModel()->instrument2DItems().front()); intensityItem->setFileName("realdata.int.gz"); - - std::unique_ptr<ProjectDocument> document(new ProjectDocument(projectFileName)); - document->setApplicationModels(&models); - + document->setModified(false); EXPECT_FALSE(document->isModified()); SaveService service; @@ -255,7 +240,7 @@ TEST_F(TestSaveService, test_autosaveEnabled) // modify several times and check SaveService signals for (size_t i = 0; i < 10; ++i) - modify_models(&models); + modify_models(document->applicationModels()); EXPECT_TRUE(document->isModified());