changeset 74:66773b576282

ColourPicker: enable transparency So, turns out that Qt's indexed images are totally okay with alpha channels. This might come in handy some day when I enable overlying tiles without regard for grid boundaries.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 10 Sep 2019 12:58:47 -0400
parents 88305fa62d7e
children f311c18ef557
files colors.py tilerswift
diffstat 2 files changed, 28 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/colors.py
+++ b/colors.py
@@ -71,3 +71,11 @@
 ]
 
 QT_NES_PALETTE = [QG.QColor(color).rgb() for color in NES_PALETTE]
+def palette_to_qt(palette):
+    return [
+        QG.QColor(0, 0, 0, 0).rgba() if color_idx is None
+        else QG.QColor(NES_PALETTE[color_idx]).rgb()
+        for color_idx in palette
+    ]
+
+
--- a/tilerswift
+++ b/tilerswift
@@ -3,7 +3,7 @@
 
 from PyQt5 import QtCore as QC, QtGui as QG, QtWidgets as QW
 
-from colors import NES_PALETTE
+from colors import NES_PALETTE, palette_to_qt
 
 
 class NES_ROM(object):
@@ -55,9 +55,6 @@
             for row in self.tile
         ) + "\n" + "-"*10
 
-    def palette_to_qt(self):
-        return [QG.QColor(NES_PALETTE[color_idx]).rgb() for color_idx in self.palette]
-
     def set_palette(self, new_palette):
         self.palette = new_palette
         self.update_pixmap()
@@ -65,7 +62,7 @@
     def update_pixmap(self):
         img_data = bytes(sum(self.tile, []))
         image = QG.QImage(img_data, 8, 8, QG.QImage.Format_Indexed8)
-        image.setColorTable(self.palette_to_qt())
+        image.setColorTable(palette_to_qt(self.palette))
         self.pixmap = QG.QPixmap(image)
 
     def get_tile(self):
@@ -339,7 +336,17 @@
                 button = ColourButton(colour_idx)
                 button.pressed.connect(lambda c=colour_idx: self.colour_picked(c))
                 layout.addWidget(button, i, j)
-        self.setLayout(layout)
+        colours = QW.QWidget()
+        colours.setLayout(layout)
+
+        vlayout = QW.QVBoxLayout()
+        vlayout.addWidget(colours)
+
+        transparent_button = QW.QPushButton("Transparent")
+
+        transparent_button.pressed.connect(lambda: self.colour_picked(None))
+        vlayout.addWidget(transparent_button)
+        self.setLayout(vlayout)
 
     def colour_picked(self, colour_idx):
         self.picked_idx = colour_idx
@@ -356,6 +363,13 @@
 
     def set_colour(self, colour_idx):
         self.colour_idx = colour_idx
+
+        if colour_idx is None:
+            # Enable transparency
+            self.setText("")
+            self.setStyleSheet("")
+            return
+
         self.setText(f"{colour_idx:0{2}X}")
         bgcolour = NES_PALETTE[colour_idx]
         qt_colour = QG.QColor(bgcolour)