Skip to content
Permalink
Browse files

[FEATURE][console] Improvements to find functionality

- add key shortcut to open find widget, set focus to find text input
automatically (uses default shortcut for DE, eg usually Ctrl+F)
- add key shortcuts for find next/previous (using default DE
shortcut, eg Ctrl+G/Shift+Ctrl+G)
- automatically find first match when typing in find box
- set initial find string to selection when opening find
- pressing Escape closes the find bar
  • Loading branch information
nyalldawson committed Apr 14, 2016
1 parent d8a314c commit 99210ecfd713f4380125c4ccfa0908142c8ee3a9
Showing with 62 additions and 19 deletions.
  1. +29 −8 python/console/console.py
  2. +33 −11 python/console/console_editor.py
@@ -22,7 +22,7 @@

from PyQt.QtCore import Qt, QTimer, QSettings, QCoreApplication, QSize, QByteArray, QFileInfo, QUrl, QDir
from PyQt.QtWidgets import QDockWidget, QToolBar, QToolButton, QWidget, QSplitter, QTreeWidget, QAction, QFileDialog, QCheckBox, QSizePolicy, QMenu, QGridLayout, QApplication
from PyQt.QtGui import QDesktopServices
from PyQt.QtGui import QDesktopServices, QKeySequence, QShortcut
from PyQt.QtWidgets import QVBoxLayout
from qgis.utils import iface
from .console_sci import ShellScintilla
@@ -501,7 +501,7 @@ def __init__(self, parent=None):

##------------ Signal -------------------------------

self.findTextButton.toggled.connect(self.findTextEditor)
self.findTextButton.triggered.connect(self._toggleFind)
self.objectListButton.toggled.connect(self.toggleObjectListWidget)
self.commentEditorButton.triggered.connect(self.commentCode)
self.uncommentEditorButton.triggered.connect(self.uncommentCode)
@@ -519,13 +519,36 @@ def __init__(self, parent=None):
self.saveAsFileButton.triggered.connect(self.saveAsScriptFile)
self.helpButton.triggered.connect(self.openHelp)
self.listClassMethod.itemClicked.connect(self.onClickGoToLine)
self.lineEditFind.returnPressed.connect(self._findText)
self.lineEditFind.returnPressed.connect(self._findNext)
self.findNextButton.clicked.connect(self._findNext)
self.findPrevButton.clicked.connect(self._findPrev)
self.lineEditFind.textChanged.connect(self._textFindChanged)

def _findText(self):
self.tabEditorWidget.currentWidget().newEditor.findText(True)
self.findScut = QShortcut(QKeySequence.Find, self.widgetEditor)
self.findScut.setContext(Qt.WidgetWithChildrenShortcut)
self.findScut.activated.connect(self._openFind)

self.findNextScut = QShortcut(QKeySequence.FindNext, self.widgetEditor)
self.findNextScut.setContext(Qt.WidgetWithChildrenShortcut)
self.findNextScut.activated.connect(self._findNext)

self.findPreviousScut = QShortcut(QKeySequence.FindPrevious, self.widgetEditor)
self.findPreviousScut.setContext(Qt.WidgetWithChildrenShortcut)
self.findPreviousScut.activated.connect(self._findPrev)

# Escape on editor hides the find bar
self.findScut = QShortcut(Qt.Key_Escape, self.widgetEditor)
self.findScut.setContext(Qt.WidgetWithChildrenShortcut)
self.findScut.activated.connect(self._closeFind)

def _toggleFind(self):
self.tabEditorWidget.currentWidget().newEditor.toggleFindWidget()

def _openFind(self):
self.tabEditorWidget.currentWidget().newEditor.openFindWidget()

def _closeFind(self):
self.tabEditorWidget.currentWidget().newEditor.closeFindWidget()

def _findNext(self):
self.tabEditorWidget.currentWidget().newEditor.findText(True)
@@ -537,6 +560,7 @@ def _textFindChanged(self):
if self.lineEditFind.text():
self.findNextButton.setEnabled(True)
self.findPrevButton.setEnabled(True)
self.tabEditorWidget.currentWidget().newEditor.findText(True, showMessage=False, findFirst=True)
else:
self.lineEditFind.setStyleSheet('')
self.findNextButton.setEnabled(False)
@@ -567,9 +591,6 @@ def toggleEditor(self, checked):
def toggleObjectListWidget(self, checked):
self.listClassMethod.show() if checked else self.listClassMethod.hide()

def findTextEditor(self, checked):
self.widgetFind.show() if checked else self.widgetFind.hide()

def pasteEditor(self):
self.tabEditorWidget.currentWidget().newEditor.paste()

@@ -320,7 +320,7 @@ def contextMenuEvent(self, e):
menu.addSeparator()
menu.addAction(iconFind,
QCoreApplication.translate("PythonConsole", "Find Text"),
self.showFindWidget)
self.openFindWidget)
cutAction = menu.addAction(iconCut,
QCoreApplication.translate("PythonConsole", "Cut"),
self.cut, QKeySequence.Cut)
@@ -380,9 +380,13 @@ def contextMenuEvent(self, e):
showCodeInspection.setEnabled(True)
menu.exec_(self.mapToGlobal(e.pos()))

def findText(self, forward):
def findText(self, forward, showMessage=True, findFirst=False):
lineFrom, indexFrom, lineTo, indexTo = self.getSelection()
line, index = self.getCursorPosition()
if findFirst:
line = 0
index = 0
else:
line, index = self.getCursorPosition()
text = self.parent.pc.lineEditFind.text()
re = False
wrap = self.parent.pc.wrapAround.isChecked()
@@ -400,13 +404,20 @@ def findText(self, forward):
if notFound:
styleError = 'QLineEdit {background-color: #d65253; \
color: #ffffff;}'
msgText = QCoreApplication.translate('PythonConsole',
'<b>"{0}"</b> was not found.').format(text)
self.parent.pc.callWidgetMessageBarEditor(msgText, 0, True)
if showMessage:
msgText = QCoreApplication.translate('PythonConsole',
'<b>"{0}"</b> was not found.').format(text)
self.parent.pc.callWidgetMessageBarEditor(msgText, 0, True)
else:
styleError = ''
self.parent.pc.lineEditFind.setStyleSheet(styleError)

def findNext(self):
self.findText(True)

def findPrevious(self):
self.findText(False)

def objectListEditor(self):
listObj = self.parent.pc.listClassMethod
if listObj.isVisible():
@@ -451,14 +462,25 @@ def hideEditor(self):
self.parent.pc.splitterObj.hide()
self.parent.pc.showEditorButton.setChecked(False)

def showFindWidget(self):
def openFindWidget(self):
wF = self.parent.pc.widgetFind
wF.show()
if self.hasSelectedText():
self.parent.pc.lineEditFind.setText(self.selectedText().strip())
self.parent.pc.lineEditFind.setFocus()
self.parent.pc.findTextButton.setChecked(True)

def closeFindWidget(self):
wF = self.parent.pc.widgetFind
wF.hide()
self.parent.pc.findTextButton.setChecked(False)

def toggleFindWidget(self):
wF = self.parent.pc.widgetFind
if wF.isVisible():
wF.hide()
self.parent.pc.findTextButton.setChecked(False)
self.closeFindWidget()
else:
wF.show()
self.parent.pc.findTextButton.setChecked(True)
self.openFindWidget()

def commentEditorCode(self, commentCheck):
self.beginUndoAction()

0 comments on commit 99210ec

Please sign in to comment.
You can’t perform that action at this time.