Gives a warning when there are unsaved changes when opening a file or closing the window.
This commit is contained in:
parent
eb06c57141
commit
35932e7a8d
2 changed files with 42 additions and 0 deletions
|
|
@ -6,6 +6,7 @@
|
||||||
#include "sqlhighlighter.h"
|
#include "sqlhighlighter.h"
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
|
#include <QMessageBox>
|
||||||
#include <QTextDocumentFragment>
|
#include <QTextDocumentFragment>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QTextTable>
|
#include <QTextTable>
|
||||||
|
|
@ -14,6 +15,7 @@
|
||||||
#include "json/json.h"
|
#include "json/json.h"
|
||||||
#include "explaintreemodelitem.h"
|
#include "explaintreemodelitem.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <QCloseEvent>
|
||||||
|
|
||||||
//#include <thread>
|
//#include <thread>
|
||||||
|
|
||||||
|
|
@ -121,6 +123,8 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
QueueTask([this, details]() { receiveNotice(details); });
|
QueueTask([this, details]() { receiveNotice(details); });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &MainWindow::queryTextChanged);
|
||||||
|
|
||||||
m_timeElapsedLabel = new QLabel(this);
|
m_timeElapsedLabel = new QLabel(this);
|
||||||
statusBar()->addPermanentWidget(m_timeElapsedLabel);
|
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()
|
void MainWindow::on_actionLoad_SQL_triggered()
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
|
if (m_queryTextChanged && !continueWithoutSaving()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||||
QString file_name = QFileDialog::getOpenFileName(this,
|
QString file_name = QFileDialog::getOpenFileName(this,
|
||||||
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
|
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();
|
QString line = stream.readLine();
|
||||||
ui->queryEdit->appendPlainText(line);
|
ui->queryEdit->appendPlainText(line);
|
||||||
}
|
}
|
||||||
|
m_queryTextChanged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -452,6 +472,7 @@ void MainWindow::on_actionSave_SQL_triggered()
|
||||||
QTextStream stream(&file);
|
QTextStream stream(&file);
|
||||||
QString text = ui->queryEdit->toPlainText();
|
QString text = ui->queryEdit->toPlainText();
|
||||||
stream << text;
|
stream << text;
|
||||||
|
m_queryTextChanged = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -519,3 +540,18 @@ void MainWindow::on_actionCancel_triggered()
|
||||||
{
|
{
|
||||||
cancel_query();
|
cancel_query();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::queryTextChanged()
|
||||||
|
{
|
||||||
|
m_queryTextChanged = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::closeEvent(QCloseEvent *event)
|
||||||
|
{
|
||||||
|
if (!m_queryTextChanged || continueWithoutSaving()) {
|
||||||
|
event->accept();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
event->ignore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ namespace Pgsql {
|
||||||
class Connection;
|
class Connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class QCloseEvent;
|
||||||
|
|
||||||
class MainWindow : public QMainWindow
|
class MainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
|
|
@ -47,6 +48,7 @@ private:
|
||||||
std::unique_ptr<QTimer> m_timer;
|
std::unique_ptr<QTimer> m_timer;
|
||||||
std::chrono::time_point<std::chrono::steady_clock> m_startTime;
|
std::chrono::time_point<std::chrono::steady_clock> m_startTime;
|
||||||
ConnectionConfig m_config;
|
ConnectionConfig m_config;
|
||||||
|
bool m_queryTextChanged = false;
|
||||||
|
|
||||||
|
|
||||||
void startTimer();
|
void startTimer();
|
||||||
|
|
@ -67,6 +69,9 @@ private:
|
||||||
void query_ready(std::shared_ptr<Pgsql::Result> res);
|
void query_ready(std::shared_ptr<Pgsql::Result> res);
|
||||||
void explain_ready(std::shared_ptr<ExplainRoot> explain);
|
void explain_ready(std::shared_ptr<ExplainRoot> explain);
|
||||||
std::string getCommand() const;
|
std::string getCommand() const;
|
||||||
|
|
||||||
|
bool continueWithoutSaving();
|
||||||
|
void closeEvent(QCloseEvent *event);
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void startConnect();
|
void startConnect();
|
||||||
|
|
@ -75,6 +80,7 @@ private slots:
|
||||||
void performExplain();
|
void performExplain();
|
||||||
|
|
||||||
void cancel_query();
|
void cancel_query();
|
||||||
|
void queryTextChanged();
|
||||||
void receiveNotice(Pgsql::ErrorDetails notice);
|
void receiveNotice(Pgsql::ErrorDetails notice);
|
||||||
|
|
||||||
void processCallableQueue();
|
void processCallableQueue();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue