From 5c4459d5df51a99bc6ee2e7389e3a7aec3f81091 Mon Sep 17 00:00:00 2001 From: "Martin T. H. Sandsmark" Date: Mon, 29 Oct 2018 12:52:17 +0100 Subject: [PATCH] Allow selecting the search result when searching for buffers 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 | 102 ++++++++++++++++++++++++++++++++--- src/uisupport/bufferview.h | 11 +++- 2 files changed, 103 insertions(+), 10 deletions(-) diff --git a/src/uisupport/bufferview.cpp b/src/uisupport/bufferview.cpp index 8387f025..5ac201b9 100644 --- a/src/uisupport/bufferview.cpp +++ b/src/uisupport/bufferview.cpp @@ -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(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(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(itemDelegate(m_currentHighlight)); + if (delegate) { + delegate->currentHighlight = QModelIndex(); + } + m_currentHighlight = QModelIndex(); + viewport()->update(); +} + // **************************************** // BufferViewDelgate // **************************************** @@ -756,12 +811,16 @@ void BufferViewDock::onFilterReturnPressed() } BufferView *view = bufferView(); - if (!view || _filterEdit->text().isEmpty()) { + if (!view) { return; } - view->selectFirstBuffer(); - _filterEdit->clear(); + if (!_filterEdit->text().isEmpty()) { + view->selectHighlighted(); + _filterEdit->clear(); + } else { + view->clearHighlight(); + } } 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(event); - if (keyEvent->key() != Qt::Key_Escape) { + + BufferView *view = bufferView(); + if (!view) { return false; } - _filterEdit->clear(); + switch (keyEvent->key()) { + case Qt::Key_Escape: { + _filterEdit->clear(); + + if (!_oldFocusItem) { + return false; + } - if (_oldFocusItem) { _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; @@ -853,3 +929,13 @@ void BufferViewDock::activateFilter() _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); +} diff --git a/src/uisupport/bufferview.h b/src/uisupport/bufferview.h index 2bd12212..feb48a11 100644 --- a/src/uisupport/bufferview.h +++ b/src/uisupport/bufferview.h @@ -69,6 +69,9 @@ public slots: void previousBuffer(); void hideCurrentBuffer(); void filterTextChanged(QString filterString); + void changeHighlight(const Direction direction); + void selectHighlighted(); + void clearHighlight(); signals: void removeBuffer(const QModelIndex &); @@ -132,6 +135,7 @@ private: WasActive = 0x02 }; QHash _expandedState; + QModelIndex m_currentHighlight; }; @@ -145,10 +149,13 @@ class BufferViewDelegate : public QStyledItemDelegate 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: - virtual void customEvent(QEvent *event); + virtual void customEvent(QEvent *event) override; }; -- 2.20.1