qgis / qgis4.0_api

Tracker for QGIS 4.0 API related issues and developer discussion
3 stars 1 forks source link

Map Layers and QgsMapLayerRegistry #16

Closed wonder-sk closed 7 years ago

wonder-sk commented 8 years ago

Currently, a map layer needs to be added to QgsMapLayerRegistry before it can be used for various tasks (e.g. in layer tree, map rendering) - however not for all tasks (e.g. get vector features, raster blocks). This is confusing to the users of the API (and makes the API less elegant, too).

The solution would be do automatically add map layers to registry in their constructor and remove map layers in their destructor. That does not mean that all layers would be automatically owned by the registry - but the registry would be at least aware of them all the time. (The transfer of ownership to the registry would still need to be explicit.)

m-kuhn commented 8 years ago

Issue #8 proposes to remove singleton behavior from QgsProject. I would propose to do the same to the map layer registry and make it a child of QgsProject.

This in turn would mean that there is no longer a single registry where a map layer would be registered to, but in turn a map layer could be registered to be part of one of several open projects.

So to sum it up, I'd prefer to reduce the usage of the map layer registry (mostly by passing pointer instead of layer ids to methods and classes) instead of making the registry do more magic than it already does.

wonder-sk commented 8 years ago

Issue #8 proposes to remove singleton behavior from QgsProject. I would propose to do the same to the map layer registry and make it a child of QgsProject.

Association of layers with a project is already done through project's layer tree. What would be the advantage of having layer registry associated with QgsProject?

My understanding of map layer registry is that it is our poor man's implementation of weak references to map layers. I'm not sure if we will be able to completely get rid of the registry in all cases. I agree though that in various places we probably don't need to use layer IDs and we could just use layer pointers directly.

My preference to remove magic from layer registry would be to remove the "addToLegend" parameter from addMapLayer() so it would be completely independent from projects.

m-kuhn commented 8 years ago

I think it's quite dangerous to use the same registry with several projects open.

If a project file is copied and each copy opened, the layers have the same ids in both. How would you handle that?

wonder-sk commented 8 years ago

If a project file is copied and each copy opened, the layers have the same ids in both. How would you handle that?

Good point :-) So what is our holy grail? Stop using layer IDs + map layer registry completely? Some kind of unique map layer references + resolving mechanism is still necessary for load/save operations. However such layer references (IDs) could be assigned on the fly and could be valid just for the particular load/save operation... This would need some more thoughts...

m-kuhn commented 8 years ago

For reference, I have recently started to work towards this (slightly related)

https://github.com/qgis/QGIS/pull/2523

I think exposing the id is ok in general. Other things may need to serialize references to layers as well. (E.g. a relation has a referencing and a referenced layer), this may be somehow resolved just in time but I don't really mind.

Abandoning the registry... Why not... If the same information (plus more) can be retrieved from the layer tree, why keep it?

In general my main goal is that we don't promote the wide usage of ids by

wonder-sk commented 7 years ago

Map layer registry has been merged into QgsProject during project refactoring - closing this.

https://github.com/qgis/QGIS-Enhancement-Proposals/issues/76