Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 from python_qt_binding.QtCore import qDebug, Qt, Signal
00032 from python_qt_binding.QtGui import QMainWindow, QToolBar
00033
00034 from .settings import Settings
00035
00036
00037 class MainWindow(QMainWindow):
00038
00039 """Main window of the application managing the geometry and state of all top-level widgets."""
00040
00041 save_settings_before_close_signal = Signal(Settings, Settings)
00042
00043 def __init__(self):
00044 super(MainWindow, self).__init__()
00045 self.setObjectName('MainWindow')
00046
00047 self._save_on_close_signaled = False
00048 self._global_settings = None
00049 self._perspective_settings = None
00050 self._settings = None
00051
00052 def closeEvent(self, event):
00053 qDebug('MainWindow.closeEvent()')
00054 if not self._save_on_close_signaled:
00055 self._save_geometry_to_perspective()
00056 self._save_state_to_perspective()
00057 self._save_on_close_signaled = True
00058 self.save_settings_before_close_signal.emit(self._global_settings, self._perspective_settings)
00059 event.ignore()
00060 else:
00061 event.accept()
00062
00063 def save_settings(self, global_settings, perspective_settings):
00064 qDebug('MainWindow.save_settings()')
00065 self._global_settings = global_settings
00066 self._perspective_settings = perspective_settings
00067 self._settings = self._perspective_settings.get_settings('mainwindow')
00068
00069 self._save_geometry_to_perspective()
00070 self._save_state_to_perspective()
00071
00072 def restore_settings(self, global_settings, perspective_settings):
00073 qDebug('MainWindow.restore_settings()')
00074
00075 self._global_settings = global_settings
00076 self._perspective_settings = perspective_settings
00077 self._settings = self._perspective_settings.get_settings('mainwindow')
00078
00079 self._restore_geometry_from_perspective()
00080
00081 def save_setup(self):
00082 qDebug('MainWindow.save_setup()')
00083
00084 self._save_geometry_to_perspective()
00085 self._save_state_to_perspective()
00086
00087 def restore_state(self):
00088 qDebug('MainWindow.restore_state()')
00089
00090 self._restore_state_from_perspective()
00091
00092 def perspective_changed(self, name):
00093 self.setWindowTitle('%s - RosGui' % str(name))
00094
00095 def _save_geometry_to_perspective(self):
00096 if self._settings is not None:
00097
00098
00099 maximized = self.isMaximized()
00100 if maximized:
00101 self.showNormal()
00102 self._settings.set_value('geometry', self.saveGeometry())
00103 if maximized:
00104 self.showMaximized()
00105
00106 def _restore_geometry_from_perspective(self):
00107 if self._settings.contains('geometry'):
00108 self.restoreGeometry(self._settings.value('geometry'))
00109
00110 def _save_state_to_perspective(self):
00111 if self._settings is not None:
00112 self._settings.set_value('state', self.saveState())
00113
00114 toolbar_settings = self._settings.get_settings('toolbar_areas')
00115 for toolbar in self.findChildren(QToolBar):
00116 area = self.toolBarArea(toolbar)
00117 if area in [Qt.LeftToolBarArea, Qt.RightToolBarArea, Qt.TopToolBarArea, Qt.BottomToolBarArea]:
00118 toolbar_settings.set_value(toolbar.objectName(), area)
00119
00120 def _restore_state_from_perspective(self):
00121 if self._settings.contains('state'):
00122 self.restoreState(self._settings.value('state'))
00123
00124 toolbar_settings = self._settings.get_settings('toolbar_areas')
00125 for toolbar in self.findChildren(QToolBar):
00126 area = toolbar_settings.value(toolbar.objectName(), Qt.NoToolBarArea)
00127 if area in [Qt.LeftToolBarArea, Qt.RightToolBarArea, Qt.TopToolBarArea, Qt.BottomToolBarArea]:
00128 self.addToolBar(area, toolbar)