Important

Translation is a community effort you can join. This page is currently translated at 48.00%.

16.2. Extraits de code

Indication

Les extraits de code de cette page nécessitent les imports suivants si vous êtes en dehors de la console pyqgis :

 1from qgis.core import (
 2    QgsProject,
 3    QgsApplication,
 4    QgsMapLayer,
 5)
 6
 7from qgis.gui import (
 8    QgsGui,
 9    QgsOptionsWidgetFactory,
10    QgsOptionsPageWidget,
11    QgsLayerTreeEmbeddedWidgetProvider,
12    QgsLayerTreeEmbeddedWidgetRegistry,
13)
14
15from qgis.PyQt.QtCore import Qt
16from qgis.PyQt.QtWidgets import (
17    QMessageBox,
18    QAction,
19    QHBoxLayout,
20    QComboBox,
21)
22from qgis.PyQt.QtGui import QIcon

Cette section contient des extraits de code pour faciliter le développement de plugins.

16.2.1. Comment appeler une méthode par un raccourci clavier

Dans le plug-in, ajoutez à la fonction initGui().

self.key_action = QAction("Test Plugin", self.iface.mainWindow())
self.iface.registerMainWindowAction(self.key_action, "Ctrl+I")  # action triggered by Ctrl+I
self.iface.addPluginToMenu("&Test plugins", self.key_action)
self.key_action.triggered.connect(self.key_action_triggered)

À unload() ajouter

self.iface.unregisterMainWindowAction(self.key_action)

La méthode qui est appelée lorsque l’on appuie sur CTRL+I

def key_action_triggered(self):
  QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed Ctrl+I")

It is also possible to allow users to customize key shortcuts for the provided actions. This is done by adding:

1# in the initGui() function
2QgsGui.shortcutsManager().registerAction(self.key_action)
3
4# and in the unload() function
5QgsGui.shortcutsManager().unregisterAction(self.key_action)

16.2.2. How to reuse QGIS icons

Because they are well-known and convey a clear message to the users, you may want sometimes to reuse QGIS icons in your plugin instead of drawing and setting a new one. Use the getThemeIcon() method.

For example, to reuse the fileOpen mActionFileOpen.svg icon available in the QGIS code repository:

1# e.g. somewhere in the initGui
2self.file_open_action = QAction(
3    QgsApplication.getThemeIcon("/mActionFileOpen.svg"),
4    self.tr("Select a File..."),
5    self.iface.mainWindow()
6)
7self.iface.addPluginToMenu("MyPlugin", self.file_open_action)

iconPath() is another method to call QGIS icons. Find examples of calls to theme icons at QGIS embedded images - Cheatsheet.

16.2.3. Interface pour le plugin dans le dialogue des options

Vous pouvez ajouter un onglet d’options de plugin personnalisé à Settings ► Options. Ceci est préférable à l’ajout d’une entrée spécifique dans le menu principal pour les options de votre plugin, car cela permet de garder tous les paramètres de l’application QGIS et les paramètres du plugin dans un seul endroit, ce qui est facile à découvrir et à naviguer pour les utilisateurs.

L’extrait suivant ajoute un nouvel onglet vierge pour les paramètres du plugin, prêt à être rempli avec toutes les options et paramètres spécifiques à votre plugin. Vous pouvez diviser les classes suivantes en différents fichiers. Dans cet exemple, nous ajoutons deux classes dans le fichier main mainPlugin.py.

 1class MyPluginOptionsFactory(QgsOptionsWidgetFactory):
 2
 3    def __init__(self):
 4        super().__init__()
 5
 6    def icon(self):
 7        return QIcon('icons/my_plugin_icon.svg')
 8
 9    def createWidget(self, parent):
10        return ConfigOptionsPage(parent)
11
12
13class ConfigOptionsPage(QgsOptionsPageWidget):
14
15    def __init__(self, parent):
16        super().__init__(parent)
17        layout = QHBoxLayout()
18        layout.setContentsMargins(0, 0, 0, 0)
19        self.setLayout(layout)

Enfin, nous ajoutons les importations et modifions la fonction __init_ :

 1from qgis.PyQt.QtWidgets import QHBoxLayout
 2from qgis.gui import QgsOptionsWidgetFactory, QgsOptionsPageWidget
 3
 4
 5class MyPlugin:
 6    """QGIS Plugin Implementation."""
 7
 8    def __init__(self, iface):
 9        """Constructor.
10
11        :param iface: An interface instance that will be passed to this class
12            which provides the hook by which you can manipulate the QGIS
13            application at run time.
14        :type iface: QgsInterface
15        """
16        # Save reference to the QGIS interface
17        self.iface = iface
18
19
20    def initGui(self):
21        self.options_factory = MyPluginOptionsFactory()
22        self.options_factory.setTitle(self.tr('My Plugin'))
23        iface.registerOptionsWidgetFactory(self.options_factory)
24
25    def unload(self):
26        iface.unregisterOptionsWidgetFactory(self.options_factory)

Astuce

Add custom tabs to layer properties dialog

Vous pouvez appliquer une logique similaire pour ajouter l’option personnalisée du plugin au dialogue des propriétés de la couche en utilisant les classes QgsMapLayerConfigWidgetFactory et QgsMapLayerConfigWidget.

16.2.4. Embed custom widgets for layers in the layer tree

Beside usual layer symbology elements displayed next or below the layer entry in the Layers panel, you can add your own widgets, allowing for quick access to some actions that are often used with a layer (setup filtering, selection, style, refreshing a layer with a button widget, create a layer based time slider or just show extra layer information in a Label there, or …). These so-called Layer tree embedded widgets are made available through the layer’s properties Legend tab for individual layers.

The following code snippet creates a drop-down in the legend which shows you the layer-styles available for the layer, allowing to quickly switch between the different layer styles.

 1class LayerStyleComboBox(QComboBox):
 2    def __init__(self, layer):
 3        QComboBox.__init__(self)
 4        self.layer = layer
 5        for style_name in layer.styleManager().styles():
 6            self.addItem(style_name)
 7
 8        idx = self.findText(layer.styleManager().currentStyle())
 9        if idx != -1:
10          self.setCurrentIndex(idx)
11
12        self.currentIndexChanged.connect(self.on_current_changed)
13
14    def on_current_changed(self, index):
15        self.layer.styleManager().setCurrentStyle(self.itemText(index))
16
17class LayerStyleWidgetProvider(QgsLayerTreeEmbeddedWidgetProvider):
18    def __init__(self):
19        QgsLayerTreeEmbeddedWidgetProvider.__init__(self)
20
21    def id(self):
22        return "style"
23
24    def name(self):
25        return "Layer style chooser"
26
27    def createWidget(self, layer, widgetIndex):
28        return LayerStyleComboBox(layer)
29
30    def supportsLayer(self, layer):
31        return True   # any layer is fine
32
33provider = LayerStyleWidgetProvider()
34QgsGui.layerTreeEmbeddedWidgetRegistry().addProvider(provider)

Then from a given layer’s Legend properties tab, drag the Layer style chooser from the Available widgets to Used widgets to enable the widget in the layer tree. Embedded widgets are ALWAYS displayed at the top of their associated layer node subitems.

If you want to use the widgets from within e.g. a plugin, you can add them like this:

1layer = iface.activeLayer()
2counter = int(layer.customProperty("embeddedWidgets/count", 0))
3layer.setCustomProperty("embeddedWidgets/count", counter+1)
4layer.setCustomProperty("embeddedWidgets/{}/id".format(counter), "style")
5view = self.iface.layerTreeView()
6view.layerTreeModel().refreshLayerLegend(view.currentLegendNode())
7view.currentNode().setExpanded(True)