Some improvements to the NWItem class, and removal of unused imports of it in the code

This commit is contained in:
Veronica K. B. Olsen
2020-05-07 00:38:25 +02:00
parent 5306b6fd60
commit 8037b3a311
3 changed files with 26 additions and 25 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ from PyQt5.QtWidgets import (
QTreeWidget, QTreeWidgetItem, QAbstractItemView, QApplication, QMessageBox QTreeWidget, QTreeWidgetItem, QAbstractItemView, QApplication, QMessageBox
) )
from nw.project import NWItem, NWDoc from nw.project import NWDoc
from nw.constants import ( from nw.constants import (
nwLabels, nwItemType, nwItemClass, nwItemLayout, nwAlert nwLabels, nwItemType, nwItemClass, nwItemLayout, nwAlert
) )
+1 -1
View File
@@ -45,7 +45,7 @@ from nw.gui import (
GuiConfigEditor, GuiProjectEditor, GuiItemEditor, GuiProjectOutline, GuiConfigEditor, GuiProjectEditor, GuiItemEditor, GuiProjectOutline,
GuiSessionLogView, GuiDocMerge, GuiDocSplit, GuiProjectLoad GuiSessionLogView, GuiDocMerge, GuiDocSplit, GuiProjectLoad
) )
from nw.project import NWProject, NWDoc, NWItem, NWIndex, NWBackup from nw.project import NWProject, NWDoc, NWIndex, NWBackup
from nw.tools import countWords from nw.tools import countWords
from nw.constants import nwFiles, nwItemType, nwAlert from nw.constants import nwFiles, nwItemType, nwAlert
+24 -23
View File
@@ -57,6 +57,21 @@ class NWItem():
self.paraCount = 0 self.paraCount = 0
self.cursorPos = 0 self.cursorPos = 0
# Map of Setters
self._setMap = {
"name" : self.setName,
"order" : self.setOrder,
"type" : self.setType,
"class" : self.setClass,
"layout" : self.setLayout,
"status" : self.setStatus,
"expanded" : self.setExpanded,
"charCount" : self.setCharCount,
"wordCount" : self.setWordCount,
"paraCount" : self.setParaCount,
"cursorPos" : self.setCursorPos,
}
return return
## ##
@@ -64,6 +79,8 @@ class NWItem():
## ##
def packXML(self, xParent): def packXML(self, xParent):
"""Packs all the data in the class instance into an XML object.
"""
xPack = etree.SubElement(xParent,"item",attrib={ xPack = etree.SubElement(xParent,"item",attrib={
"handle" : str(self.itemHandle), "handle" : str(self.itemHandle),
"order" : str(self.itemOrder), "order" : str(self.itemOrder),
@@ -82,7 +99,8 @@ class NWItem():
xSub = self._subPack(xPack,"cursorPos", text=str(self.cursorPos), none=False) xSub = self._subPack(xPack,"cursorPos", text=str(self.cursorPos), none=False)
return xPack return xPack
def _subPack(self, xParent, name, attrib=None, text=None, none=True): @staticmethod
def _subPack(xParent, name, attrib=None, text=None, none=True):
if not none and (text == None or text == "None"): if not none and (text == None or text == "None"):
return None return None
xSub = etree.SubElement(xParent,name,attrib=attrib) xSub = etree.SubElement(xParent,name,attrib=attrib)
@@ -95,29 +113,12 @@ class NWItem():
## ##
def setFromTag(self, tagName, tagValue): def setFromTag(self, tagName, tagValue):
"""Set a value from a given tag name rather than call the set
function directly. Useful when setting data read in from XML.
"""
logger.verbose("Setting tag '%s' to value '%s'" % (tagName, str(tagValue))) logger.verbose("Setting tag '%s' to value '%s'" % (tagName, str(tagValue)))
if tagName == "name": if tagName in self._setMap:
self.setName(tagValue) self._setMap[tagName](tagValue)
elif tagName == "order":
self.setOrder(tagValue)
elif tagName == "type":
self.setType(tagValue)
elif tagName == "class":
self.setClass(tagValue)
elif tagName == "layout":
self.setLayout(tagValue)
elif tagName == "status":
self.setStatus(tagValue)
elif tagName == "expanded":
self.setExpanded(tagValue)
elif tagName == "charCount":
self.setCharCount(tagValue)
elif tagName == "wordCount":
self.setWordCount(tagValue)
elif tagName == "paraCount":
self.setParaCount(tagValue)
elif tagName == "cursorPos":
self.setCursorPos(tagValue)
else: else:
logger.error("Unknown tag '%s'" % tagName) logger.error("Unknown tag '%s'" % tagName)
return return