--- a/eric6/UI/UserInterface.py Thu Jun 03 11:39:55 2021 +0200 +++ b/eric6/UI/UserInterface.py Sat Jul 03 11:47:24 2021 +0200 @@ -22,7 +22,7 @@ QIODevice, qVersion, QProcess, QSize, QUrl, QObject, Qt, QUuid, QThread, QUrlQuery ) -from PyQt5.QtGui import QKeySequence, QDesktopServices +from PyQt5.QtGui import QKeySequence, QDesktopServices, QGuiApplication from PyQt5.QtWidgets import ( QSizePolicy, QWidget, QWhatsThis, QToolBar, QDialog, QSplitter, QApplication, QMenu, QVBoxLayout, QDockWidget, QAction, QLabel @@ -130,7 +130,6 @@ self.__nWrite(self.__bufferedWrite()) -# TODO: add support for QSessionManager to save changed files class UserInterface(E5MainWindow): """ Class implementing the main user interface. @@ -747,6 +746,11 @@ if interval > 0: QApplication.setKeyboardInputInterval(interval) + # connect to the desktop environment session manager + QGuiApplication.setFallbackSessionManagementEnabled(False) + app.commitDataRequest.connect(self.__commitData, + Qt.ConnectionType.DirectConnection) + def networkAccessManager(self): """ Public method to get a reference to the network access manager object. @@ -7511,3 +7515,46 @@ @rtype CodeDocumentationViewer """ return self.codeDocumentationViewer + + ############################################### + ## Support for Desktop session management below + ############################################### + + def __commitData(self, manager): + """ + Private slot to commit unsaved data when instructed by the desktop + session manager. + + @param manager reference to the desktop session manager + @type QSessionManager + """ + if self.viewmanager.hasDirtyEditor(): + if manager.allowsInteraction(): + res = E5MessageBox.warning( + self, + self.tr("Unsaved Data Detected"), + self.tr("Some editors contain unsaved data. Shall these" + " be saved?"), + E5MessageBox.Abort | + E5MessageBox.Discard | + E5MessageBox.Save | + E5MessageBox.SaveAll, + E5MessageBox.SaveAll) + if res == E5MessageBox.SaveAll: + manager.release() + self.viewmanager.saveAllEditors() + elif res == E5MessageBox.Save: + manager.release() + ok = self.viewmanager.checkAllDirty() + if not ok: + manager.cancel() + elif res == E5MessageBox.Discard: + # nothing to do + pass + else: + # default action is to abort shutdown + manager.cancel() + else: + # We did not get permission to interact, play it safe and + # save all data. + self.viewmanager.saveAllEditors()