Allow selecting the search result when searching for buffers
authorMartin T. H. Sandsmark <martin.sandsmark@kde.org>
Mon, 29 Oct 2018 11:52:17 +0000 (12:52 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Fri, 16 Nov 2018 19:45:36 +0000 (20:45 +0100)
If multiple buffers are displayed when filtering by name, allow
selecting one of the using the up/down keys. This makes it possible
to jump to a buffer other than the topmost.

src/uisupport/bufferview.cpp
src/uisupport/bufferview.h

index 8387f02..5ac201b 100644 (file)
@@ -624,6 +624,61 @@ QSize BufferView::sizeHint() const
 }
 
 
 }
 
 
+void BufferView::changeHighlight(const BufferView::Direction direction)
+{
+    // If for some weird reason we get a new delegate
+    BufferViewDelegate *delegate = qobject_cast<BufferViewDelegate*>(itemDelegate(m_currentHighlight));
+    if (delegate) {
+        delegate->currentHighlight = QModelIndex();
+    }
+
+    QModelIndex newIndex = m_currentHighlight;
+    if (!newIndex.isValid()) {
+        newIndex = model()->index(0, 0);
+    }
+
+    if (direction == Backward) {
+        newIndex = indexBelow(newIndex);
+    } else {
+        newIndex = indexAbove(newIndex);
+    }
+
+    if (!newIndex.isValid()) {
+        return;
+    }
+
+    m_currentHighlight = newIndex;
+
+    delegate = qobject_cast<BufferViewDelegate*>(itemDelegate(m_currentHighlight));
+    if (delegate) {
+        delegate->currentHighlight = m_currentHighlight;
+    }
+    viewport()->update();
+}
+
+void BufferView::selectHighlighted()
+{
+    if (m_currentHighlight.isValid()) {
+        selectionModel()->setCurrentIndex(m_currentHighlight, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
+        selectionModel()->select(m_currentHighlight, QItemSelectionModel::ClearAndSelect);
+    } else {
+        selectFirstBuffer();
+    }
+
+    clearHighlight();
+}
+
+void BufferView::clearHighlight()
+{
+    // If for some weird reason we get a new delegate
+    BufferViewDelegate *delegate = qobject_cast<BufferViewDelegate*>(itemDelegate(m_currentHighlight));
+    if (delegate) {
+        delegate->currentHighlight = QModelIndex();
+    }
+    m_currentHighlight = QModelIndex();
+    viewport()->update();
+}
+
 // ****************************************
 //  BufferViewDelgate
 // ****************************************
 // ****************************************
 //  BufferViewDelgate
 // ****************************************
@@ -756,12 +811,16 @@ void BufferViewDock::onFilterReturnPressed()
     }
 
     BufferView *view = bufferView();
     }
 
     BufferView *view = bufferView();
-    if (!view || _filterEdit->text().isEmpty()) {
+    if (!view) {
         return;
     }
 
         return;
     }
 
-    view->selectFirstBuffer();
-    _filterEdit->clear();
+    if (!_filterEdit->text().isEmpty()) {
+        view->selectHighlighted();
+        _filterEdit->clear();
+    } else {
+        view->clearHighlight();
+    }
 }
 
 void BufferViewDock::setActive(bool active)
 }
 
 void BufferViewDock::setActive(bool active)
@@ -788,18 +847,35 @@ bool BufferViewDock::eventFilter(QObject *object, QEvent *event)
        }
    } else if (event->type() == QEvent::KeyRelease) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        }
    } else if (event->type() == QEvent::KeyRelease) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
-       if (keyEvent->key() != Qt::Key_Escape) {
+
+       BufferView *view = bufferView();
+       if (!view) {
            return false;
        }
 
            return false;
        }
 
-       _filterEdit->clear();
+       switch (keyEvent->key()) {
+       case Qt::Key_Escape: {
+           _filterEdit->clear();
+
+           if (!_oldFocusItem) {
+               return false;
+           }
 
 
-       if (_oldFocusItem) {
            _oldFocusItem->setFocus();
            _oldFocusItem->setFocus();
-           _oldFocusItem = 0;
+           _oldFocusItem = nullptr;
+           return true;
+       }
+       case Qt::Key_Down:
+           view->changeHighlight(BufferView::Backward);
+           return true;
+       case Qt::Key_Up:
+           view->changeHighlight(BufferView::Forward);
+           return true;
+       default:
+           break;
        }
 
        }
 
-       return true;
+       return false;
    }
 
    return false;
    }
 
    return false;
@@ -853,3 +929,13 @@ void BufferViewDock::activateFilter()
 
     _filterEdit->setFocus();
 }
 
     _filterEdit->setFocus();
 }
+
+
+void BufferViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    QStyleOptionViewItem newOption = option;
+    if (index == currentHighlight) {
+        newOption.state |= QStyle::State_HasFocus;
+    }
+    QStyledItemDelegate::paint(painter, newOption, index);
+}
index 2bd1221..feb48a1 100644 (file)
@@ -69,6 +69,9 @@ public slots:
     void previousBuffer();
     void hideCurrentBuffer();
     void filterTextChanged(QString filterString);
     void previousBuffer();
     void hideCurrentBuffer();
     void filterTextChanged(QString filterString);
+    void changeHighlight(const Direction direction);
+    void selectHighlighted();
+    void clearHighlight();
 
 signals:
     void removeBuffer(const QModelIndex &);
 
 signals:
     void removeBuffer(const QModelIndex &);
@@ -132,6 +135,7 @@ private:
         WasActive = 0x02
     };
     QHash<NetworkId, short> _expandedState;
         WasActive = 0x02
     };
     QHash<NetworkId, short> _expandedState;
+    QModelIndex m_currentHighlight;
 };
 
 
 };
 
 
@@ -145,10 +149,13 @@ class BufferViewDelegate : public QStyledItemDelegate
 
 public:
     BufferViewDelegate(QObject *parent = 0);
 
 public:
     BufferViewDelegate(QObject *parent = 0);
-    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
+    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
+
+    QModelIndex currentHighlight;
+    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
 
 protected:
 
 protected:
-    virtual void customEvent(QEvent *event);
+    virtual void customEvent(QEvent *event) override;
 };
 
 
 };