##// END OF EJS Templates
add confirm_exit option to qtconsole to suppress exit dialog
MinRK -
Show More
@@ -1,372 +1,402 b''
1 """ A minimal application using the Qt console-style IPython frontend.
1 """ A minimal application using the Qt console-style IPython frontend.
2 """
2 """
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Imports
5 # Imports
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7
7
8 # stdlib imports
8 # stdlib imports
9 import os
9 import os
10 import signal
10 import signal
11 import sys
11 import sys
12
12
13 # System library imports
13 # System library imports
14 from IPython.external.qt import QtGui
14 from IPython.external.qt import QtGui
15 from pygments.styles import get_all_styles
15 from pygments.styles import get_all_styles
16
16
17 # Local imports
17 # Local imports
18 from IPython.config.application import boolean_flag
18 from IPython.core.newapplication import ProfileDir, BaseIPythonApplication
19 from IPython.core.newapplication import ProfileDir, BaseIPythonApplication
19 from IPython.frontend.qt.console.frontend_widget import FrontendWidget
20 from IPython.frontend.qt.console.frontend_widget import FrontendWidget
20 from IPython.frontend.qt.console.ipython_widget import IPythonWidget
21 from IPython.frontend.qt.console.ipython_widget import IPythonWidget
21 from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
22 from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
22 from IPython.frontend.qt.console import styles
23 from IPython.frontend.qt.console import styles
23 from IPython.frontend.qt.kernelmanager import QtKernelManager
24 from IPython.frontend.qt.kernelmanager import QtKernelManager
24 from IPython.utils.traitlets import (
25 from IPython.utils.traitlets import (
25 Dict, List, Unicode, Int, CaselessStrEnum, Bool, Any
26 Dict, List, Unicode, Int, CaselessStrEnum, CBool, Any
26 )
27 )
27 from IPython.zmq.ipkernel import (
28 from IPython.zmq.ipkernel import (
28 flags as ipkernel_flags,
29 flags as ipkernel_flags,
29 aliases as ipkernel_aliases,
30 aliases as ipkernel_aliases,
30 IPKernelApp
31 IPKernelApp
31 )
32 )
32 from IPython.zmq.zmqshell import ZMQInteractiveShell
33 from IPython.zmq.zmqshell import ZMQInteractiveShell
33
34
34
35
35 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
36 # Network Constants
37 # Network Constants
37 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
38
39
39 from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS
40 from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS
40
41
41 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
42 # Classes
43 # Classes
43 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
44
45
45 class MainWindow(QtGui.QMainWindow):
46 class MainWindow(QtGui.QMainWindow):
46
47
47 #---------------------------------------------------------------------------
48 #---------------------------------------------------------------------------
48 # 'object' interface
49 # 'object' interface
49 #---------------------------------------------------------------------------
50 #---------------------------------------------------------------------------
50
51
51 def __init__(self, app, frontend, existing=False, may_close=True):
52 def __init__(self, app, frontend, existing=False, may_close=True,
53 confirm_exit=True):
52 """ Create a MainWindow for the specified FrontendWidget.
54 """ Create a MainWindow for the specified FrontendWidget.
53
55
54 The app is passed as an argument to allow for different
56 The app is passed as an argument to allow for different
55 closing behavior depending on whether we are the Kernel's parent.
57 closing behavior depending on whether we are the Kernel's parent.
56
58
57 If existing is True, then this Console does not own the Kernel.
59 If existing is True, then this Console does not own the Kernel.
58
60
59 If may_close is True, then this Console is permitted to close the kernel
61 If may_close is True, then this Console is permitted to close the kernel
60 """
62 """
61 super(MainWindow, self).__init__()
63 super(MainWindow, self).__init__()
62 self._app = app
64 self._app = app
63 self._frontend = frontend
65 self._frontend = frontend
64 self._existing = existing
66 self._existing = existing
65 if existing:
67 if existing:
66 self._may_close = may_close
68 self._may_close = may_close
67 else:
69 else:
68 self._may_close = True
70 self._may_close = True
69 self._frontend.exit_requested.connect(self.close)
71 self._frontend.exit_requested.connect(self.close)
72 self._confirm_exit = confirm_exit
70 self.setCentralWidget(frontend)
73 self.setCentralWidget(frontend)
71
74
72 #---------------------------------------------------------------------------
75 #---------------------------------------------------------------------------
73 # QWidget interface
76 # QWidget interface
74 #---------------------------------------------------------------------------
77 #---------------------------------------------------------------------------
75
78
76 def closeEvent(self, event):
79 def closeEvent(self, event):
77 """ Close the window and the kernel (if necessary).
80 """ Close the window and the kernel (if necessary).
78
81
79 This will prompt the user if they are finished with the kernel, and if
82 This will prompt the user if they are finished with the kernel, and if
80 so, closes the kernel cleanly. Alternatively, if the exit magic is used,
83 so, closes the kernel cleanly. Alternatively, if the exit magic is used,
81 it closes without prompt.
84 it closes without prompt.
82 """
85 """
83 keepkernel = None #Use the prompt by default
86 keepkernel = None #Use the prompt by default
84 if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic
87 if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic
85 keepkernel = self._frontend._keep_kernel_on_exit
88 keepkernel = self._frontend._keep_kernel_on_exit
86
89
87 kernel_manager = self._frontend.kernel_manager
90 kernel_manager = self._frontend.kernel_manager
88
91
92 if keepkernel is None and not self._confirm_exit:
93 # don't prompt, just terminate the kernel if we own it
94 # or leave it alone if we don't
95 keepkernel = not self._existing
96
89 if keepkernel is None: #show prompt
97 if keepkernel is None: #show prompt
90 if kernel_manager and kernel_manager.channels_running:
98 if kernel_manager and kernel_manager.channels_running:
91 title = self.window().windowTitle()
99 title = self.window().windowTitle()
92 cancel = QtGui.QMessageBox.Cancel
100 cancel = QtGui.QMessageBox.Cancel
93 okay = QtGui.QMessageBox.Ok
101 okay = QtGui.QMessageBox.Ok
94 if self._may_close:
102 if self._may_close:
95 msg = "You are closing this Console window."
103 msg = "You are closing this Console window."
96 info = "Would you like to quit the Kernel and all attached Consoles as well?"
104 info = "Would you like to quit the Kernel and all attached Consoles as well?"
97 justthis = QtGui.QPushButton("&No, just this Console", self)
105 justthis = QtGui.QPushButton("&No, just this Console", self)
98 justthis.setShortcut('N')
106 justthis.setShortcut('N')
99 closeall = QtGui.QPushButton("&Yes, quit everything", self)
107 closeall = QtGui.QPushButton("&Yes, quit everything", self)
100 closeall.setShortcut('Y')
108 closeall.setShortcut('Y')
101 box = QtGui.QMessageBox(QtGui.QMessageBox.Question,
109 box = QtGui.QMessageBox(QtGui.QMessageBox.Question,
102 title, msg)
110 title, msg)
103 box.setInformativeText(info)
111 box.setInformativeText(info)
104 box.addButton(cancel)
112 box.addButton(cancel)
105 box.addButton(justthis, QtGui.QMessageBox.NoRole)
113 box.addButton(justthis, QtGui.QMessageBox.NoRole)
106 box.addButton(closeall, QtGui.QMessageBox.YesRole)
114 box.addButton(closeall, QtGui.QMessageBox.YesRole)
107 box.setDefaultButton(closeall)
115 box.setDefaultButton(closeall)
108 box.setEscapeButton(cancel)
116 box.setEscapeButton(cancel)
109 reply = box.exec_()
117 reply = box.exec_()
110 if reply == 1: # close All
118 if reply == 1: # close All
111 kernel_manager.shutdown_kernel()
119 kernel_manager.shutdown_kernel()
112 #kernel_manager.stop_channels()
120 #kernel_manager.stop_channels()
113 event.accept()
121 event.accept()
114 elif reply == 0: # close Console
122 elif reply == 0: # close Console
115 if not self._existing:
123 if not self._existing:
116 # Have kernel: don't quit, just close the window
124 # Have kernel: don't quit, just close the window
117 self._app.setQuitOnLastWindowClosed(False)
125 self._app.setQuitOnLastWindowClosed(False)
118 self.deleteLater()
126 self.deleteLater()
119 event.accept()
127 event.accept()
120 else:
128 else:
121 event.ignore()
129 event.ignore()
122 else:
130 else:
123 reply = QtGui.QMessageBox.question(self, title,
131 reply = QtGui.QMessageBox.question(self, title,
124 "Are you sure you want to close this Console?"+
132 "Are you sure you want to close this Console?"+
125 "\nThe Kernel and other Consoles will remain active.",
133 "\nThe Kernel and other Consoles will remain active.",
126 okay|cancel,
134 okay|cancel,
127 defaultButton=okay
135 defaultButton=okay
128 )
136 )
129 if reply == okay:
137 if reply == okay:
130 event.accept()
138 event.accept()
131 else:
139 else:
132 event.ignore()
140 event.ignore()
133 elif keepkernel: #close console but leave kernel running (no prompt)
141 elif keepkernel: #close console but leave kernel running (no prompt)
134 if kernel_manager and kernel_manager.channels_running:
142 if kernel_manager and kernel_manager.channels_running:
135 if not self._existing:
143 if not self._existing:
136 # I have the kernel: don't quit, just close the window
144 # I have the kernel: don't quit, just close the window
137 self._app.setQuitOnLastWindowClosed(False)
145 self._app.setQuitOnLastWindowClosed(False)
138 event.accept()
146 event.accept()
139 else: #close console and kernel (no prompt)
147 else: #close console and kernel (no prompt)
140 if kernel_manager and kernel_manager.channels_running:
148 if kernel_manager and kernel_manager.channels_running:
141 kernel_manager.shutdown_kernel()
149 kernel_manager.shutdown_kernel()
142 event.accept()
150 event.accept()
143
151
144 #-----------------------------------------------------------------------------
152 #-----------------------------------------------------------------------------
145 # Aliases and Flags
153 # Aliases and Flags
146 #-----------------------------------------------------------------------------
154 #-----------------------------------------------------------------------------
147
155
148 flags = dict(ipkernel_flags)
156 flags = dict(ipkernel_flags)
149
157
150 flags.update({
158 flags.update({
151 'existing' : ({'IPythonQtConsoleApp' : {'existing' : True}},
159 'existing' : ({'IPythonQtConsoleApp' : {'existing' : True}},
152 "Connect to an existing kernel."),
160 "Connect to an existing kernel."),
153 'pure' : ({'IPythonQtConsoleApp' : {'pure' : True}},
161 'pure' : ({'IPythonQtConsoleApp' : {'pure' : True}},
154 "Use a pure Python kernel instead of an IPython kernel."),
162 "Use a pure Python kernel instead of an IPython kernel."),
155 'plain' : ({'ConsoleWidget' : {'kind' : 'plain'}},
163 'plain' : ({'ConsoleWidget' : {'kind' : 'plain'}},
156 "Disable rich text support."),
164 "Disable rich text support."),
157 'gui-completion' : ({'FrontendWidget' : {'gui_completion' : True}},
158 "use a GUI widget for tab completion"),
159 })
165 })
160
166 flags.update(boolean_flag(
161 qt_flags = ['existing', 'pure', 'plain', 'gui-completion']
167 'gui-completion', 'ConsoleWidget.gui_completion',
168 "use a GUI widget for tab completion",
169 "use plaintext output for completion"
170 ))
171 flags.update(boolean_flag(
172 'confirm-exit', 'IPythonQtConsoleApp.confirm_exit',
173 """Set to display confirmation dialog on exit. You can always use 'exit' or 'quit',
174 to force a direct exit without any confirmation.
175 """,
176 """Don't prompt the user when exiting. This will terminate the kernel
177 if it is owned by the frontend, and leave it alive if it is external.
178 """
179 ))
180 # the flags that are specific to the frontend
181 # these must be scrubbed before being passed to the kernel,
182 # or it will raise an error on unrecognized flags
183 qt_flags = ['existing', 'pure', 'plain', 'gui-completion', 'no-gui-completion',
184 'confirm-exit', 'no-confirm-exit']
162
185
163 aliases = dict(ipkernel_aliases)
186 aliases = dict(ipkernel_aliases)
164
187
165 aliases.update(dict(
188 aliases.update(dict(
166 hb = 'IPythonQtConsoleApp.hb_port',
189 hb = 'IPythonQtConsoleApp.hb_port',
167 shell = 'IPythonQtConsoleApp.shell_port',
190 shell = 'IPythonQtConsoleApp.shell_port',
168 iopub = 'IPythonQtConsoleApp.iopub_port',
191 iopub = 'IPythonQtConsoleApp.iopub_port',
169 stdin = 'IPythonQtConsoleApp.stdin_port',
192 stdin = 'IPythonQtConsoleApp.stdin_port',
170 ip = 'IPythonQtConsoleApp.ip',
193 ip = 'IPythonQtConsoleApp.ip',
171
194
172 plain = 'IPythonQtConsoleApp.plain',
195 plain = 'IPythonQtConsoleApp.plain',
173 pure = 'IPythonQtConsoleApp.pure',
196 pure = 'IPythonQtConsoleApp.pure',
174 gui_completion = 'FrontendWidget.gui_completion',
197 gui_completion = 'ConsoleWidget.gui_completion',
175 style = 'IPythonWidget.syntax_style',
198 style = 'IPythonWidget.syntax_style',
176 stylesheet = 'IPythonQtConsoleApp.stylesheet',
199 stylesheet = 'IPythonQtConsoleApp.stylesheet',
177 colors = 'ZMQInteractiveShell.colors',
200 colors = 'ZMQInteractiveShell.colors',
178
201
179 editor = 'IPythonWidget.editor',
202 editor = 'IPythonWidget.editor',
180 pi = 'IPythonWidget.in_prompt',
203 pi = 'IPythonWidget.in_prompt',
181 po = 'IPythonWidget.out_prompt',
204 po = 'IPythonWidget.out_prompt',
182 si = 'IPythonWidget.input_sep',
205 si = 'IPythonWidget.input_sep',
183 so = 'IPythonWidget.output_sep',
206 so = 'IPythonWidget.output_sep',
184 so2 = 'IPythonWidget.output_sep2',
207 so2 = 'IPythonWidget.output_sep2',
185 ))
208 ))
186
209
187 #-----------------------------------------------------------------------------
210 #-----------------------------------------------------------------------------
188 # IPythonQtConsole
211 # IPythonQtConsole
189 #-----------------------------------------------------------------------------
212 #-----------------------------------------------------------------------------
190
213
191 class IPythonQtConsoleApp(BaseIPythonApplication):
214 class IPythonQtConsoleApp(BaseIPythonApplication):
192 name = 'ipython-qtconsole'
215 name = 'ipython-qtconsole'
193 default_config_file_name='ipython_config.py'
216 default_config_file_name='ipython_config.py'
194 classes = [IPKernelApp, IPythonWidget, ZMQInteractiveShell, ProfileDir]
217 classes = [IPKernelApp, IPythonWidget, ZMQInteractiveShell, ProfileDir]
195 flags = Dict(flags)
218 flags = Dict(flags)
196 aliases = Dict(aliases)
219 aliases = Dict(aliases)
197
220
198 kernel_argv = List(Unicode)
221 kernel_argv = List(Unicode)
199
222
200 # connection info:
223 # connection info:
201 ip = Unicode(LOCALHOST, config=True,
224 ip = Unicode(LOCALHOST, config=True,
202 help="""Set the kernel\'s IP address [default localhost].
225 help="""Set the kernel\'s IP address [default localhost].
203 If the IP address is something other than localhost, then
226 If the IP address is something other than localhost, then
204 Consoles on other machines will be able to connect
227 Consoles on other machines will be able to connect
205 to the Kernel, so be careful!"""
228 to the Kernel, so be careful!"""
206 )
229 )
207 hb_port = Int(0, config=True,
230 hb_port = Int(0, config=True,
208 help="set the heartbeat port [default: random]")
231 help="set the heartbeat port [default: random]")
209 shell_port = Int(0, config=True,
232 shell_port = Int(0, config=True,
210 help="set the shell (XREP) port [default: random]")
233 help="set the shell (XREP) port [default: random]")
211 iopub_port = Int(0, config=True,
234 iopub_port = Int(0, config=True,
212 help="set the iopub (PUB) port [default: random]")
235 help="set the iopub (PUB) port [default: random]")
213 stdin_port = Int(0, config=True,
236 stdin_port = Int(0, config=True,
214 help="set the stdin (XREQ) port [default: random]")
237 help="set the stdin (XREQ) port [default: random]")
215
238
216 existing = Bool(False, config=True,
239 existing = CBool(False, config=True,
217 help="Whether to connect to an already running Kernel.")
240 help="Whether to connect to an already running Kernel.")
218
241
219 stylesheet = Unicode('', config=True,
242 stylesheet = Unicode('', config=True,
220 help="path to a custom CSS stylesheet")
243 help="path to a custom CSS stylesheet")
221
244
222 pure = Bool(False, config=True,
245 pure = CBool(False, config=True,
223 help="Use a pure Python kernel instead of an IPython kernel.")
246 help="Use a pure Python kernel instead of an IPython kernel.")
224 plain = Bool(False, config=True,
247 plain = CBool(False, config=True,
225 help="Use a plaintext widget instead of rich text (plain can't print/save).")
248 help="Use a plaintext widget instead of rich text (plain can't print/save).")
226
249
227 def _pure_changed(self, name, old, new):
250 def _pure_changed(self, name, old, new):
228 kind = 'plain' if self.plain else 'rich'
251 kind = 'plain' if self.plain else 'rich'
229 self.config.ConsoleWidget.kind = kind
252 self.config.ConsoleWidget.kind = kind
230 if self.pure:
253 if self.pure:
231 self.widget_factory = FrontendWidget
254 self.widget_factory = FrontendWidget
232 elif self.plain:
255 elif self.plain:
233 self.widget_factory = IPythonWidget
256 self.widget_factory = IPythonWidget
234 else:
257 else:
235 self.widget_factory = RichIPythonWidget
258 self.widget_factory = RichIPythonWidget
236
259
237 _plain_changed = _pure_changed
260 _plain_changed = _pure_changed
238
261
262 confirm_exit = CBool(True, config=True,
263 help="""
264 Set to display confirmation dialog on exit. You can always use 'exit' or 'quit',
265 to force a direct exit without any confirmation.""",
266 )
267
239 # the factory for creating a widget
268 # the factory for creating a widget
240 widget_factory = Any(RichIPythonWidget)
269 widget_factory = Any(RichIPythonWidget)
241
270
242 def parse_command_line(self, argv=None):
271 def parse_command_line(self, argv=None):
243 super(IPythonQtConsoleApp, self).parse_command_line(argv)
272 super(IPythonQtConsoleApp, self).parse_command_line(argv)
244 if argv is None:
273 if argv is None:
245 argv = sys.argv[1:]
274 argv = sys.argv[1:]
246
275
247 self.kernel_argv = list(argv) # copy
276 self.kernel_argv = list(argv) # copy
248
277
249 # scrub frontend-specific flags
278 # scrub frontend-specific flags
250 for a in argv:
279 for a in argv:
251 if a.startswith('--') and a[2:] in qt_flags:
280 if a.startswith('--') and a[2:] in qt_flags:
252 self.kernel_argv.remove(a)
281 self.kernel_argv.remove(a)
253
282
254 def init_kernel_manager(self):
283 def init_kernel_manager(self):
255 # Don't let Qt or ZMQ swallow KeyboardInterupts.
284 # Don't let Qt or ZMQ swallow KeyboardInterupts.
256 signal.signal(signal.SIGINT, signal.SIG_DFL)
285 signal.signal(signal.SIGINT, signal.SIG_DFL)
257
286
258 # Create a KernelManager and start a kernel.
287 # Create a KernelManager and start a kernel.
259 self.kernel_manager = QtKernelManager(
288 self.kernel_manager = QtKernelManager(
260 shell_address=(self.ip, self.shell_port),
289 shell_address=(self.ip, self.shell_port),
261 sub_address=(self.ip, self.iopub_port),
290 sub_address=(self.ip, self.iopub_port),
262 stdin_address=(self.ip, self.stdin_port),
291 stdin_address=(self.ip, self.stdin_port),
263 hb_address=(self.ip, self.hb_port)
292 hb_address=(self.ip, self.hb_port)
264 )
293 )
265 # start the kernel
294 # start the kernel
266 if not self.existing:
295 if not self.existing:
267 kwargs = dict(ip=self.ip, ipython=not self.pure)
296 kwargs = dict(ip=self.ip, ipython=not self.pure)
268 kwargs['extra_arguments'] = self.kernel_argv
297 kwargs['extra_arguments'] = self.kernel_argv
269 self.kernel_manager.start_kernel(**kwargs)
298 self.kernel_manager.start_kernel(**kwargs)
270 self.kernel_manager.start_channels()
299 self.kernel_manager.start_channels()
271
300
272
301
273 def init_qt_elements(self):
302 def init_qt_elements(self):
274 # Create the widget.
303 # Create the widget.
275 self.app = QtGui.QApplication([])
304 self.app = QtGui.QApplication([])
276 local_kernel = (not self.existing) or self.ip in LOCAL_IPS
305 local_kernel = (not self.existing) or self.ip in LOCAL_IPS
277 self.widget = self.widget_factory(config=self.config,
306 self.widget = self.widget_factory(config=self.config,
278 local_kernel=local_kernel)
307 local_kernel=local_kernel)
279 self.widget.kernel_manager = self.kernel_manager
308 self.widget.kernel_manager = self.kernel_manager
280 self.window = MainWindow(self.app, self.widget, self.existing,
309 self.window = MainWindow(self.app, self.widget, self.existing,
281 may_close=local_kernel)
310 may_close=local_kernel,
311 confirm_exit=self.confirm_exit)
282 self.window.setWindowTitle('Python' if self.pure else 'IPython')
312 self.window.setWindowTitle('Python' if self.pure else 'IPython')
283
313
284 def init_colors(self):
314 def init_colors(self):
285 """Configure the coloring of the widget"""
315 """Configure the coloring of the widget"""
286 # Note: This will be dramatically simplified when colors
316 # Note: This will be dramatically simplified when colors
287 # are removed from the backend.
317 # are removed from the backend.
288
318
289 if self.pure:
319 if self.pure:
290 # only IPythonWidget supports styling
320 # only IPythonWidget supports styling
291 return
321 return
292
322
293 # parse the colors arg down to current known labels
323 # parse the colors arg down to current known labels
294 try:
324 try:
295 colors = self.config.ZMQInteractiveShell.colors
325 colors = self.config.ZMQInteractiveShell.colors
296 except AttributeError:
326 except AttributeError:
297 colors = None
327 colors = None
298 try:
328 try:
299 style = self.config.IPythonWidget.colors
329 style = self.config.IPythonWidget.colors
300 except AttributeError:
330 except AttributeError:
301 style = None
331 style = None
302
332
303 # find the value for colors:
333 # find the value for colors:
304 if colors:
334 if colors:
305 colors=colors.lower()
335 colors=colors.lower()
306 if colors in ('lightbg', 'light'):
336 if colors in ('lightbg', 'light'):
307 colors='lightbg'
337 colors='lightbg'
308 elif colors in ('dark', 'linux'):
338 elif colors in ('dark', 'linux'):
309 colors='linux'
339 colors='linux'
310 else:
340 else:
311 colors='nocolor'
341 colors='nocolor'
312 elif style:
342 elif style:
313 if style=='bw':
343 if style=='bw':
314 colors='nocolor'
344 colors='nocolor'
315 elif styles.dark_style(style):
345 elif styles.dark_style(style):
316 colors='linux'
346 colors='linux'
317 else:
347 else:
318 colors='lightbg'
348 colors='lightbg'
319 else:
349 else:
320 colors=None
350 colors=None
321
351
322 # Configure the style.
352 # Configure the style.
323 widget = self.widget
353 widget = self.widget
324 if style:
354 if style:
325 widget.style_sheet = styles.sheet_from_template(style, colors)
355 widget.style_sheet = styles.sheet_from_template(style, colors)
326 widget.syntax_style = style
356 widget.syntax_style = style
327 widget._syntax_style_changed()
357 widget._syntax_style_changed()
328 widget._style_sheet_changed()
358 widget._style_sheet_changed()
329 elif colors:
359 elif colors:
330 # use a default style
360 # use a default style
331 widget.set_default_style(colors=colors)
361 widget.set_default_style(colors=colors)
332 else:
362 else:
333 # this is redundant for now, but allows the widget's
363 # this is redundant for now, but allows the widget's
334 # defaults to change
364 # defaults to change
335 widget.set_default_style()
365 widget.set_default_style()
336
366
337 if self.stylesheet:
367 if self.stylesheet:
338 # we got an expicit stylesheet
368 # we got an expicit stylesheet
339 if os.path.isfile(self.stylesheet):
369 if os.path.isfile(self.stylesheet):
340 with open(self.stylesheet) as f:
370 with open(self.stylesheet) as f:
341 sheet = f.read()
371 sheet = f.read()
342 widget.style_sheet = sheet
372 widget.style_sheet = sheet
343 widget._style_sheet_changed()
373 widget._style_sheet_changed()
344 else:
374 else:
345 raise IOError("Stylesheet %r not found."%self.stylesheet)
375 raise IOError("Stylesheet %r not found."%self.stylesheet)
346
376
347 def initialize(self, argv=None):
377 def initialize(self, argv=None):
348 super(IPythonQtConsoleApp, self).initialize(argv)
378 super(IPythonQtConsoleApp, self).initialize(argv)
349 self.init_kernel_manager()
379 self.init_kernel_manager()
350 self.init_qt_elements()
380 self.init_qt_elements()
351 self.init_colors()
381 self.init_colors()
352
382
353 def start(self):
383 def start(self):
354
384
355 # draw the window
385 # draw the window
356 self.window.show()
386 self.window.show()
357
387
358 # Start the application main loop.
388 # Start the application main loop.
359 self.app.exec_()
389 self.app.exec_()
360
390
361 #-----------------------------------------------------------------------------
391 #-----------------------------------------------------------------------------
362 # Main entry point
392 # Main entry point
363 #-----------------------------------------------------------------------------
393 #-----------------------------------------------------------------------------
364
394
365 def main():
395 def main():
366 app = IPythonQtConsoleApp()
396 app = IPythonQtConsoleApp()
367 app.initialize()
397 app.initialize()
368 app.start()
398 app.start()
369
399
370
400
371 if __name__ == '__main__':
401 if __name__ == '__main__':
372 main()
402 main()
General Comments 0
You need to be logged in to leave comments. Login now