Skip to content

gui: Refactor Copy button to use Tools API, remember user preference and use StringIO - #7404

Merged
petrasovaa merged 4 commits into
OSGeo:mainfrom
polucifier:gui-copy-button
May 23, 2026
Merged

petrasovaa merged 4 commits into
OSGeo:mainfrom
polucifier:gui-copy-button

Conversation

@polucifier

@polucifier polucifier commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR addresses the follow-up design and UX improvements for the newly introduced "Copy" button in GUI tool dialogs, as outlined in issue #7368. It focuses on unifying the Python API selection within the global GUI settings, introducing persistence for the chosen copy format, and ensuring fully portable script generation and parameter sharing across all available Python APIs and JSON formats.

Fixes #7368

Key Changes & Visual Proofs

1. Global Python API Selector in Preferences

  • Integrated a global preference selector into the GUI Settings -> Tools tab under the cmd group using UserSettings.
  • Using standard pythonAPI within core/settings.py and connected via wx.Choice in gui_core/preferences.py.
  • Users can now explicitly choose their preferred core API flavor (Tools API, Script API, or PyGRASS API).

Example of the new dropdown selector inside the GUI Settings -> Tools tab:

settings1

The menu:

settings2

2. Dynamic Copy Button Tooltips & Sticky Mode

  • The main Copy button now implements a persistent "sticky" state via wx.Config (CmdPanel/CopyMode), remembering whether the user last copied a Shell command, Python script, or JSON settings.
  • The button tooltip updates dynamically (_updateCopyButtonUI) to explicitly state the active mode and selected Python API flavor.
Active Mode / Flavor Tooltip Preview
Python (Tools API) tools
Python (Script API) script
Python (PyGRASS API) pygrass

3. Fully Portable Python Code with Inline File Support

  • Extracted the inline file parsing to a clean standalone helper method (_getInlineFiles).
  • Solved the tempfile portability issue across all three Python APIs for interactive text inputs (e.g., custom color rules typed into r.colors).
  • Code snippets are generated clean and portable out-of-the-box, ensuring string literals are safely escaped via pythonic repr() formatting ({!r}).

Generated Outputs Examples (r.colors with custom text rules):

  • Tools API Flavor:
import io

rules_txt = '0 red\n50 yellow\n100 blue'

Tools().r_colors(map='elevation@PERMANENT', rules=io.StringIO(rules_txt))
  • Script API Flavor:
import tempfile

rules_txt = '0 red\n50 yellow\n100 red'

with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
    f.write(rules_txt)
    rules_file = f.name

gs.run_command('r.colors', map='elevation@PERMANENT', rules=rules_file)
  • PyGRASS API Flavor:
import tempfile

rules_txt = '0 red\n50 yellow\n100 red'

with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
    f.write(rules_txt)
    rules_file = f.name

Module('r.colors', map='elevation@PERMANENT', rules=rules_file)

4. Seamless JSON Copy/Paste for Interactive Content

  • Overhauled the JSON serialization inside OnCopyJSON and OnPaste.
  • Instead of serializing volatile local temp paths, the raw multiline text of the interactive layout is embedded directly into the JSON configuration payload.
  • Upon pasting, OnPaste intercepts the text parameters, safely clears out the text field for paths, and properly populates the secondary multiline interactive input control (wxId[1]).

Generated Output Example (r.colors with custom text rules):

{
    "module": "r.colors",
    "params": {
        "flags": "",
        "map": "elevation@PERMANENT",
        "rules": "0 red\n50 yellow\n100 blue"
    }
}

Screencast demonstrating copying a tool configuration to JSON, completely wiping out the input fields, and cleanly restoring the multiline rules via Paste:

Paste1.webm

5. Other changes

  • Refactored gtask.cmd_to_python_args to cleanly return formatted argument strings, allowing the GUI frontend to handle the closing syntax gracefully.

How to Test

  1. Go to GUI Settings -> Tools and change the Default Python API (the default option is Tools API). Save settings.
  2. Open r.colors or any other tool with direct multiline text input blocks.
  3. Enter rules interactively, click the Copy dropdown, and choose your format.
  4. Verify that clipboard string matched the formatting guidelines shown above.
  5. Copy the config as JSON, clear inputs, click Paste JSON settings, and verify that the content re-populates correctly.

- Limit Python API options in the copy menu strictly to the recommended Tools API
- Persist the user's last chosen copy format (Shell, Python, or JSON) across sessions using wx.Config
- Support io.StringIO for inline file inputs (e.g., r.colors rules typed directly into the field) to make the copied Python code fully portable
@github-actions github-actions Bot added GUI wxGUI related Python Related code is in Python libraries labels May 17, 2026
@landam landam added this to the 8.6.0 milestone May 18, 2026
@landam

landam commented May 18, 2026

Copy link
Copy Markdown
Member

I think this PR is a step in the right direction. On the other hand, users should still have the option to switch to the scripting or the PyGRASS API when needed. The settings could managed by the GUI configuration (Tools tab):
image

Comment thread gui/wxpython/gui_core/forms.py Resolved
Comment thread gui/wxpython/gui_core/forms.py Resolved
Comment thread gui/wxpython/gui_core/forms.py Resolved
…rence to settings

- Integrate a default Python API flavor selector (Tools, Script, or PyGRASS) into GUI Preferences -> Tools tab via UserSettings
- Update module dialog Copy button tooltips and code generation dynamically based on the globally chosen API style
- Implement robust multi-line script formatting for interactive file fields using io.StringIO for Tools API and tempfile for external process APIs
- Enhance JSON Copy/Paste mechanism to seamlessly embed and extract raw inline text inputs instead of volatile temporary file paths
petrasovaa
petrasovaa previously approved these changes May 22, 2026

@petrasovaa petrasovaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you!

@petrasovaa
petrasovaa merged commit a5386ca into OSGeo:main May 23, 2026
26 checks passed
ninsbl pushed a commit to ninsbl/grass that referenced this pull request Jul 7, 2026
…and use StringIO (OSGeo#7404)

- Limit Python API options in the copy menu strictly to the recommended Tools API
- Persist the user's last chosen copy format (Shell, Python, or JSON) across sessions using wx.Config
- Support io.StringIO for inline file inputs (e.g., r.colors rules typed directly into the field) to make the copied Python code fully portable
- Integrate a default Python API flavor selector (Tools, Script, or PyGRASS) into GUI Preferences -> Tools tab via UserSettings
- Update module dialog Copy button tooltips and code generation dynamically based on the globally chosen API style
- Implement robust multi-line script formatting for interactive file fields using io.StringIO for Tools API and tempfile for external process APIs
- Enhance JSON Copy/Paste mechanism to seamlessly embed and extract raw inline text inputs instead of volatile temporary file paths
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GUI wxGUI related libraries Python Related code is in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat] Limit tool Copy button to Tools API and other improvements

3 participants