-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[field calculator] Add map settings to the expression context #63372
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
base: master
Are you sure you want to change the base?
[field calculator] Add map settings to the expression context #63372
Conversation
When map units is not set in the project properties, they are supposed to be taken from the map canvas, but map canvas is not available inside the calculator, the fix is to pass the map settings down into the rabbit hole by attaching them to the expression context (in a similar way to what already happens with the project scope variables, with the notable exception that the project is actually available as a global instance while the map canvas is not). Remaining issue: the settings are not available in the project properties field editor. Fix qgis#62883
🪟 Windows Qt6 buildsDownload Windows Qt6 builds of this PR for testing. 🍎 MacOS Qt6 buildsDownload MacOS Qt6 builds of this PR for testing. |
I'm a little confused why the map settings scope is required for distance units resolution -- this should be entirely accessible from the project scope alone, as it's a project level setting. My reservation with adding the map settings scope to field calculator is that there's not a 1:1 relationship with canvases there -- a single project may have many map canvases, and each of these canvases can have different settings (eg scale, extent). That's why I originally limited the use of the map settings scope to ONLY rendering in a particular canvas, and also why it's not available for use with virtual fields. |
mExpressionContext->appendScope( new QgsExpressionContextScope( mSource->mLayerScope ) ); | ||
mExpressionContext->setFeedback( mRequest.feedback() ); | ||
// We need to keep the map settings scope if set in the original context | ||
const int settingsScopeIndex = mRequest.expressionContext()->indexOfScope( QStringLiteral( "Map Settings" ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the reason for this change -- could you break this pr up into separate atomic commits so that I can understand better?
@nyalldawson Sorry if that wasn't clear:
I'll try to rephrase it: the problem is when:
The canvas is not accessible in the context of the expression from inside the iterator (which is used by the expression preview dialog and from the attribute dialog). So, I thought about the possible solutions:
|
I understand and agree, but if that's the case how can the expression get the map units from the canvas if the units are set to So either we change the meaning of Any other idea? |
I think this is the correct solution -- I believe the "get from the canvas" definition is a relic from when we ONLY had a canvas crs and had no concept of a "project crs". |
Ok, I'll proceed in that direction. |
@nyalldawson I'm having second thoughts: if we change Unknown to be the project CRS instead of the map units there are many consequences: see for instance QgsProjectProperties::updateGuiForMapUnits(). Or are you suggesting to change the concept of "Map Units" from "Canvas units" to "Project units" ? this would have an even more deeper impact. The current behavior is that when a user changes the map canvas units, the distance units (if set to Unknown) are changed to match the current canvas units. This is not going to work anymore. I assume the same apply for the area units. There is also the possibility to change the documentation of the I must say I don't really like any of the above solutions. |
When map units is not set in the project properties, they are supposed to be taken from the map canvas, but map canvas is not available inside the calculator (or the feature iterator), the fix is to pass the map settings down into the rabbit hole by attaching them to the expression context (in a similar way to what already happens with the project scope variables, with the notable exception that the project is actually available as a global instance while the map canvas is not).
Remaining issue: the settings are not available in the project properties field editor.
Fix #62883
Note: this was tricky, if anyone has a better idea on how to fix this in a better way please let me know and I will be happy to refactor this PR.