Fri, 29 Jan 2021 14:19:41 +0100
Changed the highlighting styles import logic thus, that the imported styles are applied in the configuration page.
--- a/eric6/E5XML/HighlightingStylesReader.py Fri Jan 29 14:18:50 2021 +0100 +++ b/eric6/E5XML/HighlightingStylesReader.py Fri Jan 29 14:19:41 2021 +0100 @@ -8,8 +8,6 @@ Module implementing a class for reading a highlighting styles XML file. """ -from PyQt5.QtGui import QColor, QFont - from .Config import highlightingStylesFileFormatVersion from .XMLStreamReaderBase import XMLStreamReaderBase @@ -38,6 +36,8 @@ """ Public method to read and parse the XML document. """ + self.__lexersList = [] + while not self.atEnd(): self.readNext() if self.isStartElement(): @@ -53,12 +53,18 @@ self.raiseUnexpectedStartTag(self.name()) self.showErrorMessage() + + return self.__lexersList def __readLexer(self): """ Private method to read the lexer info. """ language = self.attribute("name") + self.__lexersList.append({ + "name": language, + "styles": [], + }) if language and language in self.lexers: lexer = self.lexers[language] else: @@ -88,31 +94,34 @@ substyle = int(self.attribute("substyle", "-1")) # -1 is default for base styles - # add sub-style if not already there - if not lexer.hasStyle(style, substyle): - substyle = lexer.addSubstyle(style) + styleDict = { + "style": style, + "substyle": substyle, + } color = self.attribute("color") if color: - color = QColor(color) + styleDict["color"] = color else: - color = lexer.defaultColor(style, substyle) - lexer.setColor(color, style, substyle) + styleDict["color"] = ( + lexer.defaultColor(style, substyle).name() + ) paper = self.attribute("paper") if paper: - paper = QColor(paper) + styleDict["paper"] = paper else: - paper = lexer.defaultPaper(style, substyle) - lexer.setPaper(paper, style, substyle) + styleDict["paper"] = ( + lexer.defaultPaper(style, substyle).name() + ) fontStr = self.attribute("font") if fontStr: - font = QFont() - font.fromString(fontStr) + styleDict["font"] = fontStr else: - font = lexer.defaultFont(style, substyle) - lexer.setFont(font, style, substyle) + styleDict["font"] = ( + lexer.defaultFont(style, substyle).toString() + ) eolfill = self.attribute("eolfill") if eolfill: @@ -121,26 +130,29 @@ eolfill = lexer.defaulEolFill(style, substyle) else: eolfill = lexer.defaulEolFill(style, substyle) - lexer.setEolFill(eolfill, style, substyle) + styleDict["eolfill"] = eolfill while not self.atEnd(): self.readNext() if self.isStartElement(): - if self.name() == "Description" and substyle >= 0: - # description can only be set for sub-styles + if self.name() == "Description": description = self.readElementText().strip() if not description: description = lexer.defaultDescription( style, substyle) - lexer.setDescription(description, style, substyle) - elif self.name() == "Words" and substyle >= 0: - # words can only be set for sub-styles + styleDict["description"] = description + elif self.name() == "Words": words = self.readElementText().strip() if not words: words = lexer.defaultWords(style, substyle) - lexer.setWords(words, style, substyle) + styleDict["words"] = words if self.isEndElement() and self.name() == "Style": + if "description" not in styleDict: + styleDict["description"] = "" + if "words" not in styleDict: + styleDict["words"] = "" + self.__lexersList[-1]["styles"].append(styleDict) return while not self.atEnd():
--- a/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Fri Jan 29 14:18:50 2021 +0100 +++ b/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Fri Jan 29 14:19:41 2021 +0100 @@ -10,7 +10,7 @@ import os from PyQt5.QtCore import pyqtSlot, Qt, QFileInfo, QFile, QIODevice -from PyQt5.QtGui import QPalette, QFont +from PyQt5.QtGui import QPalette, QFont, QColor from PyQt5.QtWidgets import ( QColorDialog, QFontDialog, QInputDialog, QMenu, QTreeWidgetItem, QDialog ) @@ -607,8 +607,8 @@ HighlightingStylesFile ) highlightingStylesFile = HighlightingStylesFile() - res = highlightingStylesFile.readFile(fn, lexers) - if not res: + styles = highlightingStylesFile.readFile(fn, lexers) + if not styles: return else: # old XML based file @@ -618,8 +618,10 @@ HighlightingStylesReader ) reader = HighlightingStylesReader(f, lexers) - reader.readXML() + styles = reader.readXML() f.close() + if not styles: + return else: E5MessageBox.critical( self, @@ -631,9 +633,37 @@ ) return + self.__applyStyles(styles, lexers) self.on_lexerLanguageComboBox_activated( self.lexerLanguageComboBox.currentText()) + def __applyStyles(self, stylesList, lexersList): + """ + Private method to apply the imported styles to this dialog. + + @param stylesList list of imported lexer styles + @type list of dict + @param lexersList list of lexers to apply the styles to + @type list of PreferencesLexer + """ + for lexerDict in stylesList: + if lexerDict["name"] in lexersList: + lexer = lexersList[lexerDict["name"]] + for styleDict in lexerDict["styles"]: + style = styleDict["style"] + substyle = styleDict["substyle"] + lexer.setColor(QColor(styleDict["color"]), style, substyle) + lexer.setPaper(QColor(styleDict["paper"]), style, substyle) + font = QFont() + font.fromString(styleDict["font"]) + lexer.setFont(font, style, substyle) + lexer.setEolFill(styleDict["eolfill"], style, substyle) + if substyle >= 0: + # description and words can only be set for sub-styles + lexer.setDescription(styleDict["description"], + style, substyle) + lexer.setWords(styleDict["words"], style, substyle) + ####################################################################### ## Methods to save and restore the state #######################################################################
--- a/eric6/Preferences/HighlightingStylesFile.py Fri Jan 29 14:18:50 2021 +0100 +++ b/eric6/Preferences/HighlightingStylesFile.py Fri Jan 29 14:19:41 2021 +0100 @@ -11,7 +11,6 @@ import time from PyQt5.QtCore import QObject -from PyQt5.QtGui import QColor, QFont from E5Gui import E5MessageBox from E5Gui.E5OverrideCursor import E5OverridenCursor @@ -90,7 +89,7 @@ return True - def readFile(self, filename: str, lexers: dict) -> bool: + def readFile(self, filename: str, lexers: dict) -> list: """ Public method to read the highlighting styles data from a highlighting styles JSON file. @@ -100,8 +99,8 @@ @param lexers dictionary of lexer objects for which to import the styles @type dict of {str: PreferencesLexer} - @return flag indicating a successful read - @rtype bool + @return list of read lexer style definitions + @rtype list of dict """ try: with open(filename, "r") as f: @@ -116,24 +115,6 @@ " read.</p><p>Reason: {1}</p>" ).format(filename, str(err)) ) - return False + return [] - for lexerDict in stylesDict["lexers"]: - if lexerDict["name"] in lexers: - lexer = lexers[lexerDict["name"]] - for styleDict in lexerDict["styles"]: - style = styleDict["style"] - substyle = styleDict["substyle"] - lexer.setColor(QColor(styleDict["color"]), style, substyle) - lexer.setPaper(QColor(styleDict["paper"]), style, substyle) - font = QFont() - font.fromString(styleDict["font"]) - lexer.setFont(font, style, substyle) - lexer.setEolFill(styleDict["eolfill"], style, substyle) - if substyle >= 0: - # description and words can only be set for sub-styles - lexer.setDescription(styleDict["description"], - style, substyle) - lexer.setWords(styleDict["words"], style, substyle) - - return True + return stylesDict["lexers"]