From 78a666683913d832985943aca96bc154ff6ce5f8 Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 5 Aug 2018 09:07:12 +0200 Subject: [PATCH] WIP moving actions for toolbar to module system. --- pglab/AbstractCommand.h | 16 ++++++ pglab/MainWindow.cpp | 41 ---------------- pglab/MainWindow.h | 4 -- pglab/MainWindow.ui | 67 +------------------------ pglab/Module.cpp | 58 ++++++++++++++++++++++ pglab/Module.h | 105 ++++++++++++++++++++++++++++++++++++++++ pglab/QueryTab.cpp | 45 ++++++++++++++++- pglab/pglab.pro | 3 +- 8 files changed, 225 insertions(+), 114 deletions(-) create mode 100644 pglab/AbstractCommand.h diff --git a/pglab/AbstractCommand.h b/pglab/AbstractCommand.h new file mode 100644 index 0000000..ebb9952 --- /dev/null +++ b/pglab/AbstractCommand.h @@ -0,0 +1,16 @@ +#ifndef ABSTRACTCOMMAND_H +#define ABSTRACTCOMMAND_H + +#include + +class QAction; + +class AbstractCommand { +public: + virtual QAction* getAction() const = 0; + virtual QString getToolbar() const = 0; + virtual QString getMenuPath() const = 0; +}; + + +#endif // ABSTRACTCOMMAND_H diff --git a/pglab/MainWindow.cpp b/pglab/MainWindow.cpp index e58f190..225807c 100644 --- a/pglab/MainWindow.cpp +++ b/pglab/MainWindow.cpp @@ -179,47 +179,6 @@ void MainWindow::on_actionAbout_triggered() } -void MainWindow::on_actionExecute_SQL_triggered() -{ - QueryTab *tab = GetActiveQueryTab(); - if (tab) { - tab->execute(); - } - else { - QWidget *widget = ui->tabWidget->currentWidget(); - CrudTab *ct = dynamic_cast(widget); - if (ct) { - ct->refresh(); - } - - } -} - -void MainWindow::on_actionExplain_triggered() -{ - QueryTab *tab = GetActiveQueryTab(); - if (tab) { - tab->explain(false); - } -} - -void MainWindow::on_actionExplain_Analyze_triggered() -{ - QueryTab *tab = GetActiveQueryTab(); - if (tab) { - tab->explain(true); - } -} - -void MainWindow::on_actionCancel_triggered() -{ - QueryTab *tab = GetActiveQueryTab(); - if (tab) { - tab->cancel(); - } - -} - void MainWindow::closeEvent(QCloseEvent* /*event*/) { // TODO collect which files need saving diff --git a/pglab/MainWindow.h b/pglab/MainWindow.h index f82bd71..8fd7cc6 100644 --- a/pglab/MainWindow.h +++ b/pglab/MainWindow.h @@ -107,14 +107,10 @@ private slots: void on_actionExport_data_triggered(); void on_actionClose_triggered(); void on_actionAbout_triggered(); - void on_actionExecute_SQL_triggered(); - void on_actionExplain_Analyze_triggered(); - void on_actionCancel_triggered(); void on_actionSave_SQL_as_triggered(); void on_actionSave_copy_of_SQL_as_triggered(); void on_actionNew_SQL_triggered(); void on_tabWidget_tabCloseRequested(int index); - void on_actionExplain_triggered(); void on_actionShow_connection_manager_triggered(); void on_actionCopy_triggered(); void on_actionCopy_as_C_string_triggered(); diff --git a/pglab/MainWindow.ui b/pglab/MainWindow.ui index d0cbb29..7599ceb 100644 --- a/pglab/MainWindow.ui +++ b/pglab/MainWindow.ui @@ -45,7 +45,7 @@ 0 0 993 - 25 + 20 @@ -71,11 +71,7 @@ &Query - - - - @@ -113,10 +109,6 @@ - - - - @@ -177,47 +169,6 @@ &About - - - - :/icons/script_go.png - - - - &Execute queries - - - Execute the (selected) queries - - - F5 - - - - - - :/icons/script_delete.png - - - - &Cancel - - - Alt+Pause - - - - - - :/icons/lightbulb.png:/icons/lightbulb.png - - - Ex&plain Analyze - - - Shift+F7 - - Sa&ve SQL as @@ -241,22 +192,6 @@ Ctrl+N - - - - :/icons/lightbulb_off.png - - - - E&xplain - - - Explain the (selected) query - - - F7 - - &Show connection manager diff --git a/pglab/Module.cpp b/pglab/Module.cpp index 66ff01e..dcba800 100644 --- a/pglab/Module.cpp +++ b/pglab/Module.cpp @@ -1,8 +1,66 @@ #include "Module.h" +#include using namespace Leon; + +void ModuleRegistry::registerModule(Module *module) +{ + modules.insert(module); +} + + + +ModuleRegistry& Leon::GetModuleRegistry() +{ + static ModuleRegistry registry; + return registry; +} + Module::Module() { } + +ModuleInstance::ModuleInstance() +{ + GetModuleRegistry().registerModule(this); +} + + +//class QueryModule: public ModuleInstance { +//public: +// QueryModule(); + +// virtual QString getName() const override; + +// virtual CommandContainer getCommands() const override; +//private: +//// CommandContainer commands; + +// void createNewQueryTab(); +//}; + +//QueryModule::QueryModule() +//{ +//// commands = { +//// std::make_shared>("New SQL file", "", createNewQueryTab) +//// ->setHint("") +//// ->setDescription("") +//// ->setShortCut(Qt::CTRL + Qt::Key_N) +//// ->addOnToolbar("standard", "") +//// ->setMenuPath("file") +//// }; +//} + +//QString QueryModule::getName() const +//{ +// return "Query Module"; +//} + +////QueryModule::CommandContainer QueryModule::getCommands() const +////{ +//// return commands; +////} + +////QueryModule theQueryModuleInstance; diff --git a/pglab/Module.h b/pglab/Module.h index 62065c7..f8a3c22 100644 --- a/pglab/Module.h +++ b/pglab/Module.h @@ -1,15 +1,120 @@ #ifndef MODULE_H #define MODULE_H +#include "AbstractCommand.h" +#include +#include +#include +#include +#include +#include + +class QAction; + namespace Leon { +class Module; + +class ModuleRegistry { +public: + using ModuleContainer = std::unordered_set; + using Iterator = ModuleContainer::iterator; + + Iterator begin() { return modules.begin(); } + Iterator end() { return modules.end(); } + + void registerModule(Module *module); + +private: + + ModuleContainer modules; +}; + +ModuleRegistry& GetModuleRegistry(); + +template +class Command: public AbstractCommand { +public: + Command(QString caption, QString iconpath, Func f); + Command& setHint(QString hint); + Command& setDescription(QString description); + Command& setShortCut(QKeySequence shortcut); + Command& addOnToolbar(QString toolbar, QString group); + Command& setMenuPath(QString menu); + + + // action = new QAction(QIcon(":/icons/script_save.png"), tr("Save SQL"), this); + // action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + // connect(action, &QAction::triggered, this, &QueryTab::save); + // actions.push_back(action); +private: + mutable QAction *action = nullptr; +}; + + +/// Abstract interface for retrieving information about a subsystem +/// +/// Each module should derive a class From ModuleInstance and implement +/// the abstract methods declared in this interface. Don't do to much in your constructor +/// as it might be called very early in the startup of the program class Module { public: + using AbstractCommandSptr = std::shared_ptr; + using CommandContainer = std::vector; + Module(); + // getGlobalActions + virtual QString getName() const = 0; + + virtual CommandContainer getCommands() const = 0; }; +/// Adds auto registration to Module so not each +/// module has to implement registration +class ModuleInstance: public Module { +protected: + ModuleInstance(); +}; + + +class Path { +public: + explicit Path(QString p) + { + int ofs = 0; + if (p.isEmpty()) + return; + + if (p[0] == '/') + ++ofs; + + while (true) { + int i = p.indexOf('/', ofs); + if (i >= ofs) { + path.push_back(p.mid(ofs, i - ofs)); + ofs = i+1; + } + else { + path.push_back(p.right(ofs)); + break; + } + } + } + + int getDepth() const { return path.size(); } + QString getElem(int i) const { return path.at(i); } +private: + using PathContainer = boost::container::small_vector; + + PathContainer path; +}; + + + + + } #endif // MODULE_H diff --git a/pglab/QueryTab.cpp b/pglab/QueryTab.cpp index 465311a..0486ec2 100644 --- a/pglab/QueryTab.cpp +++ b/pglab/QueryTab.cpp @@ -1,9 +1,11 @@ -#include "QueryTab.h" + +#include "QueryTab.h" #include "ui_QueryTab.h" #include "SqlSyntaxHighlighter.h" #include #include +#include #include #include #include @@ -604,13 +606,52 @@ void QueryTab::focusEditor() ui->queryEdit->setFocus(); } + std::vector QueryTab::getToolbarActions() { if (actions.empty()) { - QAction *action = new QAction(QIcon(":/icons/script_go.png"), tr("Execute"), this); + QAction *action; + // New +// action = new QAction(QIcon(":/icons/new_query_tab.png"), tr("New"), this); +// action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); +// connect(action, &QAction::triggered, this, &QueryTab::); +// actions.push_back(action); + // Load + + // Save + action = new QAction(QIcon(":/icons/script_save.png"), tr("Save SQL"), this); + action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + connect(action, &QAction::triggered, this, &QueryTab::save); + actions.push_back(action); + // Save as + action = new QAction(QIcon(":/icons/script_go.png"), tr("Save SQL as"), this); + //action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + connect(action, &QAction::triggered, this, &QueryTab::saveAs); + actions.push_back(action); + // Save copy as + // Copy + // Copy as C-string + // Copy as raw cpp string + // Execute SQL + action = new QAction(QIcon(":/icons/script_go.png"), tr("Execute"), this); action->setShortcut(QKeySequence(Qt::Key_F5)); connect(action, &QAction::triggered, this, &QueryTab::execute); actions.push_back(action); + // Explain + action = new QAction(QIcon(":/icons/lightbulb_off.png"), tr("Explain"), this); + action->setShortcut(QKeySequence(Qt::Key_F7)); + connect(action, &QAction::triggered, this, [this] () { explain(false); }); + actions.push_back(action); + // Explain Anaylze + action = new QAction(QIcon(":/icons/lightbulb.png"), tr("Analyze"), this); + action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_F7)); + connect(action, &QAction::triggered, this, [this] () { explain(true); }); + actions.push_back(action); + // Cancel + action = new QAction(QIcon(":/icons/script_delete.png"), tr("Cancel"), this); + action->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Pause)); + connect(action, &QAction::triggered, this, &QueryTab::cancel); + actions.push_back(action); } return actions; } diff --git a/pglab/pglab.pro b/pglab/pglab.pro index b258427..3058c10 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -118,7 +118,8 @@ HEADERS += \ Module.h \ EditorGutter.h \ CodeEditor.h \ - PlgPage.h + PlgPage.h \ + AbstractCommand.h FORMS += mainwindow.ui \ ConnectionManagerWindow.ui \