Proagate word counts and save them for files.
This commit is contained in:
+8
-4
@@ -34,7 +34,7 @@ class GuiDocEditor(QWidget):
|
||||
self.theParent = theParent
|
||||
self.charCount = 0
|
||||
self.wordCount = 0
|
||||
self.lineCount = 0
|
||||
self.paraCount = 0
|
||||
self.lastEdit = 0
|
||||
|
||||
self.outerBox = QVBoxLayout()
|
||||
@@ -183,11 +183,15 @@ class GuiDocEditor(QWidget):
|
||||
def _updateCounts(self):
|
||||
"""Slot for the word counter's finished signal
|
||||
"""
|
||||
logger.verbose("Updating word counts")
|
||||
logger.verbose("Updating word count")
|
||||
|
||||
tHandle = self.theParent.theDocument.docHandle
|
||||
self.charCount = self.wCounter.charCount
|
||||
self.wordCount = self.wCounter.wordCount
|
||||
self.theParent.statusBar.setCharCount(self.charCount)
|
||||
self.theParent.statusBar.setWordCount(self.wordCount)
|
||||
self.paraCount = self.wCounter.paraCount
|
||||
self.theParent.statusBar.setCounts(self.charCount,self.wordCount,self.paraCount)
|
||||
self.theParent.treeView.propagateCount(tHandle, self.wordCount)
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
|
||||
+19
-5
@@ -15,10 +15,11 @@ import nw
|
||||
|
||||
from os import path
|
||||
from PyQt5.QtGui import QIcon
|
||||
from PyQt5.QtCore import QSize
|
||||
from PyQt5.QtCore import Qt, QSize
|
||||
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView
|
||||
|
||||
from nw.enum import nwItemType, nwItemClass
|
||||
from nw.project.item import NWItem
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -33,12 +34,11 @@ class GuiDocTree(QTreeWidget):
|
||||
self.theProject = theProject
|
||||
self.theMap = {}
|
||||
|
||||
self.setStyleSheet("QTreeWidget {font-size: 13px;}")
|
||||
self.setIconSize(QSize(13,13))
|
||||
self.setExpandsOnDoubleClick(True)
|
||||
self.setIndentation(13)
|
||||
self.setColumnCount(4)
|
||||
self.setHeaderLabels(["Name","","","Handle"])
|
||||
self.setHeaderLabels(["Name","S","#","Handle"])
|
||||
if not self.debugGUI:
|
||||
self.hideColumn(3)
|
||||
|
||||
@@ -134,15 +134,16 @@ class GuiDocTree(QTreeWidget):
|
||||
tHandle = nwItem.itemHandle
|
||||
pHandle = nwItem.parHandle
|
||||
tStatus = 0
|
||||
wCount = 0
|
||||
newItem = QTreeWidgetItem([
|
||||
tName, str(tStatus), str(wCount), tHandle
|
||||
tName, str(tStatus), "0", tHandle
|
||||
])
|
||||
self.theMap[tHandle] = newItem
|
||||
if pHandle is None:
|
||||
self.addTopLevelItem(newItem)
|
||||
else:
|
||||
self.theMap[pHandle].addChild(newItem)
|
||||
self.propagateCount(tHandle, nwItem.wordCount)
|
||||
newItem.setTextAlignment(2,Qt.AlignRight)
|
||||
newItem.setExpanded(nwItem.isExpanded)
|
||||
if nwItem.itemType == nwItemType.ROOT:
|
||||
newItem.setIcon(0, QIcon.fromTheme("drive-harddisk"))
|
||||
@@ -152,6 +153,19 @@ class GuiDocTree(QTreeWidget):
|
||||
newItem.setIcon(0, QIcon.fromTheme("x-office-document"))
|
||||
return True
|
||||
|
||||
def propagateCount(self, tHandle, theCount, nDepth=0):
|
||||
tItem = self.theMap[tHandle]
|
||||
tItem.setText(2,str(theCount))
|
||||
pItem = tItem.parent()
|
||||
if pItem is not None:
|
||||
pCount = 0
|
||||
for i in range(pItem.childCount()):
|
||||
pCount += int(pItem.child(i).text(2))
|
||||
pHandle = pItem.text(3)
|
||||
if not nDepth > NWItem.MAX_DEPTH:
|
||||
self.propagateCount(pHandle, pCount, nDepth+1)
|
||||
return
|
||||
|
||||
def buildTree(self):
|
||||
self.clear()
|
||||
for tHandle in self.theProject.projTree:
|
||||
|
||||
+9
-20
@@ -14,7 +14,7 @@ import logging
|
||||
import nw
|
||||
|
||||
from os import path
|
||||
from PyQt5.QtWidgets import QStatusBar, QLabel
|
||||
from PyQt5.QtWidgets import QStatusBar, QLabel, QFrame
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -27,35 +27,24 @@ class GuiMainStatus(QStatusBar):
|
||||
|
||||
self.mainConf = nw.CONFIG
|
||||
|
||||
self.boxWordCount = QLabel()
|
||||
self.boxCharCount = QLabel()
|
||||
self.boxDocHandle = QLabel()
|
||||
self.boxCounts = QLabel()
|
||||
self.boxCounts.setFrameStyle(QFrame.Panel | QFrame.Sunken);
|
||||
self.addPermanentWidget(self.boxCounts)
|
||||
|
||||
self.addPermanentWidget(self.boxWordCount)
|
||||
self.addPermanentWidget(self.boxCharCount)
|
||||
self.boxDocHandle = QLabel()
|
||||
self.boxDocHandle.setFrameStyle(QFrame.Panel | QFrame.Sunken);
|
||||
if self.mainConf.debugGUI:
|
||||
self.addPermanentWidget(self.boxDocHandle)
|
||||
|
||||
logger.debug("GuiMainStatus initialisation complete")
|
||||
|
||||
self.setWordCount(None)
|
||||
self.setCharCount(None)
|
||||
self.setCounts(0,0,0)
|
||||
self.setDocHandleCount(None)
|
||||
|
||||
return
|
||||
|
||||
def setWordCount(self, theCount):
|
||||
if theCount is None:
|
||||
self.boxWordCount.setText("<b>Words:</b> --")
|
||||
else:
|
||||
self.boxWordCount.setText("<b>Words:</b> {:n}".format(theCount))
|
||||
return
|
||||
|
||||
def setCharCount(self, theCount):
|
||||
if theCount is None:
|
||||
self.boxCharCount.setText("<b>Chars:</b> --")
|
||||
else:
|
||||
self.boxCharCount.setText("<b>Chars:</b> {:n}".format(theCount))
|
||||
def setCounts(self, cC, wC, pC):
|
||||
self.boxCounts.setText("<b>C:</b> {:n} <b>W:</b> {:n} <b>P:</b> {:n}".format(cC,wC,pC))
|
||||
return
|
||||
|
||||
def setDocHandleCount(self, theHandle):
|
||||
|
||||
@@ -156,6 +156,9 @@ class GuiMain(QMainWindow):
|
||||
|
||||
def saveDocument(self):
|
||||
docHtml = self.docEditor.getText()
|
||||
self.theDocument.theItem.setCharCount(self.docEditor.charCount)
|
||||
self.theDocument.theItem.setWordCount(self.docEditor.wordCount)
|
||||
self.theDocument.theItem.setParaCount(self.docEditor.paraCount)
|
||||
self.theDocument.saveDocument(docHtml)
|
||||
return
|
||||
|
||||
|
||||
+16
-2
@@ -26,12 +26,16 @@ class WordCounter(QThread):
|
||||
self.theParent = theParent
|
||||
self.charCount = 0
|
||||
self.wordCount = 0
|
||||
self.paraCount = 0
|
||||
return
|
||||
|
||||
def run(self):
|
||||
|
||||
self.charCount = 0
|
||||
self.wordCount = 0
|
||||
self.paraCount = 0
|
||||
|
||||
prevEmpty = True
|
||||
|
||||
for n in range(self.theParent.theDoc.blockCount()):
|
||||
|
||||
@@ -39,30 +43,40 @@ class WordCounter(QThread):
|
||||
if not theBlock.isValid():
|
||||
continue
|
||||
|
||||
theText = theBlock.text()
|
||||
theLen = len(theText)
|
||||
countPara = True
|
||||
theText = theBlock.text()
|
||||
theLen = len(theText)
|
||||
|
||||
if theLen == 0:
|
||||
prevEmpty = True
|
||||
continue
|
||||
if theText[0] == "@" or theText[0] == "%":
|
||||
prevEmpty = True
|
||||
continue
|
||||
|
||||
if theText[0:5] == "#### ":
|
||||
self.wordCount -= 1
|
||||
self.charCount -= 5
|
||||
countPara = False
|
||||
elif theText[0:4] == "### ":
|
||||
self.wordCount -= 1
|
||||
self.charCount -= 4
|
||||
countPara = False
|
||||
elif theText[0:3] == "## ":
|
||||
self.wordCount -= 1
|
||||
self.charCount -= 3
|
||||
countPara = False
|
||||
elif theText[0:2] == "# ":
|
||||
self.wordCount -= 1
|
||||
self.charCount -= 2
|
||||
countPara = False
|
||||
|
||||
theBuff = theText.replace("–"," ").replace("—"," ")
|
||||
self.wordCount += len(theBuff.split())
|
||||
self.charCount += theLen
|
||||
if countPara and prevEmpty:
|
||||
self.paraCount += 1
|
||||
prevEmpty = countPara == False
|
||||
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user