/
writerRegistry.py
160 lines (134 loc) · 5.54 KB
/
writerRegistry.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
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Nyall Dawson (nyall.dawson@gmail.com)
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from qgis.core import QgsProject
from qgis.PyQt.QtCore import QObject
from qgis2web.olwriter import OpenLayersWriter
from qgis2web.leafletWriter import LeafletWriter
from qgis2web.configparams import getDefaultParams
translator = QObject()
class WriterRegistry(object):
"""
A registry for known writer types.
"""
def __init__(self):
self.writers = {e.type(): e for e in
[OpenLayersWriter, LeafletWriter]}
def getWriters(self):
"""
:return: List of available writers
"""
return self.writers.values()
def saveTypeToProject(self, type):
"""
Stores a writer type as the type associated with the loaded project
:param type: type string for associated writer class
"""
assert QgsProject.instance().writeEntry("qgis2web", "mapFormat", type)
def getWriterFactoryFromProject(self):
"""
Returns the factory for the writer type associated with the
load project.
:return:
"""
try:
type = QgsProject.instance().readEntry("qgis2web",
"mapFormat")[0]
for w in self.writers.values():
if type.lower() == w.type().lower():
return w
except Exception:
pass
return OpenLayersWriter # default to OpenLayersWriter
@staticmethod
def sanitiseKey(key):
"""
Sanitises a parameter key to make it safe to store in settings
"""
return key.replace(' ', '')
def saveParamsToProject(self, params):
"""
Saves writer parameters to the current project
:param params: writer parameter dictionary
"""
for group, settings in params.items():
for param, value in settings.items():
if param == "Attribute filter":
continue
if isinstance(value, bool):
QgsProject.instance().writeEntryBool("qgis2web",
self.sanitiseKey(
param),
value)
else:
QgsProject.instance().writeEntry("qgis2web",
self.sanitiseKey(param),
value)
def readParamFromProject(self, parameter, default_value):
"""
Reads the value of a single parameter from the current
project.
:param parameter: parameter key
:param default_value: default value for parameter
"""
project = QgsProject.instance()
key_string = self.sanitiseKey(parameter)
value = default_value
if isinstance(default_value, bool):
if project.readBoolEntry(
"qgis2web", key_string)[1]:
value = project.readBoolEntry("qgis2web",
key_string)[0]
elif isinstance(default_value, int):
if project.readNumEntry(
"qgis2web", key_string)[1]:
value = project.readNumEntry("qgis2web",
key_string)[0]
else:
if (isinstance(project.readEntry("qgis2web",
key_string)[0], str) and
project.readEntry("qgis2web", key_string)[0] != ""):
value = project.readEntry("qgis2web", key_string)[0]
return value
def readParamsFromProject(self):
"""
Reads all writer parameters from the current project
:return: default writer parameters within parameters replaced
by any matching settings in the current projects
"""
default_params = getDefaultParams()
read_params = default_params
for group, settings in default_params.items():
for param, default_value in settings.items():
value = self.readParamFromProject(param, default_value)
read_params[group][param] = value
return read_params
def createWriterFromProject(self):
"""
Creates a writer matching the state from the current project
"""
writer = self.getWriterFactoryFromProject()()
writer.params = self.readParamsFromProject()
return writer
def saveWriterToProject(self, writer):
"""
Saves the settings from a writer to the current project
"""
QgsProject.instance().removeEntry("qgis2web", "/")
self.saveTypeToProject(writer.type())
self.saveParamsToProject(writer.params)
# canonical instance.
WRITER_REGISTRY = WriterRegistry()