2015-07-10 01:01:07 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
from PyQt5.QtWidgets import QToolBar, QDockWidget, QAction, QToolButton, QSizePolicy, QStylePainter, \
|
2016-03-25 01:42:47 +13:00
|
|
|
QStyleOptionButton, QStyle
|
2015-07-10 01:01:07 +12:00
|
|
|
|
2016-04-09 20:50:55 +12:00
|
|
|
from manuskript.ui import style
|
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
|
|
|
|
class collapsibleDockWidgets(QToolBar):
|
|
|
|
"""
|
|
|
|
QMainWindow "mixin" which provides auto-hiding support for dock widgets
|
|
|
|
(not toolbars).
|
|
|
|
"""
|
|
|
|
TRANSPOSED_AREA = {
|
|
|
|
Qt.LeftDockWidgetArea: Qt.LeftToolBarArea,
|
|
|
|
Qt.RightDockWidgetArea: Qt.RightToolBarArea,
|
|
|
|
Qt.TopDockWidgetArea: Qt.TopToolBarArea,
|
|
|
|
Qt.BottomDockWidgetArea: Qt.BottomToolBarArea,
|
2016-02-07 00:34:22 +13:00
|
|
|
}
|
2015-07-10 01:01:07 +12:00
|
|
|
|
|
|
|
def __init__(self, area, parent, name=""):
|
|
|
|
QToolBar.__init__(self, parent)
|
|
|
|
self._area = area
|
|
|
|
if not name:
|
|
|
|
name = self.tr("Dock Widgets Toolbar")
|
|
|
|
self.setObjectName(name)
|
|
|
|
self.setWindowTitle(name)
|
|
|
|
|
|
|
|
self.setFloatable(False)
|
|
|
|
self.setMovable(False)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
# self.setAllowedAreas(self.TRANSPOSED_AREA[self._area])
|
2015-07-10 01:01:07 +12:00
|
|
|
self.parent().addToolBar(self.TRANSPOSED_AREA[self._area], self)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-25 01:42:47 +13:00
|
|
|
self._dockToButtonAction = {}
|
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
# Dock widgets
|
|
|
|
for d in self._dockWidgets():
|
|
|
|
b = verticalButton(self)
|
|
|
|
b.setDefaultAction(d.toggleViewAction())
|
2016-02-08 22:30:32 +13:00
|
|
|
# d.setStyleSheet("QDockWidget::title{background-color: red;}")
|
|
|
|
# d.setTitleBarWidget(QLabel(d.windowTitle()))
|
2016-04-09 20:50:55 +12:00
|
|
|
d.setStyleSheet(style.dockSS())
|
2016-03-25 01:42:47 +13:00
|
|
|
a = self.addWidget(b)
|
|
|
|
self._dockToButtonAction[d] = a
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
self.addSeparator()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
# Other widgets
|
|
|
|
self.otherWidgets = []
|
|
|
|
self.currentGroup = None
|
|
|
|
|
2016-04-09 20:50:55 +12:00
|
|
|
self.setStyleSheet(style.toolBarSS())
|
2016-04-09 00:49:15 +12:00
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
def _dockWidgets(self):
|
|
|
|
mw = self.parent()
|
|
|
|
for w in mw.findChildren(QDockWidget, None):
|
|
|
|
yield w
|
|
|
|
|
2016-02-07 00:34:22 +13:00
|
|
|
def addCustomWidget(self, text, widget, group=None):
|
2015-07-10 01:01:07 +12:00
|
|
|
a = QAction(text, self)
|
|
|
|
a.setCheckable(True)
|
|
|
|
a.setChecked(widget.isVisible())
|
|
|
|
a.toggled.connect(widget.setVisible)
|
2016-02-07 00:34:22 +13:00
|
|
|
# widget.installEventFilter(self)
|
2015-07-10 01:01:07 +12:00
|
|
|
b = verticalButton(self)
|
|
|
|
b.setDefaultAction(a)
|
2016-02-29 01:54:11 +13:00
|
|
|
#b.setChecked(widget.isVisible())
|
2015-07-10 01:01:07 +12:00
|
|
|
a2 = self.addWidget(b)
|
|
|
|
self.otherWidgets.append((b, a2, widget, group))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
# def eventFilter(self, widget, event):
|
|
|
|
# if event.type() in [QEvent.Show, QEvent.Hide]:
|
|
|
|
# for btn, action, w, grp in self.otherWidgets:
|
|
|
|
# if w == widget:
|
|
|
|
# btn.defaultAction().setChecked(event.type() == QEvent.Show)
|
|
|
|
# return False
|
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
def setCurrentGroup(self, group):
|
|
|
|
self.currentGroup = group
|
|
|
|
for btn, action, widget, grp in self.otherWidgets:
|
2016-02-07 00:34:22 +13:00
|
|
|
if not grp == group or grp is None:
|
2015-07-10 01:01:07 +12:00
|
|
|
action.setVisible(False)
|
|
|
|
else:
|
|
|
|
action.setVisible(True)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-25 01:42:47 +13:00
|
|
|
def setDockVisibility(self, dock, val):
|
|
|
|
dock.setVisible(val)
|
|
|
|
self._dockToButtonAction[dock].setVisible(val)
|
|
|
|
|
2016-02-29 01:54:11 +13:00
|
|
|
def saveState(self):
|
|
|
|
# We just need to save states of the custom widgets.
|
|
|
|
state = []
|
|
|
|
for btn, act, w, grp in self.otherWidgets:
|
|
|
|
state.append(
|
|
|
|
(grp, btn.text(), btn.isChecked())
|
|
|
|
)
|
|
|
|
return state
|
|
|
|
|
|
|
|
def restoreState(self, state):
|
|
|
|
for group, title, status in state:
|
|
|
|
for btn, act, widget, grp in self.otherWidgets:
|
|
|
|
if group == grp and title == btn.text():
|
|
|
|
btn.setChecked(status)
|
|
|
|
widget.setVisible(status)
|
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
|
|
|
|
class verticalButton(QToolButton):
|
|
|
|
def __init__(self, parent):
|
|
|
|
QToolButton.__init__(self, parent)
|
|
|
|
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Minimum)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-04-09 20:50:55 +12:00
|
|
|
self.setStyleSheet(style.verticalToolButtonSS())
|
2016-04-09 00:49:15 +12:00
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
def sizeHint(self):
|
|
|
|
return QToolButton.sizeHint(self).transposed()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-10 01:01:07 +12:00
|
|
|
def paintEvent(self, event):
|
|
|
|
p = QStylePainter(self)
|
|
|
|
p.rotate(90)
|
|
|
|
p.translate(0, - self.width())
|
|
|
|
opt = QStyleOptionButton()
|
|
|
|
opt.initFrom(self)
|
|
|
|
opt.text = self.text()
|
|
|
|
if self.isChecked():
|
|
|
|
opt.state |= QStyle.State_On
|
|
|
|
s = opt.rect.size().transposed()
|
|
|
|
opt.rect.setSize(s)
|
2016-02-07 00:34:22 +13:00
|
|
|
p.drawControl(QStyle.CE_PushButton, opt)
|