Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python console interactive help #58962

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

YoannQDQ
Copy link
Contributor

@YoannQDQ YoannQDQ commented Oct 3, 2024

Description

Add a shortcut in python editors to open the API

When pressing F1, open the API page matching

  • selected text if any OR
  • word at the mouse position (if enabled in settings, see below) OR
  • word at the text cursor position

Behavior is slighltly different for the Python Console (and editors) and for the others generic QgsCodeEditorPython (e.g Expression Editor).

Generic editors

This PR only improves the already existing "Search in PyQGIS Documentation" action in the context menu to add the F1 shortcut and the word under mouse logic. Besides, if the text hovered matches a Qt class name, the Qt API is opened at the correct page. PyQGIS documentation urls cannot be deduced from the class name only as they also include the module name, so pressing F1 will only search the PyQGIS documentation.

Python console (where the PR truly shines)

Add a widget in the devtools panel to display the API documentation (PyQGIS and Qt)

api_panel

The buttons at the top of the widget access the home pages of the APIs.
The button at the top right opens the current URL in the system web browser

When pressing F1 while hovering a symbol on the Python Console or Editors tab, the object is evaluated to get its class and module. The relevant API (PyQGIS or Qt) is then opened at the correct page. It works for class names, class, or even objects

Consider the following snippet:

myLayer = iface.activeLayer() 
if isinstance(myLayer, QgsRasterLayer):
  print("It is a QgsRasterLayer")
a = QLabel()

Let's say the active layer is a vector layer. With the text cursor at the following words, pressing F1 will

  • myLayer: open the PyQGIS api page for QgsVectorLayer.
  • iface: open the PyQGIS api page for QgisInterface.
  • QLabel: open the Qt api page for QLabel
  • QgsRasterLayer (in the print statement or in the type check): open the Qt api page for QgsRasterLayer

If the symbol is not recognized, open the PyQGIS API home page.

Settings

context_help_settings

  • Open In: how should the API html page be displayed?
    • Embedded Webview
    • Default system Web browser
  • F1 on Hover: whether pressing F1 should trigger the context help for the hovered word or for the word at the textCursor

Demo

console-interactive-help-demo

@github-actions github-actions bot added this to the 3.40.0 milestone Oct 3, 2024
@nyalldawson
Copy link
Collaborator

Nice work @YoannQDQ !!

which API should be displayed

Is there a compelling reason we'd want to use C++ now? In the last month the PyQGIS docs.have greatly improved, and I'd argue they are at least as good as the C++ ones now...

PyQt doc is often incomplete)

This is the right call for the qt docs 👍

Copy link

github-actions bot commented Oct 3, 2024

🪟 Windows builds ready!

Windows builds of this PR are available for testing here. Debug symbols for this build are available here.

(Built from commit d1bdd2b)

@uclaros
Copy link
Contributor

uclaros commented Oct 4, 2024

This is great!
I find the hover + F1 functionality a little counter intuitive, though.
I would expect the word at text cursor position to always take precedence.

@YoannQDQ
Copy link
Contributor Author

YoannQDQ commented Oct 4, 2024

Is there a compelling reason we'd want to use C++ now? In the last month the PyQGIS docs.have greatly improved, and I'd argue they are at least as good as the C++ ones now...

Out of habit, I mostly read the C++ API doc myself, but I'm completely willing to letting it go. Should it also be removed from the help menu?

I find the hover + F1 functionality a little counter intuitive, though

I took it from QtCreator, but if it doesn't feel right, we can ditch it. I'd appreciate some more feedback on this.

@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch 2 times, most recently from d1bdd2b to b8bd03c Compare October 7, 2024 07:38
Copy link

github-actions bot commented Oct 7, 2024

🪟 Windows builds

Download Windows builds of this PR for testing.
Debug symbols for this build are available here.
(Built from commit c85d82c)

🪟 Windows Qt6 builds

Download Windows Qt6 builds of this PR for testing.
(Built from commit 206dcfb)

@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from 03fbd6e to 26aaf49 Compare October 7, 2024 17:10
@nyalldawson nyalldawson added the Frozen Feature freeze - Do not merge! label Oct 10, 2024
@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from 26aaf49 to 206dcfb Compare October 14, 2024 20:26
@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from 206dcfb to c85d82c Compare October 21, 2024 07:25
@uclaros
Copy link
Contributor

uclaros commented Oct 21, 2024

I took it from QtCreator, but if it doesn't feel right, we can ditch it. I'd appreciate some more feedback on this.

Just curious, what QtCreator version is this in? On my 13.0.2 the hovered item is ignored, and it makes perfect sense for me as the mouse cursor may be anywhere when navigating with the keyboard!

@YoannQDQ
Copy link
Contributor Author

It's an ancient version of qt creator that we keep using on a specific project for ungodly reasons... Namely QtCreator 4.11. This behavior has indeed disappeared in later versions.

@YoannQDQ
Copy link
Contributor Author

@nyalldawson The "search" function of the PyQGIS documentation works with QWebEngineView. Are there any plan to make it available in C++? Maybe integrate it to QgsWebView?

@nyalldawson
Copy link
Collaborator

@YoannQDQ

We should just be able to use the QWebEngineView class directly, without any need for a Qgs wrapper class. We'd just need to conditionally use it only in #ifdef HAVE_WEBENGINE blocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Frozen Feature freeze - Do not merge! Python Console Python Console
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shortcut to open pyqgis API from QGIS console/script
3 participants