diff --git a/source/tycho/cc_headers/menu.h b/source/tycho/cc_headers/menu.h index 2b13c3a..b01c64f 100644 --- a/source/tycho/cc_headers/menu.h +++ b/source/tycho/cc_headers/menu.h @@ -26,6 +26,9 @@ public slots: void openMapProperties(); void updateActions(); +protected: + void closeEvent(QCloseEvent *event) override; + private: ProjectView *activeProject() const; QMdiSubWindow *activeSubWindow() const; diff --git a/source/tycho/cc_headers/project.h b/source/tycho/cc_headers/project.h index bce62c6..a3b3c1f 100644 --- a/source/tycho/cc_headers/project.h +++ b/source/tycho/cc_headers/project.h @@ -2,7 +2,7 @@ #include "bindings.h" -#include +#include namespace Ui { @@ -17,13 +17,17 @@ public: explicit ProjectModel(); ~ProjectModel(); - bool open(QString fname); + bool isDirty() const; + +public slots: + bool open(const QString &fname); + void save(); private: Project data; }; -class ProjectView : public QWidget +class ProjectView : public QMdiSubWindow { Q_OBJECT @@ -33,6 +37,9 @@ public: QSharedPointer model(); +protected: + void closeEvent(QCloseEvent *event) override; + private: QSharedPointer ui; QSharedPointer proj; diff --git a/source/tycho/cc_source/menu.cc b/source/tycho/cc_source/menu.cc index bb5cca0..3e793d7 100644 --- a/source/tycho/cc_source/menu.cc +++ b/source/tycho/cc_source/menu.cc @@ -5,6 +5,7 @@ #include "../ui/ui_menu.h" #include "../ui/ui_about.h" +#include #include #include #include @@ -14,6 +15,13 @@ Menu::Menu(QWidget *parent) : ui(new Ui::Menu) { ui->setupUi(this); + + ui->actionOpen->setShortcut(QKeySequence(QKeySequence::Open)); + ui->actionNew->setShortcut(QKeySequence(QKeySequence::New)); + ui->actionQuit->setShortcut(QKeySequence(QKeySequence::Quit)); + ui->actionAbout->setShortcut(QKeySequence(QKeySequence::HelpContents)); + ui->actionMapProps->setShortcut(QKeySequence(tr("Ctrl+P"))); + dbgPrintFunc(); } @@ -75,6 +83,18 @@ void Menu::updateActions() ui->actionMapProps->setEnabled(active); } +void Menu::closeEvent(QCloseEvent *event) +{ + for(auto *win : ui->mdiArea->subWindowList()) { + if(!win->close()) { + event->ignore(); + return; + } + } + + event->accept(); +} + ProjectView *Menu::activeProject() const { auto win = activeSubWindow(); diff --git a/source/tycho/cc_source/projectmodel.cc b/source/tycho/cc_source/projectmodel.cc index 26384b2..a10f9ca 100644 --- a/source/tycho/cc_source/projectmodel.cc +++ b/source/tycho/cc_source/projectmodel.cc @@ -11,9 +11,19 @@ ProjectModel::~ProjectModel() dbgPrintFunc(); } -bool ProjectModel::open(QString fname) +bool ProjectModel::isDirty() const +{ + return true; +} + +bool ProjectModel::open(const QString &fname) { return data.open(fname); } +void ProjectModel::save() +{ + dbgPrintFunc(); +} + // EOF diff --git a/source/tycho/cc_source/projectview.cc b/source/tycho/cc_source/projectview.cc index fa6a452..ab9b594 100644 --- a/source/tycho/cc_source/projectview.cc +++ b/source/tycho/cc_source/projectview.cc @@ -2,12 +2,18 @@ #include "project.h" #include "../ui/ui_projectview.h" +#include +#include + ProjectView::ProjectView(QWidget *parent) : - QWidget(parent), + QMdiSubWindow(parent), ui(new Ui::ProjectView), proj(new ProjectModel) { - ui->setupUi(this); + auto widget = new QWidget(this); + ui->setupUi(widget); + setWidget(widget); + dbgPrintFunc(); } @@ -21,4 +27,33 @@ QSharedPointer ProjectView::model() return proj; } +void ProjectView::closeEvent(QCloseEvent *event) +{ + if(proj->isDirty()) { + QMessageBox msg; + msg.setText(tr("Do you want to save your changes to this project before closing it?")); + msg.setInformativeText(tr("Unsaved changes will be lost unless you save.")); + msg.setStandardButtons(QMessageBox::Save | + QMessageBox::Discard | + QMessageBox::Cancel); + msg.setDefaultButton(QMessageBox::Save); + + switch(msg.exec()) { + case QMessageBox::Save: + proj->save(); + break; + case QMessageBox::Discard: + break; + case QMessageBox::Cancel: + event->ignore(); + return; + default: + assert(true); + break; + } + } + + event->accept(); +} + // EOF diff --git a/source/tycho/ui/menu.ui b/source/tycho/ui/menu.ui index fd9582b..3c4e8ff 100644 --- a/source/tycho/ui/menu.ui +++ b/source/tycho/ui/menu.ui @@ -88,9 +88,6 @@ &Open Project - - Ctrl+O - @@ -100,9 +97,6 @@ &New Project - - Ctrl+N - @@ -124,9 +118,6 @@ &Quit - - Ctrl+Q - @@ -136,9 +127,6 @@ &About Tycho - - F1 -