From cb2ee95d1065c4ce0a6a9965dcaeb5c0c7a557d7 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 29 Sep 2019 18:50:43 +0200 Subject: [PATCH] Simple search function now working --- nw/enum.py | 1 + nw/gui/doceditor.py | 23 +++++++++++++++-------- nw/gui/mainmenu.py | 9 ++++++++- nw/gui/searchbar.py | 26 +++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/nw/enum.py b/nw/enum.py index 3ee73975..54fee904 100644 --- a/nw/enum.py +++ b/nw/enum.py @@ -67,6 +67,7 @@ class nwDocAction(Enum): SEL_PARA = 12 FIND = 13 GO_NEXT = 14 + GO_PREV = 15 # END Enum nwDocAction diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 9e2cf7e4..7f1466fc 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -17,7 +17,7 @@ from time import time from PyQt5.QtCore import Qt, QTimer, QSizeF from PyQt5.QtWidgets import QTextEdit, QAction, QMenu, QShortcut -from PyQt5.QtGui import QTextCursor, QTextOption, QIcon, QKeySequence, QFont, QColor, QPalette +from PyQt5.QtGui import QTextCursor, QTextOption, QIcon, QKeySequence, QFont, QColor, QPalette, QTextDocument from nw.project.document import NWDoc from nw.gui.dochighlight import GuiDocHighlighter @@ -281,6 +281,7 @@ class GuiDocEditor(QTextEdit): elif theAction == nwDocAction.SEL_PARA: self._makeSelection(QTextCursor.BlockUnderCursor) elif theAction == nwDocAction.FIND: self._beginSearch() elif theAction == nwDocAction.GO_NEXT: self._findNext() + elif theAction == nwDocAction.GO_PREV: self._findPrev() else: logger.error("Unknown or unsupported document action %s" % str(theAction)) return False @@ -489,11 +490,7 @@ class GuiDocEditor(QTextEdit): def _beginSearch(self): - print("Boo!") - theCursor = self.textCursor() - if self.mainConf.autoSelect and not theCursor.hasSelection(): - theCursor.select(QTextCursor.WordUnderCursor) if theCursor.hasSelection(): selText = theCursor.selectedText() else: @@ -507,11 +504,21 @@ class GuiDocEditor(QTextEdit): return def _findNext(self): - searchFor = self.theParent.searchBar.getSearchText() - self.find(searchFor) - + wasFound = self.find(searchFor) + if not wasFound: + theCursor = self.textCursor() + theCursor.movePosition(QTextCursor.Start) + self.setTextCursor(theCursor) return + def _findPrev(self): + searchFor = self.theParent.searchBar.getSearchText() + wasFound = self.find(searchFor, QTextDocument.FindBackward) + if not wasFound: + theCursor = self.textCursor() + theCursor.movePosition(QTextCursor.End) + self.setTextCursor(theCursor) + return # END Class GuiDocEditor diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 7280a617..f1c1a6d1 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -406,12 +406,19 @@ class GuiMainMenu(QMenuBar): self.editMenu.addAction(menuItem) # Edit > Find Next - menuItem = QAction(QIcon.fromTheme("go-right"), "Go Next", self) + menuItem = QAction(QIcon.fromTheme("go-next"), "Go Next", self) menuItem.setStatusTip("Find next occurrence text in document") menuItem.setShortcut("F3") menuItem.triggered.connect(lambda: self._docAction(nwDocAction.GO_NEXT)) self.editMenu.addAction(menuItem) + # Edit > Find Prev + menuItem = QAction(QIcon.fromTheme("go-previous"), "Go Previous", self) + menuItem.setStatusTip("Find previous occurrence text in document") + menuItem.setShortcut("Shift+F3") + menuItem.triggered.connect(lambda: self._docAction(nwDocAction.GO_PREV)) + self.editMenu.addAction(menuItem) + # Edit > Separator self.editMenu.addSeparator() diff --git a/nw/gui/searchbar.py b/nw/gui/searchbar.py index ea8ef748..f88de328 100644 --- a/nw/gui/searchbar.py +++ b/nw/gui/searchbar.py @@ -16,6 +16,8 @@ import nw from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QFrame, QGridLayout, QLabel, QLineEdit, QPushButton +from nw.enum import nwDocAction + logger = logging.getLogger(__name__) class GuiSearchBar(QFrame): @@ -34,17 +36,23 @@ class GuiSearchBar(QFrame): self.setLayout(self.mainBox) self.searchBox = QLineEdit() + self.closeButton = QPushButton(QIcon.fromTheme("edit-delete"),"") self.searchButton = QPushButton(QIcon.fromTheme("edit-find"),"") - self.mainBox.addWidget(QLabel(""),0,0) - self.mainBox.addWidget(QLabel("Search"),0,1) - self.mainBox.addWidget(self.searchBox,0,2) + self.closeButton.clicked.connect(self._doClose) + self.searchButton.clicked.connect(self._doSearch) + + self.mainBox.addWidget(QLabel(""), 0,0) + self.mainBox.addWidget(QLabel("Search"), 0,1) + self.mainBox.addWidget(self.searchBox, 0,2) self.mainBox.addWidget(self.searchButton,0,3) + self.mainBox.addWidget(self.closeButton, 0,4) self.mainBox.setColumnStretch(0,1) self.mainBox.setColumnStretch(1,0) self.mainBox.setColumnStretch(2,0) self.mainBox.setColumnStretch(3,0) + self.mainBox.setColumnStretch(4,0) self.mainBox.setContentsMargins(0,0,0,0) logger.debug("GuiSearchBar initialisation complete") @@ -66,4 +74,16 @@ class GuiSearchBar(QFrame): def getSearchText(self): return self.searchBox.text() + ## + # Internal Functions + ## + + def _doClose(self): + self.setVisible(False) + return + + def _doSearch(self): + self.theParent.docEditor.docAction(nwDocAction.GO_NEXT) + return + # END Class GuiSearchBar