Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore progress bar functionality #654

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions manuskript/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ def wordCount(text):
t = [l for l in t if l]
return len(t)

validate_ok = lambda *args, **kwargs: True
def uiParse(input, default, converter, validator=validate_ok):
"""
uiParse is a utility function that intends to make it easy to convert
user input to data that falls in the range of expected values the
program is expecting to handle.

It swallows all exceptions that happen during conversion.
The validator should return True to permit the converted value.
"""
result = default
try:
result = converter(input)
except:
pass # failed to convert

# Whitelist default value in case default type differs from converter output.
if (result != default) and not validator(result):
result = default
return result


def toInt(text):
if text:
Expand Down Expand Up @@ -50,15 +71,15 @@ def toString(text):

def drawProgress(painter, rect, progress, radius=0):
from manuskript.ui import style as S
progress = toFloat(progress) # handle invalid input (issue #561)
painter.setPen(Qt.NoPen)
painter.setBrush(QColor(S.base)) # "#dddddd"
painter.drawRoundedRect(rect, radius, radius)

painter.setBrush(QBrush(colorFromProgress(progress)))

r2 = QRect(rect)
r2.setWidth(r2.width() * min(toInt(progress), 1))
# ^^^^^ Avoid crash - issue #561
r2.setWidth(r2.width() * min(progress, 1))
painter.drawRoundedRect(r2, radius, radius)


Expand Down
9 changes: 5 additions & 4 deletions manuskript/ui/editors/mainEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from manuskript import settings
from manuskript.enums import Outline
from manuskript.functions import AUC, mainWindow, drawProgress, appPath, toInt
from manuskript.functions import AUC, mainWindow, drawProgress, appPath, uiParse
from manuskript.ui import style
from manuskript.ui.editors.editorWidget import editorWidget
from manuskript.ui.editors.fullScreenEditor import fullScreenEditor
Expand Down Expand Up @@ -303,7 +303,9 @@ def updateStats(self):
wc = item.data(Outline.wordCount)
goal = item.data(Outline.goal)
progress = item.data(Outline.goalPercentage)
# mw = qApp.activeWindow()

goal = uiParse(goal, None, int, lambda x: x>=0)
progress = uiParse(progress, 0.0, float)

if not wc:
wc = 0
Expand All @@ -319,8 +321,7 @@ def updateStats(self):
self.lblRedacProgress.setPixmap(self.px)
self.lblRedacWC.setText(self.tr("{} words / {} ").format(
locale.format_string("%d", wc, grouping=True),
locale.format_string("%d", toInt(goal), grouping=True)))
# ^^^^^ Avoid crash - issue #561
locale.format_string("%d", goal, grouping=True)))
else:
self.lblRedacProgress.hide()
self.lblRedacWC.setText(self.tr("{} words ").format(
Expand Down