QGIS Combo Manager is a python module to easily manage a combo box with a layer list and eventually relate it with one or several combos with list of corresponding fields.
The field combos are filled with the names of columns of the currently selected layer in the layer combo.
In your plugin, create first a LayerCombo:
from qgiscombomanager import *
self.layerComboManager = VectorLayerCombo(self.layerComboWidget)
Then, associates some FieldCombo:
self.myFieldComboManager = FieldCombo(self.myFieldComboManager, self.layerComboManager)
The managers (layer or field) must be saved as a class property (self.something), so the variable is not getting out of scope in python.
The classes offers some convenience methods: getLayer()
and setLayer(layer)
, for layer combos, and getFieldName()
, getFieldAlias()
, getFieldIndex()
for field combos.
A combo box can be assigned to list the layers. Three classes are available:
LayerCombo(widget, initLayer="", options={}, layerType=None)
VectorLayerCombo(widget, initLayer="", options={})
RasterLayerCombo(widget, initLayer="", options={})
VectorLayerCombo
and RasterLayerCombo
are convenient classes which are calling the main LayerCombo class with the same parameters and specifying the layerType
.
- widget: the QComboBox widget
- initLayer: the initally selected layer ID or a lambda function returning the ID (it could look for a value in settings).
- options: a dictionnary of options: {"opt1": val1, "opt2": val2, etc.}.
Options are listed hereunder, default values being listed first:
- hasGeometry*: None/True/False. Restrains the possible selection of layers to layers having or not geometry (None = all).
- geomType*: None/QGis.Point/QGis.Line/QGis.Polygon. Restrains the possible selection of layers to a certain type of geometry (None = all).
- dataProvider: None/postgres/etc. Filters the layers based on the data provider name (None = all).
- groupLayers: False/True. Groups layers in combobox according to the legend interface groups.
- legendInterface: if
groupLayers is True
, you must provideiface.legendInterface()
for this option. - finishInit: True/False. Set it to
False
if theLayerCombo
object must be returned before its items are filled with layers. - skipLayers: a list of layer IDs or lambda functions returning layer IDs which will not to be shown in the combo box.
*used for vector layer combos
This class offer convenient methods:
getLayer()
: returns the layer currently selected in the combo boxsetLayer(layer)
: set the current layer in the combo box (layer can be a layer or a layer id).
The LayerCombo will also emit signals:
layerChanged()
andlayerChangedLayer(QgsMapLayer)
: whenever the layer changes.
A combo box can be assigned to list the fields related to a given VectorLayerCombo.
FieldCombo(widget, vectorLayerCombo, initField="", fieldType=None)
- widget: the qcombobox widget
- vectorLayerCombo: the combobox defining the vector layer
- initField: the initially selected field name or a lambda function returning the name (it could look for a value in settings)
- options: a dictionnary of options: {"opt1": val1, "opt2": val2, etc.}.
Options are listed hereunder, default values being listed first:
- emptyItemFirst: True/False. If true, a first empty item is listed first in the combo box.
- fieldType: restrain the possible selection to a certain type of field (see QGIS doc or Qt doc).
This class offer convenient methods:
getFieldName()
: returns the name of the currently selected fieldgetFieldAlias()
: returns the alias of the currently selected fieldgetFieldIndex()
: returns the field index of the currently selected fieldsetField(fieldName)
: set the current field in the combo box
The FieldCombo will also emit a signal:
fieldChanged()
: whenever the field changes it will emit this signal.
Similarly to field combos, a combo box can be assigned to list the bands related to a given RasterLayerCombo.
BandCombo(widget, rasterLayerCombo, initBand=None)
- widget: the qcombobox widget
- rasterLayerCombo: the combobox defining the raster layer
- initBand: the initially selected band (integer) or a lambda function returning it (it could look for a value in settings)
This class offer a convenient method:
getBand()
: returns the name of the currently selected band
The BandCombo will also emit a signal:
bandChanged(str)
: whenever the band changes it will emit this signal with the new band name.
To use this module you can easily copy the files and put them in your project. A more elegant way is to use git submodule. Hence, you can keep up with latest improvements. In you plugin directory, do
git submodule add git://github.com/3nids/qgiscombomanager.git
A folder qgiscombomanager will be added to your plugin directory. However, git only references the module, and you can git pull
in this folder to get the last changes.