ft: Closing windows asks to save queries before closing the window.

fix: closetab did incorrectly close when cancel was choosen.
This commit is contained in:
eelke 2020-04-04 13:23:43 +02:00
parent adb44fc157
commit dea76e17c5
7 changed files with 59 additions and 10 deletions

View file

@ -9,6 +9,7 @@
#include "TaskExecutor.h"
#include <QAction>
#include <QApplication>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMenuBar>
#include <QMessageBox>
@ -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<ManagedPage*>(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<QueryTool*>(widget);
if (qt && qt->canClose()) {
m_tabWidget->removeTab(index);
auto mp = dynamic_cast<ManagedPage*>(widget);
if (mp) {
if (mp->CanClose(true)) {
m_tabWidget->removeTab(index);
}
}
else if (index >= 0) {
m_tabWidget->removeTab(index);
}
}
}
void DatabaseWindow::catalogLoaded()

View file

@ -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;

2
pglab/ManagedPage.cpp Normal file
View file

@ -0,0 +1,2 @@
#include "ManagedPage.h"

17
pglab/ManagedPage.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef MANAGEDPAGE_H
#define MANAGEDPAGE_H
#include <QWidget>
class ManagedPage: public QWidget
{
Q_OBJECT
public:
using QWidget::QWidget;
virtual ~ManagedPage() {}
virtual bool CanClose(bool prompt_user) = 0;
};
#endif // MANAGEDPAGE_H

View file

@ -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;

View file

@ -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);

View file

@ -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