mytestrenderer.py

Martin Dobias, 2016-01-25 08:28 AM

Download (3.22 KB)

 
1
from PyQt4.QtGui import QComboBox, QLabel, QHBoxLayout, QSpinBox, QVBoxLayout
2

    
3
from qgis.core import QgsFeatureRendererV2, QgsRendererV2AbstractMetadata
4
from qgis.gui import QgsRendererV2Widget
5

    
6
from utils import symbolForCode, getDefaultSymbol
7

    
8

    
9
class MyTestRenderer(QgsFeatureRendererV2):
10
    def __init__(self, size=40, field='', fields=[]):
11
        QgsFeatureRendererV2.__init__(self, 'MyTestRenderer')
12
        self.field = field
13
        self.fields = fields
14
        self.size = size
15
        self.def_symbol = getDefaultSymbol(self.size)
16

    
17
    def symbolForFeature(self, feature):
18
        # all symbols returned must have been prepared beforehand with startRender() call
19
        return self.def_symbol
20

    
21
    def startRender(self, context, fields):
22
        self.def_symbol.startRender(context)
23

    
24
    def stopRender(self, context):
25
        self.def_symbol.stopRender(context)
26

    
27
    def usedAttributes(self):
28
        return self.fields
29

    
30
    def symbols2(self, context):
31
        return [self.def_symbol]
32

    
33
    def clone(self):
34
        return MyTestRenderer(self.size, self.field, self.fields)
35

    
36
    def save(self, doc):
37
        elem = doc.createElement('renderer-v2')
38
        elem.setAttribute('type', 'MyTestRenderer')
39
        elem.setAttribute('size', self.size)
40
        elem.setAttribute('field', self.field)
41
        return elem
42

    
43

    
44

    
45
class MyTestRendererWidget(QgsRendererV2Widget):
46
    def __init__(self, layer, style, renderer):
47
        QgsRendererV2Widget.__init__(self, layer, style)
48

    
49
        if renderer is None or renderer.type() != 'MyTestRenderer':
50
            fields = [f.name() for f in layer.dataProvider().fields()]
51
            self.r = MyTestRenderer(field=fields[0], fields=fields)
52
        else:
53
            self.r = renderer.clone()
54

    
55
        self.combo = QComboBox()
56
        for f in layer.dataProvider().fields():
57
            self.combo.addItem(f.name(), f.name())
58

    
59
        idx =  max(0, layer.dataProvider().fieldNameIndex(self.r.field))
60

    
61
        self.combo.setCurrentIndex(idx)
62
        self.combo.currentIndexChanged.connect(self.fieldChanged)
63

    
64
        self.labelField = QLabel('Code field')
65
        self.hbox = QHBoxLayout()
66
        self.hbox.addWidget(self.labelField)
67
        self.hbox.addWidget(self.combo)
68

    
69
        self.spinSize = QSpinBox()
70
        self.spinSize.setValue(self.r.size)
71
        self.spinSize.valueChanged.connect(self.sizeChanged)
72

    
73
        self.labelSize = QLabel('Size (pixels)')
74
        self.hbox2 = QHBoxLayout()
75
        self.hbox2.addWidget(self.labelSize)
76
        self.hbox2.addWidget(self.spinSize)
77
        self.vbox = QVBoxLayout()
78
        self.vbox.addLayout(self.hbox)
79
        self.vbox.addLayout(self.hbox2)
80
        self.setLayout(self.vbox)
81

    
82
    def sizeChanged(self, value):
83
        self.r.size = value
84

    
85
    def fieldChanged(self):
86
        self.r.field = self.combo.currentText()
87

    
88
    def renderer(self):
89
        return self.r
90

    
91

    
92
class MyTestRendererMetadata(QgsRendererV2AbstractMetadata):
93
    def __init__(self):
94
        QgsRendererV2AbstractMetadata.__init__(self, 'MyTestRenderer', 'My test renderer')
95

    
96
    def createRenderer(self, element):
97
        return MyTestRenderer(int(element.attribute('size')), element.attribute('field'))
98

    
99
    def createRendererWidget(self, layer, style, renderer):
100
        return MyTestRendererWidget(layer, style, renderer)