eric6/Helpviewer/ZoomManager/ZoomManager.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a manager for site specific zoom level settings.
8 """
9
10 from __future__ import unicode_literals
11
12 import json
13
14 from PyQt5.QtCore import pyqtSignal, QObject
15
16 from Utilities.AutoSaver import AutoSaver
17 import Preferences
18
19
20 class ZoomManager(QObject):
21 """
22 Class implementing a manager for site specific zoom level settings.
23
24 @signal changed() emitted to indicate a change of the zoom level
25 """
26 changed = pyqtSignal()
27
28 def __init__(self, parent=None):
29 """
30 Constructor
31
32 @param parent reference to the parent object (QObject)
33 """
34 super(ZoomManager, self).__init__(parent)
35
36 self.__zoomDB = {}
37
38 self.__saveTimer = AutoSaver(self, self.save)
39
40 self.changed.connect(self.__saveTimer.changeOccurred)
41
42 self.__loaded = False
43
44 def close(self):
45 """
46 Public method to close the bookmark manager.
47 """
48 self.__saveTimer.saveIfNeccessary()
49
50 def load(self):
51 """
52 Public method to load the bookmarks.
53 """
54 if self.__loaded:
55 return
56
57 dbString = Preferences.getHelp("ZoomValuesDB")
58 if dbString:
59 try:
60 db = json.loads(dbString)
61 self.__zoomDB = db
62 except ValueError:
63 # ignore silently
64 pass
65
66 self.__loaded = True
67
68 def save(self):
69 """
70 Public method to save the bookmarks.
71 """
72 if not self.__loaded:
73 return
74
75 dbString = json.dumps(self.__zoomDB)
76 Preferences.setHelp("ZoomValuesDB", dbString)
77
78 def __keyFromUrl(self, url):
79 """
80 Private method to generate a DB key for an URL.
81
82 @param url URL to generate a key for
83 @type QUrl
84 @return key for the given URL
85 @rtype str
86 """
87 if url.isEmpty():
88 key = ""
89 else:
90 scheme = url.scheme()
91 host = url.host()
92 if host:
93 key = host
94 elif scheme == "file":
95 path = url.path()
96 key = path.rsplit("/", 1)[0]
97 else:
98 key = ""
99
100 return key
101
102 def setZoomValue(self, url, zoomValue):
103 """
104 Public method to record the zoom value for the given URL.
105
106 Note: Only zoom values not equal 100% are recorded.
107
108 @param url URL of the page to remember the zoom value for
109 @type QUrl
110 @param zoomValue zoom value for the URL
111 @type int
112 """
113 self.load()
114
115 key = self.__keyFromUrl(url)
116 if not key:
117 return
118
119 if ((zoomValue == 100 and key not in self.__zoomDB) or
120 (key in self.__zoomDB and self.__zoomDB[key] == zoomValue)):
121 return
122
123 if zoomValue == 100:
124 del self.__zoomDB[key]
125 else:
126 self.__zoomDB[key] = zoomValue
127
128 self.changed.emit()
129
130 def zoomValue(self, url):
131 """
132 Public method to get the zoom value for an URL.
133
134 @param url URL of the page to get the zoom value for
135 @type QUrl
136 @return zoomValue zoom value for the URL
137 @rtype int
138 """
139 self.load()
140
141 key = self.__keyFromUrl(url)
142 if not key:
143 zoom = 100
144
145 if key in self.__zoomDB:
146 zoom = self.__zoomDB[key]
147 else:
148 # default zoom value (i.e. no zoom)
149 zoom = 100
150
151 return zoom
152
153 def clear(self):
154 """
155 Public method to clear the saved zoom values.
156 """
157 self.__zoomDB = {}
158 self.__loaded = True
159
160 self.changed.emit()
161
162 def removeZoomValue(self, site):
163 """
164 Public method to remove a zoom value entry.
165
166 @param site web site name
167 @type str
168 """
169 self.load()
170
171 if site in self.__zoomDB:
172 del self.__zoomDB[site]
173 self.changed.emit()
174
175 def allSiteNames(self):
176 """
177 Public method to get a list of all site names.
178
179 @return sorted list of all site names
180 @rtype list of str
181 """
182 self.load()
183
184 return sorted(self.__zoomDB.keys())
185
186 def sitesCount(self):
187 """
188 Public method to get the number of available sites.
189
190 @return number of sites
191 @rtype int
192 """
193 self.load()
194
195 return len(self.__zoomDB)
196
197 def siteInfo(self, site):
198 """
199 Public method to get the zoom value for the site.
200
201 @param site web site name
202 @type str
203 @return zoom value for the site
204 @rtype int
205 """
206 self.load()
207
208 if site not in self.__zoomDB:
209 return None
210
211 return self.__zoomDB[site]

eric ide

mercurial