Search functionality mostly working again

This commit is contained in:
Veronica K. B. Olsen
2020-06-15 22:24:50 +02:00
parent 65492b13b7
commit a4108222b0
+65 -23
View File
@@ -41,7 +41,7 @@ from PyQt5.QtGui import (
) )
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
qApp, QTextEdit, QAction, QMenu, QShortcut, QMessageBox, QWidget, QLabel, qApp, QTextEdit, QAction, QMenu, QShortcut, QMessageBox, QWidget, QLabel,
QToolButton, QHBoxLayout, QGridLayout, QLineEdit, QPushButton, QFrame, QVBoxLayout QToolButton, QHBoxLayout, QGridLayout, QLineEdit, QPushButton, QFrame, QVBoxLayout, QSizePolicy
) )
from nw.core import NWDoc from nw.core import NWDoc
@@ -337,9 +337,13 @@ class GuiDocEditor(QTextEdit):
tH = self.docHeader.height() tH = self.docHeader.height()
tT = cM - tH tT = cM - tH
rH = self.docSearch.height() if self.docSearch.isVisible():
rW = self.docSearch.width() rH = self.docSearch.height()
rL = wW - sW - rW - tB rW = self.docSearch.width()
rL = wW - sW - rW - tB
else:
rH = 0
rL = 0
self.docHeader.setGeometry(tB, tB, tW, tH) self.docHeader.setGeometry(tB, tB, tW, tH)
self.docSearch.move(rL, tB) self.docSearch.move(rL, tB)
@@ -581,8 +585,7 @@ class GuiDocEditor(QTextEdit):
def closeSearch(self): def closeSearch(self):
"""Close the search box. """Close the search box.
""" """
self.docSearch.setVisible(False) self.docSearch.closeSearch()
self.updateDocMargins()
return self.docSearch.isVisible() return self.docSearch.isVisible()
## ##
@@ -1159,24 +1162,36 @@ class GuiDocEditor(QTextEdit):
"""Searches for the next occurrence of the search bar text in """Searches for the next occurrence of the search bar text in
the document. Wraps back to the top if not found. the document. Wraps back to the top if not found.
""" """
if not self.docSearch.isVisible():
self._beginSearch()
return
searchFor = self.docSearch.getSearchText() searchFor = self.docSearch.getSearchText()
wasFound = self.find(searchFor) wasFound = self.find(searchFor)
if not wasFound: if not wasFound:
theCursor = self.textCursor() theCursor = self.textCursor()
theCursor.movePosition(QTextCursor.Start) theCursor.movePosition(QTextCursor.Start)
self.setTextCursor(theCursor) self.setTextCursor(theCursor)
self.find(searchFor)
return return
def _findPrev(self): def _findPrev(self):
"""Searches for the previous occurrence of the search bar text """Searches for the previous occurrence of the search bar text
in the document. Wraps back to the end if not found. in the document. Wraps back to the end if not found.
""" """
if not self.docSearch.isVisible():
self._beginSearch()
return
searchFor = self.docSearch.getSearchText() searchFor = self.docSearch.getSearchText()
wasFound = self.find(searchFor, QTextDocument.FindBackward) wasFound = self.find(searchFor, QTextDocument.FindBackward)
if not wasFound: if not wasFound:
theCursor = self.textCursor() theCursor = self.textCursor()
theCursor.movePosition(QTextCursor.End) theCursor.movePosition(QTextCursor.End)
self.setTextCursor(theCursor) self.setTextCursor(theCursor)
self.find(searchFor, QTextDocument.FindBackward)
return return
def _replaceNext(self): def _replaceNext(self):
@@ -1184,6 +1199,10 @@ class GuiDocEditor(QTextEdit):
the document and replaces it with the replace text. Wraps back the document and replaces it with the replace text. Wraps back
to the top if not found. to the top if not found.
""" """
if not self.docSearch.isVisible():
self._beginSearch()
return
theCursor = self.textCursor() theCursor = self.textCursor()
searchFor = self.docSearch.getSearchText() searchFor = self.docSearch.getSearchText()
replWith = self.docSearch.getReplaceText() replWith = self.docSearch.getReplaceText()
@@ -1200,6 +1219,7 @@ class GuiDocEditor(QTextEdit):
)) ))
if searchFor != "": if searchFor != "":
self._findNext() self._findNext()
return return
def _setupSpellChecking(self): def _setupSpellChecking(self):
@@ -1266,32 +1286,45 @@ class GuiDocEditSearch(QWidget):
self.repVisible = False self.repVisible = False
mPx = self.mainConf.pxInt(6)
fPx = int(0.9*self.theTheme.fontPixelSize) fPx = int(0.9*self.theTheme.fontPixelSize)
boxFont = self.theTheme.guiFont boxFont = self.theTheme.guiFont
boxFont.setPointSizeF(0.9*self.theTheme.fontPointSize) boxFont.setPointSizeF(0.9*self.theTheme.fontPointSize)
# self.setContentsMargins(0, 0, 0, 0) self.setContentsMargins(mPx, mPx, mPx, mPx)
# self.setFrameStyle(QFrame.Box)
self.setAutoFillBackground(True) self.setAutoFillBackground(True)
self.mainBox = QGridLayout(self) self.mainBox = QGridLayout(self)
self.setLayout(self.mainBox) self.setLayout(self.mainBox)
# Text Boxes
# ==========
self.searchBox = QLineEdit() self.searchBox = QLineEdit()
self.searchBox.setFont(boxFont) self.searchBox.setFont(boxFont)
self.searchBox.returnPressed.connect(self._doSearch)
self.replaceBox = QLineEdit() self.replaceBox = QLineEdit()
self.replaceBox.setFont(boxFont) self.replaceBox.setFont(boxFont)
self.replaceBox.returnPressed.connect(self._doSearch)
# Buttons
# =======
bPx = self.searchBox.sizeHint().height()
self.searchButton = QPushButton(self.theTheme.getIcon("search"),"")
self.searchButton.setFixedSize(QSize(bPx, bPx))
self.searchButton.setToolTip("Find in current document")
self.searchButton.clicked.connect(self._doSearch)
self.replaceButton = QPushButton(self.theTheme.getIcon("search-replace"),"")
self.replaceButton.setFixedSize(QSize(bPx, bPx))
self.replaceButton.setToolTip("Find and replace in current document")
self.replaceButton.clicked.connect(self._doReplace)
self.closeButton = QPushButton(self.theTheme.getIcon("close"),"") self.closeButton = QPushButton(self.theTheme.getIcon("close"),"")
self.searchButton = QPushButton(self.theTheme.getIcon("search"),"") self.closeButton.setFixedSize(QSize(bPx, bPx))
self.replaceButton = QPushButton(self.theTheme.getIcon("search-replace"),"") self.closeButton.setToolTip("Close search tool")
self.closeButton.clicked.connect(self._doClose) self.closeButton.clicked.connect(self._doClose)
self.searchButton.clicked.connect(self._doSearch)
self.replaceButton.clicked.connect(self._doReplace)
self.searchBox.returnPressed.connect(self._doSearch)
self.replaceBox.returnPressed.connect(self._doSearch)
self.mainBox.addWidget(self.searchBox, 0, 0) self.mainBox.addWidget(self.searchBox, 0, 0)
self.mainBox.addWidget(self.searchButton, 0, 1) self.mainBox.addWidget(self.searchButton, 0, 1)
@@ -1304,20 +1337,29 @@ class GuiDocEditSearch(QWidget):
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.setColumnStretch(4, 0)
self.mainBox.setVerticalSpacing(0) self.mainBox.setSpacing(self.mainConf.pxInt(2))
self.mainBox.setHorizontalSpacing(2) self.mainBox.setContentsMargins(0, 0, 0, 0)
# self.mainBox.setContentsMargins(0, 0, 0, 0)
boxWidth = 16*self.theTheme.textNWidth boxWidth = 18*self.theTheme.textNWidth
self.searchBox.setFixedWidth(boxWidth) self.searchBox.setFixedWidth(boxWidth)
self.replaceBox.setFixedWidth(boxWidth) self.replaceBox.setFixedWidth(boxWidth)
self.adjustSize()
# self._replaceVisible(False) self._replaceVisible(False)
logger.debug("GuiDocEditSearch initialisation complete") logger.debug("GuiDocEditSearch initialisation complete")
return return
def closeSearch(self):
"""Close the search box.
"""
self._replaceVisible(False)
self.setVisible(False)
self.docEditor.updateDocMargins()
self.docEditor.setFocus()
return
## ##
# Get and Set Functions # Get and Set Functions
## ##
@@ -1358,8 +1400,7 @@ class GuiDocEditSearch(QWidget):
def _doClose(self): def _doClose(self):
"""Hide the search/replace bar. """Hide the search/replace bar.
""" """
self._replaceVisible(False) self.closeSearch()
self.docEditor.closeSearch()
return return
def _doSearch(self): def _doSearch(self):
@@ -1388,6 +1429,7 @@ class GuiDocEditSearch(QWidget):
self.replaceBox.setVisible(isVisible) self.replaceBox.setVisible(isVisible)
self.replaceButton.setVisible(isVisible) self.replaceButton.setVisible(isVisible)
self.repVisible = isVisible self.repVisible = isVisible
self.adjustSize()
return True return True
# END Class GuiDocEditSearch # END Class GuiDocEditSearch