def _on_tab_close_requested(self, index): widget = self.widget(index) dirty = False try: if widget.original is None: dirty = widget.dirty except AttributeError: pass if not dirty: self.remove_tab(index) else: # unsaved widget path = self._get_widget_path(widget) if not path: path = self.tabText(self.indexOf(widget)) dlg = DlgUnsavedFiles( self, files=[path]) if dlg.exec_() == dlg.Accepted: rm = True if not dlg.discarded: try: rm = self.save_widget(widget) except OSError: pass if rm: self.remove_tab(index) widget.close() widget.setParent(None) del widget
def closeEvent(self, event): """ Saves dirty editors on close and cancel the event if the user choosed to continue to work. :param event: close event """ dirty_widgets = [] for w in self.widgets(include_clones=False): if w.dirty: dirty_widgets.append(w) filenames = [] for w in dirty_widgets: if os.path.exists(w.file.path): filenames.append(w.file.path) else: filenames.append(w.documentTitle()) if len(filenames) == 0: self.close_all() return dlg = DlgUnsavedFiles(self, files=filenames) if dlg.exec_() == dlg.Accepted: if not dlg.discarded: for item in dlg.listWidget.selectedItems(): filename = item.text() widget = None for widget in dirty_widgets: if widget.file.path == filename or widget.documentTitle() == filename: break tw = widget.parent_tab_widget tw.save_widget(widget) tw.remove_tab(tw.indexOf(widget)) self.close_all() else: event.ignore()
def _try_close_dirty_tabs(self, exept=None): """ Tries to close dirty tabs. Uses DlgUnsavedFiles to ask the user what he wants to do. """ widgets, filenames = self._collect_dirty_tabs(skip=exept) if not len(filenames): return True dlg = DlgUnsavedFiles(self, files=filenames) if dlg.exec_() == dlg.Accepted: if not dlg.discarded: for item in dlg.listWidget.selectedItems(): filename = item.text() widget = None for widget in widgets: if widget.path == filename: break if widget != exept: self.save_widget(widget) self.remove_tab(self.indexOf(widget)) return True return False
def closeEvent(self, event): """ Saves dirty editors on close and cancel the event if the user choosed to continue to work. :param event: close event """ dirty_widgets = [] for w in self.widgets(include_clones=False): if w.dirty: dirty_widgets.append(w) filenames = [] for w in dirty_widgets: if os.path.exists(w.file.path): filenames.append(w.file.path) else: filenames.append(w.documentTitle()) if len(filenames) == 0: self.close_all() return dlg = DlgUnsavedFiles(self, files=filenames) if dlg.exec_() == dlg.Accepted: if not dlg.discarded: for item in dlg.listWidget.selectedItems(): filename = item.text() widget = None for widget in dirty_widgets: if widget.file.path == filename or \ widget.documentTitle() == filename: break tw = widget.parent_tab_widget tw.save_widget(widget) tw.remove_tab(tw.indexOf(widget)) self.close_all() else: event.ignore()