Keep project tree and outline tree in sync

This commit is contained in:
Veronica K. B. Olsen
2021-01-02 17:33:53 +01:00
parent 4d7f925c5a
commit 21d91193a2
3 changed files with 59 additions and 9 deletions
+5 -5
View File
@@ -165,7 +165,7 @@ class GuiOutline(QTreeWidget):
return
def refreshTree(self, overRide=False):
def refreshTree(self, overRide=False, novelChanged=False):
"""Called whenever the Outline tab is activated and controls
what data to load, and if necessary, force a rebuild of the
tree.
@@ -177,10 +177,10 @@ class GuiOutline(QTreeWidget):
self.firstView = False
return
# If the novel index has changed since the tree was last built,
# we rebuild the tree from the updated index.
idxChanged = self.theParent.theIndex.novelChangedSince(self.lastBuild)
doBuild = idxChanged and self.theProject.autoOutline
# If the novel index or novel tree has changed since the tree
# was last built, we rebuild the tree from the updated index.
indexChanged = self.theIndex.novelChangedSince(self.lastBuild)
doBuild = (novelChanged or indexChanged) and self.theProject.autoOutline
if doBuild or overRide:
logger.debug("Rebuilding Project Outline")
self._populateTree()
+27 -2
View File
@@ -31,7 +31,7 @@ import logging
from time import time
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtCore import Qt, QSize, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
qApp, QTreeWidget, QTreeWidgetItem, QAbstractItemView, QMenu, QAction
@@ -51,6 +51,9 @@ class GuiProjectTree(QTreeWidget):
C_EXPORT = 2
C_FLAGS = 3
novelItemChanged = pyqtSignal()
noteItemChanged = pyqtSignal()
def __init__(self, theParent):
QTreeWidget.__init__(self, theParent)
@@ -285,8 +288,11 @@ class GuiProjectTree(QTreeWidget):
pHandle = nwItem.itemParent
if pHandle is not None and pHandle in self._treeMap:
self._treeMap[pHandle].setExpanded(True)
self._emitItemChange(tHandle)
self.clearSelection()
trItem.setSelected(True)
return True
def moveTreeItem(self, nStep):
@@ -327,6 +333,7 @@ class GuiProjectTree(QTreeWidget):
self.clearSelection()
cItem.setSelected(True)
self._setTreeChanged(True)
self._emitItemChange(tHandle)
return True
@@ -788,6 +795,10 @@ class GuiProjectTree(QTreeWidget):
else:
self.theIndex.reIndexHandle(sHandle)
# Trigger dependent updates
self._setTreeChanged(True)
self._emitItemChange(sHandle)
else:
theEvent.ignore()
logger.debug("Drag'n'drop of item %s not accepted" % sHandle)
@@ -915,7 +926,6 @@ class GuiProjectTree(QTreeWidget):
pHandle = trItemP.data(self.C_NAME, Qt.UserRole)
nwItemS.setParent(pHandle)
self.setTreeItemValues(tHandle)
self._setTreeChanged(True)
logger.debug("The parent of item %s has been changed to %s" % (tHandle, pHandle))
@@ -930,6 +940,21 @@ class GuiProjectTree(QTreeWidget):
self.theProject.setProjectChanged(True)
return
def _emitItemChange(self, tHandle):
"""Emit an item change signal for a given handle.
"""
nwItem = self.theProject.projTree[tHandle]
if nwItem is None:
return
if nwItem.itemType == nwItemType.FILE:
if nwItem.itemClass == nwItemClass.NOVEL:
self.novelItemChanged.emit()
else:
self.noteItemChanged.emit()
return
# END Class GuiProjectTree
class GuiProjectTreeMenu(QMenu):