From c76c2df9255284096880b975d89ba0278de8cb97 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 11 Oct 2020 22:52:06 +0200 Subject: [PATCH] Simplify the typewriter mode --- nw/config.py | 5 +++ nw/gui/doceditor.py | 42 +++++--------------------- nw/gui/preferences.py | 24 +++++++++++++-- tests/reference/novelwriter.conf | 3 +- tests/reference/novelwriter_prefs.conf | 1 + 5 files changed, 37 insertions(+), 38 deletions(-) diff --git a/nw/config.py b/nw/config.py index 5ed12655..66b03bcc 100644 --- a/nw/config.py +++ b/nw/config.py @@ -128,6 +128,7 @@ class Config: self.doReplaceDots = True self.scrollPastEnd = True self.scollWithCursor = False + self.scollFromPoint = 40 self.wordCountTimer = 5.0 self.showTabsNSpaces = False @@ -467,6 +468,9 @@ class Config: self.scollWithCursor = self._parseLine( cnfParse, cnfSec, "scollwithcursor", self.CNF_BOOL, self.scollWithCursor ) + self.scollFromPoint = self._parseLine( + cnfParse, cnfSec, "scollfrompoint", self.CNF_INT, self.scollFromPoint + ) self.fmtSingleQuotes = self._parseLine( cnfParse, cnfSec, "fmtsinglequote", self.CNF_LIST, self.fmtSingleQuotes ) @@ -613,6 +617,7 @@ class Config: cnfParse.set(cnfSec, "repdots", str(self.doReplaceDots)) cnfParse.set(cnfSec, "scrollpastend", str(self.scrollPastEnd)) cnfParse.set(cnfSec, "scollwithcursor", str(self.scollWithCursor)) + cnfParse.set(cnfSec, "scollfrompoint", str(self.scollFromPoint)) cnfParse.set(cnfSec, "fmtsinglequote", self._packList(self.fmtSingleQuotes)) cnfParse.set(cnfSec, "fmtdoublequote", self._packList(self.fmtDoubleQuotes)) cnfParse.set(cnfSec, "spelltool", str(self.spellTool)) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 9cbe1abb..8ccdac08 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -88,8 +88,6 @@ class GuiDocEditor(QTextEdit): self.bigDoc = False # Flag for very large document size self.doReplace = False # Switch to temporarily disable auto-replace self.queuePos = None # Used for delayed change of cursor position - self.cursorLast = 0 # The last known vertical position of the cursor - self.lengthLast = 0 # Typography self.typDQOpen = self.mainConf.fmtDoubleQuotes[0] @@ -102,8 +100,6 @@ class GuiDocEditor(QTextEdit): self.qDocument.contentsChange.connect(self._docChange) self.qDocument.documentLayout().documentSizeChanged.connect(self._docSizeChanged) - self.verticalScrollBar().sliderMoved.connect(self._doVerticalScroll) - # Document Title self.docHeader = GuiDocEditHeader(self) self.docFooter = GuiDocEditFooter(self) @@ -440,7 +436,7 @@ class GuiDocEditor(QTextEdit): if self.mainConf.scrollPastEnd: docFrame = self.qDocument.rootFrame().frameFormat() - docMargin = wH - uM - lM - 5*self.theTheme.fontPixelSize + docMargin = wH - uM - lM - 4*tB - 5*self.theTheme.fontPixelSize docFrame.setBottomMargin(max(0, docMargin)) self.qDocument.rootFrame().setFrameFormat(docFrame) @@ -496,7 +492,6 @@ class GuiDocEditor(QTextEdit): theCursor.setPosition(thePosition) self.setTextCursor(theCursor) self.docFooter.updateLineCount() - self.cursorLast = self.cursorRect().center().y() return True @@ -777,20 +772,13 @@ class GuiDocEditor(QTextEdit): self.docFooter.updateLineCount() if self.mainConf.scollWithCursor: - docLen = self.qDocument.characterCount() - if docLen == self.lengthLast: - # No change, so just update last position - self.cursorLast = self.cursorRect().center().y() - else: - # The user typed something, so check if we need to - # scroll, and move the scroll bar the same distance - self.lengthLast = docLen - self.ensureCursorVisible() + kMod = keyEvent.modifiers() + if kMod == Qt.NoModifier or kMod == Qt.ShiftModifier: cPos = self.cursorRect().center().y() - if cPos != self.cursorLast: - vBar = self.verticalScrollBar() - vBar.setValue(vBar.value() + cPos - self.cursorLast) - self.cursorLast = self.cursorRect().center().y() + mPos = self.mainConf.scollFromPoint*self.height()*0.01 + vBar = self.verticalScrollBar() + vBar.setValue(vBar.value() + cPos - round(mPos)) + self.ensureCursorVisible() return @@ -817,18 +805,9 @@ class GuiDocEditor(QTextEdit): QTextEdit.mouseReleaseEvent(self, mEvent) self.docFooter.updateLineCount() - self.cursorLast = self.cursorRect().center().y() return - def wheelEvent(self, theEvent): - """Briefly capture the mouse wheel event to capture the cursor - position. - """ - QTextEdit.wheelEvent(self, theEvent) - self.cursorLast = self.cursorRect().center().y() - return - def resizeEvent(self, theEvent): """If the text editor is resize, we must make sure the document has its margins adjusted according to user preferences. @@ -863,13 +842,6 @@ class GuiDocEditor(QTextEdit): self._docAutoReplace(self.qDocument.findBlock(thePos)) return - @pyqtSlot(int) - def _doVerticalScroll(self, theChange): - """Update the cursor position on vertical scrolling. - """ - self.cursorLast = self.cursorRect().center().y() - return - @pyqtSlot("QPoint") def _openContextMenu(self, thePos): """Triggered by right click to open the context menu. Also diff --git a/nw/gui/preferences.py b/nw/gui/preferences.py index f1848bb3..7dffd340 100644 --- a/nw/gui/preferences.py +++ b/nw/gui/preferences.py @@ -555,6 +555,10 @@ class GuiConfigEditLayoutTab(QWidget): theUnit="px" ) + # Scroll Behaviour + # ================ + self.mainForm.addGroupLabel("Scroll Behaviour") + ## Scroll Past End self.scrollPastEnd = QSwitch() self.scrollPastEnd.setChecked(self.mainConf.scrollPastEnd) @@ -568,11 +572,25 @@ class GuiConfigEditLayoutTab(QWidget): self.scollWithCursor = QSwitch() self.scollWithCursor.setChecked(self.mainConf.scollWithCursor) self.mainForm.addRow( - "Typewriter style scrolling", + "Typewriter style scrolling when you type", self.scollWithCursor, - "Scrolls up when the cursor moves to a new line." + "Tries to keep the cursor at a fixed vertical position." ) + ## Font Size + self.scollFromPoint = QSpinBox(self) + self.scollFromPoint.setMinimum(10) + self.scollFromPoint.setMaximum(90) + self.scollFromPoint.setSingleStep(1) + self.scollFromPoint.setValue(self.mainConf.scollFromPoint) + self.mainForm.addRow( + "Position in the editor to keep the cursor", + self.scollFromPoint, + "In units of percentage of the editor height.", + theUnit = "%" + ) + + return def saveValues(self): @@ -592,6 +610,7 @@ class GuiConfigEditLayoutTab(QWidget): tabWidth = self.tabWidth.value() scrollPastEnd = self.scrollPastEnd.isChecked() scollWithCursor = self.scollWithCursor.isChecked() + scollFromPoint = self.scollFromPoint.value() self.mainConf.textFont = textFont self.mainConf.textSize = textSize @@ -604,6 +623,7 @@ class GuiConfigEditLayoutTab(QWidget): self.mainConf.tabWidth = tabWidth self.mainConf.scrollPastEnd = scrollPastEnd self.mainConf.scollWithCursor = scollWithCursor + self.mainConf.scollFromPoint = scollFromPoint self.mainConf.confChanged = True diff --git a/tests/reference/novelwriter.conf b/tests/reference/novelwriter.conf index f4bc8d61..aa09b213 100644 --- a/tests/reference/novelwriter.conf +++ b/tests/reference/novelwriter.conf @@ -1,5 +1,5 @@ [Main] -timestamp = 2020-10-11 18:29:34 +timestamp = 2020-10-11 22:50:45 theme = default syntax = default_light icons = typicons_colour_light @@ -41,6 +41,7 @@ repdash = True repdots = True scrollpastend = True scollwithcursor = False +scollfrompoint = 40 fmtsinglequote = ‘, ’ fmtdoublequote = “, ” spelltool = internal diff --git a/tests/reference/novelwriter_prefs.conf b/tests/reference/novelwriter_prefs.conf index 0e1d41f6..b2cceaff 100644 --- a/tests/reference/novelwriter_prefs.conf +++ b/tests/reference/novelwriter_prefs.conf @@ -41,6 +41,7 @@ repdash = True repdots = True scrollpastend = False scollwithcursor = True +scollfrompoint = 40 fmtsinglequote = ‘, ’ fmtdoublequote = “, ” spelltool = internal