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, Signal
00035 from python_qt_binding.QtGui import QDockWidget, QIcon, QMenu, 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         self.title_label.installEventFilter(self)
00092         self.title_edit.hide()
00093         self.title_edit.editingFinished.connect(self._finished_editing)
00094         self.title_edit.returnPressed.connect(self._update_title_label)
00095 
00096     def __del__(self):
00097         self._dock_widget.removeEventFilter(self)
00098 
00099     def connect_button(self, button_id, callback):
00100         button = self._extra_buttons.get(button_id, None)
00101         if button is None:
00102             qWarning('DockWidgetTitleBar.connect_button(): unknown button_id: %s' % button_id)
00103             return
00104         button.clicked.connect(callback)
00105 
00106     def connect_close_button(self, callback):
00107         self._close_callbacks.append(callback)
00108 
00109     def _close_clicked(self):
00110         for callback in self._close_callbacks:
00111             callback(self.parent())
00112 
00113     def show_button(self, button_id, visibility=True):
00114         button = self._extra_buttons.get(button_id, None)
00115         if button is None:
00116             qWarning('DockWidgetTitleBar.show_button(): unknown button_id: %s' % button_id)
00117             return
00118         button.setVisible(visibility)
00119 
00120     def hide_button(self, button_id):
00121         self.show_button(button_id, False)
00122 
00123     def eventFilter(self, obj, event):
00124         if event.type() in self._event_callbacks:
00125             ret_val = self._event_callbacks[event.type()](obj, event)
00126             if ret_val is not None:
00127                 return ret_val
00128         if event.type() == event.ContextMenu and obj == self.title_label:
00129             menu = QMenu(self)
00130             rename_action = menu.addAction(self.tr('Rename dock widget'))
00131             action = menu.exec_(self.mapToGlobal(event.pos()))
00132             if action == rename_action:
00133                 self.title_label.hide()
00134                 self.title_edit.setText(self.title_label.text())
00135                 self.title_edit.show()
00136                 self.title_edit.setFocus()
00137             return True
00138         return QObject.eventFilter(self, obj, event)
00139 
00140     def _update_icon(self, *args):
00141         pixmap = None
00142         if self.parentWidget().windowIcon():
00143             pixmap = self.parentWidget().windowIcon().pixmap(self.close_button.iconSize())
00144         self.icon_label.setPixmap(pixmap)
00145 
00146     def _update_title(self, *args):
00147         self.title_label.setText(self.parentWidget().windowTitle())
00148 
00149     def _toggle_dockable(self, enabled):
00150         dock_widget = self.parentWidget()
00151         if enabled:
00152             dock_widget.setAllowedAreas(Qt.AllDockWidgetAreas)
00153         else:
00154             dock_widget.setAllowedAreas(Qt.NoDockWidgetArea)
00155 
00156     def _toggle_floating(self):
00157         dock_widget = self.parentWidget()
00158         dock_widget.setFloating(not dock_widget.isFloating())
00159 
00160     def _minimize_dock_widget(self):
00161         dock_widget = self.parentWidget()
00162         dock_widget.hide()
00163 
00164     def _features_changed(self, features=None):
00165         if features is None:
00166             features = self.parentWidget().features()
00167 
00168         closable = bool(features & QDockWidget.DockWidgetClosable)
00169         self.close_button.setVisible(closable)
00170         self.reload_button.setVisible(closable)
00171 
00172         movable = bool(features & QDockWidget.DockWidgetMovable)
00173         self.dockable_button.setChecked(movable)
00174         self._toggle_dockable(self.dockable_button.isChecked())
00175         self.dockable_button.setVisible(movable)
00176         self.float_button.setVisible(movable)
00177         self.minimize_button.setVisible(movable)
00178 
00179     def save_settings(self, settings):
00180         settings.set_value('dock_widget_title', self._dock_widget.windowTitle())
00181 
00182         # skip saving dockable flag when layout is frozen
00183         movable = bool(self.parentWidget().features() & QDockWidget.DockWidgetMovable)
00184         if movable:
00185             settings.set_value('dockable', self.dockable_button.isChecked())
00186 
00187     def restore_settings(self, settings):
00188         dock_widget_title = settings.value('dock_widget_title', None)
00189         if dock_widget_title is not None:
00190             self.title_label.setText(dock_widget_title)
00191             self._dock_widget.setWindowTitle(dock_widget_title)
00192 
00193         dockable = settings.value('dockable', True) in [True, 'true']
00194         # only allow dockable when layout is not frozen
00195         movable = bool(self.parentWidget().features() & QDockWidget.DockWidgetMovable)
00196         self.dockable_button.setChecked(dockable and movable)
00197         self._toggle_dockable(self.dockable_button.isChecked())
00198 
00199     def _finished_editing(self):
00200         self.title_edit.hide()
00201         self.title_label.show()
00202 
00203     def _update_title_label(self):
00204         if self.title_edit.text():
00205             self.title_label.setText(self.title_edit.text())
00206             self._dock_widget.setWindowTitle(self.title_edit.text())
00207 
00208 
00209 if __name__ == '__main__':
00210     import sys
00211     from python_qt_binding.QtGui import QApplication
00212     from .dockable_main_window import DockableMainWindow
00213 
00214     app = QApplication(sys.argv)
00215 
00216     win = DockableMainWindow()
00217 
00218     dock1 = QDockWidget('dockwidget1', win)
00219     win.addDockWidget(Qt.LeftDockWidgetArea, dock1)
00220     title_bar = DockWidgetTitleBar(dock1)
00221     dock1.setTitleBarWidget(title_bar)
00222 
00223     dock2 = QDockWidget('dockwidget2')
00224     win.addDockWidget(Qt.RightDockWidgetArea, dock2)
00225     title_bar = DockWidgetTitleBar(dock2)
00226     dock2.setTitleBarWidget(title_bar)
00227 
00228     win.resize(640, 480)
00229     win.show()
00230 
00231     app.exec_()


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