eric7/EricWidgets/EricApplication.py

branch
eric7
changeset 8837
6b4b2acc5324
parent 8729
226da2e26a84
child 8840
c9fa102d0194
--- 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

eric ide

mercurial