Added capability to configure the eric application palette via a QSS file (handled internally by eric). eric7

Sat, 18 Dec 2021 18:33:01 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 18 Dec 2021 18:33:01 +0100
branch
eric7
changeset 8837
6b4b2acc5324
parent 8836
860158ee8ad6
child 8838
60ce5c218f0c

Added capability to configure the eric application palette via a QSS file (handled internally by eric).

eric7/EricWidgets/EricApplication.py file | annotate | diff | comparison | revisions
eric7/Preferences/__init__.py file | annotate | diff | comparison | revisions
eric7/Toolbox/Startup.py file | annotate | diff | comparison | revisions
eric7/eric7.py file | annotate | diff | comparison | revisions
--- a/eric7/EricWidgets/EricApplication.py	Fri Dec 17 15:39:59 2021 +0100
+++ b/eric7/EricWidgets/EricApplication.py	Sat Dec 18 18:33:01 2021 +0100
@@ -8,17 +8,37 @@
 """
 
 from PyQt6.QtCore import Qt, QCoreApplication
-from PyQt6.QtGui import QPalette
+from PyQt6.QtGui import QColor, QPalette
 from PyQt6.QtWidgets import QApplication
 
 QCoreApplication.setAttribute(
     Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True)
 
+from . import EricMessageBox
+
 
 class EricApplication(QApplication):
     """
     Eric application class with an object registry.
     """
+    PaletteRoleMapping = {
+        "alternate-base": QPalette.ColorRole.AlternateBase,
+        "base": QPalette.ColorRole.Base,
+        "text": QPalette.ColorRole.Text,
+        "bright-text": QPalette.ColorRole.BrightText,
+        "placeholder-text": QPalette.ColorRole.PlaceholderText,
+        "window": QPalette.ColorRole.Window,
+        " window-text": QPalette.ColorRole.WindowText,
+        "tooltip-base": QPalette.ColorRole.ToolTipBase,
+        "tooltip-text": QPalette.ColorRole.ToolTipText,
+        "button": QPalette.ColorRole.Button,
+        "button-text": QPalette.ColorRole.ButtonText,
+        "highlight": QPalette.ColorRole.Highlight,
+        "highlighted-text": QPalette.ColorRole.HighlightedText,
+        "link": QPalette.ColorRole.Link,
+        "link-visited": QPalette.ColorRole.LinkVisited,
+    }
+    
     def __init__(self, argv):
         """
         Constructor
@@ -158,6 +178,57 @@
         
         return self.__pluginObjectRegistry[name][1]
     
+    def setStyleSheetFile(self, filename):
+        """
+        Public method to read a QSS style sheet file and set the application
+        style sheet based on its contents.
+        
+        @param filename name of the QSS style sheet file
+        @type str
+        """
+        if filename:
+            try:
+                with open(filename, "r", encoding="utf-8") as f:
+                    styleSheet = f.read()
+            except OSError as msg:
+                EricMessageBox.warning(
+                    self,
+                    QCoreApplication.translate(
+                        "EricApplication", "Loading Style Sheet"),
+                    QCoreApplication.translate(
+                        "EricApplication",
+                        """<p>The Qt Style Sheet file <b>{0}</b> could"""
+                        """ not be read.<br>Reason: {1}</p>""")
+                    .format(filename, str(msg)))
+                return
+        else:
+            filename = ""
+        
+        if "QPalette {" in styleSheet:
+            self.__setPaletteFromStyleSheet(styleSheet)
+        
+        ericApp().setStyleSheet(styleSheet)
+    
+    def __setPaletteFromStyleSheet(self, styleSheet):
+        """
+        Private method to set the palette from a style sheet.
+        
+        @param styleSheet style sheet
+        @type str
+        """
+        palette = self.palette()
+        
+        paletteStr = styleSheet.split("QPalette {")[1].split("}")[0]
+        paletteLines = paletteStr.strip().splitlines()
+        for line in paletteLines:
+            role, value = line.strip().split()
+            role = role.strip("\t :").lower()
+            value = value.strip("\t ;")
+            if role in self.PaletteRoleMapping and value.startswith("#"):
+                palette.setColor(self.PaletteRoleMapping[role], QColor(value))
+        
+        self.setPalette(palette)
+    
     def usesDarkPalette(self):
         """
         Public method to check, if the application uses a palette with a dark
--- a/eric7/Preferences/__init__.py	Fri Dec 17 15:39:59 2021 +0100
+++ b/eric7/Preferences/__init__.py	Sat Dec 18 18:33:01 2021 +0100
@@ -1822,7 +1822,7 @@
     """
     if key in ["BgColorNew", "BgColorChanged"]:
         Prefs.settings.setValue(
-        "Debugger/" + key, value.name(QColor.NameFormat.HexArgb))
+            "Debugger/" + key, value.name(QColor.NameFormat.HexArgb))
     else:
         Prefs.settings.setValue("Debugger/" + key, value)
 
--- a/eric7/Toolbox/Startup.py	Fri Dec 17 15:39:59 2021 +0100
+++ b/eric7/Toolbox/Startup.py	Sat Dec 18 18:33:01 2021 +0100
@@ -264,6 +264,9 @@
     # the following code depends upon a valid application object
     import Preferences
     
+    # set the application style sheet
+    app.setStyleSheetFile(Preferences.getUI("StyleSheet"))
+    
     initializeResourceSearchPath(app)
     QApplication.setWindowIcon(UI.PixmapCache.getIcon("eric"))
     
--- a/eric7/eric7.py	Fri Dec 17 15:39:59 2021 +0100
+++ b/eric7/eric7.py	Sat Dec 18 18:33:01 2021 +0100
@@ -311,6 +311,9 @@
     if Preferences.getUI("SingleApplicationMode"):
         handleSingleApplication(ddindex)
     
+    # set the application style sheet
+    app.setStyleSheetFile(Preferences.getUI("StyleSheet"))
+    
     # set the search path for icons
     Startup.initializeResourceSearchPath(app)
     

eric ide

mercurial