Show More
@@ -1,135 +1,138 b'' | |||||
1 | # System library imports |
|
1 | # System library imports | |
2 | from IPython.external.qt import QtCore, QtGui |
|
2 | from IPython.external.qt 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 | # 'QObject' interface |
|
10 | # 'QObject' interface | |
11 | #-------------------------------------------------------------------------- |
|
11 | #-------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | def __init__(self, console_widget): |
|
13 | def __init__(self, console_widget): | |
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 |
text_edit = console_widget._ |
|
17 | text_edit = console_widget._control | |
18 | assert isinstance(text_edit, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) |
|
18 | assert isinstance(text_edit, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) | |
19 | super(CompletionWidget, self).__init__() |
|
19 | super(CompletionWidget, self).__init__() | |
20 |
|
20 | |||
21 | self._text_edit = text_edit |
|
21 | self._text_edit = text_edit | |
22 |
|
22 | |||
23 | self.setAttribute(QtCore.Qt.WA_StaticContents) |
|
23 | self.setAttribute(QtCore.Qt.WA_StaticContents) | |
24 | self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint) |
|
24 | self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint) | |
25 |
|
25 | |||
26 | # Ensure that the text edit keeps focus when widget is displayed. |
|
26 | # Ensure that the text edit keeps focus when widget is displayed. | |
27 | self.setFocusProxy(self._text_edit) |
|
27 | self.setFocusProxy(self._text_edit) | |
28 |
|
28 | |||
29 | self.setFrameShadow(QtGui.QFrame.Plain) |
|
29 | self.setFrameShadow(QtGui.QFrame.Plain) | |
30 | self.setFrameShape(QtGui.QFrame.StyledPanel) |
|
30 | self.setFrameShape(QtGui.QFrame.StyledPanel) | |
31 |
|
31 | |||
32 | self.itemActivated.connect(self._complete_current) |
|
32 | self.itemActivated.connect(self._complete_current) | |
33 |
|
33 | |||
34 | def eventFilter(self, obj, event): |
|
34 | def eventFilter(self, obj, event): | |
35 | """ Reimplemented to handle keyboard input and to auto-hide when the |
|
35 | """ Reimplemented to handle keyboard input and to auto-hide when the | |
36 | text edit loses focus. |
|
36 | text edit loses focus. | |
37 | """ |
|
37 | """ | |
38 | if obj == self._text_edit: |
|
38 | if obj == self._text_edit: | |
39 | etype = event.type() |
|
39 | etype = event.type() | |
40 |
|
40 | |||
41 | if etype == QtCore.QEvent.KeyPress: |
|
41 | if etype == QtCore.QEvent.KeyPress: | |
42 | key, text = event.key(), event.text() |
|
42 | key, text = event.key(), event.text() | |
43 | if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, |
|
43 | if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, | |
44 | QtCore.Qt.Key_Tab): |
|
44 | QtCore.Qt.Key_Tab): | |
45 | self._complete_current() |
|
45 | self._complete_current() | |
46 | return True |
|
46 | return True | |
47 | elif key == QtCore.Qt.Key_Escape: |
|
47 | elif key == QtCore.Qt.Key_Escape: | |
48 | self.hide() |
|
48 | self.hide() | |
49 | return True |
|
49 | return True | |
50 | elif key in (QtCore.Qt.Key_Up, QtCore.Qt.Key_Down, |
|
50 | elif key in (QtCore.Qt.Key_Up, QtCore.Qt.Key_Down, | |
51 | QtCore.Qt.Key_PageUp, QtCore.Qt.Key_PageDown, |
|
51 | QtCore.Qt.Key_PageUp, QtCore.Qt.Key_PageDown, | |
52 | QtCore.Qt.Key_Home, QtCore.Qt.Key_End): |
|
52 | QtCore.Qt.Key_Home, QtCore.Qt.Key_End): | |
53 | self.keyPressEvent(event) |
|
53 | self.keyPressEvent(event) | |
54 | return True |
|
54 | return True | |
55 |
|
55 | |||
56 | elif etype == QtCore.QEvent.FocusOut: |
|
56 | elif etype == QtCore.QEvent.FocusOut: | |
57 | self.hide() |
|
57 | self.hide() | |
58 |
|
58 | |||
59 | return super(CompletionWidget, self).eventFilter(obj, event) |
|
59 | return super(CompletionWidget, self).eventFilter(obj, event) | |
60 |
|
60 | |||
61 | #-------------------------------------------------------------------------- |
|
61 | #-------------------------------------------------------------------------- | |
62 | # 'QWidget' interface |
|
62 | # 'QWidget' interface | |
63 | #-------------------------------------------------------------------------- |
|
63 | #-------------------------------------------------------------------------- | |
64 |
|
64 | |||
65 | def hideEvent(self, event): |
|
65 | def hideEvent(self, event): | |
66 | """ Reimplemented to disconnect signal handlers and event filter. |
|
66 | """ Reimplemented to disconnect signal handlers and event filter. | |
67 | """ |
|
67 | """ | |
68 | super(CompletionWidget, self).hideEvent(event) |
|
68 | super(CompletionWidget, self).hideEvent(event) | |
69 | self._text_edit.cursorPositionChanged.disconnect(self._update_current) |
|
69 | self._text_edit.cursorPositionChanged.disconnect(self._update_current) | |
70 | self._text_edit.removeEventFilter(self) |
|
70 | self._text_edit.removeEventFilter(self) | |
71 |
|
71 | |||
72 | def showEvent(self, event): |
|
72 | def showEvent(self, event): | |
73 | """ Reimplemented to connect signal handlers and event filter. |
|
73 | """ Reimplemented to connect signal handlers and event filter. | |
74 | """ |
|
74 | """ | |
75 | super(CompletionWidget, self).showEvent(event) |
|
75 | super(CompletionWidget, self).showEvent(event) | |
76 | self._text_edit.cursorPositionChanged.connect(self._update_current) |
|
76 | self._text_edit.cursorPositionChanged.connect(self._update_current) | |
77 | self._text_edit.installEventFilter(self) |
|
77 | self._text_edit.installEventFilter(self) | |
78 |
|
78 | |||
79 | #-------------------------------------------------------------------------- |
|
79 | #-------------------------------------------------------------------------- | |
80 | # 'CompletionWidget' interface |
|
80 | # 'CompletionWidget' interface | |
81 | #-------------------------------------------------------------------------- |
|
81 | #-------------------------------------------------------------------------- | |
82 |
|
82 | |||
83 | def show_items(self, cursor, items): |
|
83 | def show_items(self, cursor, items): | |
84 | """ Shows the completion widget with 'items' at the position specified |
|
84 | """ Shows the completion widget with 'items' at the position specified | |
85 | by 'cursor'. |
|
85 | by 'cursor'. | |
86 | """ |
|
86 | """ | |
87 | text_edit = self._text_edit |
|
87 | text_edit = self._text_edit | |
88 | point = text_edit.cursorRect(cursor).bottomRight() |
|
88 | point = text_edit.cursorRect(cursor).bottomRight() | |
89 | point = text_edit.mapToGlobal(point) |
|
89 | point = text_edit.mapToGlobal(point) | |
90 | height = self.sizeHint().height() |
|
90 | height = self.sizeHint().height() | |
91 | screen_rect = QtGui.QApplication.desktop().availableGeometry(self) |
|
91 | screen_rect = QtGui.QApplication.desktop().availableGeometry(self) | |
92 | if screen_rect.size().height() - point.y() - height < 0: |
|
92 | if screen_rect.size().height() - point.y() - height < 0: | |
93 | point = text_edit.mapToGlobal(text_edit.cursorRect().topRight()) |
|
93 | point = text_edit.mapToGlobal(text_edit.cursorRect().topRight()) | |
94 | point.setY(point.y() - height) |
|
94 | point.setY(point.y() - height) | |
95 | self.move(point) |
|
95 | self.move(point) | |
96 |
|
96 | |||
97 | self._start_position = cursor.position() |
|
97 | self._start_position = cursor.position() | |
98 | self.clear() |
|
98 | self.clear() | |
99 | self.addItems(items) |
|
99 | self.addItems(items) | |
100 | self.setCurrentRow(0) |
|
100 | self.setCurrentRow(0) | |
101 | self.show() |
|
101 | self.show() | |
102 |
|
102 | |||
103 | #-------------------------------------------------------------------------- |
|
103 | #-------------------------------------------------------------------------- | |
104 | # Protected interface |
|
104 | # Protected interface | |
105 | #-------------------------------------------------------------------------- |
|
105 | #-------------------------------------------------------------------------- | |
106 |
|
106 | |||
107 | def _complete_current(self): |
|
107 | def _complete_current(self): | |
108 | """ Perform the completion with the currently selected item. |
|
108 | """ Perform the completion with the currently selected item. | |
109 | """ |
|
109 | """ | |
110 | self._current_text_cursor().insertText(self.currentItem().text()) |
|
110 | self._current_text_cursor().insertText(self.currentItem().text()) | |
111 | self.hide() |
|
111 | self.hide() | |
112 |
|
112 | |||
113 | def _current_text_cursor(self): |
|
113 | def _current_text_cursor(self): | |
114 | """ Returns a cursor with text between the start position and the |
|
114 | """ Returns a cursor with text between the start position and the | |
115 | current position selected. |
|
115 | current position selected. | |
116 | """ |
|
116 | """ | |
117 | cursor = self._text_edit.textCursor() |
|
117 | cursor = self._text_edit.textCursor() | |
118 | if cursor.position() >= self._start_position: |
|
118 | if cursor.position() >= self._start_position: | |
119 | cursor.setPosition(self._start_position, |
|
119 | cursor.setPosition(self._start_position, | |
120 | QtGui.QTextCursor.KeepAnchor) |
|
120 | QtGui.QTextCursor.KeepAnchor) | |
121 | return cursor |
|
121 | return cursor | |
122 |
|
122 | |||
123 | def _update_current(self): |
|
123 | def _update_current(self): | |
124 | """ Updates the current item based on the current text. |
|
124 | """ Updates the current item based on the current text. | |
125 | """ |
|
125 | """ | |
126 | prefix = self._current_text_cursor().selection().toPlainText() |
|
126 | prefix = self._current_text_cursor().selection().toPlainText() | |
127 | if prefix: |
|
127 | if prefix: | |
128 | items = self.findItems(prefix, (QtCore.Qt.MatchStartsWith | |
|
128 | items = self.findItems(prefix, (QtCore.Qt.MatchStartsWith | | |
129 | QtCore.Qt.MatchCaseSensitive)) |
|
129 | QtCore.Qt.MatchCaseSensitive)) | |
130 | if items: |
|
130 | if items: | |
131 | self.setCurrentItem(items[0]) |
|
131 | self.setCurrentItem(items[0]) | |
132 | else: |
|
132 | else: | |
133 | self.hide() |
|
133 | self.hide() | |
134 | else: |
|
134 | else: | |
135 | self.hide() |
|
135 | self.hide() | |
|
136 | ||||
|
137 | def _cancel_completion(self): | |||
|
138 | self.hide() |
General Comments 0
You need to be logged in to leave comments.
Login now