Preferences/PreferencesLexer.py

branch
sub_styles
changeset 6853
0922aa829e5e
parent 6645
ad476851d7e0
child 6854
f4dd76230eea
equal deleted inserted replaced
6852:273323ba9d2d 6853:0922aa829e5e
2 2
3 # Copyright (c) 2002 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2002 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a special QextScintilla lexer to handle the preferences. 7 Module implementing a special QScintilla lexer to handle the preferences.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt5.QtCore import QCoreApplication 12 from PyQt5.QtCore import QCoreApplication
13 from PyQt5.QtGui import QColor, QFont 13 from PyQt5.Qsci import QsciLexer, QsciScintillaBase
14 from PyQt5.Qsci import QsciLexer
15 14
16 import Preferences 15 import Preferences
17 import Globals 16 import Globals
18 17
19 18
32 def __repr__(self): 31 def __repr__(self):
33 """ 32 """
34 Special method returning a representation of the exception. 33 Special method returning a representation of the exception.
35 34
36 @return string representing the error message 35 @return string representing the error message
36 @rtype str
37 """ 37 """
38 return repr(self._errorMessage) 38 return repr(self._errorMessage)
39 39
40 def __str__(self): 40 def __str__(self):
41 """ 41 """
42 Special method returning a string representation of the exception. 42 Special method returning a string representation of the exception.
43 43
44 @return string representing the error message 44 @return string representing the error message
45 @rtype str
45 """ 46 """
46 return self._errorMessage 47 return self._errorMessage
47 48
48 49
49 class PreferencesLexerLanguageError(PreferencesLexerError): 50 class PreferencesLexerLanguageError(PreferencesLexerError):
52 """ 53 """
53 def __init__(self, language): 54 def __init__(self, language):
54 """ 55 """
55 Constructor 56 Constructor
56 57
57 @param language lexer language (string) 58 @param language lexer language
59 @rtype str
58 """ 60 """
59 PreferencesLexerError.__init__(self) 61 PreferencesLexerError.__init__(self)
60 self._errorMessage = QCoreApplication.translate( 62 self._errorMessage = QCoreApplication.translate(
61 "PreferencesLexerError", 63 "PreferencesLexerError",
62 'Unsupported Lexer Language: {0}').format(language) 64 'Unsupported Lexer Language: {0}').format(language)
63 65
64 66
65 class PreferencesLexer(QsciLexer): 67 class PreferencesLexer(QsciLexer):
66 """ 68 """
67 Subclass of QsciLexer to implement preferences specific lexer methods. 69 Subclass of QsciLexer to implement preferences specific lexer methods and
70 delegate some methods to the real lexer object.
68 """ 71 """
69 def __init__(self, language, parent=None): 72 def __init__(self, language, parent=None):
70 """ 73 """
71 Constructor 74 Constructor
72 75
73 @param language The lexer language. (string) 76 @param language language of the lexer
74 @param parent The parent widget of this lexer. (QextScintilla) 77 @type str
78 @param parent parent widget of this lexer (QWidget)
75 @exception PreferencesLexerLanguageError raised to indicate an invalid 79 @exception PreferencesLexerLanguageError raised to indicate an invalid
76 lexer language 80 lexer language
77 """ 81 """
78 super(PreferencesLexer, self).__init__(parent) 82 super(PreferencesLexer, self).__init__(parent)
79 83
85 else: 89 else:
86 self.__defaultFontFamily = "Bitstream Vera Sans Mono" 90 self.__defaultFontFamily = "Bitstream Vera Sans Mono"
87 91
88 # instantiate a lexer object for the given language 92 # instantiate a lexer object for the given language
89 import QScintilla.Lexers 93 import QScintilla.Lexers
90 lex = QScintilla.Lexers.getLexer(language) 94 self.__lex = QScintilla.Lexers.getLexer(language)
91 if lex is None: 95 if self.__lex is None:
92 raise PreferencesLexerLanguageError(language) 96 raise PreferencesLexerLanguageError(language)
93 97
98 # TODO: get rid of 'styles' and 'ind2style'
94 # define the local store 99 # define the local store
95 self.colours = {}
96 self.defaultColours = {}
97 self.papers = {}
98 self.defaultPapers = {}
99 self.eolFills = {}
100 self.defaultEolFills = {}
101 self.fonts = {}
102 self.defaultFonts = {}
103 self.descriptions = {}
104 self.ind2style = {} 100 self.ind2style = {}
105 self.styles = [] 101 self.styles = []
106
107 # fill local store with default values from lexer
108 # and built up styles list and conversion table from index to style
109 self.__language = lex.language()
110
111 index = 0 102 index = 0
112 for i in range(128): 103 for i in range(QsciScintillaBase.STYLE_MAX):
113 desc = lex.description(i) 104 desc = self.__lex.description(i)
114 if desc: 105 if desc:
115 self.descriptions[i] = desc
116 self.styles.append(desc) 106 self.styles.append(desc)
117
118 self.colours[i] = lex.defaultColor(i)
119 self.papers[i] = lex.defaultPaper(i)
120 self.eolFills[i] = lex.defaultEolFill(i)
121 self.fonts[i] = lex.defaultFont(i)
122 # Override QScintilla's default font family to
123 # always use a monospaced font
124 self.fonts[i].setFamily(self.__defaultFontFamily)
125
126 self.defaultColours[i] = lex.defaultColor(i)
127 self.defaultPapers[i] = lex.defaultPaper(i)
128 self.defaultEolFills[i] = lex.defaultEolFill(i)
129 self.defaultFonts[i] = lex.defaultFont(i)
130 self.defaultFonts[i].setFamily(self.__defaultFontFamily)
131
132 self.ind2style[index] = i 107 self.ind2style[index] = i
133 index += 1 108 index += 1
134 109
135 self.colorChanged.connect(self.setColor)
136 self.eolFillChanged.connect(self.setEolFill)
137 self.fontChanged.connect(self.setFont)
138 self.paperChanged.connect(self.setPaper)
139
140 # read the last stored values from preferences file 110 # read the last stored values from preferences file
141 self.readSettings(Preferences.Prefs.settings, "Scintilla") 111 self.__lex.readSettings(Preferences.Prefs.settings, "Scintilla")
142 112 # TODO: add substyles
113
114 def writeSettings(self):
115 """
116 Public method to write the lexer settings.
117 """
118 self.__lex.writeSettings(Preferences.Prefs.settings, "Scintilla")
119 # TODO: add substyles
120
143 def defaultColor(self, style): 121 def defaultColor(self, style):
144 """ 122 """
145 Public method to get the default colour of a style. 123 Public method to get the default color of a style.
146 124
147 @param style the style number (int) 125 @param style style number
148 @return colour 126 @type int
149 """ 127 @return default color
150 return self.defaultColours[style] 128 @rtype QColor
151 129 """
130 return self.__lex.defaultColor(style)
131
152 def color(self, style): 132 def color(self, style):
153 """ 133 """
154 Public method to get the colour of a style. 134 Public method to get the color of a style.
155 135
156 @param style the style number (int) 136 @param style style number
157 @return colour 137 @type int
158 """ 138 @return color
159 return self.colours[style] 139 @rtype QColor
160 140 """
141 return self.__lex.color(style)
142
161 def setColor(self, c, style): 143 def setColor(self, c, style):
162 """ 144 """
163 Public method to set the colour for a style. 145 Public method to set the color for a style.
164 146
165 @param c colour (int) 147 @param c color
166 @param style the style number (int) 148 @type QColor
167 """ 149 @param style style number
168 self.colours[style] = QColor(c) 150 @type int
169 151 """
152 self.__lex.setColor(c, style)
153
170 def defaultPaper(self, style): 154 def defaultPaper(self, style):
171 """ 155 """
172 Public method to get the default background for a style. 156 Public method to get the default background for a style.
173 157
174 @param style the style number (int) 158 @param style style number
175 @return colour 159 @type int
176 """ 160 @return default background color
177 return self.defaultPapers[style] 161 @rtype QColor
178 162 """
163 return self.__lex.defaultPaper(style)
164
179 def paper(self, style): 165 def paper(self, style):
180 """ 166 """
181 Public method to get the background for a style. 167 Public method to get the background for a style.
182 168
183 @param style the style number (int) 169 @param style the style number
184 @return colour 170 @type int
185 """ 171 @return background color
186 return self.papers[style] 172 @rtype QColor
187 173 """
174 return self.__lex.paper(style)
175
188 def setPaper(self, c, style): 176 def setPaper(self, c, style):
189 """ 177 """
190 Public method to set the background for a style. 178 Public method to set the background for a style.
191 179
192 @param c colour (int) 180 @param c background color
193 @param style the style number (int) 181 @type QColor
194 """ 182 @param style style number
195 self.papers[style] = QColor(c) 183 @type int
196 184 """
185 self.__lex.setPaper(c, style)
186
197 def defaulEolFill(self, style): 187 def defaulEolFill(self, style):
198 """ 188 """
199 Public method to get the default eolFill flag for a style. 189 Public method to get the default eolFill flag for a style.
200 190
201 @param style the style number (int) 191 @param style style number
192 @type int
193 @return default eolFill flag
194 @rtype bool
195 """
196 return self.__lex.defaultEolFill(style)
197
198 def eolFill(self, style):
199 """
200 Public method to get the eolFill flag for a style.
201
202 @param style style number
203 @type int
202 @return eolFill flag 204 @return eolFill flag
203 """ 205 @rtype bool
204 return self.defaultEolFills[style] 206 """
205 207 return self.__lex.eolFill(style)
206 def eolFill(self, style): 208
207 """
208 Public method to get the eolFill flag for a style.
209
210 @param style the style number (int)
211 @return eolFill flag
212 """
213 return self.eolFills[style]
214
215 def setEolFill(self, eolfill, style): 209 def setEolFill(self, eolfill, style):
216 """ 210 """
217 Public method to set the eolFill flag for a style. 211 Public method to set the eolFill flag for a style.
218 212
219 @param eolfill eolFill flag (boolean) 213 @param eolfill eolFill flag
220 @param style the style number (int) 214 @type bool
221 """ 215 @param style style number
222 self.eolFills[style] = eolfill 216 @type int
223 217 """
218 self.__lex.setEolFill(eolfill, style)
219
224 def defaultFont(self, style): 220 def defaultFont(self, style):
225 """ 221 """
226 Public method to get the default font for a style. 222 Public method to get the default font for a style.
227 223
228 @param style the style number (int) 224 @param style style number
225 @type int
226 @return default font
227 @rtype QFont
228 """
229 return self.__lex.defaultFont(style)
230
231 def font(self, style):
232 """
233 Public method to get the font for a style.
234
235 @param style style number
236 @type int
229 @return font 237 @return font
230 """ 238 @rtype QFont
231 return self.defaultFonts[style] 239 """
232 240 return self.__lex.font(style)
233 def font(self, style): 241
234 """
235 Public method to get the font for a style.
236
237 @param style the style number (int)
238 @return font
239 """
240 return self.fonts[style]
241
242 def setFont(self, f, style): 242 def setFont(self, f, style):
243 """ 243 """
244 Public method to set the font for a style. 244 Public method to set the font for a style.
245 245
246 @param f font 246 @param f font
247 @param style the style number (int) 247 @type QFont
248 """ 248 @param style style number
249 self.fonts[style] = QFont(f) 249 @type int
250 250 """
251 self.__lex.setFont(f, style)
252
251 def language(self): 253 def language(self):
252 """ 254 """
253 Public method to get the lexers programming language. 255 Public method to get the lexers programming language.
254 256
255 @return language 257 @return lexer programming language
256 """ 258 @rtype str
257 return self.__language 259 """
260 return self.__lex.language()
258 261
259 def description(self, style): 262 def description(self, style):
260 """ 263 """
261 Public method to get a descriptive string for a style. 264 Public method to get a descriptive string for a style.
262 265
263 @param style the style number (int) 266 @param style style number
264 @return description of the style (string) 267 @type int
265 """ 268 @return description of the style
266 if style in self.descriptions: 269 @rtype str
267 return self.descriptions[style] 270 """
268 else: 271 return self.__lex.description(style)
269 return ""

eric ide

mercurial