forked from anitagraser/TimeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimemanagerprojecthandler.py
77 lines (63 loc) · 2.35 KB
/
timemanagerprojecthandler.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 29 17:22:52 2010
@author: Anita
"""
from qgis.core import *
from PyQt4.QtCore import *
class TimeManagerProjectHandler(QObject):
"""This class manages reading from and writing to the QgsProject instance.
It's not aware of the context of the variables written/read.
Variables read from a file have to be put into context by the calling class."""
@classmethod
def set_plugin_setting(cls, name, value):
"""Set temporary settings"""
QSettings().setValue("TimeManager/"+name, value)
@classmethod
def plugin_setting(cls,name):
val = QSettings().value("TimeManager/"+name)
if isinstance(val, QPyNullVariant):
val = None
return val
@classmethod
def writeSettings(cls, settings):
"""write the list of settings to QgsProject instance"""
for (key, value) in settings.items():
cls.writeSetting(key, value)
@classmethod
def writeSetting(cls, attribute, value):
"""write plugin settings to QgsProject instance"""
QgsProject.instance().writeEntry("TimeManager", attribute, value)
@classmethod
def readSetting(cls, func, attribute):
"""read a plugin setting from QgsProject instance"""
value, ok = func("TimeManager", attribute)
if ok:
return value
else:
return None
@classmethod
def readSettings(cls, metasettings):
"""read plugin settings from QgsProject instance
:param settings: a dictionary of setting names mapped to the expected type
"""
prj = QgsProject.instance()
# use QProjects functions to extract the settings from the project XML
type_to_read_function_mapping = {
str: prj.readEntry,
int: prj.readNumEntry,
float: prj.readDoubleEntry,
long: prj.readDoubleEntry,
bool: prj.readBoolEntry,
list: prj.readListEntry,
}
settings = {}
for (setting_name, type) in metasettings.items():
try:
setting_value = cls.readSetting(type_to_read_function_mapping[type], setting_name)
if setting_value is None:
raise Exception
settings[setting_name] = setting_value
except:
pass
return settings