Preferences/ConfigurationPages/EditorHighlightingStylesPage.py

changeset 12
1d8dd9706f46
parent 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
11:b0996e4a289e 12:1d8dd9706f46
11 11
12 from PyQt4.QtCore import pyqtSlot, QFileInfo 12 from PyQt4.QtCore import pyqtSlot, QFileInfo
13 from PyQt4.QtGui import QPalette, QFileDialog, QColorDialog, QFontDialog, \ 13 from PyQt4.QtGui import QPalette, QFileDialog, QColorDialog, QFontDialog, \
14 QInputDialog, QMessageBox 14 QInputDialog, QMessageBox
15 15
16 from ConfigurationPageBase import ConfigurationPageBase 16 from .ConfigurationPageBase import ConfigurationPageBase
17 from Ui_EditorHighlightingStylesPage import Ui_EditorHighlightingStylesPage 17 from .Ui_EditorHighlightingStylesPage import Ui_EditorHighlightingStylesPage
18 18
19 from E4XML.XMLUtilities import make_parser 19 from E4XML.XMLUtilities import make_parser
20 from E4XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError 20 from E4XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError
21 from E4XML.XMLEntityResolver import XMLEntityResolver 21 from E4XML.XMLEntityResolver import XMLEntityResolver
22 from E4XML.HighlightingStylesWriter import HighlightingStylesWriter 22 from E4XML.HighlightingStylesWriter import HighlightingStylesWriter
41 41
42 self.lexer = None 42 self.lexer = None
43 self.lexers = lexers 43 self.lexers = lexers
44 44
45 # set initial values 45 # set initial values
46 languages = [''] + self.lexers.keys() 46 languages = sorted([''] + list(self.lexers.keys()))
47 languages.sort()
48 self.lexerLanguageComboBox.addItems(languages) 47 self.lexerLanguageComboBox.addItems(languages)
49 self.on_lexerLanguageComboBox_activated("") 48 self.on_lexerLanguageComboBox_activated("")
50 49
51 def save(self): 50 def save(self):
52 """ 51 """
53 Public slot to save the Editor Highlighting Styles configuration. 52 Public slot to save the Editor Highlighting Styles configuration.
54 """ 53 """
55 for lexer in self.lexers.values(): 54 for lexer in list(self.lexers.values()):
56 lexer.writeSettings(Preferences.Prefs.settings, "Scintilla") 55 lexer.writeSettings(Preferences.Prefs.settings, "Scintilla")
57 56
58 @pyqtSlot(str) 57 @pyqtSlot(str)
59 def on_lexerLanguageComboBox_activated(self, language): 58 def on_lexerLanguageComboBox_activated(self, language):
60 """ 59 """
153 if colour.isValid(): 152 if colour.isValid():
154 pl = self.sampleText.palette() 153 pl = self.sampleText.palette()
155 pl.setColor(QPalette.Base, colour) 154 pl.setColor(QPalette.Base, colour)
156 self.sampleText.setPalette(pl) 155 self.sampleText.setPalette(pl)
157 self.sampleText.repaint() 156 self.sampleText.repaint()
158 for style in self.lexer.ind2style.values(): 157 for style in list(self.lexer.ind2style.values()):
159 self.lexer.setPaper(colour, style) 158 self.lexer.setPaper(colour, style)
160 159
161 @pyqtSlot() 160 @pyqtSlot()
162 def on_fontButton_clicked(self): 161 def on_fontButton_clicked(self):
163 """ 162 """
179 Private method used to change the font of all styles of a selected lexer. 178 Private method used to change the font of all styles of a selected lexer.
180 """ 179 """
181 font, ok = QFontDialog.getFont(self.lexer.font(self.style)) 180 font, ok = QFontDialog.getFont(self.lexer.font(self.style))
182 if ok: 181 if ok:
183 self.sampleText.setFont(font) 182 self.sampleText.setFont(font)
184 for style in self.lexer.ind2style.values(): 183 for style in list(self.lexer.ind2style.values()):
185 self.lexer.setFont(font, style) 184 self.lexer.setFont(font, style)
186 185
187 def on_eolfillCheckBox_toggled(self, b): 186 def on_eolfillCheckBox_toggled(self, b):
188 """ 187 """
189 Private method used to set the eolfill for the selected style and lexer. 188 Private method used to set the eolfill for the selected style and lexer.
206 [on, off], 205 [on, off],
207 0, False) 206 0, False)
208 if ok: 207 if ok:
209 enabled = selection == on 208 enabled = selection == on
210 self.eolfillCheckBox.setChecked(enabled) 209 self.eolfillCheckBox.setChecked(enabled)
211 for style in self.lexer.ind2style.values(): 210 for style in list(self.lexer.ind2style.values()):
212 self.lexer.setEolFill(enabled, style) 211 self.lexer.setEolFill(enabled, style)
213 212
214 @pyqtSlot() 213 @pyqtSlot()
215 def on_defaultButton_clicked(self): 214 def on_defaultButton_clicked(self):
216 """ 215 """
227 @pyqtSlot() 226 @pyqtSlot()
228 def on_allDefaultButton_clicked(self): 227 def on_allDefaultButton_clicked(self):
229 """ 228 """
230 Private method to set all styles to their default values. 229 Private method to set all styles to their default values.
231 """ 230 """
232 for style in self.lexer.ind2style.values(): 231 for style in list(self.lexer.ind2style.values()):
233 self.__setToDefault(style) 232 self.__setToDefault(style)
234 self.on_styleElementList_currentRowChanged(self.styleElementList.currentRow()) 233 self.on_styleElementList_currentRowChanged(self.styleElementList.currentRow())
235 234
236 def __setToDefault(self, style): 235 def __setToDefault(self, style):
237 """ 236 """
268 @pyqtSlot() 267 @pyqtSlot()
269 def on_exportAllButton_clicked(self): 268 def on_exportAllButton_clicked(self):
270 """ 269 """
271 Private slot to export the styles of all lexers. 270 Private slot to export the styles of all lexers.
272 """ 271 """
273 self.__exportStyles(self.lexers.values()) 272 self.__exportStyles(list(self.lexers.values()))
274 273
275 def __exportStyles(self, lexers): 274 def __exportStyles(self, lexers):
276 """ 275 """
277 Private method to export the styles of the given lexers. 276 Private method to export the styles of the given lexers.
278 277
294 ex = selectedFilter.split("(*")[1].split(")")[0] 293 ex = selectedFilter.split("(*")[1].split(")")[0]
295 if ex: 294 if ex:
296 fn += ex 295 fn += ex
297 296
298 try: 297 try:
299 f = open(fn, "wb") 298 f = open(fn, "w")
300 HighlightingStylesWriter(f, lexers).writeXML() 299 HighlightingStylesWriter(f, lexers).writeXML()
301 f.close() 300 f.close()
302 except IOError, err: 301 except IOError as err:
303 QMessageBox.critical(self, 302 QMessageBox.critical(self,
304 self.trUtf8("Export Highlighting Styles"), 303 self.trUtf8("Export Highlighting Styles"),
305 self.trUtf8("""<p>The highlighting styles could not be exported""" 304 self.trUtf8("""<p>The highlighting styles could not be exported"""
306 """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ 305 """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""")\
307 .format(fn, unicode(err)) 306 .format(fn, str(err))
308 ) 307 )
309 308
310 def __importStyles(self, lexers): 309 def __importStyles(self, lexers):
311 """ 310 """
312 Private method to import the styles of the given lexers. 311 Private method to import the styles of the given lexers.
321 320
322 if not fn: 321 if not fn:
323 return 322 return
324 323
325 try: 324 try:
326 f = open(fn, "rb") 325 f = open(fn, "r")
327 try: 326 try:
328 line = f.readline() 327 line = f.readline()
329 dtdLine = f.readline() 328 dtdLine = f.readline()
330 finally: 329 finally:
331 f.close() 330 f.close()
332 except IOError, err: 331 except IOError as err:
333 QMessageBox.critical(self, 332 QMessageBox.critical(self,
334 self.trUtf8("Import Highlighting Styles"), 333 self.trUtf8("Import Highlighting Styles"),
335 self.trUtf8("""<p>The highlighting styles could not be read""" 334 self.trUtf8("""<p>The highlighting styles could not be read"""
336 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ 335 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\
337 .format(fn, unicode(err)) 336 .format(fn, str(err))
338 ) 337 )
339 return 338 return
340 339
341 validating = dtdLine.startswith("<!DOCTYPE") 340 validating = dtdLine.startswith("<!DOCTYPE")
342 parser = make_parser(validating) 341 parser = make_parser(validating)
347 parser.setContentHandler(handler) 346 parser.setContentHandler(handler)
348 parser.setEntityResolver(er) 347 parser.setEntityResolver(er)
349 parser.setErrorHandler(eh) 348 parser.setErrorHandler(eh)
350 349
351 try: 350 try:
352 f = open(fn, "rb") 351 f = open(fn, "r")
353 try: 352 try:
354 try: 353 try:
355 parser.parse(f) 354 parser.parse(f)
356 except UnicodeEncodeError: 355 except UnicodeEncodeError:
357 f.seek(0) 356 f.seek(0)
358 buf = cStringIO.StringIO(f.read()) 357 buf = cStringIO.StringIO(f.read())
359 parser.parse(buf) 358 parser.parse(buf)
360 finally: 359 finally:
361 f.close() 360 f.close()
362 except IOError, err: 361 except IOError as err:
363 QMessageBox.critical(self, 362 QMessageBox.critical(self,
364 self.trUtf8("Import Highlighting Styles"), 363 self.trUtf8("Import Highlighting Styles"),
365 self.trUtf8("""<p>The highlighting styles could not be read""" 364 self.trUtf8("""<p>The highlighting styles could not be read"""
366 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ 365 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\
367 .format(fn, unicode(err)) 366 .format(fn, str(err))
368 ) 367 )
369 return 368 return
370 except XMLFatalParseError: 369 except XMLFatalParseError:
371 QMessageBox.critical(self, 370 QMessageBox.critical(self,
372 self.trUtf8("Import Highlighting Styles"), 371 self.trUtf8("Import Highlighting Styles"),

eric ide

mercurial