File: MainWindow.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (132 lines) | stat: -rw-r--r-- 4,101 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Main/MainWindow.cpp
//! @brief     Implements class MainWindow.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Main/MainWindow.h"
#include "GUI/Model/Job/JobsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Util/Path.h"
#include "GUI/View/Base/mainwindow_constants.h"
#include "GUI/View/Main/ActionManager.h"
#include "GUI/View/Main/CentralWidget.h"
#include "GUI/View/Main/ProjectManager.h"
#include "GUI/View/Setup/FrameActions.h"
#include "GUI/View/Widget/AppConfig.h"
#include <QApplication>
#include <QDir>
#include <QMessageBox>
#include <QSettings>

MainWindow::MainWindow()
{
    m_project_manager = new ProjectManager(this);
    m_action_manager = new ActionManager(this, m_project_manager);
    m_central_widget = new CentralWidget;

    setCentralWidget(m_central_widget);

    initApplication();
    loadSettings();

    connect(gDoc.get(), &ProjectDocument::documentAboutToReopen,
            [this] { m_central_widget->currentView()->hide(); });
    connect(gDoc.get(), &ProjectDocument::documentOpened, this, &MainWindow::onDocumentOpened);

    connect(gDoc.get(), &ProjectDocument::documentOpened, m_action_manager,
            &ActionManager::updateActionEnabling);

    connect(gDoc.get(), &ProjectDocument::modifiedStateChanged, this,
            &MainWindow::onDocumentModified);

    connect(m_central_widget, &CentralWidget::currentViewChanged, m_action_manager,
            &ActionManager::onCurrentViewChanged);

    onDocumentOpened();
    gDoc->clearModified();
}

MainWindow::~MainWindow() = default;

void MainWindow::updateTitle()
{
    QString location = "not saved yet";
    if (gDoc->hasValidNameAndPath())
        location = GUI::Path::withTildeHomePath(QDir::toNativeSeparators(gDoc->projectFullPath()));
    const QString cModified = gDoc->isModified() ? "*" : "";
    setWindowTitle("BornAgain - " + cModified + gDoc->projectName() + " [" + location + "]");
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    if (gDoc->jobs()->hasUnfinishedJobs()) {
        QMessageBox::warning(this, "Cannot quit the application.",
                             "Cannot quit the application while jobs are running.\n"
                             "Cancel running jobs or wait until they are completed.");
        event->ignore();
        return;
    }
    if (m_project_manager->saveOnQuit()) {
        saveSettings();
        event->accept();
    } else {
        event->ignore();
    }
}

void MainWindow::initApplication()
{
    setDockNestingEnabled(true);
    setAcceptDrops(true);

    setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
    setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
}

void MainWindow::loadSettings()
{
    QSettings s;
    s.beginGroup(GUI::Style::S_MAIN_WINDOW);
    resize(s.value(GUI::Style::S_WINDOW_SIZE, QSize(1000, 600)).toSize());
    move(s.value(GUI::Style::S_WINDOW_POSITION, QPoint(500, 300)).toPoint());
    s.endGroup();
    m_project_manager->loadSettings();
}

void MainWindow::saveSettings()
{
    QSettings s;
    s.beginGroup(GUI::Style::S_MAIN_WINDOW);
    s.setValue(GUI::Style::S_WINDOW_SIZE, size());
    s.setValue(GUI::Style::S_WINDOW_POSITION, pos());
    s.endGroup();
    m_project_manager->saveSettings();
    gApp->saveSettings();
    s.sync();
}

void MainWindow::onDocumentOpened()
{
    updateTitle();
    m_central_widget->updateViews();
    m_central_widget->restoreView(gDoc->viewId());
}

void MainWindow::onDocumentModified()
{
    updateTitle();
}

void MainWindow::loadProject(QString projectPath)
{
    m_project_manager->openProject(projectPath);
}