Skip to content
Permalink
Browse files

python console: switch to pyqt wrappers and new style signals, flake8…

… fixes, show exceptions in console also with pyqt5
  • Loading branch information
jef-n committed Mar 21, 2016
1 parent 2526d3e commit 885d0ec99363ea0ff7f7a0999f81c4d2a2efafd6
@@ -23,4 +23,4 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from console import show_console
from .console import show_console # NOQA
@@ -20,16 +20,15 @@
"""
import os

from PyQt4.QtCore import Qt, QTimer, QSettings, QCoreApplication, QSize, QByteArray, QFileInfo, SIGNAL, QUrl, QDir
from PyQt4.QtGui import QDockWidget, QToolBar, QToolButton, QWidget,\
QSplitter, QTreeWidget, QAction, QFileDialog, QCheckBox, QSizePolicy, QMenu, QGridLayout, QApplication, \
QDesktopServices
from PyQt4.QtGui import QVBoxLayout
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.QtWidgets import QVBoxLayout
from qgis.utils import iface
from console_sci import ShellScintilla
from console_output import ShellOutputScintilla
from console_editor import EditorTabWidget
from console_settings import optionsDialog
from .console_sci import ShellScintilla
from .console_output import ShellOutputScintilla
from .console_editor import EditorTabWidget
from .console_settings import optionsDialog
from qgis.core import QgsApplication, QgsContextHelp
from qgis.gui import QgsFilterLineEdit

@@ -44,7 +43,7 @@ def show_console():
if _console is None:
parent = iface.mainWindow() if iface else None
_console = PythonConsole(parent)
_console.show() # force show even if it was restored as hidden
_console.show() # force show even if it was restored as hidden
# set focus to the console so the user can start typing
# defer the set focus event so it works also whether the console not visible yet
QTimer.singleShot(0, _console.activate)
@@ -59,7 +58,6 @@ def show_console():
QgsContextHelp.run("PythonConsole")
settings.setValue('pythonConsole/contextHelpOnFirstLaunch', False)

_old_stdout = sys.stdout
_console_output = None

# hook for python console so all output will be redirected
@@ -539,8 +537,7 @@ def __init__(self, parent=None):
self.saveFileButton.triggered.connect(self.saveScriptFile)
self.saveAsFileButton.triggered.connect(self.saveAsScriptFile)
self.helpButton.triggered.connect(self.openHelp)
self.connect(self.listClassMethod, SIGNAL('itemClicked(QTreeWidgetItem*, int)'),
self.onClickGoToLine)
self.listClassMethod.itemClicked.connect(self.onClickGoToLine)
self.lineEditFind.returnPressed.connect(self._findText)
self.findNextButton.clicked.connect(self._findNext)
self.findPrevButton.clicked.connect(self._findPrev)
@@ -21,11 +21,11 @@

import os

from PyQt4.Qsci import QsciAPIs
from PyQt4.QtGui import QDialog, QDialogButtonBox
from PyQt4.QtCore import QCoreApplication
from PyQt.Qsci import QsciAPIs
from PyQt.QtWidgets import QDialog, QDialogButtonBox
from PyQt.QtCore import QCoreApplication

from ui_console_compile_apis import Ui_APIsDialogPythonConsole
from .ui_console_compile_apis import Ui_APIsDialogPythonConsole


class PrepareAPIDialog(QDialog):
@@ -19,12 +19,10 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""

from PyQt4.QtCore import Qt, QObject, QEvent, QSettings, QCoreApplication, QFileInfo, QSize, SIGNAL
from PyQt4.QtGui import QFont, QFontMetrics, QColor, QShortcut, QKeySequence, QMenu, QApplication, QCursor, QWidget, QGridLayout, QSpacerItem, QSizePolicy, QFileDialog, QTabWidget, QTreeWidgetItem, QFrame, QLabel, QToolButton, QMessageBox
from PyQt4.Qsci import (QsciScintilla,
QsciLexerPython,
QsciAPIs,
QsciStyle)
from PyQt.QtCore import Qt, QObject, QEvent, QSettings, QCoreApplication, QFileInfo, QSize
from PyQt.QtGui import QFont, QFontMetrics, QColor, QKeySequence, QCursor
from PyQt.QtWidgets import QShortcut, QMenu, QApplication, QWidget, QGridLayout, QSpacerItem, QSizePolicy, QFileDialog, QTabWidget, QTreeWidgetItem, QFrame, QLabel, QToolButton, QMessageBox
from PyQt.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs, QsciStyle
from qgis.core import QgsApplication
from qgis.gui import QgsMessageBar
import sys
@@ -303,7 +301,7 @@ def contextMenuEvent(self, e):
menu.addAction(
QCoreApplication.translate("PythonConsole", "Hide Editor"),
self.hideEditor)
menu.addSeparator() # ------------------------------
menu.addSeparator() # ------------------------------
syntaxCheck = menu.addAction(iconSyntaxCk,
QCoreApplication.translate("PythonConsole", "Check Syntax"),
self.syntaxCheck, 'Ctrl+4')
@@ -561,8 +559,6 @@ def runScriptCode(self):
filename = tabWidget.path
msgEditorBlank = QCoreApplication.translate('PythonConsole',
'Hey, type something to run!')
msgEditorUnsaved = QCoreApplication.translate('PythonConsole',
'You have to save the file before running it.')
if filename is None:
if not self.isModified():
self.parent.pc.callWidgetMessageBarEditor(msgEditorBlank, 0, True)
@@ -895,8 +891,8 @@ def __init__(self, parent):
self.layoutTopFrame2.addWidget(self.clButton, 0, 2, 1, 1)

self.topFrame.hide()
self.connect(self.restoreTabsButton, SIGNAL('clicked()'), self.restoreTabs)
self.connect(self.clButton, SIGNAL('clicked()'), self.closeRestore)
self.restoreTabsButton.clicked.connect(self.restoreTabs)
self.clButton.clicked.connect(self.closeRestore)

## Fixes #7653
if sys.platform != 'darwin':
@@ -908,10 +904,8 @@ def __init__(self, parent):

# Menu button list tabs
self.fileTabMenu = QMenu()
self.connect(self.fileTabMenu, SIGNAL("aboutToShow()"),
self.showFileTabMenu)
self.connect(self.fileTabMenu, SIGNAL("triggered(QAction*)"),
self.showFileTabMenuTriggered)
self.fileTabMenu.aboutToShow.connect(self.showFileTabMenu)
self.fileTabMenu.triggered.connect(self.showFileTabMenuTriggered)
self.fileTabButton = QToolButton()
txtToolTipMenuFile = QCoreApplication.translate("PythonConsole",
"List all tabs")
@@ -922,8 +916,8 @@ def __init__(self, parent):
self.fileTabButton.setPopupMode(QToolButton.InstantPopup)
self.fileTabButton.setMenu(self.fileTabMenu)
self.setCornerWidget(self.fileTabButton, Qt.TopRightCorner)
self.connect(self, SIGNAL("tabCloseRequested(int)"), self._removeTab)
self.connect(self, SIGNAL('currentChanged(int)'), self._currentWidgetChanged)
self.tabCloseRequested.connect(self._removeTab)
self.currentChanged.connect(self._currentWidgetChanged)

# New Editor button
self.newTabButton = QToolButton()
@@ -934,7 +928,7 @@ def __init__(self, parent):
self.newTabButton.setIcon(QgsApplication.getThemeIcon("console/iconNewTabEditorConsole.png"))
self.newTabButton.setIconSize(QSize(24, 24))
self.setCornerWidget(self.newTabButton, Qt.TopLeftCorner)
self.connect(self.newTabButton, SIGNAL('clicked()'), self.newTabEditor)
self.newTabButton.clicked.connect(self.newTabEditor)

def _currentWidgetChanged(self, tab):
if self.settings.value("pythonConsole/enableObjectInsp",
@@ -19,9 +19,10 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""

from PyQt4.QtCore import Qt, QCoreApplication, QSettings, SIGNAL
from PyQt4.QtGui import QColor, QGridLayout, QSpacerItem, QSizePolicy, QFont, QShortcut, QKeySequence, QMenu, QApplication
from PyQt4.Qsci import QsciScintilla, QsciLexerPython
from PyQt.QtCore import Qt, QCoreApplication, QSettings
from PyQt.QtGui import QColor, QFont, QKeySequence
from PyQt.QtWidgets import QGridLayout, QSpacerItem, QSizePolicy, QShortcut, QMenu, QApplication
from PyQt.Qsci import QsciScintilla, QsciLexerPython
from qgis.core import QgsApplication
from qgis.gui import QgsMessageBar
import sys
@@ -265,10 +266,10 @@ def copy(self):
"""Copy text to clipboard... or keyboard interrupt"""
if self.hasSelectedText():
text = unicode(self.selectedText())
text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts
text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts
QApplication.clipboard().setText(text)
else:
self.emit(SIGNAL("keyboard_interrupt()"))
raise KeyboardInterrupt

def enteredSelected(self):
cmd = self.selectedText()
@@ -19,18 +19,20 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""

from PyQt4.QtCore import Qt, QSettings, QByteArray, QCoreApplication, QFile, QSize
from PyQt4.QtGui import QDialog, QMenu, QColor, QShortcut, QKeySequence, QFont, QFontMetrics, QStandardItemModel, QStandardItem, QApplication, QClipboard
from PyQt4.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs
from PyQt.QtCore import Qt, QSettings, QByteArray, QCoreApplication, QFile, QSize
from PyQt.QtWidgets import QDialog, QMenu, QShortcut, QApplication
from PyQt.QtGui import QColor, QKeySequence, QFont, QFontMetrics, QStandardItemModel, QStandardItem, QClipboard
from PyQt.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs

import sys
import os
import code
import codecs
import re
import traceback

from qgis.core import QgsApplication
from ui_console_history_dlg import Ui_HistoryDialogPythonConsole
from .ui_console_history_dlg import Ui_HistoryDialogPythonConsole

_init_commands = ["from qgis.core import *", "import qgis.utils",
"from qgis.utils import iface"]
@@ -544,7 +546,7 @@ def dropEvent(self, e):
e.setDropAction(Qt.CopyAction)
e.accept()
else:
QsciScintillaCompat.dropEvent(self, e) # FIXME
QsciScintilla.dropEvent(self, e)

def insertFromDropPaste(self, textDP):
pasteList = unicode(textDP).splitlines()
@@ -596,7 +598,7 @@ def runCommand(self, cmd):
else:
self.buffer.append(cmd)
src = u"\n".join(self.buffer)
more = self.runsource(src, "<input>")
more = self.runsource(src)
if not more:
self.buffer = []
## prevents to commands with more lines to break the console
@@ -614,6 +616,18 @@ def writeCMD(self, txt):
prompt = getCmdString[0:4]
sys.stdout.write(prompt + txt + '\n')

def runsource(self, source, filename='<input>', symbol='single'):
hook = sys.excepthook
try:
def excepthook(etype, value, tb):
self.write(u"".join(traceback.format_exception(etype, value, tb)))

sys.excepthook = excepthook

super(ShellScintilla, self).runsource(source, filename, symbol)
finally:
sys.excepthook = hook


class HistoryDialog(QDialog, Ui_HistoryDialogPythonConsole):

@@ -19,11 +19,12 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""

from PyQt4.QtCore import QCoreApplication, SIGNAL, QSize, QSettings, QFileInfo, Qt
from PyQt4.QtGui import QDialog, QIcon, QFileDialog, QMessageBox, QTableWidgetItem, QFont, QColor
from console_compile_apis import PrepareAPIDialog
from PyQt.QtCore import QCoreApplication, QSize, QSettings, QFileInfo, Qt
from PyQt.QtWidgets import QDialog, QFileDialog, QMessageBox, QTableWidgetItem
from PyQt.QtGui import QIcon, QFont, QColor
from .console_compile_apis import PrepareAPIDialog

from ui_console_settings import Ui_SettingsDialogPythonConsole
from .ui_console_settings import Ui_SettingsDialogPythonConsole


class optionsDialog(QDialog, Ui_SettingsDialogPythonConsole):
@@ -45,12 +46,9 @@ def __init__(self, parent):
self.removeAPIpath.setIcon(QIcon(":/images/themes/default/symbologyRemove.svg"))
self.removeAPIpath.setToolTip(QCoreApplication.translate("PythonConsole", "Remove API path"))

self.connect(self.preloadAPI,
SIGNAL("stateChanged(int)"), self.initialCheck)
self.connect(self.addAPIpath,
SIGNAL("clicked()"), self.loadAPIFile)
self.connect(self.removeAPIpath,
SIGNAL("clicked()"), self.removeAPI)
self.preloadAPI.stateChanged.connect(self.initialCheck)
self.addAPIpath.clicked.connect(self.loadAPIFile)
self.removeAPIpath.clicked.connect(self.removeAPI)
self.compileAPIs.clicked.connect(self._prepareAPI)

self.resetFontColor.setIcon(QIcon(":/images/themes/default/console/iconResetColorConsole.png"))

0 comments on commit 885d0ec

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