Add back extra column content

This commit is contained in:
Veronica Berglyd Olsen
2025-03-23 16:41:40 +01:00
parent 9d63a018e8
commit 95d8e3ac98
7 changed files with 113 additions and 301 deletions
+7 -6
View File
@@ -87,7 +87,7 @@ class Index:
# Storage and State
self._tagsIndex = TagsIndex()
self._itemIndex = ItemIndex(project)
self._itemIndex = ItemIndex(project, self._tagsIndex)
self._indexBroken = False
# Models
@@ -906,10 +906,11 @@ class ItemIndex:
IndexHeading object for each heading of the text.
"""
__slots__ = ("_project", "_items")
__slots__ = ("_project", "_tags", "_items")
def __init__(self, project: NWProject) -> None:
def __init__(self, project: NWProject, tagsIndex: TagsIndex) -> None:
self._project = project
self._tags = tagsIndex
self._items: dict[str, IndexNode] = {}
return
@@ -936,7 +937,7 @@ class ItemIndex:
"""Add a new item to the index. This will overwrite the item if
it already exists.
"""
self._items[tHandle] = IndexNode(tHandle, nwItem)
self._items[tHandle] = IndexNode(self._tags, tHandle, nwItem)
return
def allItemTags(self, tHandle: str) -> list[str]:
@@ -994,7 +995,7 @@ class ItemIndex:
if tHandle in self._items:
tItem = self._items[tHandle]
sTitle = tItem.nextHeading()
tItem.addHeading(IndexHeading(sTitle, lineNo, level, text))
tItem.addHeading(IndexHeading(self._tags, sTitle, lineNo, level, text))
return sTitle
return TT_NONE
@@ -1065,7 +1066,7 @@ class ItemIndex:
nwItem = self._project.tree[tHandle]
if nwItem is not None:
tItem = IndexNode(tHandle, nwItem)
tItem = IndexNode(self._tags, tHandle, nwItem)
tItem.unpackData(tData)
self._items[tHandle] = tItem
+36 -6
View File
@@ -36,6 +36,7 @@ from novelwriter.common import checkInt, isListInstance, isTitleTag
from novelwriter.constants import nwKeyWords, nwStyles
if TYPE_CHECKING: # pragma: no cover
from novelwriter.core.index import TagsIndex
from novelwriter.core.item import NWItem
logger = logging.getLogger(__name__)
@@ -56,12 +57,13 @@ class IndexNode:
must be reset each time the item is re-indexed.
"""
__slots__ = ("_handle", "_item", "_headings", "_count", "_notes")
__slots__ = ("_tags", "_handle", "_item", "_headings", "_notes", "_count")
def __init__(self, tHandle: str, nwItem: NWItem) -> None:
def __init__(self, tagsIndex: TagsIndex, tHandle: str, nwItem: NWItem) -> None:
self._tags = tagsIndex
self._handle = tHandle
self._item = nwItem
self._headings: dict[str, IndexHeading] = {TT_NONE: IndexHeading(TT_NONE)}
self._headings: dict[str, IndexHeading] = {TT_NONE: IndexHeading(self._tags, TT_NONE)}
self._notes: dict[str, set[str]] = {}
self._count = 0
return
@@ -179,7 +181,7 @@ class IndexNode:
"""Unpack an item entry from the data."""
for key, entry in data.items():
if isTitleTag(key):
heading = IndexHeading(key)
heading = IndexHeading(self._tags, key)
heading.unpackData(entry)
self.addHeading(heading)
elif key == "document":
@@ -202,9 +204,16 @@ class IndexHeading:
of all references made under the heading.
"""
__slots__ = ("_key", "_line", "_level", "_title", "_counts", "_tag", "_refs", "_comments")
__slots__ = (
"_tags", "_key", "_line", "_level", "_title",
"_counts", "_tag", "_refs", "_comments",
)
def __init__(self, key: str, line: int = 0, level: str = "H0", title: str = "") -> None:
def __init__(
self, tagsIndex: TagsIndex, key: str, line: int = 0,
level: str = "H0", title: str = "",
) -> None:
self._tags = tagsIndex
self._key = key
self._line = line
self._level = level
@@ -314,6 +323,27 @@ class IndexHeading:
self._refs[tag].add(keyword)
return
##
# Getters
##
def getReferences(self) -> dict[str, list[str]]:
"""Extract all references for this heading."""
refs = {x: [] for x in nwKeyWords.VALID_KEYS}
for tag, types in self._refs.items():
for keyword in types:
if keyword in refs:
refs[keyword].append(self._tags.tagName(tag))
return refs
def getReferencesByKeyword(self, keyword: str) -> list[str]:
"""Extract all references for this heading."""
refs = []
for tag, types in self._refs.items():
if keyword in types:
refs.append(self._tags.tagName(tag))
return refs
##
# Data Methods
##
+17 -9
View File
@@ -30,7 +30,7 @@ from PyQt6.QtCore import QAbstractTableModel, QModelIndex, Qt
from PyQt6.QtGui import QIcon, QPixmap
from novelwriter import SHARED
from novelwriter.constants import nwKeyWords, nwStyles
from novelwriter.constants import nwKeyWords, nwLabels, nwStyles, trConst
from novelwriter.core.indexdata import IndexHeading, IndexNode
from novelwriter.enum import nwNovelExtra
from novelwriter.error import logException
@@ -52,7 +52,7 @@ T_NodeData = str | tuple[str, str] | QIcon | QPixmap | Qt.AlignmentFlag | None
class NovelModel(QAbstractTableModel):
__slots__ = ("_rows", "_header", "_more", "_columns", "_extra")
__slots__ = ("_rows", "_header", "_more", "_columns", "_extraKey", "_extraLabel")
def __init__(self) -> None:
super().__init__()
@@ -60,7 +60,8 @@ class NovelModel(QAbstractTableModel):
self._header: list[QSize] = []
self._more = SHARED.theme.getIcon("more_arrow")
self._columns = 3
self._extra = ""
self._extraKey = ""
self._extraLabel = ""
return
def __del__(self) -> None: # pragma: no cover
@@ -85,16 +86,20 @@ class NovelModel(QAbstractTableModel):
match extra:
case nwNovelExtra.HIDDEN:
self._columns = 3
self._extra = ""
self._extraKey = ""
self._extraLabel = ""
case nwNovelExtra.POV:
self._columns = 4
self._extra = nwKeyWords.POV_KEY
self._extraKey = nwKeyWords.POV_KEY
self._extraLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.POV_KEY])
case nwNovelExtra.FOCUS:
self._columns = 4
self._extra = nwKeyWords.FOCUS_KEY
self._extraKey = nwKeyWords.FOCUS_KEY
self._extraLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.FOCUS_KEY])
case nwNovelExtra.PLOT:
self._columns = 4
self._extra = nwKeyWords.PLOT_KEY
self._extraKey = nwKeyWords.PLOT_KEY
self._extraLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.PLOT_KEY])
return
##
@@ -209,6 +214,7 @@ class NovelModel(QAbstractTableModel):
"""Generate a cache entry."""
iLevel = nwStyles.H_LEVEL.get(heading.level, 0)
data = {}
data[C_FACTOR*0 | R_TIP] = heading.title
data[C_FACTOR*0 | R_TEXT] = heading.title
data[C_FACTOR*0 | R_ICON] = SHARED.theme.getHeaderDecoration(iLevel)
data[C_FACTOR*1 | R_TEXT] = f"{heading.mainCount:n}"
@@ -216,8 +222,10 @@ class NovelModel(QAbstractTableModel):
if self._columns == 3:
data[C_FACTOR*2 | R_ICON] = self._more
else:
data[C_FACTOR*2 | R_TIP] = "Hello World"
data[C_FACTOR*2 | R_TEXT] = "Hello World"
if self._extraKey and (refs := heading.getReferencesByKeyword(self._extraKey)):
text = ", ".join(refs)
data[C_FACTOR*2 | R_TEXT] = text
data[C_FACTOR*2 | R_TIP] = f"<b>{self._extraLabel}:</b> {text}"
data[C_FACTOR*3 | R_ICON] = self._more
data[R_HANDLE] = handle
data[R_KEY] = key