[QGIS-trac] Re: [Quantum GIS] #2392: Plugin layer registry

Quantum GIS qgis at qgis.org
Tue Jan 26 11:03:23 EST 2010


#2392: Plugin layer registry
-----------------------------------------------------+----------------------
        Reporter:  mwalker                           |         Owner:  wonder      
            Type:  patch                             |        Status:  closed      
        Priority:  major: does not work as expected  |     Milestone:              
       Component:  Python plugins and bindings       |       Version:  HEAD        
      Resolution:  fixed                             |      Keywords:  plugin layer
Platform_version:                                    |      Platform:  All         
        Must_fix:  No                                |   Status_info:  0           
-----------------------------------------------------+----------------------
Comment (by wonder):

 Applied in r12834 with several modifications:

  * plugin layers are subclassed from QgsPluginLayer and not directly from
 QgsMapLayer for convenience
  * each plugin layer type has its unique name used in
 QgsPluginLayerRegistry - instead of dynamically assigned IDs
  * layer creator has been enhanced to return the unique layer type name
 and to be able to open layer's properties dialog (and renamed to
 QgsPluginLayerType as it's not just a creator)
  * simplified creation of plugin layers: for the unique type name the
 registry returns instance of the layer
  * when the plugin layer type is removed from registry, layer of that type
 are removed automatically

 I've modified also the sample plugin to reflect my changes.

 Sample plugin from the example:
 {{{
 LAYER_TYPE = "sample"

 class SamplePluginLayer(QgsPluginLayer):
   def __init__(self, width=None):
     QgsPluginLayer.__init__(self, LAYER_TYPE, "Sample plugin layer")

     self.width = width if width is not None else 256
     self.setValid(True)

   def draw(self, rendererContext):
     painter = rendererContext.painter()
     painter.setPen(Qt.red)
     painter.drawRect(32, 32, self.width, 128)
     return True

   def readXml(self, node):
     self.width = node.toElement().attribute("width", "256").toInt()[0]
     return True

   def writeXml(self, node, doc):
     element = node.toElement()
     # write plugin layer type to project (essential to be read from
 project)
     element.setAttribute("type", "plugin")
     element.setAttribute("name", LAYER_TYPE)
     # custom properties
     element.setAttribute("width", str(self.width))
     return True
 }}}

 Definition of the layer's type with auxiliary methods:

 {{{
 class SamplePluginLayerType(QgsPluginLayerType):
   def __init__(self):
     QgsPluginLayerType.__init__(self, LAYER_TYPE)

   def createLayer(self):
     return SamplePluginLayer()

   def showLayerProperties(self, layer):
     res = QInputDialog.getInt(None, "Sample plugin", "Set width of the
 rectangle", layer.width, 1, 1000)
     if res[1]: # dialog was not cancelled
       layer.width = res[0]
       # trigger repaint
       layer.setCacheImage(None)
       layer.emit(SIGNAL("repaintRequested()"))

     # indicate that we have shown the properties dialog
     return True
 }}}

 Registration:
 {{{
     QgsPluginLayerRegistry.instance().addPluginLayerType(
 SamplePluginLayerType() )
 }}}

 Creation of the layer:
 {{{
 layer = SamplePluginLayer()
 # -or-
 QgsPluginLayerRegistry.instance().pluginLayerType(LAYER_TYPE).createLayer()
 }}}

 Finalization:
 {{{
     QgsPluginLayerRegistry.instance().removePluginLayerType(LAYER_TYPE)
 }}}

-- 
Ticket URL: <https://trac.osgeo.org/qgis/ticket/2392#comment:5>
Quantum GIS <http://qgis.org>
Quantum GIS is an Open Source GIS viewer/editor supporting OGR, PostGIS, and GRASS formats


More information about the QGIS-trac mailing list