|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2002 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a special QextScintilla lexer to handle the preferences. |
|
8 """ |
|
9 |
|
10 import sys |
|
11 |
|
12 from PyQt4.QtCore import * |
|
13 from PyQt4.QtGui import QColor, QFont, QApplication |
|
14 from PyQt4.Qsci import QsciLexer, QsciScintilla |
|
15 |
|
16 import QScintilla.Lexers |
|
17 |
|
18 import Preferences |
|
19 |
|
20 class PreferencesLexerError(Exception): |
|
21 """ |
|
22 Class defining a special error for the PreferencesLexer class. |
|
23 """ |
|
24 def __init__(self): |
|
25 """ |
|
26 Constructor |
|
27 """ |
|
28 self._errorMessage = \ |
|
29 QApplication.translate("PreferencesLexerError", |
|
30 "Unspecific PreferencesLexer error.") |
|
31 |
|
32 def __repr__(self): |
|
33 """ |
|
34 Private method returning a representation of the exception. |
|
35 |
|
36 @return string representing the error message |
|
37 """ |
|
38 return repr(self._errorMessage) |
|
39 |
|
40 def __str__(self): |
|
41 """ |
|
42 Private method returning a string representation of the exception. |
|
43 |
|
44 @return string representing the error message |
|
45 """ |
|
46 return self._errorMessage |
|
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 PreferencesLexerError.__init__(self) |
|
57 self._errorMessage = \ |
|
58 QApplication.translate("PreferencesLexerError", |
|
59 'Unsupported Lexer Language: {0}').format(language) |
|
60 |
|
61 class PreferencesLexer(QsciLexer): |
|
62 """ |
|
63 Subclass of QsciLexer to implement preferences specific lexer methods. |
|
64 """ |
|
65 def __init__(self, language, parent=None): |
|
66 """ |
|
67 Constructor |
|
68 |
|
69 @param language The lexer language. (string) |
|
70 @param parent The parent widget of this lexer. (QextScintilla) |
|
71 """ |
|
72 QsciLexer.__init__(self, parent) |
|
73 |
|
74 # instantiate a lexer object for the given language |
|
75 lex = QScintilla.Lexers.getLexer(language) |
|
76 if lex is None: |
|
77 raise PreferencesLexerLanguageError(language) |
|
78 |
|
79 # define the local store |
|
80 self.colours = {} |
|
81 self.defaultColours = {} |
|
82 self.papers = {} |
|
83 self.defaultPapers = {} |
|
84 self.eolFills = {} |
|
85 self.defaultEolFills = {} |
|
86 self.fonts = {} |
|
87 self.defaultFonts = {} |
|
88 self.descriptions = {} |
|
89 self.ind2style = {} |
|
90 self.styles = [] |
|
91 |
|
92 # fill local store with default values from lexer |
|
93 # and built up styles list and conversion table from index to style |
|
94 self.__language = lex.language() |
|
95 |
|
96 index = 0 |
|
97 for i in range(128): |
|
98 desc = lex.description(i) |
|
99 if desc: |
|
100 self.descriptions[i] = desc |
|
101 self.styles.append(desc) |
|
102 |
|
103 self.colours[i] = lex.defaultColor(i) |
|
104 self.papers[i] = lex.defaultPaper(i) |
|
105 self.eolFills[i] = lex.defaultEolFill(i) |
|
106 self.fonts[i] = lex.defaultFont(i) |
|
107 |
|
108 self.defaultColours[i] = lex.defaultColor(i) |
|
109 self.defaultPapers[i] = lex.defaultPaper(i) |
|
110 self.defaultEolFills[i] = lex.defaultEolFill(i) |
|
111 self.defaultFonts[i] = lex.defaultFont(i) |
|
112 |
|
113 self.ind2style[index] = i |
|
114 index += 1 |
|
115 |
|
116 self.connect(self, SIGNAL("colorChanged (const QColor&, int)"), |
|
117 self.setColor) |
|
118 self.connect(self, SIGNAL("eolFillChanged (bool, int)"), |
|
119 self.setEolFill) |
|
120 self.connect(self, SIGNAL("fontChanged (const QFont&, int)"), |
|
121 self.setFont) |
|
122 self.connect(self, SIGNAL("paperChanged (const QColor&, int )"), |
|
123 self.setPaper) |
|
124 |
|
125 # read the last stored values from preferences file |
|
126 self.readSettings(Preferences.Prefs.settings, "Scintilla") |
|
127 |
|
128 def defaultColor(self, style): |
|
129 """ |
|
130 Public method to get the default colour of a style. |
|
131 |
|
132 @param style the style number (int) |
|
133 @return colour |
|
134 """ |
|
135 return self.defaultColours[style] |
|
136 |
|
137 def color(self, style): |
|
138 """ |
|
139 Public method to get the colour of a style. |
|
140 |
|
141 @param style the style number (int) |
|
142 @return colour |
|
143 """ |
|
144 return self.colours[style] |
|
145 |
|
146 def setColor(self, c, style): |
|
147 """ |
|
148 Public method to set the colour for a style. |
|
149 |
|
150 @param c colour (int) |
|
151 @param style the style number (int) |
|
152 """ |
|
153 self.colours[style] = QColor(c) |
|
154 |
|
155 def defaultPaper(self, style): |
|
156 """ |
|
157 Public method to get the default background for a style. |
|
158 |
|
159 @param style the style number (int) |
|
160 @return colour |
|
161 """ |
|
162 return self.defaultPapers[style] |
|
163 |
|
164 def paper(self, style): |
|
165 """ |
|
166 Public method to get the background for a style. |
|
167 |
|
168 @param style the style number (int) |
|
169 @return colour |
|
170 """ |
|
171 return self.papers[style] |
|
172 |
|
173 def setPaper(self, c, style): |
|
174 """ |
|
175 Public method to set the background for a style. |
|
176 |
|
177 @param c colour (int) |
|
178 @param style the style number (int) |
|
179 """ |
|
180 self.papers[style] = QColor(c) |
|
181 |
|
182 def defaulEolFill(self, style): |
|
183 """ |
|
184 Public method to get the default eolFill flag for a style. |
|
185 |
|
186 @param style the style number (int) |
|
187 @return eolFill flag |
|
188 """ |
|
189 return self.defaultEolFills[style] |
|
190 |
|
191 def eolFill(self, style): |
|
192 """ |
|
193 Public method to get the eolFill flag for a style. |
|
194 |
|
195 @param style the style number (int) |
|
196 @return eolFill flag |
|
197 """ |
|
198 return self.eolFills[style] |
|
199 |
|
200 def setEolFill(self, eolfill, style): |
|
201 """ |
|
202 Public method to set the eolFill flag for a style. |
|
203 |
|
204 @param eolfill eolFill flag (boolean) |
|
205 @param style the style number (int) |
|
206 """ |
|
207 self.eolFills[style] = eolfill |
|
208 |
|
209 def defaultFont(self, style): |
|
210 """ |
|
211 Public method to get the default font for a style. |
|
212 |
|
213 @param style the style number (int) |
|
214 @return font |
|
215 """ |
|
216 return self.defaultFonts[style] |
|
217 |
|
218 def font(self, style): |
|
219 """ |
|
220 Public method to get the font for a style. |
|
221 |
|
222 @param style the style number (int) |
|
223 @return font |
|
224 """ |
|
225 return self.fonts[style] |
|
226 |
|
227 def setFont(self, f, style): |
|
228 """ |
|
229 Public method to set the font for a style. |
|
230 |
|
231 @param f font |
|
232 @param style the style number (int) |
|
233 """ |
|
234 self.fonts[style] = QFont(f) |
|
235 |
|
236 def language(self): |
|
237 """ |
|
238 Public method to get the lexers programming language. |
|
239 |
|
240 @return language |
|
241 """ |
|
242 return self.__language |
|
243 |
|
244 def description(self, style): |
|
245 """ |
|
246 Public method to get a descriptive string for a style. |
|
247 |
|
248 @param style the style number (int) |
|
249 @return description of the style (string) |
|
250 """ |
|
251 if self.descriptions.has_key(style): |
|
252 return self.descriptions[style] |
|
253 else: |
|
254 return "" |