Gives a warning when there are unsaved changes when opening a file or closing the window.

This commit is contained in:
Eelke Klein 2017-01-19 17:48:44 +01:00
parent eb06c57141
commit 35932e7a8d
2 changed files with 42 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#include "sqlhighlighter.h"
#include <QStandardPaths>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextDocumentFragment>
#include <QTextStream>
#include <QTextTable>
@ -14,6 +15,7 @@
#include "json/json.h"
#include "explaintreemodelitem.h"
#include <algorithm>
#include <QCloseEvent>
//#include <thread>
@ -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();
}
}

View file

@ -27,6 +27,7 @@ namespace Pgsql {
class Connection;
}
class QCloseEvent;
class MainWindow : public QMainWindow
{
@ -47,6 +48,7 @@ private:
std::unique_ptr<QTimer> m_timer;
std::chrono::time_point<std::chrono::steady_clock> m_startTime;
ConnectionConfig m_config;
bool m_queryTextChanged = false;
void startTimer();
@ -67,6 +69,9 @@ private:
void query_ready(std::shared_ptr<Pgsql::Result> res);
void explain_ready(std::shared_ptr<ExplainRoot> 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();