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):
00043         super(DockWidgetTitleBar, self).__init__(dock_widget)
00044         self._dock_widget = dock_widget
00045 
00046         ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'DockWidgetTitleBar.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         self.configuration_button.setIcon(QIcon.fromTheme('emblem-system'))
00056         self.configuration_button.setText("")
00057         self.reload_button.setIcon(QIcon.fromTheme('view-refresh'))
00058         self.reload_button.setText("")
00059         self.help_button.setIcon(QIcon.fromTheme('help-browser'))
00060         self.help_button.setText("")
00061 
00062         self.close_button.setIcon(QIcon.fromTheme('window-close'))
00063         self.close_button.setText("")
00064         self.close_button.clicked.connect(self._close_clicked)
00065 
00066         self.float_button.clicked.connect(self._toggle_floating)
00067         self.dockable_button.clicked[bool].connect(self._toggle_dockable)
00068 
00069         self._dock_widget.featuresChanged.connect(self._features_changed)
00070         self._features_changed()
00071 
00072         self._update_title()
00073 
00074         self._close_callbacks = []
00075         self._event_callbacks = {
00076             QEvent.WindowTitleChange: self._update_title,
00077         }
00078         self._dock_widget.installEventFilter(self)
00079 
00080     def __del__(self):
00081         self._dock_widget.removeEventFilter(self)
00082 
00083     def connect_button(self, button_id, callback):
00084         button = self._extra_buttons.get(button_id, None)
00085         if button is None:
00086             qWarning('DockWidgetTitleBar.connect_button(): unknown button_id: %s' % button_id)
00087             return
00088         button.clicked.connect(callback)
00089 
00090     def connect_close_button(self, callback):
00091         self._close_callbacks.append(callback)
00092 
00093     def _close_clicked(self):
00094         for callback in self._close_callbacks:
00095             callback(self.parent())
00096 
00097     def show_button(self, button_id, visibility=True):
00098         button = self._extra_buttons.get(button_id, None)
00099         if button is None:
00100             qWarning('DockWidgetTitleBar.show_button(): unknown button_id: %s' % button_id)
00101             return
00102         button.setVisible(visibility)
00103 
00104     def hide_button(self, button_id):
00105         self.show_button(button_id, False)
00106 
00107     def eventFilter(self, obj, event):
00108         if event.type() in self._event_callbacks:
00109             ret_val = self._event_callbacks[event.type()](obj, event)
00110             if ret_val is not None:
00111                 return ret_val
00112         return QObject.eventFilter(self, obj, event)
00113 
00114     def _update_title(self, *args):
00115         self.title_label.setText(self.parentWidget().windowTitle())
00116 
00117     def _toggle_dockable(self, enabled):
00118         dock_widget = self.parentWidget()
00119         if enabled:
00120             dock_widget.setAllowedAreas(Qt.AllDockWidgetAreas)
00121         else:
00122             dock_widget.setAllowedAreas(Qt.NoDockWidgetArea)
00123 
00124     def _toggle_floating(self):
00125         dock_widget = self.parentWidget()
00126         dock_widget.setFloating(not dock_widget.isFloating())
00127 
00128     def _features_changed(self, features=None):
00129         if features is None:
00130             features = self.parentWidget().features()
00131         self.close_button.setVisible(bool(features & QDockWidget.DockWidgetClosable))
00132         self.float_button.setVisible(bool(features & QDockWidget.DockWidgetFloatable))
00133 
00134     def save_settings(self, settings):
00135         settings.set_value('dockable', self.dockable_button.isChecked())
00136 
00137     def restore_settings(self, settings):
00138         self.dockable_button.setChecked(settings.value('dockable', True) in [True, 'true'])
00139         self._toggle_dockable(self.dockable_button.isChecked())
00140 
00141 
00142 if __name__ == '__main__':
00143     import sys
00144     from python_qt_binding.QtGui import QApplication, QMainWindow
00145 
00146     app = QApplication(sys.argv)
00147 
00148     win = QMainWindow()
00149 
00150     dock1 = QDockWidget('dockwidget1', win)
00151     win.addDockWidget(Qt.LeftDockWidgetArea, dock1)
00152     title_bar = DockWidgetTitleBar(dock1)
00153     dock1.setTitleBarWidget(title_bar)
00154 
00155     dock2 = QDockWidget('dockwidget2')
00156     win.addDockWidget(Qt.RightDockWidgetArea, dock2)
00157     title_bar = DockWidgetTitleBar(dock2)
00158     dock2.setTitleBarWidget(title_bar)
00159 
00160     win.resize(640, 480)
00161     win.show()
00162 
00163     app.exec_()


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