PreferencesLexer, EditorHighlightingPage: got rid of the 'styles' and 'ind2style' lists (that was very old style programming). sub_styles

Mon, 11 Mar 2019 19:50:50 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 11 Mar 2019 19:50:50 +0100
branch
sub_styles
changeset 6854
f4dd76230eea
parent 6853
0922aa829e5e
child 6855
4d80c8cc99a1

PreferencesLexer, EditorHighlightingPage: got rid of the 'styles' and 'ind2style' lists (that was very old style programming).

Preferences/ConfigurationPages/EditorHighlightingStylesPage.py file | annotate | diff | comparison | revisions
Preferences/PreferencesLexer.py file | annotate | diff | comparison | revisions
--- a/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py	Mon Mar 11 19:15:34 2019 +0100
+++ b/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py	Mon Mar 11 19:50:50 2019 +0100
@@ -11,7 +11,8 @@
 
 from PyQt5.QtCore import pyqtSlot, Qt, QFileInfo, QFile, QIODevice
 from PyQt5.QtGui import QPalette, QFont
-from PyQt5.QtWidgets import QColorDialog, QFontDialog, QInputDialog, QMenu
+from PyQt5.QtWidgets import QColorDialog, QFontDialog, QInputDialog, QMenu, \
+    QListWidgetItem
 
 from .ConfigurationPageBase import ConfigurationPageBase
 from .Ui_EditorHighlightingStylesPage import Ui_EditorHighlightingStylesPage
@@ -37,6 +38,9 @@
     FAMILYANDSIZE = 2
     FONT = 99
     
+    StyleRole = Qt.UserRole + 1
+    SubstyleRole = Qt.UserRole + 2
+    
     def __init__(self, lexers):
         """
         Constructor
@@ -122,16 +126,32 @@
             return
         
         self.styleGroup.setEnabled(True)
-        self.styleElementList.addItems(self.lexer.styles)
+        for description, styleNo, subStyleNo in self.lexer.getStyles():
+            itm = QListWidgetItem(description, self.styleElementList)
+            itm.setData(self.StyleRole, styleNo)
+            itm.setData(self.SubstyleRole, subStyleNo)
+            # TODO: add substyle handling
         self.__styleAllItems()
         self.styleElementList.setCurrentRow(0)
+    
+    def __styleForRow(self, row):
+        """
+        Private method to get the style number of the item of a given row.
         
+        @param row row number
+        @type int
+        @return style number
+        @rtype int
+        """
+        itm = self.styleElementList.item(row)
+        return itm.data(self.StyleRole)
+    
     def __styleAllItems(self):
         """
         Private method to style all items of the style element list.
         """
         for row in range(self.styleElementList.count()):
-            style = self.lexer.ind2style[row]
+            style = self.__styleForRow(row)
             colour = self.lexer.color(style)
             paper = self.lexer.paper(style)
             font = self.lexer.font(style)
@@ -146,17 +166,17 @@
             else:
                 itm.setCheckState(Qt.Unchecked)
         
-    def on_styleElementList_currentRowChanged(self, index):
+    def on_styleElementList_currentRowChanged(self, row):
         """
-        Private method to set up the style element part of the source page.
+        Private method to handle a change of the current row.
         
-        @param index the style index.
+        @param row current row number
+        @type int
         """
-        try:
-            self.style = self.lexer.ind2style[index]
-        except KeyError:
+        if row < 0:
             return
         
+        self.style = self.__styleForRow(row)
         colour = self.lexer.color(self.style)
         paper = self.lexer.paper(self.style)
         eolfill = self.lexer.eolFill(self.style)
@@ -184,8 +204,8 @@
             self.sampleText.repaint()
             if len(self.styleElementList.selectedItems()) > 1:
                 for selItem in self.styleElementList.selectedItems():
-                    style = self.lexer.ind2style[
-                        self.styleElementList.row(selItem)]
+                    style = self.__styleForRow(
+                        self.styleElementList.row(selItem))
                     self.lexer.setColor(colour, style)
                     selItem.setForeground(colour)
             else:
@@ -206,8 +226,8 @@
             self.sampleText.repaint()
             if len(self.styleElementList.selectedItems()) > 1:
                 for selItem in self.styleElementList.selectedItems():
-                    style = self.lexer.ind2style[
-                        self.styleElementList.row(selItem)]
+                    style = self.__styleForRow(
+                        self.styleElementList.row(selItem))
                     self.lexer.setPaper(colour, style)
                     selItem.setBackground(colour)
             else:
@@ -226,7 +246,8 @@
             pl.setColor(QPalette.Base, colour)
             self.sampleText.setPalette(pl)
             self.sampleText.repaint()
-            for style in list(self.lexer.ind2style.values()):
+            for row in range(self.styleElementList.count()):
+                style = self.__styleForRow(row)
                 self.lexer.setPaper(colour, style)
             self.__styleAllItems()
         
@@ -287,13 +308,14 @@
         if ok:
             setSampleFont(font, familyOnly, sizeOnly)
             if doAll:
-                for style in list(self.lexer.ind2style.values()):
+                for row in range(self.styleElementList.count()):
+                    style = self.__styleForRow(row)
                     setFont(font, style, familyOnly, sizeOnly)
                 self.__styleAllItems()
             elif len(self.styleElementList.selectedItems()) > 1:
                 for selItem in self.styleElementList.selectedItems():
-                    style = self.lexer.ind2style[
-                        self.styleElementList.row(selItem)]
+                    style = self.__styleForRow(
+                        self.styleElementList.row(selItem))
                     setFont(font, style, familyOnly, sizeOnly)
                     itmFont = self.lexer.font(style)
                     selItem.setFont(itmFont)
@@ -338,8 +360,8 @@
         checkState = Qt.Checked if on else Qt.Unchecked
         if len(self.styleElementList.selectedItems()) > 1:
             for selItem in self.styleElementList.selectedItems():
-                style = self.lexer.ind2style[
-                    self.styleElementList.row(selItem)]
+                style = self.__styleForRow(
+                    self.styleElementList.row(selItem))
                 self.lexer.setEolFill(on, style)
                 selItem.setCheckState(checkState)
         else:
@@ -363,7 +385,8 @@
         if ok:
             enabled = selection == on
             self.eolfillCheckBox.setChecked(enabled)
-            for style in list(self.lexer.ind2style.values()):
+            for row in range(self.styleElementList.count()):
+                style = self.__styleForRow(row)
                 self.lexer.setEolFill(enabled, style)
             self.__styleAllItems()
         
@@ -374,8 +397,8 @@
         """
         if len(self.styleElementList.selectedItems()) > 1:
             for selItem in self.styleElementList.selectedItems():
-                style = self.lexer.ind2style[
-                    self.styleElementList.row(selItem)]
+                style = self.__styleForRow(
+                    self.styleElementList.row(selItem))
                 self.__setToDefault(style)
         else:
             self.__setToDefault(self.style)
@@ -388,7 +411,8 @@
         """
         Private method to set all styles to their default values.
         """
-        for style in list(self.lexer.ind2style.values()):
+        for row in range(self.styleElementList.count()):
+            style = self.__styleForRow(row)
             self.__setToDefault(style)
         self.on_styleElementList_currentRowChanged(
             self.styleElementList.currentRow())
--- a/Preferences/PreferencesLexer.py	Mon Mar 11 19:15:34 2019 +0100
+++ b/Preferences/PreferencesLexer.py	Mon Mar 11 19:50:50 2019 +0100
@@ -95,18 +95,6 @@
         if self.__lex is None:
             raise PreferencesLexerLanguageError(language)
         
-        # TODO: get rid of 'styles' and 'ind2style'
-        # define the local store
-        self.ind2style = {}
-        self.styles = []
-        index = 0
-        for i in range(QsciScintillaBase.STYLE_MAX):
-            desc = self.__lex.description(i)
-            if desc:
-                self.styles.append(desc)
-                self.ind2style[index] = i
-                index += 1
-        
         # read the last stored values from preferences file
         self.__lex.readSettings(Preferences.Prefs.settings, "Scintilla")
         # TODO: add substyles
@@ -118,6 +106,23 @@
         self.__lex.writeSettings(Preferences.Prefs.settings, "Scintilla")
         # TODO: add substyles
     
+    def getStyles(self):
+        """
+        Public method to get a list of all supported styles.
+        
+        @return list of tuples each containing the description of the style,
+            style number and sub-style number (or -1 for no sub-style)
+        """
+        styles = []
+        
+        for i in range(QsciScintillaBase.STYLE_MAX):
+            desc = self.__lex.description(i)
+            if desc:
+                styles.append((desc, i, -1))
+        # TODO: add substyles
+        
+        return styles
+    
     def defaultColor(self, style):
         """
         Public method to get the default color of a style.

eric ide

mercurial