mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Mouse interaction for completion popup
This commit is contained in:
parent
4c62ddb8d1
commit
09bb6a6933
@ -29,6 +29,8 @@ class CompletionPopup(QWidget):
|
|||||||
self.setFocusPolicy(Qt.NoFocus)
|
self.setFocusPolicy(Qt.NoFocus)
|
||||||
self.setFocusProxy(parent)
|
self.setFocusProxy(parent)
|
||||||
self.setVisible(False)
|
self.setVisible(False)
|
||||||
|
self.setMouseTracking(True)
|
||||||
|
self.setCursor(Qt.PointingHandCursor)
|
||||||
|
|
||||||
self.matcher = None
|
self.matcher = None
|
||||||
self.current_results = self.current_size_hint = 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
|
yield i + self.current_top_index, st, y, height
|
||||||
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):
|
def paintEvent(self, ev):
|
||||||
painter = QPainter(self)
|
painter = QPainter(self)
|
||||||
painter.setClipRect(ev.rect())
|
painter.setClipRect(ev.rect())
|
||||||
@ -214,6 +221,9 @@ class CompletionPopup(QWidget):
|
|||||||
if key in (Qt.Key_Up, Qt.Key_Down):
|
if key in (Qt.Key_Up, Qt.Key_Down):
|
||||||
self.choose_next_result(previous=key == Qt.Key_Up)
|
self.choose_next_result(previous=key == Qt.Key_Up)
|
||||||
return True
|
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
|
return False
|
||||||
|
|
||||||
def eventFilter(self, obj, ev):
|
def eventFilter(self, obj, ev):
|
||||||
@ -228,6 +238,25 @@ class CompletionPopup(QWidget):
|
|||||||
self.relayout_timer.start()
|
self.relayout_timer.start()
|
||||||
return False
|
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__':
|
if __name__ == '__main__':
|
||||||
from calibre.utils.matcher import Matcher
|
from calibre.utils.matcher import Matcher
|
||||||
def test(editor):
|
def test(editor):
|
||||||
|
@ -662,6 +662,10 @@ class TextEdit(PlainTextEdit):
|
|||||||
return self.text_for_range(c.block(), r)
|
return self.text_for_range(c.block(), r)
|
||||||
|
|
||||||
def mousePressEvent(self, ev):
|
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:
|
if ev.modifiers() & Qt.CTRL:
|
||||||
url = self.link_for_position(ev.pos())
|
url = self.link_for_position(ev.pos())
|
||||||
if url is not None:
|
if url is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user