From dea76e17c5f8a0a6c98df0c3d6f359343b5a80ea Mon Sep 17 00:00:00 2001 From: eelke Date: Sat, 4 Apr 2020 13:23:43 +0200 Subject: [PATCH] ft: Closing windows asks to save queries before closing the window. fix: closetab did incorrectly close when cancel was choosen. --- pglab/DatabaseWindow.cpp | 28 ++++++++++++++++++++++++---- pglab/DatabaseWindow.h | 4 ++++ pglab/ManagedPage.cpp | 2 ++ pglab/ManagedPage.h | 17 +++++++++++++++++ pglab/QueryTool.cpp | 9 ++++++--- pglab/QueryTool.h | 5 +++-- pglab/pglab.pro | 4 +++- 7 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 pglab/ManagedPage.cpp create mode 100644 pglab/ManagedPage.h diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index 88dc5a7..832fe02 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -9,6 +9,7 @@ #include "TaskExecutor.h" #include #include +#include #include #include #include @@ -24,6 +25,9 @@ namespace pg = Pgsql; + + + DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent) : QMainWindow(parent) , m_masterController(master) @@ -84,6 +88,20 @@ CrudTab *DatabaseWindow::GetActiveCrud() return ct; } +void DatabaseWindow::closeEvent(QCloseEvent *event) +{ + for (int idx = 0; idx < m_tabWidget->count(); ++idx) { + auto widget = m_tabWidget->widget(idx); + auto mp = dynamic_cast(widget); + if (mp) { + if (!mp->CanClose(true)) { + event->ignore(); + return; + } + } + } +} + void DatabaseWindow::setConfig(const ConnectionConfig &config) { m_config = config; @@ -352,13 +370,15 @@ void DatabaseWindow::closeTab(int index) { QWidget *widget = m_tabWidget->widget(index); - auto qt = dynamic_cast(widget); - if (qt && qt->canClose()) { - m_tabWidget->removeTab(index); + auto mp = dynamic_cast(widget); + if (mp) { + if (mp->CanClose(true)) { + m_tabWidget->removeTab(index); + } } else if (index >= 0) { m_tabWidget->removeTab(index); - } + } } void DatabaseWindow::catalogLoaded() diff --git a/pglab/DatabaseWindow.h b/pglab/DatabaseWindow.h index 7a01675..df1ae70 100644 --- a/pglab/DatabaseWindow.h +++ b/pglab/DatabaseWindow.h @@ -50,6 +50,10 @@ public: QueryTool *GetActiveQueryTool(); CrudTab *GetActiveCrud(); + +protected: + virtual void closeEvent(QCloseEvent *event) override; + private: QTabWidget *m_tabWidget = nullptr; QToolBar *m_mainToolBar = nullptr; diff --git a/pglab/ManagedPage.cpp b/pglab/ManagedPage.cpp new file mode 100644 index 0000000..a62f49e --- /dev/null +++ b/pglab/ManagedPage.cpp @@ -0,0 +1,2 @@ +#include "ManagedPage.h" + diff --git a/pglab/ManagedPage.h b/pglab/ManagedPage.h new file mode 100644 index 0000000..0f89488 --- /dev/null +++ b/pglab/ManagedPage.h @@ -0,0 +1,17 @@ +#ifndef MANAGEDPAGE_H +#define MANAGEDPAGE_H + +#include + +class ManagedPage: public QWidget +{ + Q_OBJECT +public: + using QWidget::QWidget; + + virtual ~ManagedPage() {} + + virtual bool CanClose(bool prompt_user) = 0; +}; + +#endif // MANAGEDPAGE_H diff --git a/pglab/QueryTool.cpp b/pglab/QueryTool.cpp index 8c66760..ee0dd7c 100644 --- a/pglab/QueryTool.cpp +++ b/pglab/QueryTool.cpp @@ -23,7 +23,7 @@ QueryTool::QueryTool(IDatabaseWindow *context, QWidget *parent) - : QWidget(parent) + : ManagedPage(parent) , m_context(context) , ui(new Ui::QueryTab) , m_dbConnection() @@ -61,11 +61,14 @@ QueryTool::~QueryTool() delete ui; } -bool QueryTool::canClose() +bool QueryTool::CanClose(bool prompt_user) { bool can_close; if (m_queryTextChanged) { - can_close = continueWithoutSavingWarning(); + if (prompt_user) + can_close = continueWithoutSavingWarning(); + else + can_close = false; } else { can_close = true; diff --git a/pglab/QueryTool.h b/pglab/QueryTool.h index 9d23315..0c6c4ff 100644 --- a/pglab/QueryTool.h +++ b/pglab/QueryTool.h @@ -1,6 +1,7 @@ #ifndef QUERYTAB_H #define QUERYTAB_H +#include "ManagedPage.h" #include "ASyncDBConnection.h" #include "QueryResultModel.h" #include "QueryExplainModel.h" @@ -32,7 +33,7 @@ class OpenDatabase; class QueryParamListController; class PgDatabaseCatalog; -class QueryTool : public QWidget { +class QueryTool : public ManagedPage { Q_OBJECT public: QueryTool(IDatabaseWindow *context, QWidget *parent = nullptr); @@ -43,7 +44,7 @@ public: void explain(bool analyze); - bool canClose(); + bool CanClose(bool prompt_user); void generateCode(); void exportDataToFilename(const QString &filename); diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 40e4782..bf4168f 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -25,6 +25,7 @@ SOURCES += main.cpp\ ConnectionController.cpp \ DependantsPage.cpp \ DependantsTableModel.cpp \ + ManagedPage.cpp \ NotificationListWidget.cpp \ NotificationModel.cpp \ NotificationService.cpp \ @@ -94,6 +95,7 @@ HEADERS += \ DependantsPage.h \ DependantsTableModel.h \ IDatabaseWindow.h \ + ManagedPage.h \ NotificationListWidget.h \ NotificationModel.h \ NotificationService.h \ @@ -174,7 +176,7 @@ FORMS += \ RESOURCES += \ resources.qrc -QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS,5.01 +#QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS,5.01 win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../core/release/ -lcore else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../core/debug/ -lcore