Moved the NWDoc instance from main GUI to the docEditor as it is not really needed in the main view.

This commit is contained in:
Veronica K. B. Olsen
2019-06-09 10:16:40 +02:00
parent 579db06651
commit 3793d03e4e
2 changed files with 53 additions and 27 deletions
+47 -9
View File
@@ -20,6 +20,7 @@ from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QTextEdit, QAction, QMenu, QShortcut
from PyQt5.QtGui import QTextCursor, QTextOption, QIcon, QKeySequence
from nw.project.document import NWDoc
from nw.gui.dochighlight import GuiDocHighlighter
from nw.gui.wordcounter import WordCounter
from nw.enum import nwDocAction, nwAlert
@@ -28,7 +29,7 @@ logger = logging.getLogger(__name__)
class GuiDocEditor(QTextEdit):
def __init__(self, theParent):
def __init__(self, theParent, theProject):
QTextEdit.__init__(self)
logger.debug("Initialising DocEditor ...")
@@ -36,10 +37,12 @@ class GuiDocEditor(QTextEdit):
# Class Variables
self.mainConf = nw.CONFIG
self.theParent = theParent
self.theProject = theProject
self.docChanged = False
self.pwlFile = None
self.spellCheck = False
self.theDocument = theParent.theDocument
self.theDocument = NWDoc(self.theProject, self.theParent)
self.theHandle = None
# Document Variables
self.charCount = 0
@@ -69,9 +72,6 @@ class GuiDocEditor(QTextEdit):
self.setMinimumWidth(300)
self.setAcceptRichText(False)
self.clearEditor()
self.initEditor()
self.theQDoc.setDocumentMargin(0)
self.theQDoc.contentsChange.connect(self._docChange)
@@ -87,13 +87,29 @@ class GuiDocEditor(QTextEdit):
self.wCounter = WordCounter(self)
self.wCounter.finished.connect(self._updateCounts)
self.clearEditor()
self.initEditor()
logger.debug("DocEditor initialisation complete")
return
def clearEditor(self):
self.theDocument.clearDocument()
self.setReadOnly(True)
self.clear()
self.wcTimer.stop()
self.theHandle = None
self.charCount = 0
self.wordCount = 0
self.paraCount = 0
self.lastEdit = 0
self.hasSelection = False
self.setDocumentChanged(False)
return True
def initEditor(self):
@@ -114,6 +130,7 @@ class GuiDocEditor(QTextEdit):
return True
def loadText(self, tHandle):
self.hLight.setHandle(tHandle)
self.setPlainText(self.theDocument.openDocument(tHandle))
self.setCursorPosition(self.theDocument.theItem.cursorPos)
@@ -122,6 +139,27 @@ class GuiDocEditor(QTextEdit):
self.wcTimer.start()
self.setDocumentChanged(False)
self.setReadOnly(False)
self.theHandle = tHandle
return True
def saveText(self):
if self.theDocument.theItem is None:
return False
docText = self.getText()
cursPos = self.getCursorPosition()
theItem = self.theDocument.theItem
theItem.setCharCount(self.charCount)
theItem.setWordCount(self.wordCount)
theItem.setParaCount(self.paraCount)
theItem.setCursorPos(cursPos)
self.theDocument.saveDocument(docText)
self.setDocumentChanged(False)
self.theParent.theIndex.scanText(theItem.itemHandle, docText)
return True
##
@@ -381,7 +419,7 @@ class GuiDocEditor(QTextEdit):
"""
logger.verbose("Updating word count")
tHandle = self.theParent.theDocument.docHandle
tHandle = self.theDocument.docHandle
self.charCount = self.wCounter.charCount
self.wordCount = self.wCounter.wordCount
self.paraCount = self.wCounter.paraCount
@@ -392,9 +430,9 @@ class GuiDocEditor(QTextEdit):
return
def _wrapSelection(self, tBefore, tAfter):
"""Wraps the selected text in whatever is in tBefore and tAfter. If there is no selection, the autoSelect setting decides
the action. AutoSelect will select the word under the cursor before wrapping it. If this feature is disabled, nothing is
done.
"""Wraps the selected text in whatever is in tBefore and tAfter. If there is no selection,
the autoSelect setting decides the action. AutoSelect will select the word under the cursor
before wrapping it. If this feature is disabled, nothing is done.
"""
theCursor = self.textCursor()
if self.mainConf.autoSelect and not theCursor.hasSelection():
+6 -18
View File
@@ -32,7 +32,6 @@ from nw.gui.itemeditor import GuiItemEditor
from nw.gui.statusbar import GuiMainStatus
from nw.gui.timelineview import GuiTimeLineView
from nw.project.project import NWProject
from nw.project.document import NWDoc
from nw.project.item import NWItem
from nw.project.index import NWIndex
from nw.tools.wordcount import countWords
@@ -51,7 +50,6 @@ class GuiMain(QMainWindow):
self.mainConf = nw.CONFIG
self.theTheme = Theme()
self.theProject = NWProject(self)
self.theDocument = NWDoc(self.theProject, self)
self.theIndex = NWIndex(self.theProject, self)
self.hasProject = False
@@ -61,12 +59,12 @@ class GuiMain(QMainWindow):
self.theTheme.loadTheme()
# Main GUI Elements
self.docEditor = GuiDocEditor(self)
self.statusBar = GuiMainStatus(self)
self.docEditor = GuiDocEditor(self, self.theProject)
self.docViewer = GuiDocViewer(self, self.theProject)
self.docDetails = GuiDocDetails(self, self.theProject)
self.treeView = GuiDocTree(self, self.theProject)
self.mainMenu = GuiMainMenu(self, self.theProject)
self.statusBar = GuiMainStatus(self)
# Minor Gui Elements
self.statusIcons = []
@@ -282,7 +280,6 @@ class GuiMain(QMainWindow):
if self.hasProject:
if self.docEditor.docChanged:
self.saveDocument()
self.theDocument.clearDocument()
self.docEditor.clearEditor()
return True
@@ -296,17 +293,8 @@ class GuiMain(QMainWindow):
return True
def saveDocument(self):
if self.theDocument.theItem is not None and self.hasProject:
docText = self.docEditor.getText()
cursPos = self.docEditor.getCursorPosition()
theItem = self.theDocument.theItem
theItem.setCharCount(self.docEditor.charCount)
theItem.setWordCount(self.docEditor.wordCount)
theItem.setParaCount(self.docEditor.paraCount)
theItem.setCursorPos(cursPos)
self.theDocument.saveDocument(docText)
self.docEditor.setDocumentChanged(False)
self.theIndex.scanText(theItem.itemHandle, docText)
if self.hasProject:
self.docEditor.saveText()
return True
def viewDocument(self, tHandle=None):
@@ -318,7 +306,7 @@ class GuiMain(QMainWindow):
tHandle = self.theProject.lastViewed
if tHandle is None:
logger.debug("No document selected, trying editor document")
tHandle = self.theDocument.docHandle
tHandle = self.docEditor.theHandle
if tHandle is None:
logger.debug("No document selected, giving up")
return False
@@ -580,7 +568,7 @@ class GuiMain(QMainWindow):
return
def _autoSaveDocument(self):
if self.hasProject and self.docEditor.docChanged and self.theDocument.theItem is not None:
if self.hasProject and self.docEditor.docChanged:
logger.debug("Autosaving document")
self.saveDocument()
return