Debian Codegraph @

src/librssguard/gui/reusable/edittableview.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/reusable/edittableview.h"
4
5 #include <QKeyEvent>
6
7 EditTableView::EditTableView(QWidget* parent) : QTableView(parent) {}
8
9 void EditTableView::keyPressEvent(QKeyEvent* event) {
10 if (model() != nullptr && event->key() == Qt::Key::Key_Delete) {
11 removeSelected();
12 event->accept();
13 }
14 else {
15 QAbstractItemView::keyPressEvent(event);
16 }
17 }
18
19 void EditTableView::removeSelected() {
20 if (model() != nullptr || selectionModel() != nullptr || !selectionModel()->hasSelection()) {
21 return;
22 }
23
24 const QModelIndexList selected_rows = selectionModel()->selectedRows();
25
26 if (selected_rows.isEmpty()) {
27 return;
28 }
29
30 const int new_selected_row = selected_rows.at(0).row();
31
32 for (int i = selected_rows.count() - 1; i >= 0; i--) {
33 QModelIndex idx = selected_rows.at(i);
34
35 model()->removeRow(idx.row(), rootIndex());
36 }
37
38 QModelIndex new_selected_index = model()->index(new_selected_row, 0, rootIndex());
39
40 if (!new_selected_index.isValid()) {
41 new_selected_index = model()->index(new_selected_row - 1, 0, rootIndex());
42 }
43
44 selectionModel()->select(new_selected_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
45 setCurrentIndex(new_selected_index);
46 }
47
48 void EditTableView::removeAll() {
49 if (model() != nullptr) {
50 model()->removeRows(0, model()->rowCount(rootIndex()), rootIndex());
51 }
52 }