Feature request #6269

Better tokens for text replacement in Print Composer labels

Added by Larry Shaffer about 2 years ago. Updated 4 months ago.

Status:Open Start Date:08/28/2012
Priority:Normal Due date:
Assigned to:Marco Hugentobler % Done:

0%

Category:Map Composer / Printing
Target version:Future Release - Lower Priority
Platform:All Resolution:
Platform version: Patch supplied:No
Status info: Tag:

Description

See also: Issue #1385

While there have been several requests for new 'tokens' for Composer labels that get replaced with data (e.g. $CURRENT_DATE), the concept of adding support for new tokens in code could be supplanted with a more extensible solution.

I have outlined two such solutions here: http://gis.stackexchange.com/questions/31090

To summarize from that post, the first solution is to have output from a customized Expression Editor be the label's text, and the second is to have a Python snippet, or specifically named functions in a snippet, that replace tokens.

I'm in favor of the Python snippet with tokens being replaced with the returned value of a similarly named function.

For example:

import getpass
from qgis.core import *

def pytoken_user():
    return getpass.getuser()

def pytoken_title():
    return QgsProject.instance().title()

would replace any number of in-text tokens named $pytoken_user or $pytoken_title.

For security reasons, the code would probably not be auto-run on Composer launch (could be an option to allow always, though, like new Project Macros).

label-tokens_button.png (4.2 kB) Larry Shaffer, 08/30/2012 02:17 am

label-tokens_treeview.png (22.5 kB) Larry Shaffer, 08/30/2012 02:17 am

History

Updated by Regis Haubourg about 2 years ago

Hi Larry,
I'm about to support solution 1 using expression builder. What about having both solutions? Having Expression builder for common users, and python snippet for advanced dev's ?
Régis

Updated by Hugo Mercier about 2 years ago

Hi.

If my understanding is correct, we can still stick with something that stay compliant with the two approaches: why not consider a new special token, that may be edited via the Expression Editor, named for instance '$CUSTOM_PYTHON()', with an inline Python string as argument or a reference to an external Python file ?

Updated by Marco Hugentobler about 2 years ago

It will be great to have support for advanced replacement options in QgsComposerLabel.

One issue with the python approach is that QgsComposerLabel is also used for QGIS server printing (GetPrint request). In a server environment, it is desirable to have a solution working without python (old LTS distros are common where everything needs to be compiled from source).

Updated by Larry Shaffer about 2 years ago

Marco Hugentobler wrote:

It will be great to have support for advanced replacement options in QgsComposerLabel.

Yes, especially if it is extensible, even if that means adding new sections/functions to a custom Expression Editor, as they are requested.

One issue with the python approach is that QgsComposerLabel is also used for QGIS server printing (GetPrint request). In a server environment, it is desirable to have a solution working without python (old LTS distros are common where everything needs to be compiled from source).

This did not occur to me. Best to skip Python for now, until some Expression Editor solution is in place, which should work well for (I'm guessing) 95% of general use of custom tokens. Then, adding the ability to inline Python in the Editor's output, or as an inline token in the label's text, would help, if it makes sense to add, with respects to QGIS Server's base requirements.

Concerning an implementation, I figured tokens would be something stored/available at the project level. Then, they could be reused across multiple Composers, and imported from other project files. If purely custom, named tokens were used, instead of in-code support (like CURRENT_DATE is now), only new sections/functions for the Expression Editor would need to be focused on, after the initial implementation is completed.

Here are some quick mock-ups of GUI elements:

label-tokens_button: shows access to tokens from main toolbox panel.

label-tokens_treeview: shows a tree view widget listing available project tokens. Double-clicking on row would open the Expression Editor (like with rule-based symbols tree view). Selecting a row and clicking Paste would paste the token in the text edit field for the label at the current cursor, e.g. paste $ProjectTitle. Or, the Paste button could be Edit instead, and double-click would paste a token.

Updated by Marco Hugentobler about 2 years ago

Concerning an implementation, I figured tokens would be something stored/available at the project level. Then, they could be >reused across multiple Composers, and imported from other project files. If purely custom, named tokens were used, instead of >in-code support (like CURRENT_DATE is now), only new sections/functions for the Expression Editor would need to be focused on, >after the initial implementation is completed.

The GUI and the expression management looks good. Maybe the expressions can be shared between the composer labels and other parts that use expressions (though the label cannot easily deal with attribute columns / feature area / coordinates. Don't know how that should be handled).

Updated by Regis Haubourg almost 2 years ago

Now that expression is commited in master, I understand that very few things are missing to close this issue. Maybe adding some extra properties un expression builder could help. (Project properties for title , OS properties for UserName... )
Do we still keep this for 2.0 or can we postpone?

Updated by Jürgen Fischer 7 months ago

  • Category changed from 33 to Map Composer / Printing

Updated by Jürgen Fischer 4 months ago

  • Target version changed from Version 2.0.0 to Future Release - Lower Priority

Also available in: Atom PDF