|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2002 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a special QScintilla lexer to handle the preferences. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import QCoreApplication, QObject |
|
13 from PyQt5.Qsci import QsciScintillaBase |
|
14 |
|
15 import Preferences |
|
16 import Globals |
|
17 |
|
18 |
|
19 class PreferencesLexerError(Exception): |
|
20 """ |
|
21 Class defining a special error for the PreferencesLexer class. |
|
22 """ |
|
23 def __init__(self): |
|
24 """ |
|
25 Constructor |
|
26 """ |
|
27 self._errorMessage = QCoreApplication.translate( |
|
28 "PreferencesLexerError", |
|
29 "Unspecific PreferencesLexer error.") |
|
30 |
|
31 def __repr__(self): |
|
32 """ |
|
33 Special method returning a representation of the exception. |
|
34 |
|
35 @return string representing the error message |
|
36 @rtype str |
|
37 """ |
|
38 return repr(self._errorMessage) |
|
39 |
|
40 def __str__(self): |
|
41 """ |
|
42 Special method returning a string representation of the exception. |
|
43 |
|
44 @return string representing the error message |
|
45 @rtype str |
|
46 """ |
|
47 return self._errorMessage |
|
48 |
|
49 |
|
50 class PreferencesLexerLanguageError(PreferencesLexerError): |
|
51 """ |
|
52 Class defining a special error for the PreferencesLexer class. |
|
53 """ |
|
54 def __init__(self, language): |
|
55 """ |
|
56 Constructor |
|
57 |
|
58 @param language lexer language |
|
59 @type str |
|
60 """ |
|
61 PreferencesLexerError.__init__(self) |
|
62 self._errorMessage = QCoreApplication.translate( |
|
63 "PreferencesLexerError", |
|
64 'Unsupported Lexer Language: {0}').format(language) |
|
65 |
|
66 |
|
67 class PreferencesLexer(QObject): |
|
68 """ |
|
69 Class implementing a Lexer facade for the highlighting styles |
|
70 configuration. |
|
71 """ |
|
72 def __init__(self, language, parent=None): |
|
73 """ |
|
74 Constructor |
|
75 |
|
76 @param language language of the lexer |
|
77 @type str |
|
78 @param parent parent widget of this lexer (QWidget) |
|
79 @exception PreferencesLexerLanguageError raised to indicate an invalid |
|
80 lexer language |
|
81 """ |
|
82 super(PreferencesLexer, self).__init__(parent) |
|
83 |
|
84 # These default font families are taken from QScintilla |
|
85 if Globals.isWindowsPlatform(): |
|
86 self.__defaultFontFamily = "Courier New" |
|
87 elif Globals.isMacPlatform(): |
|
88 self.__defaultFontFamily = "Courier" |
|
89 else: |
|
90 self.__defaultFontFamily = "Bitstream Vera Sans Mono" |
|
91 |
|
92 # instantiate a lexer object for the given language |
|
93 import QScintilla.Lexers |
|
94 self.__lex = QScintilla.Lexers.getLexer(language) |
|
95 if self.__lex is None: |
|
96 raise PreferencesLexerLanguageError(language) |
|
97 |
|
98 # read the last stored values from preferences file |
|
99 self.__lex.readSettings(Preferences.Prefs.settings, "Scintilla") |
|
100 if self.__lex.hasSubstyles(): |
|
101 self.__lex.loadSubstyles() |
|
102 |
|
103 def writeSettings(self): |
|
104 """ |
|
105 Public method to write the lexer settings. |
|
106 """ |
|
107 self.__lex.writeSettings(Preferences.Prefs.settings, "Scintilla") |
|
108 if self.__lex.hasSubstyles(): |
|
109 self.__lex.writeSubstyles() |
|
110 |
|
111 def getStyles(self): |
|
112 """ |
|
113 Public method to get a list of all supported styles. |
|
114 |
|
115 @return list of tuples each containing the description of the style, |
|
116 style number and sub-style number (or -1 for no sub-style) |
|
117 @rtype list of tuples of (str, int, int) |
|
118 """ |
|
119 styles = [] |
|
120 |
|
121 for i in range(QsciScintillaBase.STYLE_MAX): |
|
122 desc = self.__lex.description(i) |
|
123 if desc: |
|
124 styles.append((desc, i, -1)) |
|
125 if self.__lex.hasSubstyles(): |
|
126 for baseStyle in self.__lex.getBaseStyles(): |
|
127 for subStyle in range(self.__lex.substylesCount(baseStyle)): |
|
128 desc = self.__lex.substyleDescription(baseStyle, subStyle) |
|
129 styles.append((desc, baseStyle, subStyle)) |
|
130 |
|
131 return styles |
|
132 |
|
133 def getSubStyles(self, style): |
|
134 """ |
|
135 Public method to get a list of all sub-styles of a style. |
|
136 |
|
137 @param style style number |
|
138 @type int |
|
139 @return list of tuples each containing the description of the style, |
|
140 style number and sub-style number (or -1 for no sub-style) |
|
141 @rtype list of tuples of (str, int, int) |
|
142 """ |
|
143 styles = [] |
|
144 |
|
145 if self.isBaseStyle(style): |
|
146 for subStyle in range(self.__lex.substylesCount(style)): |
|
147 desc = self.__lex.substyleDescription(style, subStyle) |
|
148 styles.append((desc, style, subStyle)) |
|
149 |
|
150 return styles |
|
151 |
|
152 def defaultColor(self, style, substyle=-1): |
|
153 """ |
|
154 Public method to get the default color of a style. |
|
155 |
|
156 @param style style number |
|
157 @type int |
|
158 @param substyle sub-style number |
|
159 @type int |
|
160 @return default color |
|
161 @rtype QColor |
|
162 """ |
|
163 if substyle >= 0: |
|
164 color = self.__lex.substyleDefaultColor(style, substyle) |
|
165 else: |
|
166 color = self.__lex.defaultColor(style) |
|
167 |
|
168 return color |
|
169 |
|
170 def color(self, style, substyle=-1): |
|
171 """ |
|
172 Public method to get the color of a style. |
|
173 |
|
174 @param style style number |
|
175 @type int |
|
176 @param substyle sub-style number |
|
177 @type int |
|
178 @return color |
|
179 @rtype QColor |
|
180 """ |
|
181 if substyle >= 0: |
|
182 color = self.__lex.substyleColor(style, substyle) |
|
183 else: |
|
184 color = self.__lex.color(style) |
|
185 |
|
186 return color |
|
187 |
|
188 def setColor(self, c, style, substyle=-1): |
|
189 """ |
|
190 Public method to set the color for a style. |
|
191 |
|
192 @param c color |
|
193 @type QColor |
|
194 @param style style number |
|
195 @type int |
|
196 @param substyle sub-style number |
|
197 @type int |
|
198 """ |
|
199 if substyle >= 0: |
|
200 self.__lex.setSubstyleColor(c, style, substyle) |
|
201 else: |
|
202 self.__lex.setColor(c, style) |
|
203 |
|
204 def defaultPaper(self, style, substyle=-1): |
|
205 """ |
|
206 Public method to get the default background for a style. |
|
207 |
|
208 @param style style number |
|
209 @type int |
|
210 @param substyle sub-style number |
|
211 @type int |
|
212 @return default background color |
|
213 @rtype QColor |
|
214 """ |
|
215 if substyle >= 0: |
|
216 color = self.__lex.substyleDefaultPaper(style, substyle) |
|
217 else: |
|
218 color = self.__lex.defaultPaper(style) |
|
219 |
|
220 return color |
|
221 |
|
222 def paper(self, style, substyle=-1): |
|
223 """ |
|
224 Public method to get the background for a style. |
|
225 |
|
226 @param style the style number |
|
227 @type int |
|
228 @param substyle sub-style number |
|
229 @type int |
|
230 @return background color |
|
231 @rtype QColor |
|
232 """ |
|
233 if substyle >= 0: |
|
234 color = self.__lex.substylePaper(style, substyle) |
|
235 else: |
|
236 color = self.__lex.paper(style) |
|
237 |
|
238 return color |
|
239 |
|
240 def setPaper(self, c, style, substyle=-1): |
|
241 """ |
|
242 Public method to set the background for a style. |
|
243 |
|
244 @param c background color |
|
245 @type QColor |
|
246 @param style style number |
|
247 @type int |
|
248 @param substyle sub-style number |
|
249 @type int |
|
250 """ |
|
251 if substyle >= 0: |
|
252 self.__lex.setSubstylePaper(c, style, substyle) |
|
253 else: |
|
254 self.__lex.setPaper(c, style) |
|
255 |
|
256 def defaultEolFill(self, style, substyle=-1): |
|
257 """ |
|
258 Public method to get the default eolFill flag for a style. |
|
259 |
|
260 @param style style number |
|
261 @type int |
|
262 @param substyle sub-style number |
|
263 @type int |
|
264 @return default eolFill flag |
|
265 @rtype bool |
|
266 """ |
|
267 if substyle >= 0: |
|
268 eolFill = self.__lex.substyleDefaultEolFill(style, substyle) |
|
269 else: |
|
270 eolFill = self.__lex.defaultEolFill(style) |
|
271 |
|
272 return eolFill |
|
273 |
|
274 def eolFill(self, style, substyle=-1): |
|
275 """ |
|
276 Public method to get the eolFill flag for a style. |
|
277 |
|
278 @param style style number |
|
279 @type int |
|
280 @param substyle sub-style number |
|
281 @type int |
|
282 @return eolFill flag |
|
283 @rtype bool |
|
284 """ |
|
285 if substyle >= 0: |
|
286 eolFill = self.__lex.substyleEolFill(style, substyle) |
|
287 else: |
|
288 eolFill = self.__lex.eolFill(style) |
|
289 |
|
290 return eolFill |
|
291 |
|
292 def setEolFill(self, eolfill, style, substyle=-1): |
|
293 """ |
|
294 Public method to set the eolFill flag for a style. |
|
295 |
|
296 @param eolfill eolFill flag |
|
297 @type bool |
|
298 @param style style number |
|
299 @type int |
|
300 @param substyle sub-style number |
|
301 @type int |
|
302 """ |
|
303 if substyle >= 0: |
|
304 self.__lex.setSubstyleEolFill(eolfill, style, substyle) |
|
305 else: |
|
306 self.__lex.setEolFill(eolfill, style) |
|
307 |
|
308 def defaultFont(self, style, substyle=-1): |
|
309 """ |
|
310 Public method to get the default font for a style. |
|
311 |
|
312 @param style style number |
|
313 @type int |
|
314 @param substyle sub-style number |
|
315 @type int |
|
316 @return default font |
|
317 @rtype QFont |
|
318 """ |
|
319 if substyle >= 0: |
|
320 font = self.__lex.substyleDefaultFont(style, substyle) |
|
321 else: |
|
322 font = self.__lex.defaultFont(style) |
|
323 |
|
324 return font |
|
325 |
|
326 def font(self, style, substyle=-1): |
|
327 """ |
|
328 Public method to get the font for a style. |
|
329 |
|
330 @param style style number |
|
331 @type int |
|
332 @param substyle sub-style number |
|
333 @type int |
|
334 @return font |
|
335 @rtype QFont |
|
336 """ |
|
337 if substyle >= 0: |
|
338 font = self.__lex.substyleFont(style, substyle) |
|
339 else: |
|
340 font = self.__lex.font(style) |
|
341 |
|
342 return font |
|
343 |
|
344 def setFont(self, f, style, substyle=-1): |
|
345 """ |
|
346 Public method to set the font for a style. |
|
347 |
|
348 @param f font |
|
349 @type QFont |
|
350 @param style style number |
|
351 @type int |
|
352 @param substyle sub-style number |
|
353 @type int |
|
354 """ |
|
355 if substyle >= 0: |
|
356 self.__lex.setSubstyleFont(f, style, substyle) |
|
357 else: |
|
358 self.__lex.setFont(f, style) |
|
359 |
|
360 def defaultWords(self, style, substyle=-1): |
|
361 """ |
|
362 Public method to get the default list of words for a style. |
|
363 |
|
364 @param style style number |
|
365 @type int |
|
366 @param substyle sub-style number |
|
367 @type int |
|
368 @return whitespace separated default list of words |
|
369 @rtype str |
|
370 """ |
|
371 if substyle >= 0: |
|
372 words = self.__lex.substyleDefaultWords(style, substyle) |
|
373 else: |
|
374 words = "" |
|
375 |
|
376 return words |
|
377 |
|
378 def words(self, style, substyle=-1): |
|
379 """ |
|
380 Public method to get the list of words for a style. |
|
381 |
|
382 @param style style number |
|
383 @type int |
|
384 @param substyle sub-style number |
|
385 @type int |
|
386 @return whitespace separated list of words |
|
387 @rtype str |
|
388 """ |
|
389 if substyle >= 0: |
|
390 words = self.__lex.substyleWords(style, substyle) |
|
391 else: |
|
392 words = "" |
|
393 |
|
394 return words |
|
395 |
|
396 def setWords(self, words, style, substyle=-1): |
|
397 """ |
|
398 Public method to set the list of words for a style. |
|
399 |
|
400 @param words whitespace separated list of words |
|
401 @type str |
|
402 @param style style number |
|
403 @type int |
|
404 @param substyle sub-style number |
|
405 @type int |
|
406 """ |
|
407 if substyle >= 0: |
|
408 # only supported for sub-styles |
|
409 self.__lex.setSubstyleWords(words, style, substyle) |
|
410 |
|
411 def defaultDescription(self, style, substyle=-1): |
|
412 """ |
|
413 Public method to get the default descriptive string for a style. |
|
414 |
|
415 @param style style number |
|
416 @type int |
|
417 @param substyle sub-style number |
|
418 @type int |
|
419 @return default description of the style |
|
420 @rtype str |
|
421 """ |
|
422 if substyle >= 0: |
|
423 desc = self.__lex.substyleDefaultDescription(style, substyle) |
|
424 else: |
|
425 # for base styles return the hard coded description |
|
426 desc = self.__lex.description(style) |
|
427 |
|
428 return desc |
|
429 |
|
430 def description(self, style, substyle=-1): |
|
431 """ |
|
432 Public method to get a descriptive string for a style. |
|
433 |
|
434 @param style style number |
|
435 @type int |
|
436 @param substyle sub-style number |
|
437 @type int |
|
438 @return description of the style |
|
439 @rtype str |
|
440 """ |
|
441 if substyle >= 0: |
|
442 desc = self.__lex.substyleDescription(style, substyle) |
|
443 else: |
|
444 desc = self.__lex.description(style) |
|
445 |
|
446 return desc |
|
447 |
|
448 def setDescription(self, description, style, substyle=-1): |
|
449 """ |
|
450 Public method to set a descriptive string for a style. |
|
451 |
|
452 @param description description for the style |
|
453 @type str |
|
454 @param style style number |
|
455 @type int |
|
456 @param substyle sub-style number |
|
457 @type int |
|
458 """ |
|
459 if substyle >= 0: |
|
460 # only supported for sub-styles |
|
461 self.__lex.setSubstyleDescription(description, style, substyle) |
|
462 |
|
463 def language(self): |
|
464 """ |
|
465 Public method to get the lexers programming language. |
|
466 |
|
467 @return lexer programming language |
|
468 @rtype str |
|
469 """ |
|
470 return self.__lex.language() |
|
471 |
|
472 def hasStyle(self, style, substyle): |
|
473 """ |
|
474 Public method to test for a given style definition. |
|
475 |
|
476 @param style style number |
|
477 @type int |
|
478 @param substyle sub-style number |
|
479 @type int |
|
480 @return flag indicating the existence of a style definition |
|
481 @rtype bool |
|
482 """ |
|
483 if substyle >= 0: |
|
484 ok = self.__lex.hasSubstyle(style, substyle) |
|
485 else: |
|
486 ok = True |
|
487 |
|
488 return ok |
|
489 |
|
490 def isBaseStyle(self, style): |
|
491 """ |
|
492 Public method to test, if a given style may have sub-styles. |
|
493 |
|
494 @param style base style number |
|
495 @type int |
|
496 @return flag indicating that the style may have sub-styles |
|
497 @rtype bool |
|
498 """ |
|
499 return self.__lex.hasSubstyles() and self.__lex.isBaseStyle(style) |
|
500 |
|
501 def addSubstyle(self, style): |
|
502 """ |
|
503 Public method to add an empty sub-style to a given style. |
|
504 |
|
505 @param style style number |
|
506 @type int |
|
507 @return allocated sub-style number or -1 to indicate an error |
|
508 @rtype int |
|
509 """ |
|
510 return self.__lex.addSubstyle(style) |
|
511 |
|
512 def delSubstyle(self, style, substyle): |
|
513 """ |
|
514 Public method to delete a given sub-style definition. |
|
515 |
|
516 @param style base style number |
|
517 @type int |
|
518 @param substyle sub-style number |
|
519 @type int |
|
520 @return flag indicating successful deletion |
|
521 @rtype bool |
|
522 """ |
|
523 return self.__lex.delSubstyle(style, substyle) |
|
524 |
|
525 def loadDefaultSubStyles(self, style): |
|
526 """ |
|
527 Public method to load the default sub-styles for a given base style. |
|
528 |
|
529 @param style style number |
|
530 @type int |
|
531 """ |
|
532 self.__lex.loadDefaultSubStyles(style) |