Selected items can be deleted from model via menu action "Delete item(s)"
This commit is contained in:
@ -48,6 +48,11 @@ MainWindow::MainWindow(QWidget* parent)
|
|||||||
connect(this, &MainWindow::displayStatusMessage, this, &MainWindow::showStatusMessage);
|
connect(this, &MainWindow::displayStatusMessage, this, &MainWindow::showStatusMessage);
|
||||||
connect(this, &MainWindow::checkForUpdates, this,
|
connect(this, &MainWindow::checkForUpdates, this,
|
||||||
&MainWindow::on_actionCheck_for_update_triggered, Qt::QueuedConnection);
|
&MainWindow::on_actionCheck_for_update_triggered, Qt::QueuedConnection);
|
||||||
|
|
||||||
|
connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
|
||||||
|
&MainWindow::onSelectionChanged);
|
||||||
|
|
||||||
|
onSelectionChanged(QItemSelection(), QItemSelection());
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow() { delete ui; }
|
MainWindow::~MainWindow() { delete ui; }
|
||||||
@ -90,6 +95,26 @@ void MainWindow::closeEvent(QCloseEvent* event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::showStatusMessage(const QString text) {
|
||||||
|
qInfo() << text;
|
||||||
|
ui->statusbar->showMessage(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::onSelectionChanged(const QItemSelection& selected,
|
||||||
|
const QItemSelection& deselected) {
|
||||||
|
Q_UNUSED(selected);
|
||||||
|
Q_UNUSED(deselected);
|
||||||
|
|
||||||
|
QItemSelection localSelection = ui->tableView->selectionModel()->selection();
|
||||||
|
if (localSelection.empty()) {
|
||||||
|
// qDebug() << "Nothing selected. Disabling delete action";
|
||||||
|
m_deleteItemAct->setEnabled(false);
|
||||||
|
} else {
|
||||||
|
// qDebug() << "Something selected. Enabling delete action";
|
||||||
|
m_deleteItemAct->setEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::onAboutClicked() {
|
void MainWindow::onAboutClicked() {
|
||||||
const QString applicationName = APPLICATION_NAME;
|
const QString applicationName = APPLICATION_NAME;
|
||||||
const QString titlePrefix = tr("About ");
|
const QString titlePrefix = tr("About ");
|
||||||
@ -104,11 +129,6 @@ void MainWindow::onAboutClicked() {
|
|||||||
QMessageBox::about(this, titlePrefix + applicationName, aboutText);
|
QMessageBox::about(this, titlePrefix + applicationName, aboutText);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showStatusMessage(const QString text) {
|
|
||||||
qInfo() << text;
|
|
||||||
ui->statusbar->showMessage(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::on_actionCheck_for_update_triggered() {
|
void MainWindow::on_actionCheck_for_update_triggered() {
|
||||||
showStatusMessage("Checking for update...");
|
showStatusMessage("Checking for update...");
|
||||||
const bool updateAvailable = m_core->isApplicationUpdateAvailable();
|
const bool updateAvailable = m_core->isApplicationUpdateAvailable();
|
||||||
@ -133,6 +153,25 @@ void MainWindow::openNewItemDialog() {
|
|||||||
m_newItemDialog->show();
|
m_newItemDialog->show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::deleteSelectedtItems() {
|
||||||
|
showStatusMessage(tr("Invoked 'Edit|Delete Item'"));
|
||||||
|
QItemSelection localSelection = ui->tableView->selectionModel()->selection();
|
||||||
|
if (localSelection.empty()) {
|
||||||
|
qDebug() << "No items selected. Nothing to remove.";
|
||||||
|
} else {
|
||||||
|
for (QList<QItemSelectionRange>::reverse_iterator iter = localSelection.rbegin(),
|
||||||
|
rend = localSelection.rend();
|
||||||
|
iter != rend; ++iter) {
|
||||||
|
// qInfo() << "iter:" << *iter;
|
||||||
|
// const QModelIndex parentIndex = iter->parent();
|
||||||
|
const int topRow = iter->top();
|
||||||
|
const int bottomRow = iter->bottom();
|
||||||
|
const int nRows = bottomRow - topRow + 1;
|
||||||
|
m_tableModel->removeRows(topRow, nRows);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::createActions() {
|
void MainWindow::createActions() {
|
||||||
// TODO add generic menu actions (file/new, edit/cut, ...)
|
// TODO add generic menu actions (file/new, edit/cut, ...)
|
||||||
createFileActions();
|
createFileActions();
|
||||||
@ -227,7 +266,6 @@ void MainWindow::createEditActions() {
|
|||||||
m_openNewItemDialogAct->setShortcut(QKeySequence::New);
|
m_openNewItemDialogAct->setShortcut(QKeySequence::New);
|
||||||
m_openNewItemDialogAct->setStatusTip(tr("Opens a dialog to add a new item"));
|
m_openNewItemDialogAct->setStatusTip(tr("Opens a dialog to add a new item"));
|
||||||
connect(m_openNewItemDialogAct.get(), &QAction::triggered, this, &MainWindow::openNewItemDialog);
|
connect(m_openNewItemDialogAct.get(), &QAction::triggered, this, &MainWindow::openNewItemDialog);
|
||||||
// m_openNewItemDialogAct->setEnabled(false);
|
|
||||||
ui->menu_Edit->addAction(m_openNewItemDialogAct.get());
|
ui->menu_Edit->addAction(m_openNewItemDialogAct.get());
|
||||||
|
|
||||||
m_openEditItemDialogAct = make_unique<QAction>(tr("&Edit item"), this);
|
m_openEditItemDialogAct = make_unique<QAction>(tr("&Edit item"), this);
|
||||||
@ -237,11 +275,10 @@ void MainWindow::createEditActions() {
|
|||||||
m_openEditItemDialogAct->setEnabled(false);
|
m_openEditItemDialogAct->setEnabled(false);
|
||||||
ui->menu_Edit->addAction(m_openEditItemDialogAct.get());
|
ui->menu_Edit->addAction(m_openEditItemDialogAct.get());
|
||||||
|
|
||||||
m_deleteItemAct = make_unique<QAction>(tr("&Delete item"), this);
|
m_deleteItemAct = make_unique<QAction>(tr("&Delete item(s)"), this);
|
||||||
m_deleteItemAct->setShortcuts(QKeySequence::Delete);
|
m_deleteItemAct->setShortcuts(QKeySequence::Delete);
|
||||||
m_deleteItemAct->setStatusTip(tr("Delete currently selected items"));
|
m_deleteItemAct->setStatusTip(tr("Delete currently selected item(s)"));
|
||||||
// connect(m_deleteAct, &QAction::triggered, this, &MainWindow::deleteItem);
|
connect(m_deleteItemAct.get(), &QAction::triggered, this, &MainWindow::deleteSelectedtItems);
|
||||||
m_deleteItemAct->setEnabled(false);
|
|
||||||
ui->menu_Edit->addAction(m_deleteItemAct.get());
|
ui->menu_Edit->addAction(m_deleteItemAct.get());
|
||||||
|
|
||||||
ui->menu_Edit->addSeparator();
|
ui->menu_Edit->addSeparator();
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#ifndef MAINWINDOW_H
|
#ifndef MAINWINDOW_H
|
||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
|
#include <QItemSelection>
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
|
||||||
class NewItemDialog;
|
class NewItemDialog;
|
||||||
@ -32,14 +33,17 @@ class MainWindow : public QMainWindow {
|
|||||||
void closeEvent(QCloseEvent* event) override;
|
void closeEvent(QCloseEvent* event) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onAboutClicked();
|
|
||||||
void showStatusMessage(const QString text);
|
void showStatusMessage(const QString text);
|
||||||
|
void onSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
|
||||||
|
|
||||||
|
void onAboutClicked();
|
||||||
void on_actionCheck_for_update_triggered();
|
void on_actionCheck_for_update_triggered();
|
||||||
|
|
||||||
void on_pushButton_clicked();
|
void on_pushButton_clicked();
|
||||||
|
|
||||||
/// slots for menu actions
|
/// slots for menu actions
|
||||||
void openNewItemDialog();
|
void openNewItemDialog();
|
||||||
|
void deleteSelectedtItems();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow* ui;
|
Ui::MainWindow* ui;
|
||||||
|
|||||||
Reference in New Issue
Block a user