Implement navigation via outline tree in editor

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2023-02-20 02:08:38 +01:00
parent 3516242500
commit 80d29b3a68
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
3 changed files with 108 additions and 7 deletions

View file

@ -34,11 +34,21 @@ class EditorView:
self.outlineStore = builder.get_object("outline_store")
self.refreshOutlineStore()
self.outlineView = builder.get_object("outline_view")
self.editorOutlineView = builder.get_object("editor_outline_view")
self.outlineSelection = builder.get_object("outline_selection")
self.editorOutlineSelection = builder.get_object("editor_outline_selection")
self.outlineSelection.connect("changed", self._outlineSelectionChanged)
self.editorOutlineSelection.connect("changed", self._editorOutlineSelectionChanged)
self.viewStack = builder.get_object("view_stack")
self.editorTextBuffer = builder.get_object("editor_text")
self.editorFlowbox = builder.get_object("editor_flowbox")
self.editorFlowbox.connect("selected-children-changed", self._editorFlowboxSelectionChanged)
self.editorFlowbox.connect("child-activated", self._editorFlowboxChildActivated)
self.upButtons = [
@ -128,6 +138,19 @@ class EditorView:
for item in self.project.outline.items:
self.__appendOutlineItem(item)
def __selectOutlineStoreRow(self, row: Gtk.TreeModelRow):
path = row.model.get_path(row.iter)
if not self.outlineView.row_expanded(path):
self.outlineView.expand_to_path(path)
if not self.editorOutlineView.row_expanded(path):
self.editorOutlineView.expand_to_path(path)
if not self.outlineSelection.path_is_selected(path):
self.outlineSelection.select_path(path)
if not self.editorOutlineSelection.path_is_selected(path):
self.editorOutlineSelection.select_path(path)
def loadOutlineData(self, outlineItem: OutlineItem):
if outlineItem is None:
self.unloadOutlineData()
@ -153,7 +176,6 @@ class EditorView:
def unloadOutlineData(self):
self.outlineItem = None
self.loadEditorData(None)
goalKind = self.project.outline.goalKind()
@ -190,7 +212,7 @@ class EditorView:
else:
return False
def loadEditorData(self, outlineItem: OutlineItem | None):
def loadEditorData(self, outlineItem: OutlineItem | None = None):
self.editorItems = list()
self.outlineItem = None
@ -217,6 +239,82 @@ class EditorView:
self.outlineItem = outlineItem
def _outlineSelectionChanged(self, selection: Gtk.TreeSelection):
model, tree_iter = selection.get_selected()
if tree_iter is None:
self.unloadOutlineData()
return
outlineItem = self.project.outline.getItemByID(model[tree_iter][0])
self.loadOutlineData(outlineItem)
def _editorOutlineSelectionChanged(self, selection: Gtk.TreeSelection):
model, tree_iter = selection.get_selected()
if tree_iter is None:
return
outlineItem = self.project.outline.getItemByID(model[tree_iter][0])
def _editorFlowboxSelectionChanged(self, box: Gtk.FlowBox):
children = box.get_selected_children()
child = children[0] if len(children) > 0 else None
if child is None:
self.editorOutlineSelection.unselect_all()
return
index = child.get_index()
if (index < 0) or (index >= len(self.editorItems)):
return
outlineItem = self.editorItems[index]
def selectOutlineItem(model: Gtk.TreeModel, path: Gtk.TreePath, _iter: Gtk.TreeIter, outline_id: int):
if model[_iter][0] != outline_id:
return False
parent_iter = model.iter_parent(_iter)
parent_path = None if parent_iter is None else model.get_path(parent_iter)
if (parent_path is not None) and (not self.editorOutlineView.row_expanded(parent_path)):
self.editorOutlineView.expand_to_path(parent_path)
if not self.editorOutlineSelection.path_is_selected(path):
self.editorOutlineSelection.select_path(path)
return True
self.outlineStore.foreach(selectOutlineItem, outlineItem.UID.value)
def __openOutlineItem(self, outlineItem: OutlineItem | None):
if outlineItem is None:
self.outlineSelection.unselect_all()
self.editorOutlineSelection.unselect_all()
return
def selectOutlineItem(model: Gtk.TreeModel, path: Gtk.TreePath, _iter: Gtk.TreeIter, outline_id: int):
if model[_iter][0] != outline_id:
return False
if not self.editorOutlineView.row_expanded(path):
self.editorOutlineView.expand_to_path(path)
if not self.outlineView.row_expanded(path):
self.outlineView.expand_to_path(path)
if not self.outlineSelection.path_is_selected(path):
self.outlineSelection.select_path(path)
if not self.editorOutlineSelection.path_is_selected(path):
self.editorOutlineSelection.select_path(path)
return True
self.outlineStore.foreach(selectOutlineItem, outlineItem.UID.value)
def _editorFlowboxChildActivated(self, box: Gtk.FlowBox, child: Gtk.FlowBoxChild):
if child is None:
return
@ -225,13 +323,13 @@ class EditorView:
if (index < 0) or (index >= len(self.editorItems)):
return
self.loadOutlineData(self.editorItems[index])
self.__openOutlineItem(self.editorItems[index])
def _upButtonClicked(self, button: Gtk.Button):
if self.outlineItem is None:
return
self.loadOutlineData(self.outlineItem.parentItem())
self.__openOutlineItem(self.outlineItem.parentItem())
def show(self):
self.widget.show_all()

View file

@ -221,7 +221,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkTreeView">
<object class="GtkTreeView" id="outline_view">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">outline_store</property>
@ -398,6 +398,7 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
<object class="GtkFlowBox" id="editor_flowbox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="valign">start</property>
<property name="homogeneous">True</property>
<property name="activate-on-single-click">False</property>
</object>
@ -421,13 +422,13 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkTreeView">
<object class="GtkTreeView" id="editor_outline_view">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">outline_store</property>
<property name="enable-tree-lines">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
<object class="GtkTreeSelection" id="editor_outline_selection"/>
</child>
<child>
<object class="GtkTreeViewColumn">

View file

@ -83,6 +83,8 @@ along with Manuskript. If not, see <http://www.gnu.org/licenses/>.
</child>
<child>
<object class="GtkLabel" id="summary">
<property name="width-request">150</property>
<property name="height-request">50</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="wrap">True</property>