33 from python_qt_binding
import loadUi
34 from python_qt_binding.QtCore
import QEvent, QObject, Qt, qWarning
35 from python_qt_binding.QtGui
import QIcon
36 from python_qt_binding.QtWidgets
import QDockWidget, QMenu, QWidget
40 """Title bar for dock widgets providing custom actions.""" 42 def __init__(self, dock_widget, qtgui_path, hide_title=False):
43 super(DockWidgetTitleBar, self).
__init__(dock_widget)
46 ui_file = os.path.join(qtgui_path,
'resource',
'dock_widget_title_bar.ui')
49 'configuration': self.configuration_button,
50 'reload': self.reload_button,
51 'help': self.help_button,
52 'close': self.close_button,
55 icon = QIcon.fromTheme(
'emblem-system')
57 self.configuration_button.setIcon(icon)
58 self.configuration_button.setText(
'')
59 icon = QIcon.fromTheme(
'view-refresh')
61 self.reload_button.setIcon(icon)
62 self.reload_button.setText(
'')
63 icon = QIcon.fromTheme(
'help-browser')
65 self.help_button.setIcon(icon)
66 self.help_button.setText(
'')
68 icon = QIcon.fromTheme(
'window-close')
70 self.close_button.setIcon(icon)
71 self.close_button.setText(
'')
89 self._dock_widget.installEventFilter(self)
91 self.title_label.installEventFilter(self)
92 self.title_edit.hide()
98 self.icon_label.hide()
99 self.title_label.hide()
100 self.dockable_button.hide()
101 self.reload_button.hide()
102 self.help_button.hide()
103 self.minimize_button.hide()
104 self.float_button.hide()
105 self.close_button.hide()
108 self._dock_widget.removeEventFilter(self)
111 button = self._extra_buttons.get(button_id,
None)
113 qWarning(
'DockWidgetTitleBar.connect_button(): unknown button_id: %s' % button_id)
115 button.clicked.connect(callback)
118 self._close_callbacks.append(callback)
122 callback(self.parent())
125 button = self._extra_buttons.get(button_id,
None)
127 qWarning(
'DockWidgetTitleBar.show_button(): unknown button_id: %s' % button_id)
129 button.setVisible(visibility)
137 if ret_val
is not None:
139 if event.type() == event.ContextMenu
and obj == self.title_label:
141 rename_action = menu.addAction(self.tr(
'Rename dock widget'))
142 action = menu.exec_(self.mapToGlobal(event.pos()))
143 if action == rename_action:
144 self.title_label.hide()
145 self.title_edit.setText(self.title_label.text())
146 self.title_edit.show()
147 self.title_edit.setFocus()
149 return QObject.eventFilter(self, obj, event)
153 if self.parentWidget().windowIcon():
154 pixmap = self.parentWidget().windowIcon().pixmap(self.close_button.iconSize())
155 self.icon_label.setPixmap(pixmap)
158 self.title_label.setText(self.parentWidget().windowTitle())
161 dock_widget = self.parentWidget()
163 dock_widget.setAllowedAreas(Qt.AllDockWidgetAreas)
165 dock_widget.setAllowedAreas(Qt.NoDockWidgetArea)
168 dock_widget = self.parentWidget()
169 dock_widget.setFloating(
not dock_widget.isFloating())
172 dock_widget = self.parentWidget()
177 features = self.parentWidget().features()
179 closable = bool(features & QDockWidget.DockWidgetClosable)
180 self.close_button.setVisible(closable)
181 self.reload_button.setVisible(closable)
183 movable = bool(features & QDockWidget.DockWidgetMovable)
184 self.dockable_button.setChecked(movable)
186 self.dockable_button.setVisible(movable)
187 self.float_button.setVisible(movable)
188 self.minimize_button.setVisible(movable)
191 settings.set_value(
'dock_widget_title', self._dock_widget.windowTitle())
194 movable = bool(self.parentWidget().features() & QDockWidget.DockWidgetMovable)
196 settings.set_value(
'dockable', self.dockable_button.isChecked())
199 dock_widget_title = settings.value(
'dock_widget_title',
None)
200 if dock_widget_title
is not None:
201 self.title_label.setText(dock_widget_title)
202 self._dock_widget.setWindowTitle(dock_widget_title)
204 dockable = settings.value(
'dockable',
True)
in [
True,
'true']
206 movable = bool(self.parentWidget().features() & QDockWidget.DockWidgetMovable)
207 self.dockable_button.setChecked(dockable
and movable)
211 self.title_edit.hide()
212 self.title_label.show()
215 if self.title_edit.text():
216 self.title_label.setText(self.title_edit.text())
217 self._dock_widget.setWindowTitle(self.title_edit.text())
220 if __name__ ==
'__main__':
222 from python_qt_binding.QtGui
import QApplication
225 app = QApplication(sys.argv)
229 dock1 = QDockWidget(
'dockwidget1', win)
230 win.addDockWidget(Qt.LeftDockWidgetArea, dock1)
232 dock1.setTitleBarWidget(title_bar)
234 dock2 = QDockWidget(
'dockwidget2')
235 win.addDockWidget(Qt.RightDockWidgetArea, dock2)
237 dock2.setTitleBarWidget(title_bar)