From cf9a141dc7c479690590daeada6dd4828c9bbb48 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 4 Apr 2024 23:05:42 +0200 Subject: [PATCH] Remove caching of doc search options state in the search widget --- novelwriter/gui/doceditor.py | 61 +++++++++++----------------- tests/test_gui/test_gui_doceditor.py | 18 ++++---- 2 files changed, 33 insertions(+), 46 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 8c773355..d851f7dc 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -1319,9 +1319,9 @@ class GuiDocEditor(QPlainTextEdit): if len(resS) == 0 and self._docHandle: self.docSearch.setResultCount(0, 0) self._lastFind = None - if self.docSearch.doNextFile and not goBack: + if CONFIG.searchNextFile and not goBack: self.mainGui.openNextDocument( - self._docHandle, wrapAround=self.docSearch.doLoop + self._docHandle, wrapAround=CONFIG.searchLoop ) self.beginSearch() self.setFocus() @@ -1330,7 +1330,7 @@ class GuiDocEditor(QPlainTextEdit): cursor = self.textCursor() resIdx = bisect.bisect_left(resS, cursor.position()) - doLoop = self.docSearch.doLoop + doLoop = CONFIG.searchLoop maxIdx = len(resS) - 1 if goBack: @@ -1340,9 +1340,9 @@ class GuiDocEditor(QPlainTextEdit): resIdx = maxIdx if doLoop else 0 if resIdx > maxIdx and self._docHandle: - if self.docSearch.doNextFile and not goBack: + if CONFIG.searchNextFile and not goBack: self.mainGui.openNextDocument( - self._docHandle, wrapAround=self.docSearch.doLoop + self._docHandle, wrapAround=CONFIG.searchLoop ) self.beginSearch() self.setFocus() @@ -1375,9 +1375,9 @@ class GuiDocEditor(QPlainTextEdit): origB = cursor.position() findOpt = QTextDocument.FindFlag(0) - if self.docSearch.isCaseSense: + if CONFIG.searchCase: findOpt |= QTextDocument.FindFlag.FindCaseSensitively - if self.docSearch.isWholeWord: + if CONFIG.searchWord: findOpt |= QTextDocument.FindFlag.FindWholeWords searchFor = self.docSearch.getSearchObject() @@ -1446,7 +1446,7 @@ class GuiDocEditor(QPlainTextEdit): searchFor = self.docSearch.searchText replWith = self.docSearch.replaceText - if self.docSearch.doMatchCap: + if CONFIG.searchMatchCap: replWith = transferCase(cursor.selectedText(), replWith) # Make sure the selected text was selected by an actual find @@ -2392,12 +2392,6 @@ class GuiDocEditSearch(QFrame): self.docEditor = docEditor self.repVisible = False - self.isCaseSense = CONFIG.searchCase - self.isWholeWord = CONFIG.searchWord - self.isRegEx = CONFIG.searchRegEx - self.doLoop = CONFIG.searchLoop - self.doNextFile = CONFIG.searchNextFile - self.doMatchCap = CONFIG.searchMatchCap iSz = SHARED.theme.baseIconSize mPx = CONFIG.pxInt(6) @@ -2440,31 +2434,31 @@ class GuiDocEditSearch(QFrame): self.toggleCase = QAction(self.tr("Case Sensitive"), self) self.toggleCase.setCheckable(True) - self.toggleCase.setChecked(self.isCaseSense) + self.toggleCase.setChecked(CONFIG.searchCase) self.toggleCase.toggled.connect(self._doToggleCase) self.searchOpt.addAction(self.toggleCase) self.toggleWord = QAction(self.tr("Whole Words Only"), self) self.toggleWord.setCheckable(True) - self.toggleWord.setChecked(self.isWholeWord) + self.toggleWord.setChecked(CONFIG.searchWord) self.toggleWord.toggled.connect(self._doToggleWord) self.searchOpt.addAction(self.toggleWord) self.toggleRegEx = QAction(self.tr("RegEx Mode"), self) self.toggleRegEx.setCheckable(True) - self.toggleRegEx.setChecked(self.isRegEx) + self.toggleRegEx.setChecked(CONFIG.searchRegEx) self.toggleRegEx.toggled.connect(self._doToggleRegEx) self.searchOpt.addAction(self.toggleRegEx) self.toggleLoop = QAction(self.tr("Loop Search"), self) self.toggleLoop.setCheckable(True) - self.toggleLoop.setChecked(self.doLoop) + self.toggleLoop.setChecked(CONFIG.searchLoop) self.toggleLoop.toggled.connect(self._doToggleLoop) self.searchOpt.addAction(self.toggleLoop) self.toggleProject = QAction(self.tr("Search Next File"), self) self.toggleProject.setCheckable(True) - self.toggleProject.setChecked(self.doNextFile) + self.toggleProject.setChecked(CONFIG.searchNextFile) self.toggleProject.toggled.connect(self._doToggleProject) self.searchOpt.addAction(self.toggleProject) @@ -2472,7 +2466,7 @@ class GuiDocEditSearch(QFrame): self.toggleMatchCap = QAction(self.tr("Preserve Case"), self) self.toggleMatchCap.setCheckable(True) - self.toggleMatchCap.setChecked(self.doMatchCap) + self.toggleMatchCap.setChecked(CONFIG.searchMatchCap) self.toggleMatchCap.toggled.connect(self._doToggleMatchCap) self.searchOpt.addAction(self.toggleMatchCap) @@ -2550,20 +2544,20 @@ class GuiDocEditSearch(QFrame): expression object. """ text = self.searchBox.text() - if self.isRegEx: + if CONFIG.searchRegEx: # Using the Unicode-capable QRegularExpression class was # only added in Qt 5.13. Otherwise, 5.3 and up supports # only the QRegExp class. if CONFIG.verQtValue >= 0x050d00: rxOpt = QRegularExpression.PatternOption.UseUnicodePropertiesOption - if not self.isCaseSense: + if not CONFIG.searchCase: rxOpt |= QRegularExpression.PatternOption.CaseInsensitiveOption regEx = QRegularExpression(text, rxOpt) self._alertSearchValid(regEx.isValid()) return regEx else: # pragma: no cover # >= 50300 to < 51300 - if self.isCaseSense: + if CONFIG.searchCase: rxOpt = Qt.CaseSensitivity.CaseSensitive else: rxOpt = Qt.CaseSensitivity.CaseInsensitive @@ -2587,7 +2581,7 @@ class GuiDocEditSearch(QFrame): self.searchBox.setText(text) self.searchBox.setFocus() self.searchBox.selectAll() - if self.isRegEx: + if CONFIG.searchRegEx: self._alertSearchValid(True) return @@ -2659,13 +2653,6 @@ class GuiDocEditSearch(QFrame): def closeSearch(self) -> None: """Close the search box.""" - CONFIG.searchCase = self.isCaseSense - CONFIG.searchWord = self.isWholeWord - CONFIG.searchRegEx = self.isRegEx - CONFIG.searchLoop = self.doLoop - CONFIG.searchNextFile = self.doNextFile - CONFIG.searchMatchCap = self.doMatchCap - self.showReplace.setChecked(False) self.setVisible(False) self.docEditor.updateDocMargins() @@ -2725,37 +2712,37 @@ class GuiDocEditSearch(QFrame): @pyqtSlot(bool) def _doToggleCase(self, state: bool) -> None: """Enable/disable case sensitive mode.""" - self.isCaseSense = state + CONFIG.searchCase = state return @pyqtSlot(bool) def _doToggleWord(self, state: bool) -> None: """Enable/disable whole word search mode.""" - self.isWholeWord = state + CONFIG.searchWord = state return @pyqtSlot(bool) def _doToggleRegEx(self, state: bool) -> None: """Enable/disable regular expression search mode.""" - self.isRegEx = state + CONFIG.searchRegEx = state return @pyqtSlot(bool) def _doToggleLoop(self, state: bool) -> None: """Enable/disable looping the search.""" - self.doLoop = state + CONFIG.searchLoop = state return @pyqtSlot(bool) def _doToggleProject(self, state: bool) -> None: """Enable/disable continuing search in next project file.""" - self.doNextFile = state + CONFIG.searchNextFile = state return @pyqtSlot(bool) def _doToggleMatchCap(self, state: bool) -> None: """Enable/disable preserving capitalisation when replacing.""" - self.doMatchCap = state + CONFIG.searchMatchCap = state return ## diff --git a/tests/test_gui/test_gui_doceditor.py b/tests/test_gui/test_gui_doceditor.py index d947377a..f09ddb67 100644 --- a/tests/test_gui/test_gui_doceditor.py +++ b/tests/test_gui/test_gui_doceditor.py @@ -1794,7 +1794,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Activate loop search docSearch.toggleLoop.activate(QAction.Trigger) assert docSearch.toggleLoop.isChecked() - assert docSearch.doLoop is True + assert CONFIG.searchLoop is True # Find next by menu Search > Find Next nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) @@ -1819,7 +1819,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Enable RegEx search docSearch.toggleRegEx.activate(QAction.Trigger) assert docSearch.toggleRegEx.isChecked() - assert docSearch.isRegEx is True + assert CONFIG.searchRegEx is True # Set invalid RegEx docEditor.setCursorPosition(0) @@ -1848,7 +1848,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Make RegEx case sensitive docSearch.toggleCase.activate(QAction.Trigger) assert docSearch.toggleCase.isChecked() - assert docSearch.isCaseSense is True + assert CONFIG.searchCase is True # Find next/prev (one result) nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) @@ -1865,12 +1865,12 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Disable RegEx case sensitive docSearch.toggleCase.activate(QAction.Trigger) assert docSearch.toggleCase.isChecked() is False - assert docSearch.isCaseSense is False + assert CONFIG.searchCase is False # Toggle replace preserve case docSearch.toggleMatchCap.activate(QAction.Trigger) assert docSearch.toggleMatchCap.isChecked() - assert docSearch.doMatchCap is True + assert CONFIG.searchMatchCap is True # Replace "Sus" with "Foo" via menu docEditor.setCursorPosition(605) @@ -1898,7 +1898,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Disable RegEx search docSearch.toggleRegEx.activate(QAction.Trigger) assert not docSearch.toggleRegEx.isChecked() - assert docSearch.isRegEx is False + assert CONFIG.searchRegEx is False # Close search and select "est" again docSearch.cancelSearch.activate(QAction.Trigger) @@ -1915,7 +1915,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Enable full word search docSearch.toggleWord.activate(QAction.Trigger) assert docSearch.toggleWord.isChecked() - assert docSearch.isWholeWord is True + assert CONFIG.searchWord is True # Only one match nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) @@ -1926,7 +1926,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): # Enable next doc search docSearch.toggleProject.activate(QAction.Trigger) assert docSearch.toggleProject.isChecked() - assert docSearch.doNextFile is True + assert CONFIG.searchNextFile is True # Next match nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) @@ -1937,7 +1937,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, prjLipsum): assert abs(docEditor.getCursorPosition() - 1127) < 3 # Next doc, no match - assert docSearch.doNextFile is True + assert CONFIG.searchNextFile is True docSearch.setSearchText("abcdef") nwGUI.mainMenu.aFindNext.activate(QAction.Trigger) assert docEditor.docHandle != "2426c6f0ca922"