main_window.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
00002 # All rights reserved.
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 #   * Redistributions of source code must retain the above copyright
00009 #     notice, this list of conditions and the following disclaimer.
00010 #   * Redistributions in binary form must reproduce the above
00011 #     copyright notice, this list of conditions and the following
00012 #     disclaimer in the documentation and/or other materials provided
00013 #     with the distribution.
00014 #   * Neither the name of the TU Darmstadt nor the names of its
00015 #     contributors may be used to endorse or promote products derived
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 # POSSIBILITY OF SUCH DAMAGE.
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         # store current setup to current _settings
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         # remember new _settings
00075         self._global_settings = global_settings
00076         self._perspective_settings = perspective_settings
00077         self._settings = self._perspective_settings.get_settings('mainwindow')
00078         # only restore geometry, restoring state is triggered after PluginManager has been updated
00079         self._restore_geometry_from_perspective()
00080 
00081     def save_setup(self):
00082         qDebug('MainWindow.save_setup()')
00083         # store current setup to current _settings
00084         self._save_geometry_to_perspective()
00085         self._save_state_to_perspective()
00086 
00087     def restore_state(self):
00088         qDebug('MainWindow.restore_state()')
00089         # restore state from _settings
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             # unmaximizing widget before saveGeometry works around bug to restore dock-widgets
00098             # still the non-maximized size can not correctly be restored
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             # safe area for all toolbars
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             # restore area for all toolbars
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)


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:44:00