pgLab/pglab/CrudTab.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

149 lines
4 KiB
C++

#include "CrudTab.h"
#include "ui_CrudTab.h"
#include "CrudModel.h"
#include "ResultTableModelUtil.h"
#include "PgLabItemDelegate.h"
#include "IntegerRange.h"
#include "OpenDatabase.h"
#include "catalog/PgClassContainer.h"
#include "catalog/PgDatabaseCatalog.h"
#include <QDebug>
#include <QMenu>
#include <QMessageBox>
#include <iterator>
#include <set>
#include "plugin_support/PluginRegister.h"
#include "plugin_support/IPluginContentWidgetContext.h"
CrudTab::CrudTab(IPluginContentWidgetContext *context, PluginModule *module, QWidget *parent)
: PluginContentWidget(context, module, parent)
, ui(new Ui::CrudTab)
{
ui->setupUi(this);
SetTableViewDefault(ui->tableView);
auto delegate = new PgLabItemDelegate(ui->tableView);
ui->tableView->setItemDelegate(delegate);
m_crudModel = new CrudModel(parent);
ui->tableView->setModel(m_crudModel);
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->addAction(ui->actionRemove_rows);
auto horizontal_header = ui->tableView->horizontalHeader();
horizontal_header->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(horizontal_header, &QHeaderView::customContextMenuRequested,
this, &CrudTab::headerCustomContextMenu);
}
CrudTab::~CrudTab()
{
delete ui;
}
void CrudTab::setConfig(Oid oid) //std::shared_ptr<OpenDatabase> db, const PgClass &table)
{
m_db = context()->getObject<OpenDatabase>(); // getDatabase();;
m_table = *m_db->catalog()->classes()->getByKey(oid);
m_crudModel->setConfig(m_db, *m_table);
}
void CrudTab::refresh()
{
m_crudModel->loadData();
}
void CrudTab::on_actionRemove_rows_triggered()
{
std::set<IntegerRange<int>> row_ranges;
auto selection = ui->tableView->selectionModel()->selection();
for (auto range : selection) {
row_ranges.emplace(range.top(), range.height());
}
std::set<IntegerRange<int>> merged_ranges;
merge_ranges(row_ranges.begin(), row_ranges.end(), std::inserter(merged_ranges, merged_ranges.begin()));
QString msg = tr("Are you certain you want to remove the following row(s)?");
msg += "\n";
bool first = true;
for (auto range : merged_ranges) {
if (first) first = false;
else msg += ", ";
auto s = range.start() + 1, e = range.end();
if (s == e)
msg += QString("%1").arg(s);
else
msg += QString("%1 through %2").arg(s).arg(e);
msg += " ";
}
auto res = QMessageBox::question(this, "pgLab", msg, QMessageBox::Yes, QMessageBox::No);
if (res == QMessageBox::Yes) {
auto [res, msg] = m_crudModel->removeRows(merged_ranges);
if (!res) {
QMessageBox::critical(this, "pgLab", msg, QMessageBox::Close);
}
}
}
void CrudTab::headerCustomContextMenu(const QPoint &pos)
{
auto menu = new QMenu(this);
QAction *action = new QAction(QIcon(":/icons/script_go.png"), tr("Refresh"), this);
action->setShortcut(QKeySequence(Qt::Key_F5));
connect(action, &QAction::triggered, this, &CrudTab::refresh);
menu->addAction(action);
auto horizontal_header = ui->tableView->horizontalHeader();
menu->popup(horizontal_header->mapToGlobal(pos));
}
void CrudTab::initActions()
{
{
auto ac = new QAction(QIcon(":/icons/script_go.png"), tr("Refresh"), this);
ac->setShortcut(QKeySequence(Qt::Key_F5));
connect(ac, &QAction::triggered, this, &CrudTab::refresh);
m_refreshAction = ac;
}
}
QList<QAction *> CrudTab::actions()
{
return { m_refreshAction };
}
void CrudPageModule::init()
{
registerModuleAction("open",
[this] (IPluginContentWidgetContext* context,
const ModuleActionParameters &params)
{
moduleAction_open(context, params);
});
}
void CrudPageModule::moduleAction_open(
IPluginContentWidgetContext* context,
const ModuleActionParameters &params
)
{
// create new widget for specified table
// hand widget to context for display
CrudTab *ct = new CrudTab(context, this);
context->addContentWidget(ct); // maybe CrudTab should do this
ct->setConfig(params.at("oid").toUInt());
}
REGISTER_PLUGIN_MODULE_CAT(CrudPageModule, "CRUD tool", "pglab.crudpage", "database")