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