I have created a widget for PyQGIS applications that is a (really) simplified version of the QGIS layer list widget. The main difference is that it has no group layers. The widget is also based on OpenOceanMap's legend written by Aaron Racicot but instead of working with QCheckBox controls it uses QTreeWidgetItem for representing every map layer.
This is a screenshot of what you may get:
What does the widget allow?
- Get an ordered list of map layers.
- Move up or down a layer affecting its z-order in the map.
- Make a layer visible or invisible in the map.
- Visualize/change the layer symbology (actually, you have to develop your own layer symbology dialog, I just give you a foo example).
- Change some layer properties.
- Load/Save QGIS layer style files.
- Remove layers.
- Integrate your map with other tools acting over loaded layers (e.g. Show/Hide all, Remove all).
What about the license?
It is GNU GPL v.2.
How to include it?
To include the widget on your PyQGIS application you should follow these steps:
1. Download this ZIP file and extract it to your application folder. It contains the legend.py file, which is actually the widget, a number of images (icons) to represent layer types and menu options, and finally, a dialog that is used to set layer properties.
You can get the icons from the QGIS project, I have not created them.
2. Import the Legend class.
In the main class of your application, which inherits from QMainWindow, import the Legend class from the legend.py file, this way:
3. Add the CreateLegendWidget method.
Include the CreateLegendWidget method in the main class of the application to add the dock widget into the GUI and associate the layer list widget to the canvas. The method is this:
In the __init__ method of the application main class call the just added method:
4. Compile/Import a resource file to include legend widget icons.
The layer list widget include some icons you would like to see. To accomplish this, compile the provided resources.qrc file with the following command:
The legend.py file already contains a reference to the genereated file. Nevertheless, if you have your own resources file, you may add all the images in the imgs folder into a new prefix, for instance, "legend". If you want to choose another prefix, make sure to change the resource_prefix variable in the legend.py file as well.
5. Let the legend to load and remove layers in order to avoid segmentation fault.
You must have a method to load layers into your map application, but now that you want to include a layer list widget you should let it control all these matters. The layer list widget has a SLOT to manage the layer set at the time you load, move or remove a layer. All you have to do is to call the appropriate methods (either addMapLayer or removeMapLayer) from QgsMapLayerRegistry instance, but do not use the setLayerSet canvas method yourself, the new widget will. So, comment or delete your setLayerSet calls.
6. Load the properties dialog.
To allow the user set some layer properties you have to compile the dlgLayerProperties.ui file, which will provide a basic dialog.
7. (Optional) Enable/Disable tools
The layer list widget emits a SIGNAL that allows you to enable/disable tools in your application, according to the active layer type, either raster or vector. The SIGNAL is called activeLayerChanged and if you want to use it, you are suppose to write something like this in your application main class:
8. (Optional) Use a few Legend class methods to expose functionality to other tools or plugins:
- Iterate over the layers:
- Show (or hide) all the layers in the map:
- Get the active layer (for plugins):
- Refresh the layer symbology (for plugins):
- Remove layers by Id (for plugins):