From 1bf539d4906e40f1cf20b41ce3c6da3cbc3602f0 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 29 Sep 2019 17:16:17 +0200 Subject: [PATCH] Basic search feature now works, but needs more tweaking --- nw/enum.py | 2 ++ nw/gui/docdetails.py | 2 +- nw/gui/doceditor.py | 29 +++++++++++++++++++++++++++++ nw/gui/mainmenu.py | 17 +++++++++++++++++ nw/gui/searchbar.py | 43 +++++++++++++++++++++++++++++++++++++++---- nw/gui/winmain.py | 13 +++++++++++-- 6 files changed, 99 insertions(+), 7 deletions(-) diff --git a/nw/enum.py b/nw/enum.py index 61540882..3ee73975 100644 --- a/nw/enum.py +++ b/nw/enum.py @@ -65,6 +65,8 @@ class nwDocAction(Enum): D_QUOTE = 10 SEL_ALL = 11 SEL_PARA = 12 + FIND = 13 + GO_NEXT = 14 # END Enum nwDocAction diff --git a/nw/gui/docdetails.py b/nw/gui/docdetails.py index 43c51ccd..804b43bc 100644 --- a/nw/gui/docdetails.py +++ b/nw/gui/docdetails.py @@ -60,7 +60,7 @@ class GuiDocDetails(QFrame): lblOne.setFont(self.fntOne) self.mainBox.addWidget(lblOne,nRow,0) self.mainBox.addWidget(self.colTwo[nRow],nRow,1) - + self.mainBox.setColumnStretch(0,0) self.mainBox.setColumnStretch(1,1) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 383640ea..9e2cf7e4 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -279,6 +279,8 @@ class GuiDocEditor(QTextEdit): elif theAction == nwDocAction.D_QUOTE: self._wrapSelection(self.typDQOpen,self.typDQClose) elif theAction == nwDocAction.SEL_ALL: self._makeSelection(QTextCursor.Document) elif theAction == nwDocAction.SEL_PARA: self._makeSelection(QTextCursor.BlockUnderCursor) + elif theAction == nwDocAction.FIND: self._beginSearch() + elif theAction == nwDocAction.GO_NEXT: self._findNext() else: logger.error("Unknown or unsupported document action %s" % str(theAction)) return False @@ -485,4 +487,31 @@ class GuiDocEditor(QTextEdit): self.setTextCursor(theCursor) return + 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: + selText = "" + + self.theParent.searchBar.setSearchText(selText) + + if selText != "": + self._findNext() + + return + + def _findNext(self): + + searchFor = self.theParent.searchBar.getSearchText() + self.find(searchFor) + + return + + # END Class GuiDocEditor diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 155fc5a3..7280a617 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -398,6 +398,23 @@ class GuiMainMenu(QMenuBar): # Edit > Separator self.editMenu.addSeparator() + # Edit > Find + menuItem = QAction(QIcon.fromTheme("edit-find"), "Find", self) + menuItem.setStatusTip("Find text in document") + menuItem.setShortcut("Ctrl+F") + menuItem.triggered.connect(lambda: self._docAction(nwDocAction.FIND)) + self.editMenu.addAction(menuItem) + + # Edit > Find Next + menuItem = QAction(QIcon.fromTheme("go-right"), "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 > Separator + self.editMenu.addSeparator() + # Edit > Select All menuItem = QAction(QIcon.fromTheme("edit-select-all"), "Select All", self) menuItem.setStatusTip("Select all text in document") diff --git a/nw/gui/searchbar.py b/nw/gui/searchbar.py index 90dd8c72..ea8ef748 100644 --- a/nw/gui/searchbar.py +++ b/nw/gui/searchbar.py @@ -13,22 +13,57 @@ import logging import nw -from PyQt5.QtWidgets import QFrame +from PyQt5.QtGui import QIcon +from PyQt5.QtWidgets import QFrame, QGridLayout, QLabel, QLineEdit, QPushButton logger = logging.getLogger(__name__) class GuiSearchBar(QFrame): def __init__(self, theParent): + QFrame.__init__(self, theParent) logger.debug("Initialising GuiSearchBar ...") - self.mainConf = nw.CONFIG - self.theParent = theParent - self.refTime = None + self.mainConf = nw.CONFIG + self.theParent = theParent + + self.setContentsMargins(0,0,0,0) + + self.mainBox = QGridLayout(self) + self.setLayout(self.mainBox) + + self.searchBox = QLineEdit() + 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.mainBox.addWidget(self.searchButton,0,3) + + self.mainBox.setColumnStretch(0,1) + self.mainBox.setColumnStretch(1,0) + self.mainBox.setColumnStretch(2,0) + self.mainBox.setColumnStretch(3,0) + self.mainBox.setContentsMargins(0,0,0,0) logger.debug("GuiSearchBar initialisation complete") return + def setSearchText(self, theText): + + if not self.isVisible(): + self.setVisible(True) + + self.searchBox.setText(theText) + self.searchBox.setFocus(True) + + logger.debug("Setting search text to '%s'" % theText) + + return True + + def getSearchText(self): + return self.searchBox.text() + # END Class GuiSearchBar diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 7884da83..c39aa6f5 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -26,6 +26,7 @@ from nw.gui.doctree import GuiDocTree from nw.gui.doceditor import GuiDocEditor from nw.gui.docviewer import GuiDocViewer from nw.gui.docdetails import GuiDocDetails +from nw.gui.searchbar import GuiSearchBar from nw.gui.mainmenu import GuiMainMenu from nw.gui.configeditor import GuiConfigEditor from nw.gui.projecteditor import GuiProjectEditor @@ -69,6 +70,7 @@ class GuiMain(QMainWindow): self.docEditor = GuiDocEditor(self, self.theProject) self.docViewer = GuiDocViewer(self, self.theProject) self.docDetails = GuiDocDetails(self, self.theProject) + self.searchBar = GuiSearchBar(self) self.treeView = GuiDocTree(self, self.theProject) self.mainMenu = GuiMainMenu(self, self.theProject) @@ -83,8 +85,14 @@ class GuiMain(QMainWindow): self.treeBox.addWidget(self.docDetails) self.treePane.setLayout(self.treeBox) + self.docPane = QFrame() + self.docView = QVBoxLayout() + self.docView.addWidget(self.searchBar) + self.docView.addWidget(self.docEditor) + self.docPane.setLayout(self.docView) + self.splitView = QSplitter(Qt.Horizontal) - self.splitView.addWidget(self.docEditor) + self.splitView.addWidget(self.docPane) self.splitView.addWidget(self.docViewer) self.splitView.splitterMoved.connect(self._splitViewMove) @@ -98,7 +106,7 @@ class GuiMain(QMainWindow): self.idxTree = self.splitMain.indexOf(self.treePane) self.idxMain = self.splitMain.indexOf(self.splitView) - self.idxEditor = self.splitView.indexOf(self.docEditor) + self.idxEditor = self.splitView.indexOf(self.docPane) self.idxViewer = self.splitView.indexOf(self.docViewer) self.splitMain.setCollapsible(self.idxTree, False) @@ -107,6 +115,7 @@ class GuiMain(QMainWindow): self.splitView.setCollapsible(self.idxViewer, True) self.docViewer.setVisible(False) + self.searchBar.setVisible(False) # Build The Tree View self.treeView.itemSelectionChanged.connect(self._treeSingleClick)