manuskript/manuskript/ui/collapsibleDockWidgets.py
Curtis Gedak e93c958598 Fixes: Manuskript fails to load last state of panels
See issue #14.

Four panels use custom widgets on the Right-Hand-Side tabs to control
visibility.  These panels are:

  PANEL NAME     VIEWABLE ON NAVIGATION TAB
  ------------   --------------------------
  Book summary   Plots
  Project tree   Redaction
  Metadata       Redaction
  Story line     Redaction

When the custom widget is created, it is assigned a name that is
marked for translation.  The final text name appears to have a
shortcut letter automatically assigned.  For example in English:

  Book summary   ->   B&ook summary
  Project tree   ->   &Project tree
  Metadata       ->   &Metadata
  Story line     ->   Story &line

On restoration the choice to restore state is based on successful
comparison between title and btn.text().  Currently this comparison
fails because title contains "&" and btn.text() does not contain "&".

Fix by removing all ampersand "&" characters from both title and
btn.text() when performing comparison.
2017-08-06 10:14:40 -06:00

133 lines
4.3 KiB
Python

#!/usr/bin/env python
# --!-- coding: utf8 --!--
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QToolBar, QDockWidget, QAction, QToolButton, QSizePolicy, QStylePainter, \
QStyleOptionButton, QStyle
from manuskript.ui import style
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,
}
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)
# self.setAllowedAreas(self.TRANSPOSED_AREA[self._area])
self.parent().addToolBar(self.TRANSPOSED_AREA[self._area], self)
self._dockToButtonAction = {}
# Dock widgets
for d in self._dockWidgets():
b = verticalButton(self)
b.setDefaultAction(d.toggleViewAction())
# d.setStyleSheet("QDockWidget::title{background-color: red;}")
# d.setTitleBarWidget(QLabel(d.windowTitle()))
d.setStyleSheet(style.dockSS())
a = self.addWidget(b)
self._dockToButtonAction[d] = a
self.addSeparator()
# Other widgets
self.otherWidgets = []
self.currentGroup = None
self.setStyleSheet(style.toolBarSS())
def _dockWidgets(self):
mw = self.parent()
for w in mw.findChildren(QDockWidget, None):
yield w
def addCustomWidget(self, text, widget, group=None):
a = QAction(text, self)
a.setCheckable(True)
a.setChecked(widget.isVisible())
a.toggled.connect(widget.setVisible)
# widget.installEventFilter(self)
b = verticalButton(self)
b.setDefaultAction(a)
#b.setChecked(widget.isVisible())
a2 = self.addWidget(b)
self.otherWidgets.append((b, a2, widget, group))
# 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
def setCurrentGroup(self, group):
self.currentGroup = group
for btn, action, widget, grp in self.otherWidgets:
if not grp == group or grp is None:
action.setVisible(False)
else:
action.setVisible(True)
def setDockVisibility(self, dock, val):
dock.setVisible(val)
self._dockToButtonAction[dock].setVisible(val)
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:
# Strip '&' from both title and btn.text() to improve matching because
# title contains "&" shortcut character whereas btn.text() does not.
if group == grp and title.replace('&', '') == btn.text().replace('&', ''):
btn.setChecked(status)
widget.setVisible(status)
class verticalButton(QToolButton):
def __init__(self, parent):
QToolButton.__init__(self, parent)
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Minimum)
self.setStyleSheet(style.verticalToolButtonSS())
def sizeHint(self):
return QToolButton.sizeHint(self).transposed()
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)
p.drawControl(QStyle.CE_PushButton, opt)