-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOrtoTools.py
195 lines (174 loc) · 8.26 KB
/
OrtoTools.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
import json
from typing import Union
from qgis.PyQt.QtCore import QVariant, NULL
from qgis.core import QgsMapLayer, Qgis
from qgis.PyQt.QtWidgets import QToolButton, QMenu, QAction
from .Wms_wmts import WMS_WMTS
from .Wms_wmts import orto_action_service
def check_if_value_empty(value: Union[str, None, QVariant]) -> bool:
return value in [None, NULL, 'NULL', 'None', '']
class OrtoAddingTool(object):
def __init__(self, parent, button, iface, group_names=None):
self.parent = parent
self.button = button
self.iface = iface
self.button.setToolTip("Dodaj zdefiniowane adresy WMS/WMTS")
self.button.setPopupMode(QToolButton.InstantPopup)
self.group_names = group_names
self.layer_actions_dict = {}
self.services = []
self.json_file = orto_action_service.wms_wmts_json_path()
with open(self.json_file, 'r+') as json_getaddress:
self.data = json.load(json_getaddress)
self.create_menu()
self.connect_ortofotomapa_group()
orto_action_service.project.projectSaved.connect(self.remove_wms_wmts_temp_group)
def get_group_names(self) -> set:
return set([value[1] for value in self.data.values() if value])
def connect_ortofotomapa_group(self) -> None:
"""
Łączy sygnał wywoływany podczas zmiany widoczności w grupie o nazwie
self.group_name z funkcją self.create_menu.
"""
for group_name in self.get_group_names():
group = orto_action_service.root.findGroup(group_name)
if group:
group.visibilityChanged.connect(self.my_refresh_menu)
group.addedChildren.connect(self.my_refresh_menu)
group.removedChildren.connect(self.my_refresh_menu)
def disconnect_ortofotomapa_group(self) -> None:
"""
Odłącza sygnał wywoływany podczas zmiany widoczności w grupie o nazwie
self.group_name z funkcją self.create_menu.
"""
for group_name in self.get_group_names():
group = orto_action_service.root.findGroup(group_name)
if group:
try:
group.visibilityChanged.disconnect(self.my_refresh_menu)
group.addedChildren.disconnect(self.my_refresh_menu)
group.removedChildren.disconnect(self.my_refresh_menu)
except:
pass
def action_clicked(self, item: QAction) -> None:
"""
Uaktualnia widoczność warstw na podstawie akcji z menu przycisku.
"""
self.layer_name = item.text()
source, group_name = self.return_orto_data(item)
identified_layers_list = orto_action_service.project.mapLayersByName(self.layer_name)
group = orto_action_service.root.findGroup(self.layer_name)
if identified_layers_list:
try:
for map_layer in identified_layers_list:
if orto_action_service.root.findLayer(map_layer).parent().name() == group_name and map_layer.source() == source:
self.set_visibility(map_layer, item)
for layer, action in self.layer_actions_dict.items():
self.set_visibility(layer, action)
except RuntimeError:
item.setChecked(True)
def set_visibility(self, layer, action):
item = orto_action_service.root.findLayer(layer)
if item:
item.parent().setItemVisibilityChecked(True)
item.setItemVisibilityCheckedRecursive(action.isChecked())
def my_refresh_menu(self):
with open(self.json_file, 'r+') as json_getaddress:
self.data = json.load(json_getaddress)
try:
self.create_menu(self.parent.runOrtoTool)
except:
self.create_menu(self.parent.main_widgets.runOrtoTool)
tab_lay = []
try:
tabs = self.parent.tabs
except:
tabs = self.parent.main_widgets.tabs
for tab in tabs:
for index in range(tab.lay.count()):
if tab.lay.itemAt(index).widget():
if tab.lay.itemAt(index).widget().objectName() == 'giapSection':
for ch in tab.lay.itemAt(index).widget().children():
if ch.objectName() == 'giapWMS':
tab_lay.append(ch)
for button in tab_lay:
button.setMenu(self.ortomenu)
def create_menu(self, but=None) -> None:
layers_names = []
self.layer_name_dict = {}
self.layer_actions_dict = {}
menu = QMenu(self.iface.mainWindow())
self.services = []
for name, service_list in self.data.items():
action = QAction(name, self.iface.mainWindow())
action.setCheckable(True)
action.triggered.connect(lambda checked, item=action: self.action_clicked(item))
service, group_name = service_list
self.layer_name_dict[action] = name
self.services.append(orto_action_service.OrtoActionService(action, service, name))
menu.addAction(action)
menu.aboutToShow.connect(self.ortocheck)
for group_name in self.get_group_names():
group = orto_action_service.root.findGroup(group_name)
if group:
for lr in group.findLayers():
layer = lr.layer()
if not layer:
continue
layers_names.append(layer.name())
action = QAction(layer.name(), self.iface.mainWindow())
action.setCheckable(True)
action.triggered.connect(lambda checked, item=action: self.action_clicked(item))
if layer.name() not in self.layer_name_dict.values():
if layer.name() in self.data.keys():
self.layer_name_dict[action] = layer.name()
self.layer_actions_dict[layer] = action
values = list(self.layer_actions_dict.values())
values.sort(key=lambda x: x.text())
list(map(menu.addAction, values))
menu.addSeparator()
setts = QAction('USTAWIENIA LISTY', self.iface.mainWindow())
setts.triggered.connect(self.setts)
menu.addAction(setts)
if but not in [None, NULL]:
but.setMenu(menu)
else:
self.button.setMenu(menu)
self.ortomenu = menu
def setts(self):
self.configure_wms = WMS_WMTS.WMS_WMTS(self)
self.configure_wms.run()
def ortocheck(self) -> None:
for orto in self.ortomenu.actions():
if orto.text() not in ['', 'USTAWIENIA LISTY']:
source, group_name = self.return_orto_data(orto)
orto_layers = orto_action_service.project.mapLayersByName(orto.text())
group = orto_action_service.root.findGroup(orto.text())
orto.setChecked(False)
if orto_layers:
for layer in orto_layers:
if orto_action_service.root.findLayer(layer).isVisible() and orto_action_service.root.findLayer(layer).parent().name() == group_name \
and layer.source() == source:
orto.setChecked(True)
break
if group and group.parent().name() == group_name:
orto.setChecked(True)
def return_orto_data(self, orto):
source = self.data[orto.text()][0]
group_name = self.data[orto.text()][1]
if check_if_value_empty(group_name):
group_name = orto_action_service.temp_group_name
return source, group_name
def remove_wms_wmts_temp_group(self):
group = orto_action_service.root.findGroup(orto_action_service.temp_group_name)
if not group:
return
for child in group.children():
if isinstance(child, QgsMapLayer):
orto_action_service.project.removeMapLayer(child)
group.removeAllChildren()
orto_action_service.root.removeChildNode(group)
self.iface.messageBar().pushMessage(
f"Grupa warstw tymczasowych <b>'{orto_action_service.temp_group_name}'</b> została usunięta z projektu",
level=Qgis.Info, duration=0)