Skip to content

Commit

Permalink
Merge pull request #49 from vkbo/search
Browse files Browse the repository at this point in the history
Simple Search Function
  • Loading branch information
vkbo authored Sep 29, 2019
2 parents 4467661 + cb2ee95 commit 5d93b43
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 4 deletions.
3 changes: 3 additions & 0 deletions nw/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion nw/gui/docdetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, theParent, theProject):
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)

Expand Down
38 changes: 37 additions & 1 deletion nw/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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
Expand Down Expand Up @@ -279,6 +279,9 @@ def docAction(self, theAction):
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
Expand Down Expand Up @@ -485,4 +488,37 @@ def _makeSelection(self, selMode):
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 changes: 24 additions & 0 deletions nw/gui/mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,30 @@ def _buildEditMenu(self):
# 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")
Expand Down
89 changes: 89 additions & 0 deletions nw/gui/searchbar.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 11 additions & 2 deletions nw/gui/winmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(self):
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)

Expand All @@ -83,8 +85,14 @@ def __init__(self):
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)

Expand All @@ -98,7 +106,7 @@ def __init__(self):

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)
Expand All @@ -107,6 +115,7 @@ def __init__(self):
self.splitView.setCollapsible(self.idxViewer, True)

self.docViewer.setVisible(False)
self.searchBar.setVisible(False)

# Build The Tree View
self.treeView.itemSelectionChanged.connect(self._treeSingleClick)
Expand Down

0 comments on commit 5d93b43

Please sign in to comment.