##// END OF EJS Templates
trying to move closing logic on a tab basis
Matthias BUSSONNIER -
Show More
@@ -631,6 +631,9 b' class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):'
631 631 self.paste(text=text)
632 632 self.execute()
633 633
634 def exit_magic(self):
635 self.pasteMagic("%exit")
636
634 637 def who_magic(self):
635 638 self.pasteMagic("%who")
636 639
@@ -87,21 +87,29 b' class MainWindow(QtGui.QMainWindow):'
87 87
88 88 If may_close is True, then this Console is permitted to close the kernel
89 89 """
90 # ** current atributes, for window:
91 # _existing, (kernel) passed by constructor.
92 # _may_close (the kernel) passed by constructor
93 # _confirm_exit
94 #
95 # ** for the frontend
96 # _keep_kernel_on_exit (may be set by %exit)
97
90 98 super(MainWindow, self).__init__()
91 99 self._app = app
92 self._frontend = frontend
93 self._existing = existing
94 if existing:
95 self._may_close = may_close
96 else:
97 self._may_close = True
98 self._frontend.exit_requested.connect(self.close)
99 self._confirm_exit = confirm_exit
100 #self._frontend = frontend
101 #self._existing = existing
102 #if existing:
103 # self._may_close = may_close
104 #else:
105 # self._may_close = True
106 #self._frontend.exit_requested.connect(self.close)
107 #self._confirm_exit = confirm_exit
100 108
101 109 self.tabWidget = QtGui.QTabWidget(self)
102 110 self.tabWidget.setDocumentMode(True)
103 111 self.tabWidget.setTabsClosable(True)
104 self.tabWidget.addTab(frontend,"QtConsole1")
112 #self.tabWidget.addTab(frontend,"QtConsole1")
105 113 self.tabWidget.tabCloseRequested[int].connect(self.closetab)
106 114
107 115 self.setCentralWidget(self.tabWidget)
@@ -120,27 +128,99 b' class MainWindow(QtGui.QMainWindow):'
120 128 else:
121 129 self.tabWidget.tabBar().setVisible(True)
122 130
131 @property
123 132 def activeFrontend(self):
124 133 return self.tabWidget.currentWidget()
125 134
126 def closetab(self,tab):
135 def closetab(self,tab,force=None):
127 136 """ Called when a user try to close a tab
128 137
129 138 It takes the number of the tab to be closed as argument, (does not for
130 139 now, but should) take care of whether or not shuting down the kernel
131 140 attached to the frontend
132 141 """
133 print "closing tab",tab
134 try:
135 if self.tabWidget.widget(tab)._local_kernel:
136 kernel_manager = self.tabWidget.widget(tab).kernel_manager.shutdown_kernel()
137 else:
138 print "not owning the kernel"
139 except:
140 print "can't ask the kernel to shutdown"
142 print "trying to closing tab",tab
143 closing_widget=self.tabWidget.widget(tab)
144 keepkernel = None #Use the prompt by default
145 if hasattr(closing_widget,'_keep_kernel_on_exit'): #set by exit magic
146 keepkernel = closing_widget._keep_kernel_on_exit
147
148 kernel_manager = closing_widget.kernel_manager
149
150 if keepkernel is None and not closing_widget._confirm_exit:
151 # don't prompt, just terminate the kernel if we own it
152 # or leave it alone if we don't
153 keepkernel = not closing_widget._existing
154
155 if keepkernel is None: #show prompt
156 if kernel_manager and kernel_manager.channels_running:
157 title = self.window().windowTitle()
158 cancel = QtGui.QMessageBox.Cancel
159 okay = QtGui.QMessageBox.Ok
160 if closing_widget._may_close:
161 msg = "You are closing this Console window."
162 info = "Would you like to quit the Kernel and all attached Consoles as well?"
163 justthis = QtGui.QPushButton("&No, just this Console", self)
164 justthis.setShortcut('N')
165 closeall = QtGui.QPushButton("&Yes, quit everything", self)
166 closeall.setShortcut('Y')
167 box = QtGui.QMessageBox(QtGui.QMessageBox.Question,
168 title, msg)
169 box.setInformativeText(info)
170 box.addButton(cancel)
171 box.addButton(justthis, QtGui.QMessageBox.NoRole)
172 box.addButton(closeall, QtGui.QMessageBox.YesRole)
173 box.setDefaultButton(closeall)
174 box.setEscapeButton(cancel)
175 pixmap = QtGui.QPixmap(':/icon/IPythonConsole.png')
176 scaledpixmap = pixmap.scaledToWidth(64,mode=QtCore.Qt.SmoothTransformation)
177 box.setIconPixmap(scaledpixmap)
178 reply = box.exec_()
179 if reply == 1: # close All
180 kernel_manager.shutdown_kernel()
181 closing_widget.pasteMagic("exit")
182 self.tabWidget.removeTab(tab)
183 #kernel_manager.stop_channels()
184 #event.accept()
185 elif reply == 0: # close Console
186 if not closing_widget._existing:
187 # Have kernel: don't quit, just close the window
188 self._app.setQuitOnLastWindowClosed(False)
189 self.deleteLater()
190 #event.accept()
191 #else:
192 #event.ignore()
193 else:
194 reply = QtGui.QMessageBox.question(self, title,
195 "Are you sure you want to close this Console?"+
196 "\nThe Kernel and other Consoles will remain active.",
197 okay|cancel,
198 defaultButton=okay
199 )
200 if reply == okay:
201 self.tabWidget.removeTab(tab)
202 else:
203 event.ignore()
204 elif keepkernel: #close console but leave kernel running (no prompt)
205 if kernel_manager and kernel_manager.channels_running:
206 if not currentWidget._existing:
207 # I have the kernel: don't quit, just close the window
208 self._app.setQuitOnLastWindowClosed(False)
209 #event.accept()
210 else: #close console and kernel (no prompt)
211 if kernel_manager and kernel_manager.channels_running:
212 kernel_manager.shutdown_kernel()
213 #event.accept()
214 #try:
215 # if closing_widget._local_kernel and not keepkernel:
216 # kernel_manager = self.tabWidget.widget(tab).kernel_manager.shutdown_kernel()
217 # else:
218 # print "not owning the kernel/asked not to shut it down"
219 #except:
220 # print "can't ask the kernel to shutdown"
141 221 #if self.tabWidget.count() == 1:
142 222 #self.close()
143 self.tabWidget.removeTab(tab)
223 #self.tabWidget.removeTab(tab)
144 224 self.updateTabBarVisibility()
145 225
146 226 def addTabWithFrontend(self,frontend,name=None):
@@ -324,40 +404,40 b' class MainWindow(QtGui.QMainWindow):'
324 404 print "trying to add unexisting action (whos), skipping"
325 405
326 406 def undo_active_frontend(self):
327 self.activeFrontend().undo()
407 self.activeFrontend.undo()
328 408
329 409 def redo_active_frontend(self):
330 self.activeFrontend().redo()
410 self.activeFrontend.redo()
331 411 def reset_magic_active_frontend(self):
332 self.activeFrontend().reset_magic()
412 self.activeFrontend.reset_magic()
333 413 def history_magic_active_frontend(self):
334 self.activeFrontend().history_magic()
414 self.activeFrontend.history_magic()
335 415 def save_magic_active_frontend(self):
336 self.activeFrontend().save_magic()
416 self.activeFrontend.save_magic()
337 417 def clear_magic_active_frontend(self):
338 self.activeFrontend().clear_magic()
418 self.activeFrontend.clear_magic()
339 419 def who_magic_active_frontend(self):
340 self.activeFrontend().who_magic()
420 self.activeFrontend.who_magic()
341 421 def who_ls_magic_active_frontend(self):
342 self.activeFrontend().who_ls_magic()
422 self.activeFrontend.who_ls_magic()
343 423 def whos_magic_active_frontend(self):
344 self.activeFrontend().whos_magic()
424 self.activeFrontend.whos_magic()
345 425
346 426 def print_action_active_frontend(self):
347 self.activeFrontend().print_action.trigger()
427 self.activeFrontend.print_action.trigger()
348 428
349 429 def export_action_active_frontend(self):
350 self.activeFrontend().export_action.trigger()
430 self.activeFrontend.export_action.trigger()
351 431
352 432 def select_all_active_frontend(self):
353 self.activeFrontend().select_all_action.trigger()
433 self.activeFrontend.select_all_action.trigger()
354 434
355 435 def increase_font_size_active_frontend(self):
356 self.activeFrontend().increase_font_size.trigger()
436 self.activeFrontend.increase_font_size.trigger()
357 437 def decrease_font_size_active_frontend(self):
358 self.activeFrontend().decrease_font_size.trigger()
438 self.activeFrontend.decrease_font_size.trigger()
359 439 def reset_font_size_active_frontend(self):
360 self.activeFrontend().reset_font_size.trigger()
440 self.activeFrontend.reset_font_size.trigger()
361 441 #---------------------------------------------------------------------------
362 442 # QWidget interface
363 443 #---------------------------------------------------------------------------
@@ -369,17 +449,38 b' class MainWindow(QtGui.QMainWindow):'
369 449 so, closes the kernel cleanly. Alternatively, if the exit magic is used,
370 450 it closes without prompt.
371 451 """
372 keepkernel = None #Use the prompt by default
373 if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic
374 keepkernel = self._frontend._keep_kernel_on_exit
452 # Should change the logic
453 # put this code iin a per tab bases, with each atributes specified on the tab.
454 # otherwise, we should ask the user:
455 # ====================================
456 # close all the tabs and kernel ?
457 # Yes | No | Cancel | Keep the kernel runing | Ask for each
458 # ====================================
459 # We might also add an action "close all `non local kernel` console"
460
461
462 # Closing widget is based on the tab index, so we have to build the
463 # list of widget before actually looping throught it. By Sending the %exit magic
464 # you are sure to avoid question about restarting the kernel. Otherwise you'll have to
465 # close the 'non local kernel' tabs before
466
467 #allWidget = [ self.tabWidget.widget(n) for n in range(self.tabWidget.count())]
468
469 #for w in allWidget:
470 # w.exit_magic()
471 #return
472
473 #keepkernel = None #Use the prompt by default
474 #if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic
475 # keepkernel = self._frontend._keep_kernel_on_exit
375 476
376 kernel_manager = self._frontend.kernel_manager
377
378 if keepkernel is None and not self._confirm_exit:
379 # don't prompt, just terminate the kernel if we own it
380 # or leave it alone if we don't
381 keepkernel = not self._existing
477 #kernel_manager = self._frontend.kernel_manager
382 478
479 #if keepkernel is None and not self._confirm_exit:
480 # # don't prompt, just terminate the kernel if we own it
481 # # or leave it alone if we don't
482 # keepkernel = not self._existing
483 return
383 484 if keepkernel is None: #show prompt
384 485 if kernel_manager and kernel_manager.channels_running:
385 486 title = self.window().windowTitle()
@@ -756,6 +857,8 b' class IPythonQtConsoleApp(BaseIPythonApplication):'
756 857 widget = self.widget_factory(config=self.config,
757 858 local_kernel=local_kernel)
758 859 widget.kernel_manager = kernel_manager
860 widget._confirm_exit=True;
861 widget._may_close=True;
759 862 self.window.addTabWithFrontend(widget)
760 863
761 864 def createTabAttachedToCurrentTabKernel(self):
@@ -774,6 +877,8 b' class IPythonQtConsoleApp(BaseIPythonApplication):'
774 877 local_kernel = (not self.existing) or self.ip in LOCAL_IPS
775 878 widget = self.widget_factory(config=self.config,
776 879 local_kernel=False)
880 widget._confirm_exit=True;
881 widget._may_close=False;
777 882 widget.kernel_manager = kernel_manager
778 883 self.window.addTabWithFrontend(widget,name=str('('+cwname+') slave'))
779 884
@@ -791,6 +896,7 b' class IPythonQtConsoleApp(BaseIPythonApplication):'
791 896 self.window = MainWindow(self.app, self.widget, self.existing,
792 897 may_close=local_kernel,
793 898 confirm_exit=self.confirm_exit)
899 self.window.addTabWithFrontend(self.widget)
794 900 self.window.initMenuBar()
795 901 self.window.setWindowTitle('Python' if self.pure else 'IPython')
796 902
General Comments 0
You need to be logged in to leave comments. Login now