From 36c8fffee27990d7a630be66ae3d2c4648096363 Mon Sep 17 00:00:00 2001 From: kornes <28986062+kornes@users.noreply.github.com> Date: Tue, 6 Dec 2022 22:52:58 +0000 Subject: [PATCH] feat: Automatically select newly added table rows (#4216) Co-authored-by: pajlada --- CHANGELOG.md | 1 + src/widgets/helper/EditableModelView.cpp | 18 ++++++++---------- src/widgets/helper/EditableModelView.hpp | 3 --- .../settingspages/KeyboardSettingsPage.cpp | 12 ------------ 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 587d397bb..72c9c49f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Minor: Added ability to negate search options by prefixing it with an exclamation mark (e.g. `!badge:mod` to search for messages where the author does not have the moderator badge). (#4207) - Minor: Search window input will automatically use currently selected text if present. (#4178) - Minor: Cleared up highlight sound settings (#4194) +- Minor: Tables in settings window will now scroll to newly added rows. (#4216) - Minor: Added link to streamlink docs for easier user setup. (#4217) - Bugfix: Fixed highlight sounds not reloading on change properly. (#4194) - Bugfix: Fixed CTRL + C not working in reply thread popups. (#4209) diff --git a/src/widgets/helper/EditableModelView.cpp b/src/widgets/helper/EditableModelView.cpp index 1a81852d0..649f85775 100644 --- a/src/widgets/helper/EditableModelView.cpp +++ b/src/widgets/helper/EditableModelView.cpp @@ -82,7 +82,13 @@ EditableModelView::EditableModelView(QAbstractTableModel *model, bool movable) QObject::connect(this->model_, &QAbstractTableModel::rowsMoved, this, [this](const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row) { - this->selectRow(row); + this->tableView_->selectRow(row); + }); + + // select freshly added row + QObject::connect(this->model_, &QAbstractTableModel::rowsInserted, this, + [this](const QModelIndex &parent, int first, int last) { + this->tableView_->selectRow(last); }); // add tableview @@ -149,15 +155,7 @@ void EditableModelView::moveRow(int dir) model_->moveRows(model_->index(row, 0), row, selected.size(), model_->index(row + dir, 0), row + dir); - this->selectRow(row + dir); -} - -void EditableModelView::selectRow(int row) -{ - this->getTableView()->selectionModel()->clear(); - this->getTableView()->selectionModel()->select( - this->model_->index(row, 0), - QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); + this->tableView_->selectRow(row + dir); } } // namespace chatterino diff --git a/src/widgets/helper/EditableModelView.hpp b/src/widgets/helper/EditableModelView.hpp index 64c6783f1..37bff144a 100644 --- a/src/widgets/helper/EditableModelView.hpp +++ b/src/widgets/helper/EditableModelView.hpp @@ -31,9 +31,6 @@ private: QHBoxLayout *buttons_{}; void moveRow(int dir); - -public: - void selectRow(int row); }; } // namespace chatterino diff --git a/src/widgets/settingspages/KeyboardSettingsPage.cpp b/src/widgets/settingspages/KeyboardSettingsPage.cpp index ac16bf7ec..89932fcd5 100644 --- a/src/widgets/settingspages/KeyboardSettingsPage.cpp +++ b/src/widgets/settingspages/KeyboardSettingsPage.cpp @@ -40,13 +40,6 @@ KeyboardSettingsPage::KeyboardSettingsPage() auto newHotkey = dialog.data(); int vectorIndex = getApp()->hotkeys->hotkeys_.append(newHotkey); getApp()->hotkeys->save(); - - // Select and scroll to newly added hotkey - auto modelRow = model->getModelIndexFromVectorIndex(vectorIndex); - auto modelIndex = model->index(modelRow, 0); - view->selectRow(modelRow); - view->getTableView()->scrollTo(modelIndex, - QAbstractItemView::PositionAtCenter); } }); @@ -89,11 +82,6 @@ void KeyboardSettingsPage::tableCellClicked(const QModelIndex &clicked, auto vectorIndex = getApp()->hotkeys->replaceHotkey(hotkey->name(), newHotkey); getApp()->hotkeys->save(); - - // Select the replaced hotkey - auto modelRow = model->getModelIndexFromVectorIndex(vectorIndex); - auto modelIndex = model->index(modelRow, 0); - view->selectRow(modelRow); } }