diff --git a/manuskript/data/outline.py b/manuskript/data/outline.py index 1ce3e9f6..5f7611c9 100644 --- a/manuskript/data/outline.py +++ b/manuskript/data/outline.py @@ -101,6 +101,9 @@ class OutlineItem: def textCount(self, counterKind: CounterKind = None) -> int: return 0 + def goalKind(self) -> CounterKind: + return CounterKind.WORDS if self.goal is None else self.goal.kind + def goalCount(self) -> int: return 0 if self.goal is None else self.goal.value @@ -120,7 +123,7 @@ class OutlineText(OutlineItem): def textCount(self, counterKind: CounterKind = None) -> int: if counterKind is None: - counterKind = CounterKind.WORDS if self.goal is None else self.goal.kind + counterKind = self.goalKind() return super().textCount(counterKind) + countText(self.text, counterKind) @@ -187,7 +190,7 @@ class OutlineFolder(OutlineItem): def textCount(self, counterKind: CounterKind = None) -> int: if counterKind is None: - counterKind = CounterKind.WORDS if self.goal is None else self.goal.kind + counterKind = self.goalKind() count = super().textCount(counterKind) @@ -261,6 +264,29 @@ class Outline: return result + def textCount(self, counterKind: CounterKind = None) -> int: + if counterKind is None: + counterKind = self.goalKind() + + count = 0 + for item in self.items: + count += item.textCount(counterKind) + + return count + + def goalKind(self) -> CounterKind: + if len(self.items) > 0: + return self.items[0].goalKind() + + return CounterKind.WORDS + + def goalCount(self) -> int: + count = 0 + for item in self.items: + count += item.goalCount() + + return count + def load(self): self.items.clear() diff --git a/manuskript/ui/views/editorView.py b/manuskript/ui/views/editorView.py index 055c3990..fe7ecdd8 100644 --- a/manuskript/ui/views/editorView.py +++ b/manuskript/ui/views/editorView.py @@ -6,10 +6,10 @@ import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Pango -from manuskript.data import Project, OutlineFolder, OutlineText, OutlineItem, OutlineState +from manuskript.data import Project, OutlineFolder, OutlineText, OutlineItem, OutlineState, Goal from manuskript.ui.editor import GridItem from manuskript.ui.util import pixbufFromColor, iconByOutlineItemType -from manuskript.util import validString, validInt +from manuskript.util import validString, validInt, safeFraction import inspect @@ -19,6 +19,7 @@ class EditorView: def __init__(self, project: Project): self.project = project self.outlineItem = None + self.editorItems = list() builder = Gtk.Builder() builder.add_from_file("ui/editor.glade") @@ -34,7 +35,7 @@ class EditorView: self.outlineStore = builder.get_object("outline_store") self.refreshOutlineStore() - self.editorItems = list() + self.viewStack = builder.get_object("view_stack") self.editorTextBuffer = builder.get_object("editor_text") self.editorFlowbox = builder.get_object("editor_flowbox") @@ -49,6 +50,9 @@ class EditorView: for button in self.upButtons: button.connect("clicked", self.upButtonClicked) + self.counterLabel = builder.get_object("counter") + self.counterProgressBar = builder.get_object("counter_progress") + self.unloadOutlineData() def refreshLabelStore(self): @@ -115,10 +119,26 @@ class EditorView: self.__appendOutlineItem(item) def loadOutlineData(self, outlineItem: OutlineItem): - self.outlineItem = None + if outlineItem is None: + self.unloadOutlineData() + return + self.outlineItem = None self.loadEditorData(outlineItem) + if type(outlineItem) is OutlineText: + self.viewStack.set_visible_child_name("page_text") + else: + self.viewStack.set_visible_child_name("page_stack") + + goalKind = outlineItem.goalKind() + textCount = outlineItem.textCount(goalKind) + goalCount = outlineItem.goalCount() + + self.counterLabel.set_text("{0} {1}".format(textCount, goalKind.name.lower())) + self.counterProgressBar.set_text("{0} / {1} {2}".format(textCount, goalCount, goalKind.name.lower())) + self.counterProgressBar.set_fraction(safeFraction(textCount, 0, goalCount)) + self.outlineItem = outlineItem def unloadOutlineData(self): @@ -126,6 +146,14 @@ class EditorView: self.loadEditorData(None) + goalKind = self.project.outline.goalKind() + textCount = self.project.outline.textCount(goalKind) + goalCount = self.project.outline.goalCount() + + self.counterLabel.set_text("{0} {1}".format(textCount, goalKind.name.lower())) + self.counterProgressBar.set_text("{0} / {1} {2}".format(textCount, goalCount, goalKind.name.lower())) + self.counterProgressBar.set_fraction(safeFraction(textCount, 0, goalCount)) + def __appendOutlineItemText(self, outlineItem: OutlineItem): end_iter = self.editorTextBuffer.get_end_iter() @@ -187,7 +215,7 @@ class EditorView: if (index < 0) or (index >= len(self.editorItems)): return - self.loadEditorData(self.editorItems[index]) + self.loadOutlineData(self.editorItems[index]) def upButtonClicked(self, button: Gtk.Button): if self.outlineItem is None: diff --git a/manuskript/util/__init__.py b/manuskript/util/__init__.py index 6f3dcf16..c5669d5a 100644 --- a/manuskript/util/__init__.py +++ b/manuskript/util/__init__.py @@ -61,3 +61,11 @@ def countText(text: str, kind: CounterKind = CounterKind.WORDS): return PageCounter.count(text) else: return 0 + +def safeFraction(value, low, high) -> float: + if value < low: + return 0.0 + elif value > high: + return 1.0 + else: + return 1.0 * (value - low) / (high - low) diff --git a/ui/editor.glade b/ui/editor.glade index 233526b8..5476cf29 100644 --- a/ui/editor.glade +++ b/ui/editor.glade @@ -347,7 +347,7 @@ along with Manuskript. If not, see . - + True False @@ -381,8 +381,7 @@ along with Manuskript. If not, see . - page0 - page0 + page_text view-continuous-symbolic @@ -400,14 +399,14 @@ along with Manuskript. If not, see . True False True + False - page1 - page1 + page_grid view-grid-symbolic 1 @@ -547,8 +546,7 @@ along with Manuskript. If not, see . - page2 - page2 + page_tree view-list-symbolic 2 @@ -601,7 +599,7 @@ along with Manuskript. If not, see . - + True False center @@ -642,8 +640,7 @@ along with Manuskript. If not, see . - page0 - page0 + page_stack @@ -664,6 +661,9 @@ along with Manuskript. If not, see . True True + word-char + editor_text + GTK_INPUT_HINT_SPELLCHECK | GTK_INPUT_HINT_WORD_COMPLETION | GTK_INPUT_HINT_NONE @@ -724,7 +724,7 @@ along with Manuskript. If not, see . - + True False 8 @@ -748,8 +748,7 @@ along with Manuskript. If not, see . - page1 - page1 + page_text 1