#include "DatabaseWindow.h" #include "plugin_support/IPluginContentWidgetContext.h" #include "TablesPage.h" #include "FunctionsPage.h" #include "SequencesPage.h" #include "util.h" #include "CodeGenerator.h" #include "MasterController.h" #include "ScopeGuard.h" #include "EditTableWidget.h" #include "TaskExecutor.h" #include #include #include #include #include #include #include #include #include #include #include namespace pg = Pgsql; DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent) : LMainWindow(parent) , m_masterController(master) { connect(&loadWatcher, &QFutureWatcher::finished, this, &DatabaseWindow::catalogLoaded); initModuleMenus(); QMetaObject::connectSlotsByName(this); } DatabaseWindow::~DatabaseWindow() = default; void DatabaseWindow::newCreateTablePage() { auto w = new EditTableWidget(m_database, this); m_tabWidget->addTab(w, "Create table"); } void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr dbres) { auto cgtab = new CodeGenerator(context(), this); cgtab->Init(m_database->catalog(), query, dbres); addPage(cgtab, "Codegen"); } void DatabaseWindow::setConfig(const ConnectionConfig &config) { m_config = config; try { QString title = "pglab - "; title += m_config.name().c_str(); setWindowTitle(title); auto f = TaskExecutor::run(new LoadCatalog(m_config)); loadWatcher.setFuture(f); } catch (std::runtime_error &ex) { QMessageBox::critical(this, "Error reading database", QString::fromUtf8(ex.what())); close(); } } void DatabaseWindow::catalogLoaded() { try { //SCOPE_EXIT { loadFuture = {}; }; m_database = loadWatcher.future().result(); auto ctx = context(); ctx->registerObject(m_database); auto tt = new TablesPage(ctx, this); tt->setCatalog(m_database->catalog()); m_tabWidget->addTab(tt, "Tables"); 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(ctx, this); info_schema_tables->setNamespaceFilter(TablesTableModel::InformationSchema); info_schema_tables->setCatalog(m_database->catalog()); m_tabWidget->addTab(info_schema_tables, "information_schema"); auto functions_page = new FunctionsPage(this); functions_page->setCatalog(m_database->catalog()); m_tabWidget->addTab(functions_page, "Functions"); auto sequences_page = new SequencesPage(this); sequences_page->setCatalog(m_database->catalog()); m_tabWidget->addTab(sequences_page, "Sequences"); newCreateTablePage(); } catch (std::runtime_error &ex) { QMessageBox::critical(this, "Error reading database", QString::fromUtf8(ex.what())); close(); } } void DatabaseWindow::on_actionAbout_triggered() { QMessageBox::about(this, "pgLab 0.1", tr( "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 " "FOR A PARTICULAR PURPOSE.\n" "\n" "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 " "attribution 3.0 license." )); } void DatabaseWindow::closeEvent(QCloseEvent* /*event*/) { // TODO collect which files need saving // std::vector files_to_save; // int n = ui->tabWidget->count(); // for (int i = 0; i < n; ++i) { // QWidget *w = ui->tabWidget->widget(i); // QueryTab *qt = dynamic_cast(w); // if (qt) { // if (qt->isChanged()) { // files_to_save.push_back(qt->fileName()); // } // } // } // QString s; // for (const auto& e : files_to_save) { // s += e + "\n"; // } // QMessageBox msgBox; // msgBox.setIcon(QMessageBox::Warning); // msgBox.setText("The following documents need to be saved"); // msgBox.setInformativeText(s); // msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); // msgBox.setDefaultButton(QMessageBox::Cancel); // //int ret = // msgBox.exec(); } void DatabaseWindow::showEvent(QShowEvent *event) { if (!event->spontaneous()) { // m_queryTextChanged = false; } event->accept(); } void DatabaseWindow::on_actionShow_connection_manager_triggered() { m_masterController->showConnectionManager(); } void DatabaseWindow::on_actionCopy_triggered() { // What should be copied? QWidget *w = QApplication::focusWidget(); QTableView *tv = dynamic_cast(w); if (tv) { copySelectionToClipboard(tv); } else { const QMetaObject *meta = w->metaObject(); int i = meta->indexOfSlot("copy"); if (i != -1) { QMetaMethod method = meta->method(i); method.invoke(w, Qt::AutoConnection); } } }