rodekruis / sims_maps_qgis_plugin

QGIS plugin for creating Red Cross Red Crescent maps
GNU General Public License v2.0
6 stars 4 forks source link

overview layer not working when reopening project on different machine #15

Closed raymondnijssen closed 5 years ago

raymondnijssen commented 5 years ago

When reopening a saved project with sims map layouts, the overview layer is likely to point to a non existent location for the world_map.shp

raymondnijssen commented 5 years ago

Asked for suggestions on the dev list: https://lists.osgeo.org/pipermail/qgis-developer/2019-May/057470.html

raymondnijssen commented 5 years ago

Possible (smelly!) solution is using the QgsProject 'loadingLayer' signal and connect it to a function that checks the layerName and overwrites the data source if it contains 'SIMS_world_overview'

raymondnijssen commented 5 years ago
@pyqtSlot('QString')
def layerLoaded(value):
    print('layerLoded: {}'.format(value))
    if 'SIMS_world_overview' in value:
        # set data source here

QgsProject.instance().loadingLayer.connect(layerLoaded)
arongergely commented 5 years ago

As of this point @raymondnijssen, @r2rc and @arongergely came up with the idea of shipping the overview layer with the plugin as the most reasonable solution

That data we currently use for this layer is from a built-in "ester egg" in QGIS (accessible by typing world in the coordinate box and pressing Enter )

Checklist of tasks, with assigned person:

arongergely commented 5 years ago

Locally implemented and tested a solution that is based on the QgsProject.readPoject signal. (API doc link).

While it works, it may confuse the user:

Apparently, readProject is emitted only after all layers are initialized at project load, and an eventual "Handle Unavailable Layers" popup window is closed. When choosing to "keep unavaulable layers", the window closes and readProject is emitted and a slot is run that replaces the overview map data path with the right one. I think this will trick users into believing the overview map is broken while it would be fixed in the next step after the popup window.

Thus I am now looking at using a different signal, after some trial & error I found QgsProject.layerLoaded (API doc link) which seems suitable: It is emitted for each layer on load, and before the mentioned popup window would appear for missing layers.

arongergely commented 5 years ago

The above behaviour is still present with the layerLoaded signal. The problem likely because "bad layers" are collected behorehand and passed to a bad layer handler that subsequently runs and opens the popup window.

It may be better to implement a custom bad layer handler that handles the world map layer appropriately and set it for QgsProject via QgsProject.setBadLayerHandler() (API doc link)

https://gis.stackexchange.com/questions/67291/how-to-set-default-bad-layers-handler-in-the-python-plugin https://gis.stackexchange.com/questions/310953/qgis-badlayerhandler-setdataprovider