Made a step in removing knowledge of DatabaseWindow from QueryTab as an effort to move
in the direction of a plugin system. DatabaseWindow now passes a Context to QueryTab and other pages that give those pages an API for passing information up the system without knowing anything about the sytem.
This commit is contained in:
parent
f6ea2ce0a6
commit
2a7e505dbf
13 changed files with 220 additions and 113 deletions
|
|
@ -15,20 +15,61 @@
|
|||
#include <QMetaMethod>
|
||||
#include "QueryTab.h"
|
||||
#include "util.h"
|
||||
#include "PlgPage.h"
|
||||
#include "PluginContentWidget.h"
|
||||
#include "CodeGenerator.h"
|
||||
#include "MasterController.h"
|
||||
#include "CrudTab.h"
|
||||
#include "WorkManager.h"
|
||||
#include "ScopeGuard.h"
|
||||
#include "EditTableWidget.h"
|
||||
#include "IPluginContentWidgetContext.h"
|
||||
|
||||
namespace pg = Pgsql;
|
||||
|
||||
namespace DatabaseWindow_details {
|
||||
|
||||
class DatabaseWindowContentContext: public IPluginContentWidgetContext {
|
||||
public:
|
||||
explicit DatabaseWindowContentContext(DatabaseWindow *window)
|
||||
: m_window(window)
|
||||
{}
|
||||
|
||||
void setCaption(PluginContentWidget *content, const QString &caption, const QString &hint = {}) override
|
||||
{
|
||||
m_window->setTabCaptionForWidget(content, caption, hint);
|
||||
}
|
||||
|
||||
void setIcon(PluginContentWidget *content, const QString &iconname) override
|
||||
{
|
||||
m_window->setTabIcon(content, iconname);
|
||||
}
|
||||
|
||||
std::shared_ptr<OpenDatabase> getDatabase() override
|
||||
{
|
||||
return m_window->getDatabase();
|
||||
}
|
||||
|
||||
void QueueTask(TSQueue::t_Callable c) override
|
||||
{
|
||||
m_window->QueueTask(c);
|
||||
}
|
||||
|
||||
void showStatusMessage(const QString &msg) override
|
||||
{
|
||||
m_window->statusBar()->showMessage(msg);
|
||||
}
|
||||
private:
|
||||
DatabaseWindow *m_window;
|
||||
};
|
||||
|
||||
}
|
||||
using namespace DatabaseWindow_details;
|
||||
|
||||
|
||||
DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
|
||||
: ASyncWindow(parent)
|
||||
, ui(new Ui::MainWindow)
|
||||
, m_context(new DatabaseWindowContentContext(this))
|
||||
, m_masterController(master)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
|
@ -38,12 +79,13 @@ DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
|
|||
DatabaseWindow::~DatabaseWindow()
|
||||
{
|
||||
loader.reset();
|
||||
delete m_context;
|
||||
delete ui;
|
||||
}
|
||||
|
||||
QueryTab* DatabaseWindow::newSqlPage()
|
||||
{
|
||||
QueryTab *qt = new QueryTab(this);
|
||||
QueryTab *qt = new QueryTab(m_context);
|
||||
qt->setConfig(m_config, m_database->catalog());
|
||||
addPage(qt, "Tab");
|
||||
qt->newdoc();
|
||||
|
|
@ -59,14 +101,14 @@ void DatabaseWindow::newCreateTablePage()
|
|||
|
||||
void DatabaseWindow::newCrudPage(const PgClass &table)
|
||||
{
|
||||
CrudTab *ct = new CrudTab(this);
|
||||
CrudTab *ct = new CrudTab(m_context, this);
|
||||
ct->setConfig(m_database, table);
|
||||
addPage(ct, table.objectName());
|
||||
}
|
||||
|
||||
void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres)
|
||||
{
|
||||
auto cgtab = new CodeGenerator(this);
|
||||
auto cgtab = new CodeGenerator(m_context, this);
|
||||
cgtab->Init(m_database->catalog(), query, dbres);
|
||||
addPage(cgtab, "Codegen");
|
||||
}
|
||||
|
|
@ -195,13 +237,13 @@ void DatabaseWindow::on_actionClose_triggered()
|
|||
void DatabaseWindow::on_actionAbout_triggered()
|
||||
{
|
||||
QMessageBox::about(this, "pgLab 0.1", tr(
|
||||
"Copyrights 2016-2017, Eelke Klein, All Rights Reserved.\n"
|
||||
"Copyrights 2016-2018, Eelke Klein, All Rights Reserved.\n"
|
||||
"\n"
|
||||
"The program is provided AS IS with NO WARRANTY OF ANY KIND, "
|
||||
"INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS "
|
||||
"FOR A PARTICULAR PURPOSE.\n"
|
||||
"\n"
|
||||
"This program is dynamically linked with Qt 5.9 Copyright (C) 2017 "
|
||||
"This program is dynamically linked with Qt 5.12 Copyright (C) 2018 "
|
||||
"The Qt Company Ltd. https://www.qt.io/licensing/. \n"
|
||||
"\n"
|
||||
"Icons by fatcow http://www.fatcow.com/free-icons provided under Creative Commons "
|
||||
|
|
@ -259,7 +301,7 @@ void DatabaseWindow::on_actionNew_SQL_triggered()
|
|||
void DatabaseWindow::on_tabWidget_tabCloseRequested(int index)
|
||||
{
|
||||
QWidget *widget = ui->tabWidget->widget(index);
|
||||
PlgPage *plg_page = dynamic_cast<PlgPage*>(widget);
|
||||
PluginContentWidget *plg_page = dynamic_cast<PluginContentWidget*>(widget);
|
||||
if (plg_page) {
|
||||
if (plg_page->canClose()) {
|
||||
removePage(plg_page);
|
||||
|
|
@ -325,7 +367,7 @@ void DatabaseWindow::on_actionCopy_as_raw_Cpp_string_triggered()
|
|||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::addToolBarButtonsForPage(PlgPage *page)
|
||||
void DatabaseWindow::addToolBarButtonsForPage(PluginContentWidget *page)
|
||||
{
|
||||
std::vector<QAction*> actions = page->getToolbarActions();
|
||||
QList<QAction*> list;
|
||||
|
|
@ -335,7 +377,7 @@ void DatabaseWindow::addToolBarButtonsForPage(PlgPage *page)
|
|||
ui->mainToolBar->addActions(list);
|
||||
}
|
||||
|
||||
void DatabaseWindow::removeToolBarButtonsForPage(PlgPage *page)
|
||||
void DatabaseWindow::removeToolBarButtonsForPage(PluginContentWidget *page)
|
||||
{
|
||||
std::vector<QAction*> actions = page->getToolbarActions();
|
||||
for (auto act : actions) {
|
||||
|
|
@ -344,7 +386,7 @@ void DatabaseWindow::removeToolBarButtonsForPage(PlgPage *page)
|
|||
}
|
||||
|
||||
|
||||
void DatabaseWindow::addPage(PlgPage* page, QString caption)
|
||||
void DatabaseWindow::addPage(PluginContentWidget* page, QString caption)
|
||||
{
|
||||
ui->tabWidget->addTab(page, caption);
|
||||
ui->tabWidget->setCurrentWidget(page);
|
||||
|
|
@ -352,7 +394,7 @@ void DatabaseWindow::addPage(PlgPage* page, QString caption)
|
|||
//addToolBarButtonsForPage(page);
|
||||
}
|
||||
|
||||
void DatabaseWindow::removePage(PlgPage *)
|
||||
void DatabaseWindow::removePage(PluginContentWidget *)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -365,10 +407,10 @@ void DatabaseWindow::on_tabWidget_currentChanged(int index)
|
|||
}
|
||||
|
||||
// add buttons of new page
|
||||
PlgPage * page = nullptr;
|
||||
PluginContentWidget * page = nullptr;
|
||||
if (index >= 0) {
|
||||
QWidget *widget = ui->tabWidget->widget(index);
|
||||
page = dynamic_cast<PlgPage*>(widget);
|
||||
page = dynamic_cast<PluginContentWidget*>(widget);
|
||||
if (page) {
|
||||
addToolBarButtonsForPage(page);
|
||||
}
|
||||
|
|
@ -384,3 +426,17 @@ void DatabaseWindow::on_actionGenerate_code_triggered()
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
void DatabaseWindow::setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint)
|
||||
{
|
||||
auto index = ui->tabWidget->indexOf(widget);
|
||||
ui->tabWidget->setTabText(index, caption);
|
||||
ui->tabWidget->setTabToolTip(index, hint);
|
||||
}
|
||||
|
||||
void DatabaseWindow::setTabIcon(QWidget *widget, const QString &iconname)
|
||||
{
|
||||
auto index = ui->tabWidget->indexOf(widget);
|
||||
auto n = ":/icons/16x16/" + iconname;
|
||||
ui->tabWidget->setTabIcon(index, QIcon(n));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue