Show More
@@ -1,121 +1,124 b'' | |||
|
1 | 1 | # System library imports |
|
2 | 2 | from PyQt4 import QtCore, QtGui |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | class CompletionWidget(QtGui.QListWidget): |
|
6 | 6 | """ A widget for GUI tab completion. |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | #-------------------------------------------------------------------------- |
|
10 | 10 | # 'QWidget' interface |
|
11 | 11 | #-------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | def __init__(self, parent): |
|
14 | 14 | """ Create a completion widget that is attached to the specified Qt |
|
15 | 15 | text edit widget. |
|
16 | 16 | """ |
|
17 | 17 | assert isinstance(parent, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) |
|
18 | 18 | QtGui.QListWidget.__init__(self, parent) |
|
19 | 19 | |
|
20 | 20 | self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint) |
|
21 | 21 | self.setAttribute(QtCore.Qt.WA_StaticContents) |
|
22 | 22 | |
|
23 | 23 | # Ensure that parent keeps focus when widget is displayed. |
|
24 | 24 | self.setFocusProxy(parent) |
|
25 | 25 | |
|
26 | 26 | self.setFrameShadow(QtGui.QFrame.Plain) |
|
27 | 27 | self.setFrameShape(QtGui.QFrame.StyledPanel) |
|
28 | 28 | |
|
29 | 29 | self.itemActivated.connect(self._complete_current) |
|
30 | 30 | |
|
31 | 31 | def hideEvent(self, event): |
|
32 | 32 | """ Reimplemented to disconnect the cursor movement handler. |
|
33 | 33 | """ |
|
34 | 34 | QtGui.QListWidget.hideEvent(self, event) |
|
35 | self.parent().cursorPositionChanged.disconnect(self._update_current) | |
|
35 | try: | |
|
36 | self.parent().cursorPositionChanged.disconnect(self._update_current) | |
|
37 | except TypeError: | |
|
38 | pass | |
|
36 | 39 | |
|
37 | 40 | def keyPressEvent(self, event): |
|
38 | 41 | """ Reimplemented to update the list. |
|
39 | 42 | """ |
|
40 | 43 | key, text = event.key(), event.text() |
|
41 | 44 | |
|
42 | 45 | if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, |
|
43 | 46 | QtCore.Qt.Key_Tab): |
|
44 | 47 | self._complete_current() |
|
45 | 48 | event.accept() |
|
46 | 49 | |
|
47 | 50 | elif key == QtCore.Qt.Key_Escape: |
|
48 | 51 | self.hide() |
|
49 | 52 | event.accept() |
|
50 | 53 | |
|
51 | 54 | elif key in (QtCore.Qt.Key_Up, QtCore.Qt.Key_Down, |
|
52 | 55 | QtCore.Qt.Key_PageUp, QtCore.Qt.Key_PageDown, |
|
53 | 56 | QtCore.Qt.Key_Home, QtCore.Qt.Key_End): |
|
54 | 57 | QtGui.QListWidget.keyPressEvent(self, event) |
|
55 | 58 | event.accept() |
|
56 | 59 | |
|
57 | 60 | else: |
|
58 | 61 | event.ignore() |
|
59 | 62 | |
|
60 | 63 | def showEvent(self, event): |
|
61 | 64 | """ Reimplemented to connect the cursor movement handler. |
|
62 | 65 | """ |
|
63 | 66 | QtGui.QListWidget.showEvent(self, event) |
|
64 | 67 | self.parent().cursorPositionChanged.connect(self._update_current) |
|
65 | 68 | |
|
66 | 69 | #-------------------------------------------------------------------------- |
|
67 | 70 | # 'CompletionWidget' interface |
|
68 | 71 | #-------------------------------------------------------------------------- |
|
69 | 72 | |
|
70 | 73 | def show_items(self, cursor, items): |
|
71 | 74 | """ Shows the completion widget with 'items' at the position specified |
|
72 | 75 | by 'cursor'. |
|
73 | 76 | """ |
|
74 | 77 | text_edit = self.parent() |
|
75 | 78 | point = text_edit.cursorRect(cursor).bottomRight() |
|
76 | 79 | point = text_edit.mapToGlobal(point) |
|
77 | 80 | screen_rect = QtGui.QApplication.desktop().availableGeometry(self) |
|
78 | 81 | if screen_rect.size().height() - point.y() - self.height() < 0: |
|
79 | 82 | point = text_edit.mapToGlobal(text_edit.cursorRect().topRight()) |
|
80 | 83 | point.setY(point.y() - self.height()) |
|
81 | 84 | self.move(point) |
|
82 | 85 | |
|
83 | 86 | self._start_position = cursor.position() |
|
84 | 87 | self.clear() |
|
85 | 88 | self.addItems(items) |
|
86 | 89 | self.setCurrentRow(0) |
|
87 | 90 | self.show() |
|
88 | 91 | |
|
89 | 92 | #-------------------------------------------------------------------------- |
|
90 | 93 | # Protected interface |
|
91 | 94 | #-------------------------------------------------------------------------- |
|
92 | 95 | |
|
93 | 96 | def _complete_current(self): |
|
94 | 97 | """ Perform the completion with the currently selected item. |
|
95 | 98 | """ |
|
96 | 99 | self._current_text_cursor().insertText(self.currentItem().text()) |
|
97 | 100 | self.hide() |
|
98 | 101 | |
|
99 | 102 | def _current_text_cursor(self): |
|
100 | 103 | """ Returns a cursor with text between the start position and the |
|
101 | 104 | current position selected. |
|
102 | 105 | """ |
|
103 | 106 | cursor = self.parent().textCursor() |
|
104 | 107 | if cursor.position() >= self._start_position: |
|
105 | 108 | cursor.setPosition(self._start_position, |
|
106 | 109 | QtGui.QTextCursor.KeepAnchor) |
|
107 | 110 | return cursor |
|
108 | 111 | |
|
109 | 112 | def _update_current(self): |
|
110 | 113 | """ Updates the current item based on the current text. |
|
111 | 114 | """ |
|
112 | 115 | prefix = self._current_text_cursor().selectedText() |
|
113 | 116 | if prefix: |
|
114 | 117 | items = self.findItems(prefix, (QtCore.Qt.MatchStartsWith | |
|
115 | 118 | QtCore.Qt.MatchCaseSensitive)) |
|
116 | 119 | if items: |
|
117 | 120 | self.setCurrentItem(items[0]) |
|
118 | 121 | else: |
|
119 | 122 | self.hide() |
|
120 | 123 | else: |
|
121 | 124 | self.hide() |
General Comments 0
You need to be logged in to leave comments.
Login now