dock_widget_title_bar.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 import os
00032 
00033 from python_qt_binding import loadUi
00034 from python_qt_binding.QtCore import QEvent, QObject, Qt, qWarning
00035 from python_qt_binding.QtGui import QDockWidget, QIcon, QWidget
00036 
00037 
00038 class DockWidgetTitleBar(QWidget):
00039 
00040     """Title bar for dock widgets providing custom actions."""
00041 
00042     def __init__(self, dock_widget, qtgui_path):
00043         super(DockWidgetTitleBar, self).__init__(dock_widget)
00044         self._dock_widget = dock_widget
00045 
00046         ui_file = os.path.join(qtgui_path, 'resource', 'dock_widget_title_bar.ui')
00047         loadUi(ui_file, self)
00048         self._extra_buttons = {
00049             'configuration': self.configuration_button,
00050             'reload': self.reload_button,
00051             'help': self.help_button,
00052             'close': self.close_button,
00053         }
00054 
00055         icon = QIcon.fromTheme('emblem-system')
00056         if not icon.isNull():
00057             self.configuration_button.setIcon(icon)
00058             self.configuration_button.setText("")
00059         icon = QIcon.fromTheme('view-refresh')
00060         if not icon.isNull():
00061             self.reload_button.setIcon(icon)
00062             self.reload_button.setText("")
00063         icon = QIcon.fromTheme('help-browser')
00064         if not icon.isNull():
00065             self.help_button.setIcon(icon)
00066             self.help_button.setText("")
00067 
00068         icon = QIcon.fromTheme('window-close')
00069         if not icon.isNull():
00070             self.close_button.setIcon(icon)
00071             self.close_button.setText("")
00072         self.close_button.clicked.connect(self._close_clicked)
00073 
00074         self.float_button.clicked.connect(self._toggle_floating)
00075         self.dockable_button.clicked[bool].connect(self._toggle_dockable)
00076         self.minimize_button.clicked.connect(self._minimize_dock_widget)
00077 
00078         self._dock_widget.featuresChanged.connect(self._features_changed)
00079         self._features_changed()
00080 
00081         self._update_icon()
00082         self._update_title()
00083 
00084         self._close_callbacks = []
00085         self._event_callbacks = {
00086             QEvent.WindowIconChange: self._update_icon,
00087             QEvent.WindowTitleChange: self._update_title,
00088         }
00089         self._dock_widget.installEventFilter(self)
00090 
00091     def __del__(self):
00092         self._dock_widget.removeEventFilter(self)
00093 
00094     def connect_button(self, button_id, callback):
00095         button = self._extra_buttons.get(button_id, None)
00096         if button is None:
00097             qWarning('DockWidgetTitleBar.connect_button(): unknown button_id: %s' % button_id)
00098             return
00099         button.clicked.connect(callback)
00100 
00101     def connect_close_button(self, callback):
00102         self._close_callbacks.append(callback)
00103 
00104     def _close_clicked(self):
00105         for callback in self._close_callbacks:
00106             callback(self.parent())
00107 
00108     def show_button(self, button_id, visibility=True):
00109         button = self._extra_buttons.get(button_id, None)
00110         if button is None:
00111             qWarning('DockWidgetTitleBar.show_button(): unknown button_id: %s' % button_id)
00112             return
00113         button.setVisible(visibility)
00114 
00115     def hide_button(self, button_id):
00116         self.show_button(button_id, False)
00117 
00118     def eventFilter(self, obj, event):
00119         if event.type() in self._event_callbacks:
00120             ret_val = self._event_callbacks[event.type()](obj, event)
00121             if ret_val is not None:
00122                 return ret_val
00123         return QObject.eventFilter(self, obj, event)
00124 
00125     def _update_icon(self, *args):
00126         pixmap = None
00127         if self.parentWidget().windowIcon():
00128             pixmap = self.parentWidget().windowIcon().pixmap(self.close_button.iconSize())
00129         self.icon_label.setPixmap(pixmap)
00130 
00131     def _update_title(self, *args):
00132         self.title_label.setText(self.parentWidget().windowTitle())
00133 
00134     def _toggle_dockable(self, enabled):
00135         dock_widget = self.parentWidget()
00136         if enabled:
00137             dock_widget.setAllowedAreas(Qt.AllDockWidgetAreas)
00138         else:
00139             dock_widget.setAllowedAreas(Qt.NoDockWidgetArea)
00140 
00141     def _toggle_floating(self):
00142         dock_widget = self.parentWidget()
00143         dock_widget.setFloating(not dock_widget.isFloating())
00144 
00145     def _minimize_dock_widget(self):
00146         dock_widget = self.parentWidget()
00147         dock_widget.hide()
00148 
00149     def _features_changed(self, features=None):
00150         if features is None:
00151             features = self.parentWidget().features()
00152 
00153         closable = bool(features & QDockWidget.DockWidgetClosable)
00154         self.close_button.setVisible(closable)
00155         self.reload_button.setVisible(closable)
00156 
00157         movable = bool(features & QDockWidget.DockWidgetMovable)
00158         self.dockable_button.setChecked(movable)
00159         self._toggle_dockable(self.dockable_button.isChecked())
00160         self.dockable_button.setVisible(movable)
00161         self.float_button.setVisible(movable)
00162         self.minimize_button.setVisible(movable)
00163 
00164     def save_settings(self, settings):
00165         # skip saving dockable flag when layout is frozen
00166         movable = bool(self.parentWidget().features() & QDockWidget.DockWidgetMovable)
00167         if movable:
00168             settings.set_value('dockable', self.dockable_button.isChecked())
00169 
00170     def restore_settings(self, settings):
00171         dockable = settings.value('dockable', True) in [True, 'true']
00172         # only allow dockable when layout is not frozen
00173         movable = bool(self.parentWidget().features() & QDockWidget.DockWidgetMovable)
00174         self.dockable_button.setChecked(dockable and movable)
00175         self._toggle_dockable(self.dockable_button.isChecked())
00176 
00177 
00178 if __name__ == '__main__':
00179     import sys
00180     from python_qt_binding.QtGui import QApplication
00181     from .dockable_main_window import DockableMainWindow
00182 
00183     app = QApplication(sys.argv)
00184 
00185     win = DockableMainWindow()
00186 
00187     dock1 = QDockWidget('dockwidget1', win)
00188     win.addDockWidget(Qt.LeftDockWidgetArea, dock1)
00189     title_bar = DockWidgetTitleBar(dock1)
00190     dock1.setTitleBarWidget(title_bar)
00191 
00192     dock2 = QDockWidget('dockwidget2')
00193     win.addDockWidget(Qt.RightDockWidgetArea, dock2)
00194     title_bar = DockWidgetTitleBar(dock2)
00195     dock2.setTitleBarWidget(title_bar)
00196 
00197     win.resize(640, 480)
00198     win.show()
00199 
00200     app.exec_()


qt_gui
Author(s): Dirk Thomas
autogenerated on Mon Oct 6 2014 03:57:52