2018-01-09 20:39:43 +01:00
|
|
|
|
#include "CrudTab.h"
|
2018-01-08 20:45:52 +01:00
|
|
|
|
#include "ui_CrudTab.h"
|
2018-01-09 20:39:43 +01:00
|
|
|
|
#include "CrudModel.h"
|
|
|
|
|
|
#include "ResultTableModelUtil.h"
|
2018-01-15 13:30:30 +01:00
|
|
|
|
#include "PgLabItemDelegate.h"
|
2018-12-15 11:24:58 +01:00
|
|
|
|
#include "IntegerRange.h"
|
2018-12-31 15:20:55 +01:00
|
|
|
|
#include "OpenDatabase.h"
|
|
|
|
|
|
#include "catalog/PgClassContainer.h"
|
|
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
2018-12-15 11:24:58 +01:00
|
|
|
|
#include <QDebug>
|
2018-11-18 19:30:45 +01:00
|
|
|
|
#include <QMenu>
|
2018-12-15 11:24:58 +01:00
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
#include <iterator>
|
|
|
|
|
|
#include <set>
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
|
2019-08-16 10:49:38 +02:00
|
|
|
|
CrudTab::CrudTab(IDatabaseWindow *context, QWidget *parent)
|
2019-08-15 18:00:19 +02:00
|
|
|
|
: QWidget(parent)
|
2018-01-09 20:39:43 +01:00
|
|
|
|
, ui(new Ui::CrudTab)
|
2022-01-22 16:22:29 +01:00
|
|
|
|
, m_context(context)
|
2018-01-08 20:45:52 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
2018-01-09 20:39:43 +01:00
|
|
|
|
|
2019-08-16 10:49:38 +02:00
|
|
|
|
m_db = context->openDatabase();
|
2019-08-15 18:00:19 +02:00
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
SetTableViewDefault(ui->tableView);
|
2018-01-15 13:30:30 +01:00
|
|
|
|
|
|
|
|
|
|
auto delegate = new PgLabItemDelegate(ui->tableView);
|
|
|
|
|
|
ui->tableView->setItemDelegate(delegate);
|
|
|
|
|
|
|
2021-09-17 18:55:10 +02:00
|
|
|
|
m_crudModel = new CrudModel(nullptr);
|
2021-07-02 20:04:34 +02:00
|
|
|
|
|
|
|
|
|
|
m_SortFilterProxy = new QSortFilterProxyModel(this);
|
|
|
|
|
|
m_SortFilterProxy->setSourceModel(m_crudModel);
|
|
|
|
|
|
ui->tableView->setModel(m_SortFilterProxy);
|
|
|
|
|
|
ui->tableView->setSortingEnabled(true);
|
2018-01-09 20:39:43 +01:00
|
|
|
|
|
2018-12-15 11:24:58 +01:00
|
|
|
|
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
|
|
|
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
2018-04-08 09:02:22 +02:00
|
|
|
|
ui->tableView->addAction(ui->actionRemove_rows);
|
2018-11-18 19:30:45 +01:00
|
|
|
|
|
|
|
|
|
|
auto horizontal_header = ui->tableView->horizontalHeader();
|
|
|
|
|
|
horizontal_header->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
|
|
|
|
|
|
connect(horizontal_header, &QHeaderView::customContextMenuRequested,
|
|
|
|
|
|
this, &CrudTab::headerCustomContextMenu);
|
|
|
|
|
|
|
2018-01-08 20:45:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CrudTab::~CrudTab()
|
|
|
|
|
|
{
|
2021-09-17 18:55:10 +02:00
|
|
|
|
delete m_crudModel;
|
2018-01-08 20:45:52 +01:00
|
|
|
|
delete ui;
|
|
|
|
|
|
}
|
2018-01-09 20:39:43 +01:00
|
|
|
|
|
2018-12-31 15:20:55 +01:00
|
|
|
|
void CrudTab::setConfig(Oid oid) //std::shared_ptr<OpenDatabase> db, const PgClass &table)
|
2018-01-09 20:39:43 +01:00
|
|
|
|
{
|
2018-12-31 15:20:55 +01:00
|
|
|
|
m_table = *m_db->catalog()->classes()->getByKey(oid);
|
|
|
|
|
|
m_crudModel->setConfig(m_db, *m_table);
|
2019-08-16 10:49:38 +02:00
|
|
|
|
m_context->setTitleForWidget(this, m_table->objectName(), "");
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
2018-02-05 21:42:54 +01:00
|
|
|
|
|
2018-02-05 22:40:17 +01:00
|
|
|
|
void CrudTab::refresh()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_crudModel->loadData();
|
|
|
|
|
|
}
|
2018-04-08 09:02:22 +02:00
|
|
|
|
|
|
|
|
|
|
void CrudTab::on_actionRemove_rows_triggered()
|
|
|
|
|
|
{
|
2018-12-15 11:24:58 +01:00
|
|
|
|
std::set<IntegerRange<int>> row_ranges;
|
2021-07-02 20:04:34 +02:00
|
|
|
|
auto selection = m_SortFilterProxy->mapSelectionToSource(ui->tableView->selectionModel()->selection());
|
2018-12-15 11:24:58 +01:00
|
|
|
|
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);
|
2018-04-08 09:02:22 +02:00
|
|
|
|
|
2018-12-15 11:24:58 +01:00
|
|
|
|
if (res == QMessageBox::Yes) {
|
|
|
|
|
|
auto [res, msg] = m_crudModel->removeRows(merged_ranges);
|
|
|
|
|
|
if (!res) {
|
|
|
|
|
|
QMessageBox::critical(this, "pgLab", msg, QMessageBox::Close);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
2018-05-20 13:42:30 +02:00
|
|
|
|
|
2018-11-18 19:30:45 +01:00
|
|
|
|
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));
|
|
|
|
|
|
}
|