summaryrefslogtreecommitdiff
path: root/numberator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'numberator.cpp')
-rw-r--r--numberator.cpp72
1 files changed, 54 insertions, 18 deletions
diff --git a/numberator.cpp b/numberator.cpp
index 2497403..912c5e3 100644
--- a/numberator.cpp
+++ b/numberator.cpp
@@ -2,6 +2,8 @@
#include "ui_numberator.h"
#include "ui_TagListDock.h"
+#include <QMessageBox>
+
Numberator::Numberator(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Numberator)
@@ -14,6 +16,12 @@ Numberator::Numberator(QWidget *parent)
{
ui->setupUi(this);
+ connect(&proj, &SQLiteSaveFile::fileIOError, [=](auto e, QString errorName, QString description) {
+ Q_UNUSED(e);
+ qDebug() << errorName << ": " << description;
+ QMessageBox::critical(this, errorName, description);
+ });
+
QDockWidget *dock = new QDockWidget(this);
tagsDockUi->setupUi(dock);
addDockWidget(Qt::LeftDockWidgetArea, dock);
@@ -45,26 +53,29 @@ Numberator::Numberator(QWidget *parent)
connect(&saveOpenDialog, &QFileDialog::accepted, [=]() {
settings.setValue("MainWindow/SaveAsFileDialogState", this->saveOpenDialog.saveState());
});
- connect(ui->actionSave_Project, &QAction::triggered, [=](bool checked){
- Q_UNUSED(checked);
- this->saveOpenDialog.setWindowTitle("Save Project as...");
- disconnect(&this->saveOpenDialog, &QFileDialog::fileSelected, nullptr, nullptr);
- connect(&this->saveOpenDialog, &QFileDialog::fileSelected,
- &this->proj, &SQLiteSaveFile::saveAs);
- this->saveOpenDialog.open();
+ connect(ui->actionSave_Project, &QAction::triggered, this, &Numberator::showSaveDialog);
+ connect(ui->actionOpen_Project, &QAction::triggered, [=](){
+ if (!showConfirmDiscardDialog())
+ return;
+
+ saveOpenDialog.setWindowTitle("Open Project...");
+ saveOpenDialog.setAcceptMode(QFileDialog::AcceptOpen);
+ if (saveOpenDialog.exec() == QDialog::Accepted)
+ proj.open(saveOpenDialog.selectedFiles().value(0));
});
- connect(ui->actionOpen_Project, &QAction::triggered, [=](bool checked){
- Q_UNUSED(checked);
- this->saveOpenDialog.setWindowTitle("Open Project...");
- disconnect(&this->saveOpenDialog, &QFileDialog::fileSelected, nullptr, nullptr);
- connect(&this->saveOpenDialog, &QFileDialog::fileSelected,
- this, &Numberator::openFile);
- this->saveOpenDialog.open();
+
+ connect(ui->actionNew_Project, &QAction::triggered, [=]() {
+ if (!showConfirmDiscardDialog())
+ return;
+
+ proj.clearNew();
});
+ connect(ui->actionQuit, &QAction::triggered, [=]() {
+ if (!showConfirmDiscardDialog())
+ return;
- connect(ui->actionNew_Project, &QAction::triggered,
- &proj, &SQLiteSaveFile::clearNew);
- connect(ui->actionQuit, &QAction::triggered, &QApplication::quit);
+ QApplication::quit();
+ });
connect(ui->actionAbout, &QAction::triggered, &aboutDialog, &AboutDialog::open);
connect(tagsDockUi->tagList->selectionModel(), &QItemSelectionModel::currentChanged,
@@ -79,8 +90,33 @@ Numberator::~Numberator()
delete ui;
}
-void Numberator::openFile(const QString &path)
+bool Numberator::showConfirmDiscardDialog()
{
+ if (!proj.isMemory() || !proj.isDirty())
+ return true;
+
+ auto btn = QMessageBox::warning(this, "Discard unsaved changes?", "This document contains unsaved changes. Do you want to save these changes?",
+ QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
+
+ if (btn == QMessageBox::Cancel)
+ return false;
+
+ if (btn == QMessageBox::Save)
+ return showSaveDialog();
+ /* else, the discard button was clicked */
+ return true;
}
+bool Numberator::showSaveDialog()
+{
+ saveOpenDialog.setWindowTitle("Save Project as...");
+ saveOpenDialog.setAcceptMode(QFileDialog::AcceptSave);
+ if (saveOpenDialog.exec() == QDialog::Accepted) {
+ QString fn = this->saveOpenDialog.selectedFiles().value(0);
+ qDebug() << QString("Calling saveas(%1)").arg(fn);
+ return proj.saveAs(fn);
+ }
+
+ return false;
+}