From 7c2eff8b9bbbbc925cac6697ad404f8085dea7a0 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 21 Apr 2019 01:04:13 +0200 Subject: [PATCH] First attempt at a word counter, but this is a bit slow. --- nw/gui/doceditor.py | 75 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index dc824aad..497d004a 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -32,8 +32,15 @@ class GuiDocEditor(QWidget): self.mainConf = nw.CONFIG self.theParent = theParent self.charCount = 0 + self.wordCount = 0 self.lineCount = 0 + # Internal Temps + self.blockWords = {} + self.currEditBlock = -1 + self.currEditStart = 0 + self.currEditFinal = 0 + self.outerBox = QVBoxLayout() self.guiEditor = QTextEdit() if self.mainConf.textFixedW: @@ -66,8 +73,25 @@ class GuiDocEditor(QWidget): return def setText(self, theText): + self.guiEditor.setPlainText(theText) + self.charCount = self.theDoc.characterCount() + self.wordCount = 0 + + self.currEditBlock = -1 + self.currEditWords = 0 + + tStart = time() + self.blockWords = {} + for n in range(self.theDoc.blockCount()): + self._countWords(self.theDoc.findBlockByNumber(n)) + self.wordCount = sum(self.blockWords.values()) + logger.verbose("Doc word count took %.3f µs" % ((time()-tStart)*1e6)) + + self.theParent.statusBar.setCharCount(self.charCount) + self.theParent.statusBar.setWordCount(self.wordCount) + return True def getText(self): @@ -87,18 +111,30 @@ class GuiDocEditor(QWidget): return def _docChange(self, thePos, charsRemoved, charsAdded): + tStart = time() - # logger.debug("Contents changed: %d %d %d" % (thePos, charsRemoved, charsAdded)) + self.charCount = self.theDoc.characterCount() self.lineCount = self.theDoc.lineCount() self.theParent.statusBar.setCharCount(self.charCount) + + currBlock = self.theDoc.findBlock(thePos) + self._countWords(currBlock) + self.wordCount = sum(self.blockWords.values()) + self.theParent.statusBar.setWordCount(self.wordCount) + if self.mainConf.doReplace: - self._docAutoReplace(self.theDoc.findBlock(thePos)) + self._docAutoReplace(currBlock) + logger.verbose("Doc change signal took %.3f µs" % ((time()-tStart)*1e6)) + return def _docAutoReplace(self, theBlock): + if not theBlock.isValid(): + return + theText = theBlock.text() theCursor = self.guiEditor.textCursor() thePos = theCursor.positionInBlock() @@ -123,17 +159,17 @@ class GuiDocEditor(QWidget): self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().insertText(qOpen) - if self.mainConf.doReplaceDash and theTwo == "--": + elif self.mainConf.doReplaceDash and theTwo == "--": self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().insertText("–") - if self.mainConf.doReplaceDash and theTwo == "–-": + elif self.mainConf.doReplaceDash and theTwo == "–-": self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().insertText("—") - if self.mainConf.doReplaceDots and theThree == "...": + elif self.mainConf.doReplaceDots and theThree == "...": self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().deletePreviousChar() self.guiEditor.textCursor().deletePreviousChar() @@ -141,6 +177,35 @@ class GuiDocEditor(QWidget): return + def _countWords(self, theBlock): + """Count the number of words in a given block, but only if it's a text block. I.e. we skip + blocks starting with @ and %, and we also skip the ### in titles. Dashes are replaced with + spaces before the count to ensure, for instance, 'word1—word2' is counted as two words. + """ + + if not theBlock.isValid(): + return + + theText = theBlock.text() + theLen = len(theText) + self.blockWords[theBlock.blockNumber()] = 0 + + if theLen == 0: + return 0 + if theText[0] == "@" or theText[0] == "%": + return 0 + if theText[0] == "#": + nWords = -1 + else: + nWords = 0 + + theBuff = theText.replace("–"," ").replace("—"," ") + nWords += len(theBuff.split()) + + self.blockWords[theBlock.blockNumber()] = nWords + + return nWords + def _buildTabToolBar(self): toolBar = self.editToolBar toolBar.setToolButtonStyle(Qt.ToolButtonIconOnly)