eric7/HexEdit/HexEditWidget.py

branch
eric7
changeset 8875
67c3ea933787
parent 8857
8191d15b8974
child 8881
54e42bc2437a
--- a/eric7/HexEdit/HexEditWidget.py	Tue Dec 28 19:10:38 2021 +0100
+++ b/eric7/HexEdit/HexEditWidget.py	Wed Dec 29 16:38:32 2021 +0100
@@ -12,11 +12,11 @@
 from PyQt6.QtCore import (
     pyqtSignal, pyqtSlot, Qt, QByteArray, QTimer, QRect, QBuffer, QIODevice
 )
-from PyQt6.QtGui import (
-    QBrush, QPen, QColor, QFont, QPalette, QKeySequence, QPainter
-)
+from PyQt6.QtGui import QFont, QPalette, QKeySequence, QPainter
 from PyQt6.QtWidgets import QAbstractScrollArea, QApplication
 
+from EricWidgets.EricApplication import ericApp
+
 from .HexEditChunks import HexEditChunks
 from .HexEditUndoStack import HexEditUndoStack
 
@@ -65,9 +65,6 @@
         # Properties
         self.__addressArea = True
         # switch the address area on/off
-        self.__addressAreaBrush = QBrush()
-        self.__addressAreaPen = QPen()
-        # background and pen of the address area
         self.__addressOffset = 0
         # offset into the shown address range
         self.__addressWidth = 4
@@ -78,14 +75,8 @@
         # contents of the hex editor
         self.__highlighting = True
         # switch the highlighting feature on/off
-        self.__highlightingBrush = QBrush()
-        self.__highlightingPen = QPen()
-        # background and pen of highlighted text
         self.__overwriteMode = True
         # set overwrite mode on/off
-        self.__selectionBrush = QBrush()
-        self.__selectionPen = QPen()
-        # background and pen of selected text
         self.__readOnly = False
         # set read only mode on/off
         self.__cursorPosition = 0
@@ -133,17 +124,6 @@
         else:
             self.setFont(QFont(["Monospace"], 10))
         
-        # TODO: make some colors custimizable
-        self.setAddressAreaColors(
-            self.palette().color(QPalette.ColorRole.WindowText),
-            self.palette().alternateBase().color())
-        self.setHighlightColors(
-            self.palette().color(QPalette.ColorRole.WindowText),
-            QColor(0xff, 0xff, 0x99, 0xff))
-        self.setSelectionColors(
-            self.palette().highlightedText().color(),
-            self.palette().highlight().color())
-        
         self.__cursorTimer = QTimer()
         self.__cursorTimer.timeout.connect(self.__updateCursor)
         
@@ -214,28 +194,6 @@
         self.setCursorPosition(self.__cursorPosition)
         self.viewport().update()
     
-    def addressAreaColors(self):
-        """
-        Public method to get the address area colors.
-        
-        @return address area foreground and background colors
-        @rtype tuple of 2 QColor
-        """
-        return self.__addressAreaPen.color(), self.__addressAreaBrush.color()
-    
-    def setAddressAreaColors(self, foreground, background):
-        """
-        Public method to set the address area colors.
-        
-        @param foreground address area foreground color
-        @type QColor
-        @param background address area background color
-        @type QColor
-        """
-        self.__addressAreaPen = QPen(foreground)
-        self.__addressAreaBrush = QBrush(background)
-        self.viewport().update()
-    
     def addressOffset(self):
         """
         Public method to get the address offset.
@@ -506,28 +464,6 @@
         self.__highlighting = on
         self.viewport().update()
     
-    def highlightColors(self):
-        """
-        Public method to get the highlight colors.
-        
-        @return highlight foreground and background colors
-        @rtype tuple of 2 QColor
-        """
-        return self.__highlightingPen.color(), self.__highlightingBrush.color()
-    
-    def setHighlightColors(self, foreground, background):
-        """
-        Public method to set the highlight colors.
-        
-        @param foreground highlight foreground color
-        @type QColor
-        @param background highlight background color
-        @type QColor
-        """
-        self.__highlightingPen = QPen(foreground)
-        self.__highlightingBrush = QBrush(background)
-        self.viewport().update()
-    
     def overwriteMode(self):
         """
         Public method to get the overwrite mode.
@@ -556,28 +492,6 @@
         self.__blink = True
         self.viewport().update(self.__cursorRect)
     
-    def selectionColors(self):
-        """
-        Public method to get the selection colors.
-        
-        @return selection foreground and background colors
-        @rtype tuple of 2 QColor
-        """
-        return self.__selectionPen.color(), self.__selectionBrush.color()
-    
-    def setSelectionColors(self, foreground, background):
-        """
-        Public method to set the selection colors.
-        
-        @param foreground selection foreground color
-        @type QColor
-        @param background selection background color
-        @type QColor
-        """
-        self.__selectionPen = QPen(foreground)
-        self.__selectionBrush = QBrush(background)
-        self.viewport().update()
-    
     def isReadOnly(self):
         """
         Public method to test the read only state.
@@ -1358,6 +1272,34 @@
         """
         painter = QPainter(self.viewport())
         
+        # initialize colors
+        if ericApp().usesDarkPalette():
+            addressAreaForeground = self.palette().color(
+                QPalette.ColorRole.Text)
+            addressAreaBackground = self.palette().color(
+                QPalette.ColorRole.Base).lighter(200)
+            highlightingForeground = self.palette().color(
+                QPalette.ColorRole.HighlightedText).darker(200)
+            highlightingBackground = self.palette().color(
+                QPalette.ColorRole.Highlight).lighter()
+        else:
+            addressAreaForeground = self.palette().color(
+                QPalette.ColorRole.Text)
+            addressAreaBackground = self.palette().color(
+                QPalette.ColorRole.Base).darker()
+            highlightingForeground = self.palette().color(
+                QPalette.ColorRole.HighlightedText).lighter()
+            highlightingBackground = self.palette().color(
+                QPalette.ColorRole.Highlight).darker()
+        selectionForeground = self.palette().color(
+            QPalette.ColorRole.HighlightedText)
+        selectionBackground = self.palette().color(
+            QPalette.ColorRole.Highlight)
+        standardBackground = self.viewport().palette().color(
+            QPalette.ColorRole.Base)
+        standardForeground = self.viewport().palette().color(
+            QPalette.ColorRole.Text)
+        
         if (
             evt.rect() != self.__cursorRect and
             evt.rect() != self.__cursorRectAscii
@@ -1366,27 +1308,22 @@
             pxPosStartY = self.__pxCharHeight
             
             # draw some patterns if needed
-            painter.fillRect(
-                evt.rect(),
-                self.viewport().palette().color(QPalette.ColorRole.Base))
+            painter.fillRect(evt.rect(), standardBackground)
             if self.__addressArea:
                 painter.fillRect(
                     QRect(-pxOfsX, evt.rect().top(),
                           self.__pxPosHexX - self.__pxGapAdrHex // 2 - pxOfsX,
                           self.height()),
-                    self.__addressAreaBrush)
+                    addressAreaBackground)
             if self.__asciiArea:
                 linePos = self.__pxPosAsciiX - (self.__pxGapHexAscii // 2)
                 painter.setPen(Qt.GlobalColor.gray)
                 painter.drawLine(linePos - pxOfsX, evt.rect().top(),
                                  linePos - pxOfsX, self.height())
             
-            painter.setPen(
-                self.viewport().palette().color(QPalette.ColorRole.WindowText))
-            
             # paint the address area
             if self.__addressArea:
-                painter.setPen(self.__addressAreaPen)
+                painter.setPen(addressAreaForeground)
                 address = ""
                 row = 0
                 pxPosY = self.__pxCharHeight
@@ -1402,9 +1339,6 @@
                     pxPosY += self.__pxCharHeight
             
             # paint hex and ascii area
-            colStandard = QPen(
-                self.viewport().palette().color(QPalette.ColorRole.WindowText))
-            
             painter.setBackgroundMode(Qt.BGMode.TransparentMode)
             
             row = 0
@@ -1419,42 +1353,41 @@
                     bPosLine + colIdx < len(self.__dataShown) and
                     colIdx < self.BYTES_PER_LINE
                 ):
-                    c = self.viewport().palette().color(
-                        QPalette.ColorRole.Base)
-                    painter.setPen(colStandard)
+                    background = standardBackground
+                    painter.setPen(standardForeground)
                     
                     posBa = self.__bPosFirst + bPosLine + colIdx
                     if (
                         self.getSelectionBegin() <= posBa and
                         self.getSelectionEnd() > posBa
                     ):
-                        c = self.__selectionBrush.color()
-                        painter.setPen(self.__selectionPen)
+                        background = selectionBackground
+                        painter.setPen(selectionForeground)
                     elif (
                         self.__highlighting and
                         self.__markedShown and
                         self.__markedShown[posBa - self.__bPosFirst]
                     ):
-                        c = self.__highlightingBrush.color()
-                        painter.setPen(self.__highlightingPen)
+                        background = highlightingBackground
+                        painter.setPen(highlightingForeground)
                     
                     # render hex value
-                    r = QRect()
+                    rect = QRect()
                     if colIdx == 0:
-                        r.setRect(
+                        rect.setRect(
                             pxPosX,
                             pxPosY - self.__pxCharHeight +
                             self.__pxSelectionSub,
                             2 * self.__pxCharWidth,
                             self.__pxCharHeight)
                     else:
-                        r.setRect(
+                        rect.setRect(
                             pxPosX - self.__pxCharWidth,
                             pxPosY - self.__pxCharHeight +
                             self.__pxSelectionSub,
                             3 * self.__pxCharWidth,
                             self.__pxCharHeight)
-                    painter.fillRect(r, c)
+                    painter.fillRect(rect, background)
                     hexStr = (
                         chr(self.__hexDataShown[(bPosLine + colIdx) * 2]) +
                         chr(self.__hexDataShown[(bPosLine + colIdx) * 2 + 1])
@@ -1469,13 +1402,13 @@
                             ch = "."
                         else:
                             ch = chr(by)
-                        r.setRect(
+                        rect.setRect(
                             pxPosAsciiX2,
                             pxPosY - self.__pxCharHeight +
                             self.__pxSelectionSub,
                             self.__pxCharWidth,
                             self.__pxCharHeight)
-                        painter.fillRect(r, c)
+                        painter.fillRect(rect, background)
                         painter.drawText(pxPosAsciiX2, pxPosY, ch)
                         pxPosAsciiX2 += self.__pxCharWidth
                     
@@ -1487,14 +1420,11 @@
                 pxPosY += self.__pxCharHeight
         
         painter.setBackgroundMode(Qt.BGMode.TransparentMode)
-        painter.setPen(
-            self.viewport().palette().color(QPalette.ColorRole.WindowText))
-            
+        painter.setPen(standardForeground)
+        
         # paint cursor
         if self.__blink and not self.__readOnly and self.isActiveWindow():
-            painter.fillRect(
-                self.__cursorRect,
-                self.palette().color(QPalette.ColorRole.WindowText))
+            painter.fillRect(self.__cursorRect, standardForeground)
         else:
             if self.__hexDataShown:
                 try:

eric ide

mercurial