Merge pull request #49 from vkbo/search

Simple Search Function
This commit is contained in:
Veronica K. Berglyd Olsen
2019-09-29 18:59:46 +02:00
committed by GitHub
6 changed files with 165 additions and 4 deletions
+3
View File
@@ -65,6 +65,9 @@ class nwDocAction(Enum):
D_QUOTE = 10
SEL_ALL = 11
SEL_PARA = 12
FIND = 13
GO_NEXT = 14
GO_PREV = 15
# 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)
+37 -1
View File
@@ -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
@@ -279,6 +279,9 @@ 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()
elif theAction == nwDocAction.GO_PREV: self._findPrev()
else:
logger.error("Unknown or unsupported document action %s" % str(theAction))
return False
@@ -485,4 +488,37 @@ class GuiDocEditor(QTextEdit):
self.setTextCursor(theCursor)
return
def _beginSearch(self):
theCursor = self.textCursor()
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()
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
+24
View File
@@ -398,6 +398,30 @@ 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-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()
# Edit > Select All
menuItem = QAction(QIcon.fromTheme("edit-select-all"), "Select All", self)
menuItem.setStatusTip("Select all text in document")
+89
View File
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
"""novelWriter GUI Main Window SearchBar
novelWriter GUI Main Window SearchBar
=========================================
Class holding the main window search bar
File History:
Created: 2019-09-29 [0.2.1]
"""
import logging
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):
def __init__(self, theParent):
QFrame.__init__(self, theParent)
logger.debug("Initialising GuiSearchBar ...")
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.closeButton = QPushButton(QIcon.fromTheme("edit-delete"),"")
self.searchButton = QPushButton(QIcon.fromTheme("edit-find"),"")
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")
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()
##
# Internal Functions
##
def _doClose(self):
self.setVisible(False)
return
def _doSearch(self):
self.theParent.docEditor.docAction(nwDocAction.GO_NEXT)
return
# 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)