Simple search function now working
This commit is contained in:
@@ -67,6 +67,7 @@ class nwDocAction(Enum):
|
||||
SEL_PARA = 12
|
||||
FIND = 13
|
||||
GO_NEXT = 14
|
||||
GO_PREV = 15
|
||||
|
||||
# END Enum nwDocAction
|
||||
|
||||
|
||||
+15
-8
@@ -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
|
||||
|
||||
+8
-1
@@ -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()
|
||||
|
||||
|
||||
+23
-3
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user