Skip to content

Commit

Permalink
Fixes: Some panels require initial two clicks of RHS tab to hide oliv…
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierkes authored and gedakc committed Oct 20, 2017
1 parent 3b1a3f7 commit d95c3f8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 19 deletions.
97 changes: 85 additions & 12 deletions manuskript/mainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from PyQt5.QtCore import pyqtSignal, QSignalMapper, QTimer, QSettings, Qt, QRegExp, QUrl, QSize
from PyQt5.QtGui import QStandardItemModel, QIcon, QColor
from PyQt5.QtWidgets import QMainWindow, QHeaderView, qApp, QMenu, QActionGroup, QAction, QStyle, QListWidgetItem, \
QLabel
QLabel, QDockWidget

from manuskript import settings
from manuskript.enums import Character, PlotStep, Plot, World, Outline
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(self):

# Welcome
self.welcome.updateValues()
self.stack.setCurrentIndex(0)
self.switchToWelcome()

# Word count
self.mprWordCount = QSignalMapper(self)
Expand Down Expand Up @@ -124,6 +124,61 @@ def __init__(self):
self.makeUIConnections()

# self.loadProject(os.path.join(appPath(), "test_project.zip"))

def updateDockVisibility(self, restore=False):
"""
Saves the state of the docks visibility. Or if `restore` is True,
restores from `self._dckVisibility`. This allows to hide the docks
while showing the welcome screen, and then restore them as they
were.
If `self._dckVisibility` contains "LOCK", then we don't override values
with current visibility state. This is used the first time we load.
"LOCK" is then removed.
"""
docks = [
self.dckCheatSheet,
self.dckNavigation,
self.dckSearch,
]

for d in docks:
if not restore:
# We store the values, but only if "LOCK" is not present
if not "LOCK" in self._dckVisibility:
self._dckVisibility[d.objectName()] = d.isVisible()
# Hide the dock
d.setVisible(False)
else:
# Restore the dock's visibily based on stored value
d.setVisible(self._dckVisibility[d.objectName()])

# Lock is used only once, at start up. We can remove it
if "LOCK" in self._dckVisibility:
self._dckVisibility.pop("LOCK")

def switchToWelcome(self):
"""
While switching to welcome screen, we have to hide all the docks.
Otherwise one could use the search dock, and manuskript would crash.
Plus it's unncessary distraction.
But we also want to restore them to their visibility prior to switching,
so we store states.
"""
# Stores the state of docks
self.updateDockVisibility()
# Hides the toolbar
self.toolbar.setVisible(False)
# Switch to welcome screen
self.stack.setCurrentIndex(0)

def switchToProject(self):
"""Restores docks and toolbar visibility, and switch to project."""
# Restores the docks visibility
self.updateDockVisibility(restore=True)
# Show the toolbar
self.toolbar.setVisible(True)
self.stack.setCurrentIndex(1)

###############################################################################
# SUMMARY
Expand Down Expand Up @@ -406,7 +461,7 @@ def loadProject(self, project, loadFromFile=True):
QSettings().setValue("lastProject", project)

# Show main Window
self.stack.setCurrentIndex(1)
self.switchToProject()

def closeProject(self):

Expand Down Expand Up @@ -444,7 +499,7 @@ def closeProject(self):
self.welcome.updateValues()

# Show welcome dialog
self.stack.setCurrentIndex(0)
self.switchToWelcome()

def readSettings(self):
# Load State and geometry
Expand All @@ -453,9 +508,21 @@ def readSettings(self):
self.restoreGeometry(sttgns.value("geometry"))
if sttgns.contains("windowState"):
self.restoreState(sttgns.value("windowState"))

if sttgns.contains("docks"):
self._dckVisibility = {}
vals = sttgns.value("docks")
for name in vals:
self._dckVisibility[name] = vals[name]
else:
self.dckCheatSheet.hide()
self.dckSearch.hide()
# Create default settings
self._dckVisibility = {
self.dckNavigation.objectName() : True,
self.dckCheatSheet.objectName() : False,
self.dckSearch.objectName() : False,
}
self._dckVisibility["LOCK"] = True # prevent overiding loaded values

if sttgns.contains("metadataState"):
state = [False if v == "false" else True for v in sttgns.value("metadataState")]
self.redacMetadata.restoreState(state)
Expand All @@ -472,7 +539,6 @@ def readSettings(self):
else:
self._toolbarState = ""


def closeEvent(self, event):
# Save State and geometry and other things
sttgns = QSettings(qApp.organizationName(), qApp.applicationName())
Expand All @@ -483,7 +549,14 @@ def closeEvent(self, event):
sttgns.setValue("splitterRedacH", self.splitterRedacH.saveState())
sttgns.setValue("splitterRedacV", self.splitterRedacV.saveState())
sttgns.setValue("toolbar", self.toolbar.saveState())


# If we are not in the welcome window, we update the visibility
# of the docks widgets
if self.stack.currentIndex() == 1:
self.updateDockVisibility()
# Storing the visibility of docks to restore it on restart
sttgns.setValue("docks", self._dckVisibility)

# Specific settings to save before quitting
settings.lastTab = self.tabMain.currentIndex()

Expand Down Expand Up @@ -883,10 +956,10 @@ def setupMoreUi(self):

# Tool bar on the right
self.toolbar = collapsibleDockWidgets(Qt.RightDockWidgetArea, self)
self.toolbar.addCustomWidget(self.tr("Book summary"), self.grpPlotSummary, self.TabPlots)
self.toolbar.addCustomWidget(self.tr("Project tree"), self.treeRedacWidget, self.TabRedac)
self.toolbar.addCustomWidget(self.tr("Metadata"), self.redacMetadata, self.TabRedac)
self.toolbar.addCustomWidget(self.tr("Story line"), self.storylineView, self.TabRedac)
self.toolbar.addCustomWidget(self.tr("Book summary"), self.grpPlotSummary, self.TabPlots, False)
self.toolbar.addCustomWidget(self.tr("Project tree"), self.treeRedacWidget, self.TabRedac, True)
self.toolbar.addCustomWidget(self.tr("Metadata"), self.redacMetadata, self.TabRedac, False)
self.toolbar.addCustomWidget(self.tr("Story line"), self.storylineView, self.TabRedac, False)
if self._toolbarState:
self.toolbar.restoreState(self._toolbarState)

Expand Down
26 changes: 23 additions & 3 deletions manuskript/ui/collapsibleDockWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,26 @@ def _dockWidgets(self):
for w in mw.findChildren(QDockWidget, None):
yield w

def addCustomWidget(self, text, widget, group=None):
def addCustomWidget(self, text, widget, group=None, defaultVisibility=True):
"""
Adds a custom widget to the toolbar.
`text` is the name that will displayed on the button to switch visibility.
`widget` is the widget to control from the toolbar.
`group` is an integer (or any hashable) if the current widget should not
be displayed all the time. Call `collapsibleDockWidgets.setCurrentGroup`
to switch to that group and hide other widgets.
`defaultVisibility` is the default visibility of the item when it is added.
This allows for the widget to be added to `collapsibleDockWidgets` after
they've been created but before they are shown, and yet specify their
desired visibility. Otherwise it creates troubes, see #167 on github:
https://github.com/olivierkes/manuskript/issues/167.
"""
a = QAction(text, self)
a.setCheckable(True)
a.setChecked(widget.isVisible())
a.setChecked(defaultVisibility)
a.toggled.connect(widget.setVisible)
widget.setVisible(defaultVisibility)
# widget.installEventFilter(self)
b = verticalButton(self)
b.setDefaultAction(a)
Expand Down Expand Up @@ -90,7 +105,10 @@ def setDockVisibility(self, dock, val):
self._dockToButtonAction[dock].setVisible(val)

def saveState(self):
# We just need to save states of the custom widgets.
"""
Saves and returns the state of the custom widgets. The visibility of the
docks is not saved since it is included in `QMainWindow.saveState`.
"""
state = []
for btn, act, w, grp in self.otherWidgets:
state.append(
Expand All @@ -99,12 +117,14 @@ def saveState(self):
return state

def restoreState(self, state):
"""Restores the state of the custom widgets."""
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)
btn.defaultAction().setChecked(status)
widget.setVisible(status)


Expand Down
5 changes: 1 addition & 4 deletions manuskript/ui/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ def showEvent(self, event):

def getAutoLoadValues(self):
sttgns = QSettings()
if sttgns.contains("autoLoad"):
autoLoad = True if sttgns.value("autoLoad") in ["true", True] else False
else:
autoLoad = False
autoLoad = sttgns.value("autoLoad", type=bool)
if autoLoad and sttgns.contains("lastProject"):
last = sttgns.value("lastProject")
else:
Expand Down

0 comments on commit d95c3f8

Please sign in to comment.