BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
actionmanager.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/mainwindow/actionmanager.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <QAction>
18 #include <QMainWindow>
19 #include <QMenuBar>
20 #include <QToolBar>
21 
22 namespace gui2 {
23 
24 ActionManager::ActionManager(QMainWindow* mainwindow)
25  : QObject(mainwindow), m_mainWindow(mainwindow)
26 {
27  createActions();
28  setupMenus(m_mainWindow->menuBar());
29 }
30 
32 {
33  m_recentProjectMenu->clear();
34  m_recentProjectMenu->setEnabled(!m_recentProjects.isEmpty());
35 
36  for (auto project_dir : m_recentProjects) {
37  auto trimmed_project_dir = ModelView::Utils::WithTildeHomePath(project_dir);
38  auto action = m_recentProjectMenu->addAction(trimmed_project_dir);
39  action->setData(QVariant::fromValue(project_dir));
40  auto on_project_selected = [this, project_dir]() {
41  openExistingProjectRequest(project_dir);
42  };
43  connect(action, &QAction::triggered, on_project_selected);
44  }
45 
46  if (!m_recentProjects.empty()) {
47  m_recentProjectMenu->addSeparator();
48  auto action = m_recentProjectMenu->addAction("Clear Menu");
49  connect(action, &QAction::triggered, [this]() { clearResentProjectListRequest(); });
50  }
51 }
52 
53 void ActionManager::setRecentProjectsList(const QStringList& projects)
54 {
55  m_recentProjects = projects;
56 }
57 
58 //! Creates application-wise actions to create, open, save, and save-as projects.
59 
61 {
62  m_createNewProjectAction = new QAction("&New Project", this);
63  m_createNewProjectAction->setShortcuts(QKeySequence::New);
64  m_createNewProjectAction->setStatusTip("Create a new project");
65  connect(m_createNewProjectAction, &QAction::triggered, this,
67 
68  m_openExistingProjectAction = new QAction("&Open Project", this);
69  m_openExistingProjectAction->setShortcuts(QKeySequence::Open);
70  m_openExistingProjectAction->setStatusTip("Open an existing project");
71  connect(m_openExistingProjectAction, &QAction::triggered,
72  [this]() { openExistingProjectRequest({}); });
73 
74  m_saveCurrentProjectAction = new QAction("&Save Project", this);
75  m_saveCurrentProjectAction->setShortcuts(QKeySequence::Save);
76  m_saveCurrentProjectAction->setStatusTip("Save project");
77  m_saveCurrentProjectAction->setShortcutContext(Qt::ApplicationShortcut);
78  connect(m_saveCurrentProjectAction, &QAction::triggered, this,
80 
81  m_saveProjectAsAction = new QAction("Save &As...", this);
82  m_saveProjectAsAction->setShortcuts(QKeySequence::SaveAs);
83  m_saveProjectAsAction->setStatusTip("Save project under different name");
84  connect(m_saveProjectAsAction, &QAction::triggered, this, &ActionManager::saveProjectAsRequest);
85 
86  m_exitAction = new QAction("E&xit Application", this);
87  m_exitAction->setShortcuts(QKeySequence::Quit);
88  m_exitAction->setStatusTip("Exit the application");
89  connect(m_exitAction, &QAction::triggered, m_mainWindow, &QMainWindow::close);
90 }
91 
92 //! Equips menu with actions.
93 
94 void ActionManager::setupMenus(QMenuBar* menubar)
95 {
96  auto fileMenu = menubar->addMenu("&File");
97  connect(fileMenu, &QMenu::aboutToShow, this, &ActionManager::aboutToShowFileMenu);
98  fileMenu->addAction(m_createNewProjectAction);
99  fileMenu->addAction(m_openExistingProjectAction);
100  m_recentProjectMenu = fileMenu->addMenu("Recent Projects");
101 
102  fileMenu->addSeparator();
103  fileMenu->addAction(m_saveCurrentProjectAction);
104  fileMenu->addAction(m_saveProjectAsAction);
105 
106  fileMenu->addSeparator();
107  fileMenu->addAction(m_exitAction);
108 }
109 
110 } // namespace gui2
QAction * m_exitAction
Definition: actionmanager.h:60
void saveCurrentProjectRequest()
void setupMenus(QMenuBar *menubar)
Equips menu with actions.
void clearResentProjectListRequest()
QStringList m_recentProjects
Definition: actionmanager.h:64
QAction * m_saveCurrentProjectAction
Definition: actionmanager.h:58
void createActions()
Creates application-wise actions to create, open, save, and save-as projects.
QMainWindow * m_mainWindow
Definition: actionmanager.h:54
QAction * m_saveProjectAsAction
Definition: actionmanager.h:59
void setRecentProjectsList(const QStringList &projects)
QMenu * m_recentProjectMenu
Definition: actionmanager.h:62
void openExistingProjectRequest(const QString &dirname)
QAction * m_createNewProjectAction
Definition: actionmanager.h:56
void createNewProjectRequest()
QAction * m_openExistingProjectAction
Definition: actionmanager.h:57
ActionManager(QMainWindow *mainwindow=nullptr)
Defines class CLASS?
MVVM_VIEW_EXPORT QString WithTildeHomePath(const QString &path)
Returns a string where Linux path to the file is striped using '~/'.
Definition: widgetutils.cpp:79
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?