pgLab/pglab/QueryToolModule.cpp
eelke 601d071d0f Proof of concept for having the context actions statically defined in the module.
Needs work for correctly placing the items in menu and on toolbar.
Old system still needs to be removed left in place to keep app useable.
2019-08-14 09:06:48 +02:00

83 lines
2.9 KiB
C++

#include "QueryToolModule.h"
#include "QueryTool.h"
#include "plugin_support/IPluginContentWidgetContext.h"
#include "plugin_support/PluginRegister.h"
#include <QStandardPaths>
#include <QFileDialog>
void QueryToolModule::init()
{
std::string slot_name = SLOT(QueryTool::execute());
{
StaticAction ma("New SQL", [this] (IPluginContentWidgetContext* context)
{ staticAction_new(context); });
ma.setMenuLocation(MenuPath("File/New"));
ma.setToolbarLocation(ToolbarLocation("main", "new"));
ma.setIcon(QIcon(":/icons/new_query_tab.png"));
ma.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
registerStaticAction(ma);
}
{
StaticAction ma("Open SQL", [this] (IPluginContentWidgetContext* context)
{ staticAction_open(context); });
ma.setMenuLocation(MenuPath("File/Open"));
ma.setToolbarLocation(ToolbarLocation("main", "open"));
ma.setIcon(QIcon(":/icons/folder.png"));
registerStaticAction(ma);
}
{
auto ca = makeContextAction<QueryTool>(tr("Save SQL"), &QueryTool::save);
ca->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
// ca->setMenuLocation(MenuPath("File/Save"));
// ca->setToolbarLocation(ToolbarLocation("main", "save"));
// how we tell the system we want this to become a menu button with this as it's default action
registerContextAction(ca);
}
// {
// auto ca = makeContextAction<QueryTool>(tr("Save SQL as"), &QueryTool::saveAs);
// ca->setMenuLocation(MenuPath("File/Save"));
// ca->setToolbarLocation(ToolbarLocation("main", "save"));
// // how we tell the system we want this to become a secondary action for the previous button?
// registerContextAction(ca);
// }
// {
// auto ca = makeContextAction<QueryTool>(tr("Save copy of SQL as"), &QueryTool::saveCopyAs);
// ca->setMenuLocation(MenuPath("File/Save"));
// ca->setToolbarLocation(ToolbarLocation("main", "save"));
// // how we tell the system we want this to become a secondary action for the previous button?
// registerContextAction(ca);
// }
}
void QueryToolModule::staticAction_new(IPluginContentWidgetContext* context)
{
auto *ct = new QueryTool(context, this);
// Should we let constructor of PluginContentWidget do this?
// Saves a line but what if we don't want it.
context->addContentWidget(ct);
// should content widget now to which module it belongs?
// That way the context would not need to keep track of this.
ct->newdoc();
}
void QueryToolModule::staticAction_open(IPluginContentWidgetContext* context)
{
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
QString file_name = QFileDialog::getOpenFileName(context->container(),
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
if ( ! file_name.isEmpty()) {
auto *ct = new QueryTool(context, this);
context->addContentWidget(ct);
if (!ct->load(file_name)) {
// TODO load has failed remove widget or never add it?
}
}
}
REGISTER_PLUGIN_MODULE_CAT(QueryToolModule, "Query tool", "pglab.querytool", "database")