From 554f138d6d3848b7b818f561bf91f8d9c8d8adf6 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 25 Dec 2017 15:33:10 +0100 Subject: [PATCH] Made slow part of MainWindow initialization asynchronous. --- pglab/MainWindow.cpp | 30 ++++++++++++++++++++++-------- pglab/MainWindow.h | 4 +++- pglab/OpenDatabase.h | 4 ++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/pglab/MainWindow.cpp b/pglab/MainWindow.cpp index 47eb17c..be3287f 100644 --- a/pglab/MainWindow.cpp +++ b/pglab/MainWindow.cpp @@ -14,7 +14,7 @@ #include "QueryTab.h" #include "util.h" #include "MasterController.h" -#include "OpenDatabase.h" +#include "WorkManager.h" namespace pg = Pgsql; @@ -56,13 +56,31 @@ void MainWindow::setConfig(const ConnectionConfig &config) { m_config = config; try { - auto res = OpenDatabase::createOpenDatabase(config); - //if (res.valid()) { - m_database = res.get(); QString title = "pglab - "; title += m_config.name().c_str(); setWindowTitle(title); + WorkManager::getWorkManager()->addWork( + [config, this]() { + auto res = OpenDatabase::createOpenDatabase(config); + QueueTask([this, res] () { + catalogLoaded(res); + }); + }); + } catch (std::runtime_error &ex) { + QMessageBox::critical(this, "Error reading database", + QString::fromUtf8(ex.what())); + + close(); + } +} + +void MainWindow::catalogLoaded(Expected res) +{ + try { + + m_database = res.get(); + auto tt = new TablesPage(this); tt->setCatalog(m_database->catalogue()); ui->tabWidget->addTab(tt, "Tables"); @@ -70,10 +88,6 @@ void MainWindow::setConfig(const ConnectionConfig &config) newSqlPage(); } catch (std::runtime_error &ex) { -// QMessageBox msgBox; -// msgBox.setText(QString::fromUtf8(ex.what())); -// msgBox.exec(); - QMessageBox::critical(this, "Error reading database", QString::fromUtf8(ex.what())); diff --git a/pglab/MainWindow.h b/pglab/MainWindow.h index 28fc99d..e727c4f 100644 --- a/pglab/MainWindow.h +++ b/pglab/MainWindow.h @@ -3,7 +3,7 @@ #include "ASyncDBConnection.h" #include "ConnectionConfig.h" -#include "tsqueue.h" +#include "OpenDatabase.h" #include #include "ASyncWindow.h" #include @@ -64,6 +64,8 @@ private: void showEvent(QShowEvent *event); QueryTab *newSqlPage(); + void catalogLoaded(Expected result); + private slots: void on_actionLoad_SQL_triggered(); diff --git a/pglab/OpenDatabase.h b/pglab/OpenDatabase.h index 7c8735b..d9c88e9 100644 --- a/pglab/OpenDatabase.h +++ b/pglab/OpenDatabase.h @@ -16,8 +16,8 @@ public: using OpenDatabaseSPtr = std::shared_ptr; static Expected createOpenDatabase(const ConnectionConfig &cfg); -// using on_createResult_callback = std::function>, qint64)>; -// void asyncCreateOpenDatabase(const ConnectionConfig &cfg, ); +// using on_createResult_callback = std::function>)>; +// void asyncCreateOpenDatabase(const ConnectionConfig &cfg, on_createResult_callback); OpenDatabase(const OpenDatabase &) = delete; OpenDatabase& operator=(const OpenDatabase &) = delete;