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