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

Merged
merged 12 commits into from
Nov 13, 2024

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 c790356)

🪟 Windows Qt6 builds

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

@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 2 times, most recently 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

@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from c85d82c to 3781162 Compare October 28, 2024 22:09
src/gui/codeeditors/qgscodeeditor.h Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditor.h Outdated Show resolved Hide resolved
src/app/qgisapp.cpp Outdated Show resolved Hide resolved
src/ui/qgsdocumentationpanelbase.ui Show resolved Hide resolved
src/gui/qgisinterface.h Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.h Outdated Show resolved Hide resolved
src/app/qgsdevtoolspanelwidget.h Outdated Show resolved Hide resolved
src/app/qgsdevtoolspanelwidget.cpp Outdated Show resolved Hide resolved
src/app/qgsdevtoolspanelwidget.cpp Outdated Show resolved Hide resolved
python/console/console_settings.ui Outdated Show resolved Hide resolved
@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from 3781162 to 5057238 Compare October 31, 2024 09:02
@YoannQDQ
Copy link
Contributor Author

Thanks for the review! I also added the conditional QWebEngineView include.

@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from 4789e4b to f2e5f90 Compare October 31, 2024 12:10
@YoannQDQ YoannQDQ modified the milestones: 3.40.0, 3.42.0 Oct 31, 2024
@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch 3 times, most recently from 5978cbc to a2c9374 Compare November 5, 2024 12:54
@YoannQDQ YoannQDQ removed the Frozen Feature freeze - Do not merge! label Nov 5, 2024
@YoannQDQ
Copy link
Contributor Author

@nyalldawson Would it be possible to merge this now?

@nyalldawson
Copy link
Collaborator

@YoannQDQ

Would it be possible to merge this now?

Sorry, was off last week with FOSS4G Oceania ... it's going to take me a bit of time to catch back up on QGIS work now 😆

@DelazJ DelazJ added Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Nov 11, 2024
@qgis-bot
Copy link
Collaborator

@YoannQDQ

This pull request has been tagged for the changelog.

  • The description will be harvested so please provide a "nearly-ready" text for the final changelog
  • If possible, add a nice illustration of the feature. Only the first one in the description will be harvested (GIF accepted as well)
  • If you can, it's better to give credits to your sponsor, see below for different formats.

You can edit the description.

Format available for credits
  • Funded by NAME
  • Funded by URL
  • Funded by NAME URL
  • Sponsored by NAME
  • Sponsored by URL
  • Sponsored by NAME URL

Thank you!

@qgis-bot
Copy link
Collaborator

@YoannQDQ
This pull request has been tagged as requiring documentation.

A documentation ticket will be opened at https://github.com/qgis/QGIS-Documentation when this PR is merged.

Please update the description (not the comments) with helpful description and screenshot to help the work from documentors.
Also, any commit having [needs-doc] or [Needs Documentation] in will see its message pushed to the issue, so please be as verbose as you can.

Thank you!

CMakeLists.txt Outdated Show resolved Hide resolved
src/gui/codeeditors/qgscodeeditorpython.h Outdated Show resolved Hide resolved
src/core/qgis.h Outdated Show resolved Hide resolved
src/core/CMakeLists.txt Outdated Show resolved Hide resolved
python/console/console_settings.py Outdated Show resolved Hide resolved
@nyalldawson
Copy link
Collaborator

@YoannQDQ just a few minor cleanups left 👍

@YoannQDQ YoannQDQ force-pushed the python-console-interactive-help branch from a2c9374 to b50aeaa Compare November 13, 2024 18:31
src/gui/qgsgui.h Outdated Show resolved Hide resolved
@nyalldawson nyalldawson merged commit 739b924 into qgis:master Nov 13, 2024
31 checks passed
@nyalldawson
Copy link
Collaborator

Thanks @YoannQDQ !

@qgis-bot
Copy link
Collaborator

@YoannQDQ
A documentation ticket has been opened at qgis/QGIS-Documentation#9384
It is your responsibility to visit this ticket and add as much detail as possible for the documentation team to correctly document this change.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog Items that are queued to appear in the visual changelog - remove after harvesting Feature Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. 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
5 participants