From 09bb6a6933d60189c7631ac74c2a1bcf2d25add9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 25 Dec 2014 09:42:32 +0530 Subject: [PATCH] Mouse interaction for completion popup --- .../gui2/tweak_book/completion/popup.py | 29 +++++++++++++++++++ src/calibre/gui2/tweak_book/editor/text.py | 4 +++ 2 files changed, 33 insertions(+) diff --git a/src/calibre/gui2/tweak_book/completion/popup.py b/src/calibre/gui2/tweak_book/completion/popup.py index dd1a596750..e7d22c3d11 100644 --- a/src/calibre/gui2/tweak_book/completion/popup.py +++ b/src/calibre/gui2/tweak_book/completion/popup.py @@ -29,6 +29,8 @@ class CompletionPopup(QWidget): self.setFocusPolicy(Qt.NoFocus) self.setFocusProxy(parent) self.setVisible(False) + self.setMouseTracking(True) + self.setCursor(Qt.PointingHandCursor) self.matcher = None self.current_results = self.current_size_hint = None @@ -98,6 +100,11 @@ class CompletionPopup(QWidget): yield i + self.current_top_index, st, y, height y += height + def index_for_y(self, y): + for idx, st, top, height in self.iter_visible_items(): + if top <= y < top + height: + return idx + def paintEvent(self, ev): painter = QPainter(self) painter.setClipRect(ev.rect()) @@ -214,6 +221,9 @@ class CompletionPopup(QWidget): if key in (Qt.Key_Up, Qt.Key_Down): self.choose_next_result(previous=key == Qt.Key_Up) return True + if key in (Qt.Key_Enter, Qt.Key_Return) and self.current_index > -1: + self.index_activated(self.current_index) + return True return False def eventFilter(self, obj, ev): @@ -228,6 +238,25 @@ class CompletionPopup(QWidget): self.relayout_timer.start() return False + def mouseMoveEvent(self, ev): + y = ev.pos().y() + idx = self.index_for_y(y) + if idx is not None and idx != self.current_index: + self.current_index = idx + self.update() + ev.accept() + + def mouseReleaseEvent(self, ev): + y = ev.pos().y() + idx = self.index_for_y(y) + if idx is not None: + self.index_activated(idx) + ev.accept() + + def index_activated(self, idx): + print ('index activated', idx) + self.hide() + if __name__ == '__main__': from calibre.utils.matcher import Matcher def test(editor): diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index c9ff204ec3..f71971ea20 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -662,6 +662,10 @@ class TextEdit(PlainTextEdit): return self.text_for_range(c.block(), r) def mousePressEvent(self, ev): + if self.completion_popup.isVisible() and not self.completion_popup.rect().contains(ev.pos()): + # For some reason using eventFilter for this does not work, so we + # implement it here + self.completion_popup.hide() if ev.modifiers() & Qt.CTRL: url = self.link_for_position(ev.pos()) if url is not None: