From: APTX Date: Tue, 11 Feb 2014 20:18:57 +0000 (+0100) Subject: Update filter bar arrow key selection. X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=b631fa7895a949769cbc8ee4ddd3621c32b3fac3;p=localmylist.git Update filter bar arrow key selection. Do not track the selected index independently of the selection model. This is the first commit using uniform initialization. This means a C++11 compiler at least as good as VS2013 is required to build LML. --- diff --git a/localmylist-management/tabs/mylisttab.cpp b/localmylist-management/tabs/mylisttab.cpp index 4165c89..6153d94 100644 --- a/localmylist-management/tabs/mylisttab.cpp +++ b/localmylist-management/tabs/mylisttab.cpp @@ -19,8 +19,7 @@ using namespace LocalMyList; MyListTab::MyListTab(QWidget *parent) : AbstractTabBase(parent), - ui(new Ui::MyListTab), - selectedRow(-1) + ui(new Ui::MyListTab) { ui->setupUi(this); m_label = tr("MyList"); @@ -68,8 +67,6 @@ void MyListTab::init() connect(ui->filterInput, SIGNAL(textChanged(QString)), this, SLOT(currentSelectionChanged())); myListFilterModel->configChanged(); - - selectedRow = -1; } void MyListTab::activate() @@ -80,7 +77,6 @@ void MyListTab::activate() void MyListTab::reload() { myListModel()->reload(); - selectedRow = -1; } void MyListTab::loadSettings(QSettings *settings) @@ -250,39 +246,54 @@ void MyListTab::on_filterType_currentIndexChanged(int) void MyListTab::on_filterInput_keyUpPressed() { - selectedRow = qMax(-1, selectedRow - 1); - updateSelection(); + const int rowCount{ui->myListView->model()->rowCount()}; + + if (!rowCount) + return; + + const QModelIndex currentIdx{ui->myListView->selectionModel()->currentIndex()}; + QModelIndex nextIdx{ui->myListView->model()->index(currentIdx.row() - 1, 0)}; + + if (!nextIdx.isValid()) + nextIdx = ui->myListView->model()->index(rowCount - 1, 0); + ui->myListView->selectionModel()-> + setCurrentIndex(nextIdx, QItemSelectionModel::ClearAndSelect + | QItemSelectionModel::Rows); } void MyListTab::on_filterInput_keyDownPressed() { - int newSelectedRow = qMin(myListModel()->rowCount() - 1, selectedRow + 1); - - if (selectedRow == newSelectedRow) + if (!ui->myListView->model()->rowCount()) return; - selectedRow = newSelectedRow; - updateSelection(); + const QModelIndex currentIdx{ui->myListView->selectionModel()->currentIndex()}; + QModelIndex nextIdx{ui->myListView->model()->index(currentIdx.row() + 1, 0)}; + + if (!nextIdx.isValid()) + nextIdx = ui->myListView->model()->index(0, 0); + + ui->myListView->selectionModel()-> + setCurrentIndex(nextIdx, QItemSelectionModel::ClearAndSelect + | QItemSelectionModel::Rows); } void MyListTab::on_filterInput_returnPressed() { - if (selectedRow < 0) + const QModelIndex idx{ui->myListView->selectionModel()->currentIndex()}; + + if (!idx.isValid()) return; - const QModelIndex idx = myListFilterModel->index(selectedRow, 0); on_myListView_openFileRequested(idx); } void MyListTab::currentSelectionChanged(const QModelIndex ¤t, const QModelIndex &) { - selectedRow = current.row(); } void MyListTab::currentSelectionChanged() { - selectedRow = -1; } MyListModel *MyListTab::myListModel() const @@ -292,14 +303,5 @@ MyListModel *MyListTab::myListModel() const void MyListTab::updateSelection() { - if (selectedRow < 0) - { - ui->myListView->selectionModel()->clear(); - return; - } - const QModelIndex idx = myListFilterModel->index(selectedRow, 0); - ui->myListView->selectionModel()-> - setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect - | QItemSelectionModel::Rows); } diff --git a/localmylist-management/tabs/mylisttab.h b/localmylist-management/tabs/mylisttab.h index 695b2c9..b50a471 100644 --- a/localmylist-management/tabs/mylisttab.h +++ b/localmylist-management/tabs/mylisttab.h @@ -47,9 +47,6 @@ private slots: void on_filterInput_keyDownPressed(); void on_filterInput_returnPressed(); - void currentSelectionChanged(const QModelIndex ¤t, const QModelIndex &previous); - void currentSelectionChanged(); - private: LocalMyList::MyListModel *myListModel() const; @@ -58,7 +55,6 @@ private: Ui::MyListTab *ui; MyListFilterModel *myListFilterModel; - int selectedRow; }; #endif // MYLISTTAB_H