diff --git a/pglab/CrudTab.cpp b/pglab/CrudTab.cpp index ef5d9e0..98fc220 100644 --- a/pglab/CrudTab.cpp +++ b/pglab/CrudTab.cpp @@ -7,7 +7,7 @@ CrudTab::CrudTab(MainWindow *parent) - : QWidget(parent) + : PlgPage(parent) , ui(new Ui::CrudTab) , m_window(parent) { diff --git a/pglab/CrudTab.h b/pglab/CrudTab.h index 768e2a3..d783dc1 100644 --- a/pglab/CrudTab.h +++ b/pglab/CrudTab.h @@ -3,6 +3,7 @@ #include "PgClass.h" #include +#include "PlgPage.h" #include namespace Ui { @@ -13,7 +14,7 @@ class OpenDatabase; class CrudModel; class MainWindow; -class CrudTab : public QWidget +class CrudTab : public PlgPage { Q_OBJECT diff --git a/pglab/MainWindow.cpp b/pglab/MainWindow.cpp index e8c136c..e58f190 100644 --- a/pglab/MainWindow.cpp +++ b/pglab/MainWindow.cpp @@ -13,6 +13,7 @@ #include #include "QueryTab.h" #include "util.h" +#include "PlgPage.h" #include "MasterController.h" #include "CrudTab.h" #include "WorkManager.h" @@ -40,8 +41,9 @@ QueryTab* MainWindow::newSqlPage() { QueryTab *qt = new QueryTab(this); qt->setConfig(m_config, m_database->catalogue()); - ui->tabWidget->addTab(qt, "Tab"); - ui->tabWidget->setCurrentWidget(qt); +// ui->tabWidget->addTab(qt, "Tab"); +// ui->tabWidget->setCurrentWidget(qt); + addPage(qt, "Tab"); qt->newdoc(); qt->focusEditor(); return qt; @@ -51,8 +53,9 @@ void MainWindow::newCrudPage(const PgClass &table) { CrudTab *ct = new CrudTab(this); ct->setConfig(m_database, table); - ui->tabWidget->addTab(ct, table.name); - ui->tabWidget->setCurrentWidget(ct); +// ui->tabWidget->addTab(ct, table.name); +// ui->tabWidget->setCurrentWidget(ct); + addPage(ct, table.name); } QueryTab *MainWindow::GetActiveQueryTab() @@ -262,15 +265,27 @@ void MainWindow::on_actionNew_SQL_triggered() newSqlPage(); } + void MainWindow::on_tabWidget_tabCloseRequested(int index) { QWidget *widget = ui->tabWidget->widget(index); - QueryTab *qt = dynamic_cast(widget); - if (qt && qt->canClose()) { - ui->tabWidget->removeTab(index); + PlgPage *plg_page = dynamic_cast(widget); + if (plg_page) { + if (plg_page->canClose()) { + removePage(plg_page); + ui->tabWidget->removeTab(index); + } } - else if (index > 0) { - ui->tabWidget->removeTab(index); + else { + // old behaviour shouldn't be needed any more when all pages have been migrated + // to PlgPage + QueryTab *qt = dynamic_cast(widget); + if (qt && qt->canClose()) { + ui->tabWidget->removeTab(index); + } + else if (index > 0) { + ui->tabWidget->removeTab(index); + } } } @@ -319,3 +334,54 @@ void MainWindow::on_actionCopy_as_raw_Cpp_string_triggered() tab->copyQueryAsRawCppString(); } } + +void MainWindow::addToolBarButtonsForPage(PlgPage *page) +{ + std::vector actions = page->getToolbarActions(); + QList list; + for (auto act : actions) { + list.append(act); + } + ui->mainToolBar->addActions(list); +} + +void MainWindow::removeToolBarButtonsForPage(PlgPage *page) +{ + std::vector actions = page->getToolbarActions(); + for (auto act : actions) { + ui->mainToolBar->removeAction(act); + } +} + + +void MainWindow::addPage(PlgPage* page, QString caption) +{ + ui->tabWidget->addTab(page, caption); + ui->tabWidget->setCurrentWidget(page); + + //addToolBarButtonsForPage(page); +} + +void MainWindow::removePage(PlgPage *page) +{ + +} + +void MainWindow::on_tabWidget_currentChanged(int index) +{ + // remove buttons of old page + if (m_previousPage) { + removeToolBarButtonsForPage(m_previousPage); + } + + // add buttons of new page + PlgPage * page = nullptr; + if (index >= 0) { + QWidget *widget = ui->tabWidget->widget(index); + page = dynamic_cast(widget); + if (page) { + addToolBarButtonsForPage(page); + } + } + m_previousPage = page; +} diff --git a/pglab/MainWindow.h b/pglab/MainWindow.h index 6bd1876..f82bd71 100644 --- a/pglab/MainWindow.h +++ b/pglab/MainWindow.h @@ -33,6 +33,7 @@ class MasterController; class QCloseEvent; class OpenDatabase; class PgClass; +class PlgPage; class MainWindow : public ASyncWindow { Q_OBJECT @@ -53,6 +54,7 @@ private: std::shared_ptr m_database; MasterController *m_masterController; + PlgPage *m_previousPage = nullptr; ///< tracks which pages buttons were previously being displayed class QLoad : public QueuedBackgroundTask { public: @@ -90,6 +92,14 @@ private: void catalogLoaded(); + /// Called when a newly created page is added to the QTabWidget + void addPage(PlgPage* page, QString caption); + /// Called when a page is completely removed from the QTabWidget + void removePage(PlgPage *page); + + void addToolBarButtonsForPage(PlgPage *page); + void removeToolBarButtonsForPage(PlgPage *page); + //class PageData private slots: void on_actionLoad_SQL_triggered(); @@ -109,6 +119,7 @@ private slots: void on_actionCopy_triggered(); void on_actionCopy_as_C_string_triggered(); void on_actionCopy_as_raw_Cpp_string_triggered(); + void on_tabWidget_currentChanged(int index); }; #endif // MAINWINDOW_H diff --git a/pglab/PglPage.cpp b/pglab/PglPage.cpp deleted file mode 100644 index a42347d..0000000 --- a/pglab/PglPage.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "PglPage.h" - -//PglPage::PglPage() -//{ - -//} diff --git a/pglab/PlgPage.cpp b/pglab/PlgPage.cpp new file mode 100644 index 0000000..9356af5 --- /dev/null +++ b/pglab/PlgPage.cpp @@ -0,0 +1,16 @@ +#include "PlgPage.h" + +//PglPage::PglPage() +//{ + +//} + +std::vector PlgPage::getToolbarActions() +{ + return std::vector(); +} + +bool PlgPage::canClose() +{ + return true; +} diff --git a/pglab/PglPage.h b/pglab/PlgPage.h similarity index 74% rename from pglab/PglPage.h rename to pglab/PlgPage.h index 8e7438d..ecb31ff 100644 --- a/pglab/PglPage.h +++ b/pglab/PlgPage.h @@ -2,6 +2,7 @@ #define PGLPAGE_H #include +#include /// Provides a pluggable system for toolbar buttons and menu actions /// @@ -14,11 +15,13 @@ /// Can we use same groupings for toolbars and menu's /// How about additional toolbars? /// -class PglPage: public QWidget{ +class PlgPage: public QWidget{ public: using QWidget::QWidget; - + /// Returns the toolbar buttons for this page + virtual std::vector getToolbarActions(); + virtual bool canClose(); }; #endif // PGLPAGE_H diff --git a/pglab/QueryTab.cpp b/pglab/QueryTab.cpp index ea04a7c..5f0402c 100644 --- a/pglab/QueryTab.cpp +++ b/pglab/QueryTab.cpp @@ -22,7 +22,7 @@ #include "GlobalIoService.h" QueryTab::QueryTab(MainWindow *win, QWidget *parent) : - PglPage(parent), + PlgPage(parent), ui(new Ui::QueryTab), m_win(win), m_dbConnection(*getGlobalAsioIoService()) @@ -603,3 +603,13 @@ void QueryTab::focusEditor() { ui->queryEdit->setFocus(); } + +std::vector QueryTab::getToolbarActions() +{ + if (actions.empty()) { + QAction *action = new QAction(tr("Execute"), this); + connect(action, &QAction::triggered, this, &QueryTab::execute); + actions.push_back(action); + } + return actions; +} diff --git a/pglab/QueryTab.h b/pglab/QueryTab.h index cd6e209..0d86c0b 100644 --- a/pglab/QueryTab.h +++ b/pglab/QueryTab.h @@ -8,7 +8,7 @@ #include "tuplesresultwidget.h" #include -#include "PglPage.h" +#include "PlgPage.h" #include namespace Ui { @@ -32,7 +32,7 @@ class OpenDatabase; class QueryParamListController; class PgDatabaseCatalog; -class QueryTab : public PglPage { +class QueryTab : public PlgPage { Q_OBJECT public: QueryTab(MainWindow *win, QWidget *parent = nullptr); @@ -61,6 +61,8 @@ public: bool isChanged() const { return m_queryTextChanged; } bool isNew() const { return m_new; } void focusEditor(); + + virtual std::vector getToolbarActions() override; private: using ResultTabContainer = std::vector; @@ -70,6 +72,7 @@ private: SqlSyntaxHighlighter* highlighter; ConnectionConfig m_config; StopWatch m_stopwatch; + std::vector actions; QueryParamListController *m_queryParamListController = nullptr; diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 6dae043..b258427 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -71,9 +71,9 @@ SOURCES += main.cpp\ CrudModel.cpp \ PgLabItemDelegate.cpp \ Module.cpp \ - PglPage.cpp \ EditorGutter.cpp \ - CodeEditor.cpp + CodeEditor.cpp \ + PlgPage.cpp HEADERS += \ QueryResultModel.h \ @@ -116,9 +116,9 @@ HEADERS += \ CrudModel.h \ PgLabItemDelegate.h \ Module.h \ - PglPage.h \ EditorGutter.h \ - CodeEditor.h + CodeEditor.h \ + PlgPage.h FORMS += mainwindow.ui \ ConnectionManagerWindow.ui \