Improve outline handling in editor view and implemented visual counter update

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2022-11-20 00:51:05 +01:00
parent 7fd205cd7f
commit 1b436a3af8
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
4 changed files with 81 additions and 20 deletions

View file

@ -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()

View file

@ -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:

View file

@ -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)

View file

@ -347,7 +347,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</packing>
</child>
<child>
<object class="GtkStack">
<object class="GtkStack" id="view_stack">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
@ -381,8 +381,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</child>
</object>
<packing>
<property name="name">page0</property>
<property name="title" translatable="yes">page0</property>
<property name="name">page_text</property>
<property name="icon-name">view-continuous-symbolic</property>
</packing>
</child>
@ -400,14 +399,14 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="homogeneous">True</property>
<property name="activate-on-single-click">False</property>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="name">page1</property>
<property name="title" translatable="yes">page1</property>
<property name="name">page_grid</property>
<property name="icon-name">view-grid-symbolic</property>
<property name="position">1</property>
</packing>
@ -547,8 +546,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</child>
</object>
<packing>
<property name="name">page2</property>
<property name="title" translatable="yes">page2</property>
<property name="name">page_tree</property>
<property name="icon-name">view-list-symbolic</property>
<property name="position">2</property>
</packing>
@ -601,7 +599,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</packing>
</child>
<child>
<object class="GtkProgressBar">
<object class="GtkProgressBar" id="counter_progress">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="valign">center</property>
@ -642,8 +640,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</child>
</object>
<packing>
<property name="name">page0</property>
<property name="title" translatable="yes">page0</property>
<property name="name">page_stack</property>
</packing>
</child>
<child>
@ -664,6 +661,9 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
<object class="GtkTextView">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="wrap-mode">word-char</property>
<property name="buffer">editor_text</property>
<property name="input-hints">GTK_INPUT_HINT_SPELLCHECK | GTK_INPUT_HINT_WORD_COMPLETION | GTK_INPUT_HINT_NONE</property>
</object>
</child>
</object>
@ -724,7 +724,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</packing>
</child>
<child>
<object class="GtkLabel">
<object class="GtkLabel" id="counter">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">8</property>
@ -748,8 +748,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</child>
</object>
<packing>
<property name="name">page1</property>
<property name="title" translatable="yes">page1</property>
<property name="name">page_text</property>
<property name="position">1</property>
</packing>
</child>