Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve console help
  • Loading branch information
YoannQDQ authored and nyalldawson committed May 25, 2023
1 parent c286091 commit 88c3c41
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 18 deletions.
46 changes: 38 additions & 8 deletions python/console/console_output.py
Expand Up @@ -19,16 +19,12 @@
Some portions of code were taken from https://code.google.com/p/pydee/
"""

from qgis.PyQt.QtCore import Qt, QCoreApplication, QThread, QMetaObject, Q_RETURN_ARG, Q_ARG, QObject, pyqtSlot
from qgis.PyQt.QtGui import QColor, QFont, QKeySequence, QFontDatabase
from qgis.PyQt.QtCore import Qt, QCoreApplication, QThread, QMetaObject, Q_ARG, QObject, pyqtSlot
from qgis.PyQt.QtGui import QColor, QKeySequence
from qgis.PyQt.QtWidgets import QGridLayout, QSpacerItem, QSizePolicy, QShortcut, QMenu, QApplication
from qgis.PyQt.Qsci import QsciScintilla
from qgis.core import Qgis, QgsApplication, QgsSettings
from qgis.gui import (
QgsMessageBar,
QgsCodeEditorPython,
QgsCodeInterpreter
)
from qgis.gui import QgsMessageBar, QgsCodeEditorPython
import sys


Expand Down Expand Up @@ -85,6 +81,36 @@ def isatty(self):
return False


FULL_HELP_TEXT = QCoreApplication.translate("PythonConsole", """QGIS Python Console
======================================
The console is a Python interpreter that allows you to execute python commands.
Modules from QGIS (analysis, core, gui, 3d) and Qt (QtCore, QtGui, QtNetwork,
QtWidgets, QtXml) as well as Python's math, os, re and sys modules are already
imported and can be used directly.
Useful variables:
- iface.mainWindow() will return the Qt Main Window
- iface.mapCanvas() will return the map canvas
- iface.layerTreeView() will return the Layer Tree
- iface.activeLayer() will return the active layer
- QgsProject.instance() will return the current project
From the console, you can type the following special commands:
- _pyqgis, _pyqgis(object): Open the QGIS Python API (or the Qt documentation) in a web browser
- _api, _api(object): Open the QGIS C++ API (or the Qt documentation) in a web browser
- _cookbook: Open the PyQGIS Developer Cookbook in a web browser
- System commands: Any command starting with an exclamation mark (!) will be executed by the system shell. Examples:
!gdalinfo --formats: List all available GDAL drivers
!ogr2ogr --help: Show help for the ogr2ogr command
!ping www.qgis.org: Ping the QGIS website
!pip install black: install black python formatter using pip (if available)
- ?: Show this help
""")


class ShellOutputScintilla(QgsCodeEditorPython):

def __init__(self, parent=None):
Expand Down Expand Up @@ -130,7 +156,7 @@ def __init__(self, parent=None):
def insertInitText(self):
txtInit = QCoreApplication.translate("PythonConsole",
"Python Console\n"
"Use iface to access QGIS API interface or type help(iface) for more info\n"
"Use iface to access QGIS API interface or type '?' for more info\n"
"Security warning: typing commands from an untrusted source can harm your computer")

txtInit = '\n'.join(['# ' + line for line in txtInit.split('\n')])
Expand All @@ -143,6 +169,10 @@ def insertInitText(self):
else:
self.setText(txtInit + '\n')

def insertHelp(self):
self.append(FULL_HELP_TEXT)
self.moveCursorToEnd()

def initializeLexer(self):
super().initializeLexer()
self.setFoldingVisible(False)
Expand Down
88 changes: 78 additions & 10 deletions python/console/console_sci.py
Expand Up @@ -68,6 +68,71 @@
"from qgis.PyQt.QtWidgets import *",
"from qgis.PyQt.QtNetwork import *",
"from qgis.PyQt.QtXml import *",

r"""
def __parse_object(object=None):
if not object:
return None
import inspect
if inspect.isclass(object):
str_class = str(object)
else:
str_class = str(object.__class__)
qgis_api_pattern = r".*qgis\._(\w+)\.(\w+).*"
match = re.match(qgis_api_pattern, str_class)
if match:
module = match[1]
obj = match[2]
return 'qgis', module, obj
pyqt_pattern = r".*PyQt5\.(\w+)\.(\w+).*"
match = re.match(pyqt_pattern, str_class)
if match:
module = match[1]
obj = match[2]
return 'qt', module, obj
""",
r"""
def _api(object=None):
'''
Link to the QGIS API documentation for the given object.
If no object is given, the main API page is opened.
If the object is not part of the QGIS API but is a Qt object the Qt documentation is opened.
'''
import webbrowser
api = __parse_object(object)
version = '' if 'master' in Qgis.QGIS_VERSION.lower() else re.findall(r'^\d.[0-9]*', Qgis.QGIS_VERSION)[0]
if not api:
webbrowser.open(f"https://qgis.org/api/{version}")
elif api[0] == 'qgis':
webbrowser.open(f"https://api.qgis.org/api/{version}/class{api[2]}.html")
elif api[0] == 'qt':
qtversion = '.'.join(qVersion().split(".")[:2])
webbrowser.open(f"https://doc.qt.io/qt-{qtversion}/{api[2].lower()}.html")
""",
r"""
def _pyqgis(object=None):
'''
Link to the PyQGIS API documentation for the given object.
If no object is given, the main PyQGIS API page is opened.
If the object is not part of the QGIS API but is a Qt object the Qt documentation is opened.
'''
import webbrowser
api = __parse_object(object)
version = 'master' if 'master' in Qgis.QGIS_VERSION.lower() else re.findall(r'^\d.[0-9]*', Qgis.QGIS_VERSION)[0]
if not api:
webbrowser.open(f"https://qgis.org/pyqgis/{version}")
elif api[0] == 'qgis':
webbrowser.open(f"https://qgis.org/pyqgis/{version}/{api[1]}/{api[2]}.html")
elif api[0] == 'qt':
qtversion = '.'.join(qVersion().split(".")[:2])
webbrowser.open(f"https://doc.qt.io/qt-{qtversion}/{api[2].lower()}.html")
"""
]


Expand Down Expand Up @@ -141,19 +206,22 @@ def execCommandImpl(self, cmd, show_input=True):
self.sub_process.finished.connect(self.processFinished)
return 0

res = 0

import webbrowser
version = 'master' if 'master' in Qgis.QGIS_VERSION.lower() else \
re.findall(r'^\d.[0-9]*', Qgis.QGIS_VERSION)[0]
if cmd in ('_pyqgis', '_api', '_cookbook'):
if cmd == '_pyqgis':
webbrowser.open("https://qgis.org/pyqgis/{}".format(version))
elif cmd == '_api':
webbrowser.open(
"https://qgis.org/api/{}".format('' if version == 'master' else version))
elif cmd == '_cookbook':
webbrowser.open(
"https://docs.qgis.org/{}/en/docs/pyqgis_developer_cookbook/".format(
'testing' if version == 'master' else version))

if cmd == "?":
self.shell.parent.shellOut.insertHelp()
elif cmd == '_pyqgis':
webbrowser.open("https://qgis.org/pyqgis/{}".format(version))
elif cmd == '_api':
webbrowser.open("https://qgis.org/api/{}".format('' if version == 'master' else version))
elif cmd == '_cookbook':
webbrowser.open(
"https://docs.qgis.org/{}/en/docs/pyqgis_developer_cookbook/".format(
'testing' if version == 'master' else version))
else:
self.buffer.append(cmd)
src = "\n".join(self.buffer)
Expand Down

0 comments on commit 88c3c41

Please sign in to comment.