1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
# (C) Copyright 2007 Riverbank Computing Limited
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import warnings
from pyface.qt import QtCore, QtGui
from traits.api import Bool, Event, provides, Str
from pyface.i_python_editor import IPythonEditor, MPythonEditor
from pyface.key_pressed_event import KeyPressedEvent
from pyface.ui.qt.layout_widget import LayoutWidget
from pyface.ui.qt.code_editor.code_widget import AdvancedCodeWidget
@provides(IPythonEditor)
class PythonEditor(MPythonEditor, LayoutWidget):
""" The toolkit specific implementation of a PythonEditor. See the
IPythonEditor interface for the API documentation.
"""
# 'IPythonEditor' interface --------------------------------------------
dirty = Bool(False)
path = Str()
show_line_numbers = Bool(True)
# Events ----
changed = Event()
key_pressed = Event(KeyPressedEvent)
# ------------------------------------------------------------------------
# 'object' interface.
# ------------------------------------------------------------------------
def __init__(self, parent=None, **traits):
create = traits.pop("create", None)
super().__init__(parent=parent, **traits)
if create:
self.create()
warnings.warn(
"automatic widget creation is deprecated and will be removed "
"in a future Pyface version, code should not pass the create "
"parameter and should instead call create() explicitly",
DeprecationWarning,
stacklevel=2,
)
elif create is not None:
warnings.warn(
"setting create=False is no longer required",
DeprecationWarning,
stacklevel=2,
)
# ------------------------------------------------------------------------
# 'PythonEditor' interface.
# ------------------------------------------------------------------------
def load(self, path=None):
""" Loads the contents of the editor.
"""
if path is None:
path = self.path
# We will have no path for a new script.
if len(path) > 0:
f = open(self.path, "r")
text = f.read()
f.close()
else:
text = ""
self.control.code.setPlainText(text)
self.dirty = False
def save(self, path=None):
""" Saves the contents of the editor.
"""
if path is None:
path = self.path
f = open(path, "w")
f.write(self.control.code.toPlainText())
f.close()
self.dirty = False
def select_line(self, lineno):
""" Selects the specified line.
"""
self.control.code.set_line_column(lineno, 0)
self.control.code.moveCursor(
QtGui.QTextCursor.MoveOperation.EndOfLine, QtGui.QTextCursor.MoveMode.KeepAnchor
)
# ------------------------------------------------------------------------
# 'Widget' interface.
# ------------------------------------------------------------------------
def _add_event_listeners(self):
super()._add_event_listeners()
self.control.code.installEventFilter(self._event_filter)
# Connect signals for text changes.
self.control.code.modificationChanged.connect(self._on_dirty_changed)
self.control.code.textChanged.connect(self._on_text_changed)
def _remove_event_listeners(self):
if self.control is not None:
# Disconnect signals for text changes.
self.control.code.modificationChanged.disconnect(
self._on_dirty_changed
)
self.control.code.textChanged.disconnect(self._on_text_changed)
# Disconnect signals from control and other dependent widgets
self.control._remove_event_listeners()
if self._event_filter is not None:
self.control.code.removeEventFilter(self._event_filter)
super()._remove_event_listeners()
def __event_filter_default(self):
return PythonEditorEventFilter(self, self.control)
# ------------------------------------------------------------------------
# Trait handlers.
# ------------------------------------------------------------------------
def _path_changed(self):
self._changed_path()
def _show_line_numbers_changed(self):
if self.control is not None:
self.control.code.line_number_widget.setVisible(
self.show_line_numbers
)
self.control.code.update_line_number_width()
# ------------------------------------------------------------------------
# Private interface.
# ------------------------------------------------------------------------
def _create_control(self, parent):
""" Creates the toolkit-specific control for the widget.
"""
self.control = control = AdvancedCodeWidget(parent)
self._show_line_numbers_changed()
# Load the editor's contents.
self.load()
return control
def _on_dirty_changed(self, dirty):
""" Called whenever a change is made to the dirty state of the
document.
"""
self.dirty = dirty
def _on_text_changed(self):
""" Called whenever a change is made to the text of the document.
"""
self.changed = True
class PythonEditorEventFilter(QtCore.QObject):
""" A thin wrapper around the advanced code widget to handle the key_pressed
Event.
"""
def __init__(self, editor, parent):
super().__init__(parent)
self.__editor = editor
def eventFilter(self, obj, event):
""" Reimplemented to trap key presses.
"""
if (
self.__editor.control
and obj == self.__editor.control
and event.type() == QtCore.QEvent.Type.FocusOut
):
# Hack for Traits UI compatibility.
self.__editor.control.lostFocus.emit()
elif (
self.__editor.control
and obj == self.__editor.control.code
and event.type() == QtCore.QEvent.Type.KeyPress
):
# Pyface doesn't seem to be Str aware. Only keep the key code
# if it corresponds to a single Latin1 character.
kstr = event.text()
try:
kcode = ord(str(kstr))
except:
kcode = 0
mods = event.modifiers()
self.key_pressed = KeyPressedEvent(
alt_down=(
(mods & QtCore.Qt.KeyboardModifier.AltModifier) == QtCore.Qt.KeyboardModifier.AltModifier
),
control_down=(
(mods & QtCore.Qt.KeyboardModifier.ControlModifier)
== QtCore.Qt.KeyboardModifier.ControlModifier
),
shift_down=(
(mods & QtCore.Qt.KeyboardModifier.ShiftModifier) == QtCore.Qt.KeyboardModifier.ShiftModifier
),
key_code=kcode,
event=event,
)
return super().eventFilter(obj, event)
|