From e0addbdea2950c20854de8455a080a3f1db9162d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 19 Apr 2017 00:41:08 -0500 Subject: [PATCH] Add New and Quit menu items to menu bar --- src/studio/main.cpp | 6 +++- src/studio/mainwindow.cpp | 62 +++++++++++++++++++++++++++++++++++++++ src/studio/mainwindow.hpp | 17 ++++++++++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/studio/main.cpp b/src/studio/main.cpp index 913dec4e..abc07038 100644 --- a/src/studio/main.cpp +++ b/src/studio/main.cpp @@ -16,7 +16,7 @@ using namespace nostalgia::studio; using namespace ox::clargs; -int main(int argc, char **args) { +int run(int argc, char **args) { ClArgs clargs(argc, (const char**) args); QString argProfilePath = clargs.getString("profile").c_str(); @@ -38,3 +38,7 @@ int main(int argc, char **args) { return app.exec(); } + +int main(int argc, char **args) { + return run(argc, args); +} diff --git a/src/studio/mainwindow.cpp b/src/studio/mainwindow.cpp index 97702c5e..6938ba76 100644 --- a/src/studio/mainwindow.cpp +++ b/src/studio/mainwindow.cpp @@ -6,13 +6,18 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include #include #include +#include +#include #include "mainwindow.hpp" namespace nostalgia { namespace studio { +using namespace std; + MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) { auto screenSize = QApplication::desktop()->screenGeometry(); @@ -23,9 +28,66 @@ MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) { move(screenSize.width() * (1 - sizePct) / 2, screenSize.height() * (1 - sizePct) / 2); setWindowTitle(config.app_name); + + setupMenu(); } MainWindow::~MainWindow() { + for (auto f : m_cleanupTasks) { + f(); + } +} + +void MainWindow::setupMenu() { + auto menu = menuBar(); + auto fileMenu = menu->addMenu(tr("&File")); + + // New... + addAction( + fileMenu, + tr("&New..."), + tr(""), + QKeySequence::New, + this, + SLOT(showNewDialog()) + ); + + // Exit + addAction( + fileMenu, + tr("E&xit"), + tr("Exit the application"), + QKeySequence::Quit, + QApplication::quit + ); +} + +void MainWindow::addAction(QMenu *menu, QString text, QString toolTip, + QKeySequence::StandardKey key, const QObject *tgt, const char *cb) { + auto action = menu->addAction(text); + action->setShortcuts(key); + action->setStatusTip(toolTip); + auto conn = connect(action, SIGNAL(triggered()), tgt, cb); + m_cleanupTasks.push_back([this, conn]() { + QObject::disconnect(conn); + }); +} + +void MainWindow::addAction(QMenu *menu, QString text, QString toolTip, + QKeySequence::StandardKey key, void (*cb)()) { + auto action = menu->addAction(text); + action->setShortcuts(key); + action->setStatusTip(toolTip); + auto conn = connect(action, &QAction::triggered, cb); + m_cleanupTasks.push_back([this, conn]() { + QObject::disconnect(conn); + }); +} + +void MainWindow::showNewDialog() { + QDialog dialog; + dialog.show(); + dialog.exec(); } } diff --git a/src/studio/mainwindow.hpp b/src/studio/mainwindow.hpp index bdb2f2fc..acd13448 100644 --- a/src/studio/mainwindow.hpp +++ b/src/studio/mainwindow.hpp @@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #pragma once #include @@ -12,6 +13,8 @@ #include #include #include +#include +#include namespace nostalgia { namespace studio { @@ -36,6 +39,7 @@ class MainWindow: public QMainWindow { private: QString m_projectPath; + QVector> m_cleanupTasks; public: MainWindow(NostalgiaStudioProfile config, QWidget *parent = 0); @@ -45,10 +49,21 @@ class MainWindow: public QMainWindow { private: void setupDockWidgets(); + + void setupMenu(); + + void addAction(QMenu *menu, QString text, QString toolTip, + QKeySequence::StandardKey key, const QObject *tgt, const char *cb); + + void addAction(QMenu *menu, QString text, QString toolTip, + QKeySequence::StandardKey key, void (*cb)()); + int readSettings(QString path); + int writeSettings(QString path); - public slots: + private slots: + void showNewDialog(); }; }