##// END OF EJS Templates
Added preliminary editor support to IPythonWidget.
epatters -
Show More
@@ -45,7 +45,7 b' class ConsoleWidget(QtGui.QWidget):'
45 45
46 46 # Signal emitted when paging is needed and the paging style has been
47 47 # specified as 'custom'.
48 custom_page_requested = QtCore.pyqtSignal(QtCore.QString)
48 custom_page_requested = QtCore.pyqtSignal(object)
49 49
50 50 # Protected class variables.
51 51 _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left,
@@ -81,7 +81,7 b' class ConsoleWidget(QtGui.QWidget):'
81 81 'vsplit' : Similar to 'hsplit', except that a vertical splitter
82 82 used.
83 83 'custom' : No action is taken by the widget beyond emitting a
84 'custom_page_requested(QString)' signal.
84 'custom_page_requested(str)' signal.
85 85 'none' : The text is written directly to the console.
86 86
87 87 parent : QWidget, optional [default None]
@@ -1,3 +1,6 b''
1 # Standard library imports
2 from subprocess import Popen
3
1 4 # System library imports
2 5 from PyQt4 import QtCore, QtGui
3 6
@@ -11,6 +14,10 b' class IPythonWidget(FrontendWidget):'
11 14 """ A FrontendWidget for an IPython kernel.
12 15 """
13 16
17 # Signal emitted when an editor is needed for a file and the editor has been
18 # specified as 'custom'.
19 custom_edit_requested = QtCore.pyqtSignal(object)
20
14 21 # The default stylesheet: black text on a white background.
15 22 default_stylesheet = """
16 23 .error { color: red; }
@@ -43,13 +50,14 b' class IPythonWidget(FrontendWidget):'
43 50 super(IPythonWidget, self).__init__(*args, **kw)
44 51
45 52 # FrontendWidget protected variables.
46 self._input_splitter = IPythonInputSplitter(input_mode='replace')
53 #self._input_splitter = IPythonInputSplitter(input_mode='replace')
47 54
48 55 # IPythonWidget protected variables.
49 56 self._previous_prompt_blocks = []
50 57 self._prompt_count = 0
51 58
52 # Set a default stylesheet.
59 # Set a default editor and stylesheet.
60 self.set_editor('default')
53 61 self.reset_styling()
54 62
55 63 #---------------------------------------------------------------------------
@@ -128,12 +136,59 b' class IPythonWidget(FrontendWidget):'
128 136 # 'IPythonWidget' interface
129 137 #---------------------------------------------------------------------------
130 138
139 def edit(self, filename):
140 """ Opens a Python script for editing.
141
142 Parameters:
143 -----------
144 filename : str
145 A path to a local system file.
146
147 Raises:
148 -------
149 OSError
150 If the editor command cannot be executed.
151 """
152 if self._editor == 'default':
153 url = QtCore.QUrl.fromLocalFile(filename)
154 if not QtGui.QDesktopServices.openUrl(url):
155 message = 'Failed to open %s with the default application'
156 raise OSError(message % repr(filename))
157 elif self._editor is None:
158 self.custom_edit_requested.emit(filename)
159 else:
160 Popen(self._editor + [filename])
161
131 162 def reset_styling(self):
132 163 """ Restores the default IPythonWidget styling.
133 164 """
134 165 self.set_styling(self.default_stylesheet, syntax_style='default')
135 166 #self.set_styling(self.dark_stylesheet, syntax_style='monokai')
136 167
168 def set_editor(self, editor):
169 """ Sets the editor to use with the %edit magic.
170
171 Parameters:
172 -----------
173 editor : str or sequence of str
174 A command suitable for use with Popen. This command will be executed
175 with a single argument--a filename--when editing is requested.
176
177 This parameter also takes two special values:
178 'default' : Files will be edited with the system default
179 application for Python files.
180 'custom' : Emit a 'custom_edit_requested(str)' signal instead
181 of opening an editor.
182 """
183 if editor == 'default':
184 self._editor = 'default'
185 elif editor == 'custom':
186 self._editor = None
187 elif isinstance(editor, basestring):
188 self._editor = [ editor ]
189 else:
190 self._editor = list(editor)
191
137 192 def set_styling(self, stylesheet, syntax_style=None):
138 193 """ Sets the IPythonWidget styling.
139 194
General Comments 0
You need to be logged in to leave comments. Login now