Improved support from removing rows in crud tabs.

It can handle now complex selections and reports back errors encountered
when removing the rows fails.
This commit is contained in:
eelke 2018-12-15 11:24:58 +01:00
parent 950fea873c
commit 62c6ad5bfb
10 changed files with 365 additions and 116 deletions

View file

@ -4,7 +4,12 @@
#include "MainWindow.h"
#include "ResultTableModelUtil.h"
#include "PgLabItemDelegate.h"
#include "IntegerRange.h"
#include <QDebug>
#include <QMenu>
#include <QMessageBox>
#include <iterator>
#include <set>
CrudTab::CrudTab(MainWindow *parent)
@ -22,7 +27,8 @@ CrudTab::CrudTab(MainWindow *parent)
m_crudModel = new CrudModel(parent);
ui->tableView->setModel(m_crudModel);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->addAction(ui->actionRemove_rows);
auto horizontal_header = ui->tableView->horizontalHeader();
@ -30,9 +36,6 @@ CrudTab::CrudTab(MainWindow *parent)
connect(horizontal_header, &QHeaderView::customContextMenuRequested,
this, &CrudTab::headerCustomContextMenu);
//auto selection_model = ui->tableView->selectionModel();
// connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
// &CrudTab::tableView_currentRowChanged);
}
CrudTab::~CrudTab()
@ -44,21 +47,9 @@ void CrudTab::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table)
{
m_db = db;
m_table = table;
// m_catalog = cat;
// m_tablesModel->setCatalog(cat);
// ui->tableListTable->resizeColumnsToContents();
// m_namespaceFilterWidget->init(cat->namespaces());
// auto highlighter = new SqlSyntaxHighlighter(ui->constraintSqlEdit->document());
// highlighter->setTypes(*cat->types());
m_crudModel->setConfig(db, table);
}
//void CrudTab::tableView_currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
//{
//}
void CrudTab::refresh()
{
m_crudModel->loadData();
@ -66,14 +57,37 @@ void CrudTab::refresh()
void CrudTab::on_actionRemove_rows_triggered()
{
// determine selection
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()));
// ui->tableView->currentIndex()
// selectionModel()->
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 += ", ";
//ui->tableView->selectionModel()->selectedRows()
m_crudModel->removeRow(ui->tableView->currentIndex().row());
//removeRows();
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);
}
}
}
std::vector<QAction*> CrudTab::getToolbarActions()
@ -83,6 +97,8 @@ std::vector<QAction*> CrudTab::getToolbarActions()
action->setShortcut(QKeySequence(Qt::Key_F5));
connect(action, &QAction::triggered, this, &CrudTab::refresh);
actions.push_back(action);
actions.push_back(ui->actionRemove_rows);
}
return actions;
}