First tab at building a mechanism where tabpages can supply a list of actions that are added to the global main toolbar.

This commit is contained in:
eelke 2018-05-14 20:24:41 +02:00
parent c2d725ec6d
commit 3d516e6006
10 changed files with 130 additions and 26 deletions

View file

@ -7,7 +7,7 @@
CrudTab::CrudTab(MainWindow *parent)
: QWidget(parent)
: PlgPage(parent)
, ui(new Ui::CrudTab)
, m_window(parent)
{

View file

@ -3,6 +3,7 @@
#include "PgClass.h"
#include <QWidget>
#include "PlgPage.h"
#include <memory>
namespace Ui {
@ -13,7 +14,7 @@ class OpenDatabase;
class CrudModel;
class MainWindow;
class CrudTab : public QWidget
class CrudTab : public PlgPage
{
Q_OBJECT

View file

@ -13,6 +13,7 @@
#include <QMetaMethod>
#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,9 +265,20 @@ void MainWindow::on_actionNew_SQL_triggered()
newSqlPage();
}
void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
QWidget *widget = ui->tabWidget->widget(index);
PlgPage *plg_page = dynamic_cast<PlgPage*>(widget);
if (plg_page) {
if (plg_page->canClose()) {
removePage(plg_page);
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<QueryTab*>(widget);
if (qt && qt->canClose()) {
ui->tabWidget->removeTab(index);
@ -273,6 +287,7 @@ void MainWindow::on_tabWidget_tabCloseRequested(int index)
ui->tabWidget->removeTab(index);
}
}
}
void MainWindow::on_actionShow_connection_manager_triggered()
@ -319,3 +334,54 @@ void MainWindow::on_actionCopy_as_raw_Cpp_string_triggered()
tab->copyQueryAsRawCppString();
}
}
void MainWindow::addToolBarButtonsForPage(PlgPage *page)
{
std::vector<QAction*> actions = page->getToolbarActions();
QList<QAction*> list;
for (auto act : actions) {
list.append(act);
}
ui->mainToolBar->addActions(list);
}
void MainWindow::removeToolBarButtonsForPage(PlgPage *page)
{
std::vector<QAction*> 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<PlgPage*>(widget);
if (page) {
addToolBarButtonsForPage(page);
}
}
m_previousPage = page;
}

View file

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

View file

@ -1,6 +0,0 @@
#include "PglPage.h"
//PglPage::PglPage()
//{
//}

16
pglab/PlgPage.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "PlgPage.h"
//PglPage::PglPage()
//{
//}
std::vector<QAction*> PlgPage::getToolbarActions()
{
return std::vector<QAction*>();
}
bool PlgPage::canClose()
{
return true;
}

View file

@ -2,6 +2,7 @@
#define PGLPAGE_H
#include <QWidget>
#include <vector>
/// 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<QAction*> getToolbarActions();
virtual bool canClose();
};
#endif // PGLPAGE_H

View file

@ -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<QAction*> 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;
}

View file

@ -8,7 +8,7 @@
#include "tuplesresultwidget.h"
#include <QWidget>
#include "PglPage.h"
#include "PlgPage.h"
#include <memory>
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<QAction*> getToolbarActions() override;
private:
using ResultTabContainer = std::vector<TuplesResultWidget*>;
@ -70,6 +72,7 @@ private:
SqlSyntaxHighlighter* highlighter;
ConnectionConfig m_config;
StopWatch m_stopwatch;
std::vector<QAction*> actions;
QueryParamListController *m_queryParamListController = nullptr;

View file

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