]> Some of my projects - localmylist.git/commitdiff
Update filter bar arrow key selection.
authorAPTX <marek321@gmail.com>
Tue, 11 Feb 2014 20:18:57 +0000 (21:18 +0100)
committerAPTX <marek321@gmail.com>
Tue, 11 Feb 2014 20:18:57 +0000 (21:18 +0100)
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.

localmylist-management/tabs/mylisttab.cpp
localmylist-management/tabs/mylisttab.h

index 4165c899d0b4d03fd3c4e2f54e64d8b19404c069..6153d94b88120b462975c164b7417166b2ff0f19 100644 (file)
@@ -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 &current, 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);
 }
index 695b2c94da0eafe368cf0df353a2cefd24e16aef..b50a47162d9903476b72133379ef27071fca2ee0 100644 (file)
@@ -47,9 +47,6 @@ private slots:
        void on_filterInput_keyDownPressed();
        void on_filterInput_returnPressed();
 
-       void currentSelectionChanged(const QModelIndex &current, 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