00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
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_()