Basic search feature now works, but needs more tweaking

This commit is contained in:
Veronica K. B. Olsen
2019-09-29 17:16:17 +02:00
parent 7c60d9e9fd
commit 1bf539d490
6 changed files with 99 additions and 7 deletions
+2
View File
@@ -65,6 +65,8 @@ class nwDocAction(Enum):
D_QUOTE = 10
SEL_ALL = 11
SEL_PARA = 12
FIND = 13
GO_NEXT = 14
# END Enum nwDocAction
+1 -1
View File
@@ -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)
+29
View File
@@ -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
+17
View File
@@ -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")
+39 -4
View File
@@ -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
+11 -2
View File
@@ -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)