Simple search function now working
This commit is contained in:
@@ -67,6 +67,7 @@ class nwDocAction(Enum):
|
|||||||
SEL_PARA = 12
|
SEL_PARA = 12
|
||||||
FIND = 13
|
FIND = 13
|
||||||
GO_NEXT = 14
|
GO_NEXT = 14
|
||||||
|
GO_PREV = 15
|
||||||
|
|
||||||
# END Enum nwDocAction
|
# END Enum nwDocAction
|
||||||
|
|
||||||
|
|||||||
+15
-8
@@ -17,7 +17,7 @@ from time import time
|
|||||||
|
|
||||||
from PyQt5.QtCore import Qt, QTimer, QSizeF
|
from PyQt5.QtCore import Qt, QTimer, QSizeF
|
||||||
from PyQt5.QtWidgets import QTextEdit, QAction, QMenu, QShortcut
|
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.project.document import NWDoc
|
||||||
from nw.gui.dochighlight import GuiDocHighlighter
|
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.SEL_PARA: self._makeSelection(QTextCursor.BlockUnderCursor)
|
||||||
elif theAction == nwDocAction.FIND: self._beginSearch()
|
elif theAction == nwDocAction.FIND: self._beginSearch()
|
||||||
elif theAction == nwDocAction.GO_NEXT: self._findNext()
|
elif theAction == nwDocAction.GO_NEXT: self._findNext()
|
||||||
|
elif theAction == nwDocAction.GO_PREV: self._findPrev()
|
||||||
else:
|
else:
|
||||||
logger.error("Unknown or unsupported document action %s" % str(theAction))
|
logger.error("Unknown or unsupported document action %s" % str(theAction))
|
||||||
return False
|
return False
|
||||||
@@ -489,11 +490,7 @@ class GuiDocEditor(QTextEdit):
|
|||||||
|
|
||||||
def _beginSearch(self):
|
def _beginSearch(self):
|
||||||
|
|
||||||
print("Boo!")
|
|
||||||
|
|
||||||
theCursor = self.textCursor()
|
theCursor = self.textCursor()
|
||||||
if self.mainConf.autoSelect and not theCursor.hasSelection():
|
|
||||||
theCursor.select(QTextCursor.WordUnderCursor)
|
|
||||||
if theCursor.hasSelection():
|
if theCursor.hasSelection():
|
||||||
selText = theCursor.selectedText()
|
selText = theCursor.selectedText()
|
||||||
else:
|
else:
|
||||||
@@ -507,11 +504,21 @@ class GuiDocEditor(QTextEdit):
|
|||||||
return
|
return
|
||||||
|
|
||||||
def _findNext(self):
|
def _findNext(self):
|
||||||
|
|
||||||
searchFor = self.theParent.searchBar.getSearchText()
|
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
|
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
|
# END Class GuiDocEditor
|
||||||
|
|||||||
+8
-1
@@ -406,12 +406,19 @@ class GuiMainMenu(QMenuBar):
|
|||||||
self.editMenu.addAction(menuItem)
|
self.editMenu.addAction(menuItem)
|
||||||
|
|
||||||
# Edit > Find Next
|
# 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.setStatusTip("Find next occurrence text in document")
|
||||||
menuItem.setShortcut("F3")
|
menuItem.setShortcut("F3")
|
||||||
menuItem.triggered.connect(lambda: self._docAction(nwDocAction.GO_NEXT))
|
menuItem.triggered.connect(lambda: self._docAction(nwDocAction.GO_NEXT))
|
||||||
self.editMenu.addAction(menuItem)
|
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
|
# Edit > Separator
|
||||||
self.editMenu.addSeparator()
|
self.editMenu.addSeparator()
|
||||||
|
|
||||||
|
|||||||
+23
-3
@@ -16,6 +16,8 @@ import nw
|
|||||||
from PyQt5.QtGui import QIcon
|
from PyQt5.QtGui import QIcon
|
||||||
from PyQt5.QtWidgets import QFrame, QGridLayout, QLabel, QLineEdit, QPushButton
|
from PyQt5.QtWidgets import QFrame, QGridLayout, QLabel, QLineEdit, QPushButton
|
||||||
|
|
||||||
|
from nw.enum import nwDocAction
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class GuiSearchBar(QFrame):
|
class GuiSearchBar(QFrame):
|
||||||
@@ -34,17 +36,23 @@ class GuiSearchBar(QFrame):
|
|||||||
self.setLayout(self.mainBox)
|
self.setLayout(self.mainBox)
|
||||||
|
|
||||||
self.searchBox = QLineEdit()
|
self.searchBox = QLineEdit()
|
||||||
|
self.closeButton = QPushButton(QIcon.fromTheme("edit-delete"),"")
|
||||||
self.searchButton = QPushButton(QIcon.fromTheme("edit-find"),"")
|
self.searchButton = QPushButton(QIcon.fromTheme("edit-find"),"")
|
||||||
|
|
||||||
self.mainBox.addWidget(QLabel(""),0,0)
|
self.closeButton.clicked.connect(self._doClose)
|
||||||
self.mainBox.addWidget(QLabel("Search"),0,1)
|
self.searchButton.clicked.connect(self._doSearch)
|
||||||
self.mainBox.addWidget(self.searchBox,0,2)
|
|
||||||
|
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.searchButton,0,3)
|
||||||
|
self.mainBox.addWidget(self.closeButton, 0,4)
|
||||||
|
|
||||||
self.mainBox.setColumnStretch(0,1)
|
self.mainBox.setColumnStretch(0,1)
|
||||||
self.mainBox.setColumnStretch(1,0)
|
self.mainBox.setColumnStretch(1,0)
|
||||||
self.mainBox.setColumnStretch(2,0)
|
self.mainBox.setColumnStretch(2,0)
|
||||||
self.mainBox.setColumnStretch(3,0)
|
self.mainBox.setColumnStretch(3,0)
|
||||||
|
self.mainBox.setColumnStretch(4,0)
|
||||||
self.mainBox.setContentsMargins(0,0,0,0)
|
self.mainBox.setContentsMargins(0,0,0,0)
|
||||||
|
|
||||||
logger.debug("GuiSearchBar initialisation complete")
|
logger.debug("GuiSearchBar initialisation complete")
|
||||||
@@ -66,4 +74,16 @@ class GuiSearchBar(QFrame):
|
|||||||
def getSearchText(self):
|
def getSearchText(self):
|
||||||
return self.searchBox.text()
|
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
|
# END Class GuiSearchBar
|
||||||
|
|||||||
Reference in New Issue
Block a user