diff --git a/mainwindow.cpp b/mainwindow.cpp index 959f847..b87974d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -6,6 +6,7 @@ #include "sqlhighlighter.h" #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include "json/json.h" #include "explaintreemodelitem.h" #include +#include //#include @@ -121,6 +123,8 @@ MainWindow::MainWindow(QWidget *parent) QueueTask([this, details]() { receiveNotice(details); }); }); + connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &MainWindow::queryTextChanged); + m_timeElapsedLabel = new QLabel(this); statusBar()->addPermanentWidget(m_timeElapsedLabel); } @@ -420,9 +424,24 @@ void MainWindow::endTimer() } } +bool MainWindow::continueWithoutSaving() +{ + QMessageBox msgBox; + msgBox.setText("The document has been modified."); + msgBox.setInformativeText("The current query has unsaved changes, do you want to continue without saving those changes?"); + msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + msgBox.setDefaultButton(QMessageBox::No); + int ret = msgBox.exec(); + return ret == QMessageBox::Yes; +} + void MainWindow::on_actionLoad_SQL_triggered() { // + if (m_queryTextChanged && !continueWithoutSaving()) { + return; + } + QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory); QString file_name = QFileDialog::getOpenFileName(this, tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)")); @@ -435,6 +454,7 @@ void MainWindow::on_actionLoad_SQL_triggered() QString line = stream.readLine(); ui->queryEdit->appendPlainText(line); } + m_queryTextChanged = false; } @@ -452,6 +472,7 @@ void MainWindow::on_actionSave_SQL_triggered() QTextStream stream(&file); QString text = ui->queryEdit->toPlainText(); stream << text; + m_queryTextChanged = false; } } } @@ -519,3 +540,18 @@ void MainWindow::on_actionCancel_triggered() { cancel_query(); } + +void MainWindow::queryTextChanged() +{ + m_queryTextChanged = true; +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + if (!m_queryTextChanged || continueWithoutSaving()) { + event->accept(); + } + else { + event->ignore(); + } +} diff --git a/mainwindow.h b/mainwindow.h index 2a834a1..05dc4da 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -27,6 +27,7 @@ namespace Pgsql { class Connection; } +class QCloseEvent; class MainWindow : public QMainWindow { @@ -47,6 +48,7 @@ private: std::unique_ptr m_timer; std::chrono::time_point m_startTime; ConnectionConfig m_config; + bool m_queryTextChanged = false; void startTimer(); @@ -67,6 +69,9 @@ private: void query_ready(std::shared_ptr res); void explain_ready(std::shared_ptr explain); std::string getCommand() const; + + bool continueWithoutSaving(); + void closeEvent(QCloseEvent *event); private slots: void startConnect(); @@ -75,6 +80,7 @@ private slots: void performExplain(); void cancel_query(); + void queryTextChanged(); void receiveNotice(Pgsql::ErrorDetails notice); void processCallableQueue();