Moving things from the application specific DatabaseWindow to generic LMainWindow (Leon framework)

To achieve flexibility the getDatabase call on the context which was application specific to
has been replaced with a type based object registry.
This commit is contained in:
eelke 2019-01-05 09:49:12 +01:00
parent 4a78330153
commit fd603a7434
5 changed files with 137 additions and 99 deletions

View file

@ -48,7 +48,7 @@ CrudTab::~CrudTab()
void CrudTab::setConfig(Oid oid) //std::shared_ptr<OpenDatabase> db, const PgClass &table) void CrudTab::setConfig(Oid oid) //std::shared_ptr<OpenDatabase> db, const PgClass &table)
{ {
m_db = context()->getDatabase();; m_db = context()->getObject<OpenDatabase>(); // getDatabase();;
m_table = *m_db->catalog()->classes()->getByKey(oid); m_table = *m_db->catalog()->classes()->getByKey(oid);
m_crudModel->setConfig(m_db, *m_table); m_crudModel->setConfig(m_db, *m_table);
} }

View file

@ -47,10 +47,10 @@ namespace DatabaseWindow_details {
m_window->setTabIcon(content, iconname); m_window->setTabIcon(content, iconname);
} }
std::shared_ptr<OpenDatabase> getDatabase() override // std::shared_ptr<OpenDatabase> getDatabase() override
{ // {
return m_window->getDatabase(); // return m_window->getDatabase();
} // }
void showStatusMessage(const QString &msg) override void showStatusMessage(const QString &msg) override
{ {
@ -72,9 +72,30 @@ using namespace DatabaseWindow_details;
LMainWindow::LMainWindow(QWidget *parent) LMainWindow::LMainWindow(QWidget *parent)
: QMainWindow(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); m_fileMenu = new QMenu("File T", this);
createActions();
m_mainToolBar->addAction(m_closeAction);
// QMetaObject::connectSlotsByName(this);
} }
void LMainWindow::initModuleMenus() void LMainWindow::initModuleMenus()
@ -95,6 +116,19 @@ void LMainWindow::addModuleMenuActions()
} }
} }
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) void LMainWindow::addMenuAction(const MenuAction &ma)
{ {
qDebug() << "add action " << ma.text(); qDebug() << "add action " << ma.text();
@ -109,69 +143,56 @@ void LMainWindow::addMenuAction(const MenuAction &ma)
// auto ac = new QAction(this); // auto ac = new QAction(this);
// ac-> // 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) DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
: LMainWindow(parent) : LMainWindow(parent)
// , ui(new Ui::DatabaseWindow)
, m_masterController(master) , m_masterController(master)
{ {
// ui->setupUi(this);
// ui->tabWidget->setDocumentMode(true);
// auto centralWidget = new QWidget(this);
// auto verticalLayout_3 = new QVBoxLayout(centralWidget);
// verticalLayout_3->setSpacing(4);
// verticalLayout_3->setContentsMargins(4, 4, 4, 4);
m_tabWidget = new QTabWidget(this);
m_tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
m_tabWidget->setDocumentMode(true);
// verticalLayout_3->addWidget(m_tabWidget);
setCentralWidget(m_tabWidget);
createActions();
auto menuBar = new QMenuBar(this);
setMenuBar(menuBar);
m_mainToolBar = new QToolBar(this);
m_mainToolBar->addAction(m_closeAction);
addToolBar(Qt::TopToolBarArea, m_mainToolBar);
auto statusBar = new QStatusBar(this);
setStatusBar(statusBar);
m_context = new DatabaseWindowContentContext(this); m_context = new DatabaseWindowContentContext(this);
connect(&loadWatcher, &QFutureWatcher<LoadCatalog::Result>::finished, connect(&loadWatcher, &QFutureWatcher<LoadCatalog::Result>::finished,
this, &DatabaseWindow::catalogLoaded); this, &DatabaseWindow::catalogLoaded);
initModuleMenus(); initModuleMenus();
QMetaObject::connectSlotsByName(this); QMetaObject::connectSlotsByName(this);
} }
DatabaseWindow::~DatabaseWindow() DatabaseWindow::~DatabaseWindow()
{ {
delete m_context; delete m_context;
// delete ui;
} }
void DatabaseWindow::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);
}
}
QueryTab* DatabaseWindow::newSqlPage() QueryTab* DatabaseWindow::newSqlPage()
{ {
@ -226,6 +247,7 @@ void DatabaseWindow::catalogLoaded()
try { try {
//SCOPE_EXIT { loadFuture = {}; }; //SCOPE_EXIT { loadFuture = {}; };
m_database = loadWatcher.future().result(); m_database = loadWatcher.future().result();
m_context->registerObject(m_database);
auto tt = new TablesPage(m_context, this); auto tt = new TablesPage(m_context, this);
tt->setCatalog(m_database->catalog()); tt->setCatalog(m_database->catalog());
@ -308,11 +330,6 @@ void DatabaseWindow::on_actionExport_data_triggered()
} }
} }
void DatabaseWindow::actionClose_triggered()
{
on_tabWidget_tabCloseRequested(m_tabWidget->currentIndex());
}
void DatabaseWindow::on_actionAbout_triggered() void DatabaseWindow::on_actionAbout_triggered()
{ {
QMessageBox::about(this, "pgLab 0.1", tr( QMessageBox::about(this, "pgLab 0.1", tr(
@ -468,15 +485,6 @@ void DatabaseWindow::removeToolBarButtonsForPage(PluginContentWidget *page)
} }
} }
void DatabaseWindow::addPage(PluginContentWidget* page, QString caption)
{
m_tabWidget->addTab(page, caption);
m_tabWidget->setCurrentWidget(page);
//addToolBarButtonsForPage(page);
}
void DatabaseWindow::removePage(PluginContentWidget *) void DatabaseWindow::removePage(PluginContentWidget *)
{ {
@ -509,17 +517,3 @@ void DatabaseWindow::on_actionGenerate_code_triggered()
} }
} }
void DatabaseWindow::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 DatabaseWindow::setTabIcon(QWidget *widget, const QString &iconname)
{
auto index = m_tabWidget->indexOf(widget);
auto n = ":/icons/16x16/" + iconname;
m_tabWidget->setTabIcon(index, QIcon(n));
}

View file

@ -51,16 +51,33 @@ public:
LMainWindow(QWidget *parent = nullptr); LMainWindow(QWidget *parent = nullptr);
void initModuleMenus(); void initModuleMenus();
void setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint);
void setTabIcon(QWidget *widget, const QString &iconname);
/// Called when a newly created page is added to the QTabWidget
void addPage(PluginContentWidget* page, QString caption);
protected: protected:
QTabWidget *m_tabWidget = nullptr;
QToolBar *m_mainToolBar = nullptr;
// Standard menu's
QMenu *m_fileMenu = nullptr;
// Standard actions
QAction *m_closeAction = nullptr;
DatabaseWindow_details::DatabaseWindowContentContext *m_context; DatabaseWindow_details::DatabaseWindowContentContext *m_context;
void addModuleMenuActions(); void addModuleMenuActions();
private:
QMenu *m_fileMenu = nullptr;
private:
void createActions();
void addMenuAction(const MenuAction &ma); void addMenuAction(const MenuAction &ma);
private slots:
void actionClose_triggered();
}; };
@ -79,19 +96,8 @@ public:
void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres); void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres);
void setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint);
void setTabIcon(QWidget *widget, const QString &iconname);
/// Called when a newly created page is added to the QTabWidget
void addPage(PluginContentWidget* page, QString caption);
private: private:
QTabWidget *m_tabWidget = nullptr;
QToolBar *m_mainToolBar = nullptr;
// Standard actions
QAction *m_closeAction = nullptr;
ConnectionConfig m_config; ConnectionConfig m_config;
std::shared_ptr<OpenDatabase> m_database; std::shared_ptr<OpenDatabase> m_database;
@ -143,7 +149,6 @@ private:
QFutureWatcher<LoadCatalog::Result> loadWatcher; QFutureWatcher<LoadCatalog::Result> loadWatcher;
void createActions();
QueryTab *GetActiveQueryTab(); QueryTab *GetActiveQueryTab();
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
@ -165,7 +170,6 @@ private slots:
void on_actionLoad_SQL_triggered(); void on_actionLoad_SQL_triggered();
void on_actionSave_SQL_triggered(); void on_actionSave_SQL_triggered();
void on_actionExport_data_triggered(); void on_actionExport_data_triggered();
void actionClose_triggered();
void on_actionAbout_triggered(); void on_actionAbout_triggered();
void on_actionSave_SQL_as_triggered(); void on_actionSave_SQL_as_triggered();
void on_actionSave_copy_of_SQL_as_triggered(); void on_actionSave_copy_of_SQL_as_triggered();

View file

@ -31,8 +31,9 @@ QueryTab::QueryTab(IPluginContentWidgetContext *context_, QWidget *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
m_config = context()->getDatabase()->config(); auto db = context()->getObject<OpenDatabase>();
m_catalog = context()->getDatabase()->catalog(); m_config = db->config();
m_catalog = db->catalog();
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTab::connectionStateChanged); connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTab::connectionStateChanged);
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTab::receiveNotice); connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTab::receiveNotice);
@ -40,7 +41,7 @@ QueryTab::QueryTab(IPluginContentWidgetContext *context_, QWidget *parent)
ui->queryEdit->setFont(UserConfiguration::instance()->codeFont()); ui->queryEdit->setFont(UserConfiguration::instance()->codeFont());
highlighter = new SqlSyntaxHighlighter(ui->queryEdit->document()); highlighter = new SqlSyntaxHighlighter(ui->queryEdit->document());
auto open_database = context()->getDatabase(); auto open_database = context()->getObject<OpenDatabase>();
if (open_database) { if (open_database) {
auto cat = open_database->catalog(); auto cat = open_database->catalog();
highlighter->setTypes(*cat->types()); highlighter->setTypes(*cat->types());

View file

@ -2,7 +2,9 @@
#define IPLUGINCONTENTWIDGETCONTEXT_H #define IPLUGINCONTENTWIDGETCONTEXT_H
#include <QString> #include <QString>
#include <map>
#include <memory> #include <memory>
#include <typeindex>
#include "plugin_support/ModuleActionParameters.h" #include "plugin_support/ModuleActionParameters.h"
class OpenDatabase; class OpenDatabase;
@ -13,6 +15,9 @@ class PluginContentWidget;
* *
* It provides interface for operating on the context without needing to many details. * It provides interface for operating on the context without needing to many details.
* Actual default implementation is in PluginContentWidgetContextBase. * Actual default implementation is in PluginContentWidgetContextBase.
*
* objectRegistry implementation is from Igor Tandetnik answer to the following question
* https://stackoverflow.com/questions/35413745/using-shared-ptr-with-a-generic-registry-or-shared-object-storage-or-not
*/ */
class IPluginContentWidgetContext { class IPluginContentWidgetContext {
public: public:
@ -32,11 +37,6 @@ public:
*/ */
virtual void setIcon(PluginContentWidget *content, const QString &iconname) = 0; virtual void setIcon(PluginContentWidget *content, const QString &iconname) = 0;
/** Returns an OpenDatabase object the widget can use to access
* the database.
*/
virtual std::shared_ptr<OpenDatabase> getDatabase() = 0;
virtual void showStatusMessage(const QString &msg) = 0; virtual void showStatusMessage(const QString &msg) = 0;
virtual void moduleAction( virtual void moduleAction(
@ -46,6 +46,45 @@ public:
) = 0; ) = 0;
virtual void addContentWidget(PluginContentWidget *widget) = 0; virtual void addContentWidget(PluginContentWidget *widget) = 0;
template<typename T, class...Args>
bool addObjects(Args&&...args);
template<typename T>
bool registerObject(std::shared_ptr<T> object);
template<typename T>
std::shared_ptr<T> getObject() const;
private:
std::map<std::type_index, std::shared_ptr<void> > m_objectRegistry;
}; };
template<typename T, class...Args>
bool IPluginContentWidgetContext::addObjects(Args&&...args)
{
std::type_index key(typeid(T));
if (!m_objectRegistry.count(key)) {
auto p = std::make_shared<T>(std::forward<Args>(args)...);
m_objectRegistry[key] = p;
return true;
}
return false;
}
template<typename T>
bool IPluginContentWidgetContext::registerObject(std::shared_ptr<T> object)
{
return m_objectRegistry.emplace(std::type_index(typeid(T)), object).second;
}
template<typename T>
std::shared_ptr<T> IPluginContentWidgetContext::getObject() const
{
auto it = m_objectRegistry.find(typeid(T));
if (it == m_objectRegistry.end()) {
return {};
}
return std::static_pointer_cast<T>(it->second);
}
#endif // IPLUGINCONTENTWIDGETCONTEXT_H #endif // IPLUGINCONTENTWIDGETCONTEXT_H