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 QToolBar
00033 
00034 from .dockable_main_window import DockableMainWindow
00035 from .settings import Settings
00036 
00037 
00038 class MainWindow(DockableMainWindow):
00039 
00040     """Main window of the application managing the geometry and state of all top-level widgets."""
00041 
00042     save_settings_before_close_signal = Signal(Settings, Settings)
00043 
00044     def __init__(self):
00045         super(MainWindow, self).__init__()
00046         self.setObjectName('MainWindow')
00047 
00048         self._save_on_close_signaled = False
00049         self._global_settings = None
00050         self._perspective_settings = None
00051         self._settings = None
00052 
00053     def closeEvent(self, event):
00054         qDebug('MainWindow.closeEvent()')
00055         if not self._save_on_close_signaled:
00056             self._save_geometry_to_perspective()
00057             self._save_state_to_perspective()
00058             self._save_on_close_signaled = True
00059             self.save_settings_before_close_signal.emit(self._global_settings, self._perspective_settings)
00060             event.ignore()
00061         else:
00062             event.accept()
00063 
00064     def save_settings(self, global_settings, perspective_settings):
00065         qDebug('MainWindow.save_settings()')
00066         self._global_settings = global_settings
00067         self._perspective_settings = perspective_settings
00068         self._settings = self._perspective_settings.get_settings('mainwindow')
00069         # store current setup to current _settings
00070         self._save_geometry_to_perspective()
00071         self._save_state_to_perspective()
00072 
00073     def restore_settings(self, global_settings, perspective_settings):
00074         qDebug('MainWindow.restore_settings()')
00075         # remember new _settings
00076         self._global_settings = global_settings
00077         self._perspective_settings = perspective_settings
00078         self._settings = self._perspective_settings.get_settings('mainwindow')
00079         # only restore geometry, restoring state is triggered after PluginManager has been updated
00080         self._restore_geometry_from_perspective()
00081 
00082     def save_setup(self):
00083         qDebug('MainWindow.save_setup()')
00084         # store current setup to current _settings
00085         self._save_geometry_to_perspective()
00086         self._save_state_to_perspective()
00087 
00088     def restore_state(self):
00089         qDebug('MainWindow.restore_state()')
00090         # restore state from _settings
00091         self._restore_state_from_perspective()
00092 
00093     def perspective_changed(self, name):
00094         self.setWindowTitle('%s - rqt' % str(name))
00095 
00096     def _save_geometry_to_perspective(self):
00097         if self._settings is not None:
00098             # unmaximizing widget before saveGeometry works around bug to restore dock-widgets
00099             # still the non-maximized size can not correctly be restored
00100             maximized = self.isMaximized()
00101             if maximized:
00102                 self.showNormal()
00103             self._settings.set_value('geometry', self.saveGeometry())
00104             if maximized:
00105                 self.showMaximized()
00106 
00107     def _restore_geometry_from_perspective(self):
00108         if self._settings.contains('geometry'):
00109             self.restoreGeometry(self._settings.value('geometry'))
00110 
00111     def _save_state_to_perspective(self):
00112         if self._settings is not None:
00113             self._settings.set_value('state', self.saveState())
00114             # safe area for all toolbars
00115             toolbar_settings = self._settings.get_settings('toolbar_areas')
00116             for toolbar in self.findChildren(QToolBar):
00117                 area = self.toolBarArea(toolbar)
00118                 if area in [Qt.LeftToolBarArea, Qt.RightToolBarArea, Qt.TopToolBarArea, Qt.BottomToolBarArea]:
00119                     toolbar_settings.set_value(toolbar.objectName(), area)
00120 
00121     def _restore_state_from_perspective(self):
00122         if self._settings.contains('state'):
00123             self.restoreState(self._settings.value('state'))
00124             # restore area for all toolbars
00125             toolbar_settings = self._settings.get_settings('toolbar_areas')
00126             for toolbar in self.findChildren(QToolBar):
00127                 area = Qt.ToolBarArea(int(toolbar_settings.value(toolbar.objectName(), Qt.NoToolBarArea)))
00128                 if area in [Qt.LeftToolBarArea, Qt.RightToolBarArea, Qt.TopToolBarArea, Qt.BottomToolBarArea]:
00129                     self.addToolBar(area, toolbar)


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Feb 3 2017 03:42:12