-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
test_qgsmapthemecollection.py
154 lines (124 loc) · 6.73 KB
/
test_qgsmapthemecollection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsMapThemeCollection.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '8/03/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import qgis # NOQA
from qgis.core import (QgsMapThemeCollection,
QgsProject,
QgsVectorLayer)
from qgis.testing import start_app, unittest
from qgis.PyQt.QtTest import QSignalSpy
app = start_app()
class TestQgsMapThemeCollection(unittest.TestCase):
def setUp(self):
pass
def testThemeChanged(self):
"""
Test that the mapTheme(s)Changed signals are correctly emitted in all relevant situations
"""
project = QgsProject()
collection = QgsMapThemeCollection(project)
record = QgsMapThemeCollection.MapThemeRecord()
theme_changed_spy = QSignalSpy(collection.mapThemeChanged)
themes_changed_spy = QSignalSpy(collection.mapThemesChanged)
collection.insert('theme1', record)
self.assertEqual(len(theme_changed_spy), 1)
self.assertEqual(theme_changed_spy[-1][0], 'theme1')
self.assertEqual(len(themes_changed_spy), 1)
# reinsert
collection.insert('theme1', record)
self.assertEqual(len(theme_changed_spy), 2)
self.assertEqual(theme_changed_spy[-1][0], 'theme1')
self.assertEqual(len(themes_changed_spy), 2)
# update
collection.update('theme1', record)
self.assertEqual(len(theme_changed_spy), 3)
self.assertEqual(theme_changed_spy[-1][0], 'theme1')
self.assertEqual(len(themes_changed_spy), 3)
# remove invalid
collection.removeMapTheme('i wish i was a slave to an age old trade... like riding around on rail cars and working long days')
self.assertEqual(len(theme_changed_spy), 3)
self.assertEqual(len(themes_changed_spy), 3)
# remove valid
collection.removeMapTheme('theme1')
self.assertEqual(len(theme_changed_spy), 3) # not changed - removed!
self.assertEqual(len(themes_changed_spy), 4)
# reinsert
collection.insert('theme1', record)
self.assertEqual(len(theme_changed_spy), 4)
self.assertEqual(len(themes_changed_spy), 5)
# clear
collection.clear()
self.assertEqual(len(theme_changed_spy), 4) # not changed - removed!
self.assertEqual(len(themes_changed_spy), 6)
# check that mapThemeChanged is emitted if layer is removed
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
project.addMapLayers([layer, layer2])
# record for layer1
record.addLayerRecord(QgsMapThemeCollection.MapThemeLayerRecord(layer))
collection.insert('theme1', record)
self.assertEqual(len(theme_changed_spy), 5)
self.assertEqual(len(themes_changed_spy), 7)
# now kill layer 2
project.removeMapLayer(layer2)
self.assertEqual(len(theme_changed_spy), 5) # signal should not be emitted - layer is not in record
# now kill layer 1
project.removeMapLayer(layer)
app.processEvents()
self.assertEqual(len(theme_changed_spy), 6) # signal should be emitted - layer is in record
def testMasterLayerOrder(self):
""" test master layer order"""
prj = QgsProject()
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string",
"layer2", "memory")
layer3 = QgsVectorLayer("Point?field=fldtxt:string",
"layer3", "memory")
prj.addMapLayers([layer, layer2, layer3])
prj.setLayerOrder([layer2, layer])
self.assertEqual(prj.mapThemeCollection().masterLayerOrder(), [layer2, layer])
prj.setLayerOrder([layer, layer2, layer3])
# make some themes...
theme1 = QgsMapThemeCollection.MapThemeRecord()
theme1.setLayerRecords([QgsMapThemeCollection.MapThemeLayerRecord(layer3),
QgsMapThemeCollection.MapThemeLayerRecord(layer)])
theme2 = QgsMapThemeCollection.MapThemeRecord()
theme2.setLayerRecords([QgsMapThemeCollection.MapThemeLayerRecord(layer3),
QgsMapThemeCollection.MapThemeLayerRecord(layer2),
QgsMapThemeCollection.MapThemeLayerRecord(layer)])
theme3 = QgsMapThemeCollection.MapThemeRecord()
theme3.setLayerRecords([QgsMapThemeCollection.MapThemeLayerRecord(layer2),
QgsMapThemeCollection.MapThemeLayerRecord(layer)])
prj.mapThemeCollection().insert('theme1', theme1)
prj.mapThemeCollection().insert('theme2', theme2)
prj.mapThemeCollection().insert('theme3', theme3)
#order of layers in theme should respect master order
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme1'), [layer, layer3])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme2'), [layer, layer2, layer3])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme3'), [layer, layer2])
# also check ids!
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayerIds('theme1'), [layer.id(), layer3.id()])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayerIds('theme2'), [layer.id(), layer2.id(), layer3.id()])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayerIds('theme3'), [layer.id(), layer2.id()])
# reset master order
prj.setLayerOrder([layer2, layer3, layer])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme1'), [layer3, layer])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme2'), [layer2, layer3, layer])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayers('theme3'), [layer2, layer])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayerIds('theme1'), [layer3.id(), layer.id()])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayerIds('theme2'), [layer2.id(), layer3.id(), layer.id()])
self.assertEqual(prj.mapThemeCollection().mapThemeVisibleLayerIds('theme3'), [layer2.id(), layer.id()])
if __name__ == '__main__':
unittest.main()