Merge branch 'main' into multi_root

This commit is contained in:
Veronica Berglyd Olsen
2022-04-03 22:53:45 +02:00
29 changed files with 481 additions and 419 deletions
+40 -7
View File
@@ -51,6 +51,7 @@ class NWItem():
self._class = nwItemClass.NO_CLASS
self._layout = nwItemLayout.NO_LAYOUT
self._status = None
self._import = None
self._expanded = False
self._exported = True
@@ -109,6 +110,10 @@ class NWItem():
def itemStatus(self):
return self._status
@property
def itemImport(self):
return self._import
@property
def isExpanded(self):
return self._expanded
@@ -165,12 +170,13 @@ class NWItem():
nameAttrib = {}
nameAttrib["status"] = str(self._status)
nameAttrib["import"] = str(self._import)
if self._type == nwItemType.FILE:
nameAttrib["exported"] = str(self._exported)
xPack = etree.SubElement(xParent, "item", attrib=itemAttrib)
self._subPack(xPack, "meta", attrib=metaAttrib)
self._subPack(xPack, "name", text=str(self._name), attrib=nameAttrib)
self._subPack(xPack, "meta", attrib=metaAttrib)
self._subPack(xPack, "name", text=str(self._name), attrib=nameAttrib)
return
@@ -204,11 +210,12 @@ class NWItem():
elif xValue.tag == "name":
self.setName(xValue.text)
self.setStatus(xValue.attrib.get("status", None))
self.setImport(xValue.attrib.get("import", None))
self.setExported(xValue.attrib.get("exported", True))
# Legacy Format (1.3 and earlier)
elif xValue.tag == "status":
self.setStatus(xValue.text)
self.setImportStatus(xValue.text)
elif xValue.tag == "type":
self.setType(xValue.text)
elif xValue.tag == "class":
@@ -275,6 +282,28 @@ class NWItem():
return trConst(nwLabels.ITEM_DESCRIPTION.get(descKey, ""))
def getImportStatus(self):
"""Return the relevant importance or status label and icon for
the current item based on its class.
"""
if self._class in nwLists.CLS_NOVEL:
stName = self.theProject.statusItems.checkEntry(self._status)
stIcon = self.theProject.statusItems.getIcon(stName)
else:
stName = self.theProject.importItems.checkEntry(self._import)
stIcon = self.theProject.importItems.getIcon(stName)
return stName, stIcon
def setImportStatus(self, theLabel):
"""Update the importance or status value based on class. This is
a wrapper setter for setStatus and setImport.
"""
if self._class in nwLists.CLS_NOVEL:
self.setStatus(theLabel)
else:
self.setImport(theLabel)
return
##
# Set Item Values
##
@@ -372,10 +401,14 @@ class NWItem():
"""Set the item status by looking it up in the valid status
items of the current project.
"""
if self._class in nwLists.CLS_NOVEL:
self._status = self.theProject.statusItems.checkEntry(theStatus)
else:
self._status = self.theProject.importItems.checkEntry(theStatus)
self._status = self.theProject.statusItems.checkEntry(theStatus)
return
def setImport(self, theImport):
"""Set the item importance by looking it up in the valid import
items of the current project.
"""
self._import = self.theProject.importItems.checkEntry(theImport)
return
def setExpanded(self, expState):
+7 -7
View File
@@ -46,7 +46,7 @@ from novelwriter.common import (
checkString, checkBool, checkInt, isHandle, formatTimeStamp,
makeFileNameSafe, hexToInt
)
from novelwriter.constants import trConst, nwFiles, nwLabels
from novelwriter.constants import nwLists, trConst, nwFiles, nwLabels
logger = logging.getLogger(__name__)
@@ -1077,7 +1077,7 @@ class NWProject():
"""
replaceMap = self.statusItems.setNewEntries(newCols)
for nwItem in self.projTree:
if nwItem.itemClass == nwItemClass.NOVEL:
if nwItem.itemClass in nwLists.CLS_NOVEL:
if nwItem.itemStatus in replaceMap:
nwItem.setStatus(replaceMap[nwItem.itemStatus])
self.setProjectChanged(True)
@@ -1089,9 +1089,9 @@ class NWProject():
"""
replaceMap = self.importItems.setNewEntries(newCols)
for nwItem in self.projTree:
if nwItem.itemClass != nwItemClass.NOVEL:
if nwItem.itemStatus in replaceMap:
nwItem.setStatus(replaceMap[nwItem.itemStatus])
if nwItem.itemClass not in nwLists.CLS_NOVEL:
if nwItem.itemImport in replaceMap:
nwItem.setImport(replaceMap[nwItem.itemImport])
self.setProjectChanged(True)
return True
@@ -1212,10 +1212,10 @@ class NWProject():
self.statusItems.resetCounts()
self.importItems.resetCounts()
for nwItem in self.projTree:
if nwItem.itemClass == nwItemClass.NOVEL:
if nwItem.itemClass in nwLists.CLS_NOVEL:
self.statusItems.countEntry(nwItem.itemStatus)
else:
self.importItems.countEntry(nwItem.itemStatus)
self.importItems.countEntry(nwItem.itemImport)
return
def localLookup(self, theWord):
+46 -30
View File
@@ -24,9 +24,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import logging
import novelwriter
from lxml import etree
from PyQt5.QtGui import QIcon, QPixmap, QColor
from novelwriter.common import checkInt
logger = logging.getLogger(__name__)
@@ -39,9 +42,11 @@ class NWStatus():
self._theLabels = []
self._theColours = []
self._theCounts = []
self._theIcons = []
self._theMap = {}
self._theLength = 0
self._theIndex = 0
self._iconSize = novelwriter.CONFIG.pxInt(32)
return
@@ -50,38 +55,35 @@ class NWStatus():
a duplicate.
"""
theLabel = theLabel.strip()
if self.lookupEntry(theLabel) is None:
if self._getIndex(theLabel) is None:
theIcon = QPixmap(self._iconSize, self._iconSize)
theIcon.fill(QColor(*theColours))
self._theIcons.append(QIcon(theIcon))
self._theLabels.append(theLabel)
self._theColours.append(theColours)
self._theCounts.append(0)
self._theMap[theLabel] = self._theLength
self._theLength += 1
return True
def lookupEntry(self, theLabel):
"""Look up a status entry in the object lists, and return it if
it exists.
"""
if theLabel is None:
return None
theLabel = theLabel.strip()
if theLabel in self._theMap.keys():
return self._theMap[theLabel]
return None
return True
def checkEntry(self, theStatus):
"""Check if a status value is valid, and returns the safe
reference to be used internally.
"""
if isinstance(theStatus, str):
theStatus = theStatus.strip()
if self.lookupEntry(theStatus) is not None:
return theStatus
theStatus = checkInt(theStatus, 0, False)
if theStatus >= 0 and theStatus < self._theLength:
return self._theLabels[theStatus]
if self._getIndex(theStatus) is not None:
return theStatus.strip()
return self._theLabels[0]
def getIcon(self, theLabel):
"""Return the icon for the given status item.
"""
theIndex = self._getIndex(theLabel)
if theIndex is not None:
return self._theIcons[theIndex]
return QIcon()
def setNewEntries(self, newList):
"""Update the list of entries after they have been modified by
the GUI tool.
@@ -92,6 +94,7 @@ class NWStatus():
self._theLabels = []
self._theColours = []
self._theCounts = []
self._theIcons = []
self._theMap = {}
self._theLength = 0
self._theIndex = 0
@@ -113,7 +116,7 @@ class NWStatus():
"""Increment the counter for a given label. This should be used
together with resetCounts in a loop over project items.
"""
theIndex = self.lookupEntry(theLabel)
theIndex = self._getIndex(theLabel)
if theIndex is not None:
self._theCounts[theIndex] += 1
return
@@ -124,9 +127,9 @@ class NWStatus():
"""
for n in range(self._theLength):
xSub = etree.SubElement(xParent, "entry", attrib={
"blue": str(self._theColours[n][2]),
"green": str(self._theColours[n][1]),
"red": str(self._theColours[n][0]),
"green": str(self._theColours[n][1]),
"blue": str(self._theColours[n][2]),
})
xSub.text = self._theLabels[n]
return True
@@ -145,18 +148,31 @@ class NWStatus():
theColours.append((cR, cG, cB))
if len(theLabels) > 0:
self._theLabels = []
self._theLabels = []
self._theColours = []
self._theCounts = []
self._theMap = {}
self._theLength = 0
self._theIndex = 0
self._theCounts = []
self._theIcons = []
self._theMap = {}
self._theLength = 0
self._theIndex = 0
for n in range(len(theLabels)):
self.addEntry(theLabels[n], theColours[n])
return True
##
# Internal Functions
##
def _getIndex(self, theLabel):
"""Look up a status entry in the object lists, and return it if
it exists.
"""
if theLabel is None:
return None
return self._theMap.get(theLabel.strip(), None)
##
# Iterator Bits
##
@@ -165,8 +181,8 @@ class NWStatus():
"""Return an entry by its index.
"""
if n >= 0 and n < self._theLength:
return self._theLabels[n], self._theColours[n], self._theCounts[n]
return None, None, None
return self._theLabels[n], self._theColours[n], self._theCounts[n], self._theIcons[n]
return None, None, None, QIcon()
def __iter__(self):
"""Initialise the iterator.
@@ -178,9 +194,9 @@ class NWStatus():
"""Return the next entry for the iterator.
"""
if self._theIndex < self._theLength:
theLabel, theColour, theCount = self.__getitem__(self._theIndex)
theLabel, theColour, theCount, theIcon = self.__getitem__(self._theIndex)
self._theIndex += 1
return theLabel, theColour, theCount
return theLabel, theColour, theCount, theIcon
else:
raise StopIteration