DatabaseWindow has no knowledge more of the existence of QueryTab but user can still create and use them within that window.
This commit is contained in:
parent
fd603a7434
commit
d0c4dabe8b
8 changed files with 327 additions and 451 deletions
|
|
@ -1,12 +1,9 @@
|
|||
#include "DatabaseWindow.h"
|
||||
//#include "ui_DatabaseWindow.h"
|
||||
#include "plugin_support/IPluginContentWidgetContext.h"
|
||||
#include "TablesPage.h"
|
||||
#include "FunctionsPage.h"
|
||||
#include "SequencesPage.h"
|
||||
#include "QueryTab.h"
|
||||
#include "util.h"
|
||||
#include "plugin_support/PluginContentWidget.h"
|
||||
#include "plugin_support/PluginContentWidgetContextBase.h"
|
||||
#include "CodeGenerator.h"
|
||||
#include "MasterController.h"
|
||||
#include "ScopeGuard.h"
|
||||
|
|
@ -16,171 +13,22 @@
|
|||
#include <QCloseEvent>
|
||||
#include <QElapsedTimer>
|
||||
#include <QFileDialog>
|
||||
#include <QMenuBar>
|
||||
#include <QMessageBox>
|
||||
#include <QMetaMethod>
|
||||
#include <QMetaObject>
|
||||
#include <QStandardPaths>
|
||||
#include <QStatusBar>
|
||||
#include <QTextTable>
|
||||
#include <QToolBar>
|
||||
#include <QVBoxLayout>
|
||||
#include <algorithm>
|
||||
|
||||
namespace pg = Pgsql;
|
||||
|
||||
namespace DatabaseWindow_details {
|
||||
|
||||
class DatabaseWindowContentContext: public PluginContentWidgetContextBase {
|
||||
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 showStatusMessage(const QString &msg) override
|
||||
{
|
||||
m_window->statusBar()->showMessage(msg);
|
||||
}
|
||||
|
||||
void addContentWidget(PluginContentWidget *widget) override
|
||||
{
|
||||
m_window->addPage(widget, "");
|
||||
}
|
||||
private:
|
||||
DatabaseWindow *m_window;
|
||||
};
|
||||
|
||||
}
|
||||
using namespace DatabaseWindow_details;
|
||||
|
||||
|
||||
LMainWindow::LMainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
{
|
||||
m_tabWidget = new QTabWidget(this);
|
||||
m_tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
|
||||
m_tabWidget->setDocumentMode(true);
|
||||
|
||||
setCentralWidget(m_tabWidget);
|
||||
|
||||
// menu
|
||||
auto menuBar = new QMenuBar(this);
|
||||
setMenuBar(menuBar);
|
||||
|
||||
// tooolbar
|
||||
m_mainToolBar = new QToolBar(this);
|
||||
addToolBar(Qt::TopToolBarArea, m_mainToolBar);
|
||||
|
||||
// statusbar
|
||||
auto statusBar = new QStatusBar(this);
|
||||
setStatusBar(statusBar);
|
||||
|
||||
m_fileMenu = new QMenu("File T", this);
|
||||
|
||||
createActions();
|
||||
m_mainToolBar->addAction(m_closeAction);
|
||||
|
||||
// QMetaObject::connectSlotsByName(this);
|
||||
}
|
||||
|
||||
void LMainWindow::initModuleMenus()
|
||||
{
|
||||
menuBar()->addMenu(m_fileMenu);
|
||||
addModuleMenuActions();
|
||||
}
|
||||
|
||||
void LMainWindow::addModuleMenuActions()
|
||||
{
|
||||
auto reg = PluginRegister::getInstance();
|
||||
auto mods = reg->modules();
|
||||
for (auto && mod : mods) {
|
||||
auto items = mod.second->menuActions();
|
||||
for (auto && item : items) {
|
||||
addMenuAction(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LMainWindow::createActions()
|
||||
{
|
||||
{
|
||||
auto action = m_closeAction = new QAction(this);
|
||||
QIcon icon;
|
||||
icon.addFile(QString::fromUtf8(":/icons/page_white_delete.png"), QSize(), QIcon::Normal, QIcon::On);
|
||||
action->setIcon(icon);
|
||||
connect(m_closeAction, &QAction::triggered, this, &DatabaseWindow::actionClose_triggered);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void LMainWindow::addMenuAction(const MenuAction &ma)
|
||||
{
|
||||
qDebug() << "add action " << ma.text();
|
||||
//auto ac =
|
||||
m_fileMenu->addAction(ma.icon(), ma.text(),
|
||||
[ma, this] ()
|
||||
{
|
||||
ma.perform(m_context);
|
||||
},
|
||||
ma.shortCut());
|
||||
|
||||
|
||||
// auto ac = new QAction(this);
|
||||
// ac->
|
||||
}
|
||||
|
||||
void LMainWindow::actionClose_triggered()
|
||||
{
|
||||
m_tabWidget->tabCloseRequested(m_tabWidget->currentIndex());
|
||||
}
|
||||
|
||||
|
||||
void LMainWindow::addPage(PluginContentWidget* page, QString caption)
|
||||
{
|
||||
m_tabWidget->addTab(page, caption);
|
||||
m_tabWidget->setCurrentWidget(page);
|
||||
}
|
||||
|
||||
void LMainWindow::setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint)
|
||||
{
|
||||
auto index = m_tabWidget->indexOf(widget);
|
||||
m_tabWidget->setTabText(index, caption);
|
||||
m_tabWidget->setTabToolTip(index, hint);
|
||||
}
|
||||
|
||||
void LMainWindow::setTabIcon(QWidget *widget, const QString &iconname)
|
||||
{
|
||||
auto index = m_tabWidget->indexOf(widget);
|
||||
auto n = ":/icons/16x16/" + iconname;
|
||||
m_tabWidget->setTabIcon(index, QIcon(n));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
|
||||
: LMainWindow(parent)
|
||||
|
||||
, m_masterController(master)
|
||||
{
|
||||
m_context = new DatabaseWindowContentContext(this);
|
||||
|
||||
connect(&loadWatcher, &QFutureWatcher<LoadCatalog::Result>::finished,
|
||||
this, &DatabaseWindow::catalogLoaded);
|
||||
|
||||
|
|
@ -188,20 +36,7 @@ DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
|
|||
QMetaObject::connectSlotsByName(this);
|
||||
}
|
||||
|
||||
DatabaseWindow::~DatabaseWindow()
|
||||
{
|
||||
delete m_context;
|
||||
}
|
||||
|
||||
|
||||
QueryTab* DatabaseWindow::newSqlPage()
|
||||
{
|
||||
QueryTab *qt = new QueryTab(m_context);
|
||||
qt->newdoc();
|
||||
qt->focusEditor();
|
||||
addPage(qt, "Tab");
|
||||
return qt;
|
||||
}
|
||||
DatabaseWindow::~DatabaseWindow() = default;
|
||||
|
||||
void DatabaseWindow::newCreateTablePage()
|
||||
{
|
||||
|
|
@ -211,18 +46,11 @@ void DatabaseWindow::newCreateTablePage()
|
|||
|
||||
void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres)
|
||||
{
|
||||
auto cgtab = new CodeGenerator(m_context, this);
|
||||
auto cgtab = new CodeGenerator(context(), this);
|
||||
cgtab->Init(m_database->catalog(), query, dbres);
|
||||
addPage(cgtab, "Codegen");
|
||||
}
|
||||
|
||||
QueryTab *DatabaseWindow::GetActiveQueryTab()
|
||||
{
|
||||
QWidget *widget = m_tabWidget->currentWidget();
|
||||
QueryTab *qt = dynamic_cast<QueryTab*>(widget);
|
||||
return qt;
|
||||
}
|
||||
|
||||
void DatabaseWindow::setConfig(const ConnectionConfig &config)
|
||||
{
|
||||
m_config = config;
|
||||
|
|
@ -247,18 +75,19 @@ void DatabaseWindow::catalogLoaded()
|
|||
try {
|
||||
//SCOPE_EXIT { loadFuture = {}; };
|
||||
m_database = loadWatcher.future().result();
|
||||
m_context->registerObject(m_database);
|
||||
auto ctx = context();
|
||||
ctx->registerObject(m_database);
|
||||
|
||||
auto tt = new TablesPage(m_context, this);
|
||||
auto tt = new TablesPage(ctx, this);
|
||||
tt->setCatalog(m_database->catalog());
|
||||
m_tabWidget->addTab(tt, "Tables");
|
||||
|
||||
auto pg_cat_tables = new TablesPage(m_context, this);
|
||||
auto pg_cat_tables = new TablesPage(ctx, this);
|
||||
pg_cat_tables->setNamespaceFilter(TablesTableModel::PgCatalog);
|
||||
pg_cat_tables->setCatalog(m_database->catalog());
|
||||
m_tabWidget->addTab(pg_cat_tables, "pg_catalog");
|
||||
|
||||
auto info_schema_tables = new TablesPage(m_context, this);
|
||||
auto info_schema_tables = new TablesPage(ctx, this);
|
||||
info_schema_tables->setNamespaceFilter(TablesTableModel::InformationSchema);
|
||||
info_schema_tables->setCatalog(m_database->catalog());
|
||||
m_tabWidget->addTab(info_schema_tables, "information_schema");
|
||||
|
|
@ -271,7 +100,6 @@ void DatabaseWindow::catalogLoaded()
|
|||
sequences_page->setCatalog(m_database->catalog());
|
||||
m_tabWidget->addTab(sequences_page, "Sequences");
|
||||
|
||||
newSqlPage();
|
||||
newCreateTablePage();
|
||||
} catch (std::runtime_error &ex) {
|
||||
QMessageBox::critical(this, "Error reading database",
|
||||
|
|
@ -281,59 +109,11 @@ void DatabaseWindow::catalogLoaded()
|
|||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionLoad_SQL_triggered()
|
||||
{
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
QString file_name = QFileDialog::getOpenFileName(this,
|
||||
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
|
||||
if ( ! file_name.isEmpty()) {
|
||||
QueryTab* qt = newSqlPage();
|
||||
qt->load(file_name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionSave_SQL_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->save();
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionSave_SQL_as_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->saveAs();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionSave_copy_of_SQL_as_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->saveCopyAs();
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionExport_data_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
QString file_name = QFileDialog::getSaveFileName(this,
|
||||
tr("Export data"), home_dir, tr("CSV file (*.csv)"));
|
||||
|
||||
tab->exportData(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionAbout_triggered()
|
||||
{
|
||||
QMessageBox::about(this, "pgLab 0.1", tr(
|
||||
"Copyrights 2016-2018, Eelke Klein, All Rights Reserved.\n"
|
||||
"Copyrights 2016-2019, 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 "
|
||||
|
|
@ -388,40 +168,6 @@ void DatabaseWindow::showEvent(QShowEvent *event)
|
|||
event->accept();
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionNew_SQL_triggered()
|
||||
{
|
||||
newSqlPage();
|
||||
}
|
||||
|
||||
|
||||
void DatabaseWindow::on_tabWidget_tabCloseRequested(int index)
|
||||
{
|
||||
QWidget *widget = m_tabWidget->widget(index);
|
||||
PluginContentWidget *plg_page = dynamic_cast<PluginContentWidget*>(widget);
|
||||
if (plg_page) {
|
||||
if (plg_page->canClose()) {
|
||||
removePage(plg_page);
|
||||
m_tabWidget->removeTab(index);
|
||||
delete plg_page;
|
||||
}
|
||||
}
|
||||
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()) {
|
||||
m_tabWidget->removeTab(index);
|
||||
delete qt;
|
||||
}
|
||||
else if (index > 0) {
|
||||
m_tabWidget->removeTab(index);
|
||||
delete widget;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DatabaseWindow::on_actionShow_connection_manager_triggered()
|
||||
{
|
||||
m_masterController->showConnectionManager();
|
||||
|
|
@ -447,73 +193,3 @@ void DatabaseWindow::on_actionCopy_triggered()
|
|||
}
|
||||
|
||||
|
||||
void DatabaseWindow::on_actionCopy_as_C_string_triggered()
|
||||
{
|
||||
// Find which edit is active, copy the selected text or all text if no selection present
|
||||
// Put quote's around each line and add escapes.
|
||||
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->copyQueryAsCString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DatabaseWindow::on_actionCopy_as_raw_Cpp_string_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->copyQueryAsRawCppString();
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::addToolBarButtonsForPage(PluginContentWidget *page)
|
||||
{
|
||||
std::vector<QAction*> actions = page->getToolbarActions();
|
||||
QList<QAction*> list;
|
||||
for (auto act : actions) {
|
||||
list.append(act);
|
||||
}
|
||||
m_mainToolBar->addActions(list);
|
||||
}
|
||||
|
||||
void DatabaseWindow::removeToolBarButtonsForPage(PluginContentWidget *page)
|
||||
{
|
||||
std::vector<QAction*> actions = page->getToolbarActions();
|
||||
for (auto act : actions) {
|
||||
m_mainToolBar->removeAction(act);
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseWindow::removePage(PluginContentWidget *)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_tabWidget_currentChanged(int index)
|
||||
{
|
||||
// remove buttons of old page
|
||||
if (m_previousPage) {
|
||||
removeToolBarButtonsForPage(m_previousPage);
|
||||
}
|
||||
|
||||
// add buttons of new page
|
||||
PluginContentWidget * page = nullptr;
|
||||
if (index >= 0) {
|
||||
QWidget *widget = m_tabWidget->widget(index);
|
||||
page = dynamic_cast<PluginContentWidget*>(widget);
|
||||
if (page) {
|
||||
addToolBarButtonsForPage(page);
|
||||
}
|
||||
}
|
||||
m_previousPage = page;
|
||||
}
|
||||
|
||||
void DatabaseWindow::on_actionGenerate_code_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->generateCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue