changeset 105:37e3f2781fdf

PaletteComboBox: new widget in ROM canvas toolbar This allows picking from a pre-defined set of palettes. The styling isn't great yet, and I may need to fix this later.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 15 Sep 2019 22:05:36 -0400
parents dd2a309eefa9
children d5e69fe5c81a
files widgets.py
diffstat 1 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/widgets.py
+++ b/widgets.py
@@ -341,6 +341,53 @@
         self.setLayout(self.layout)
 
 
+class PaletteDropdownItemDelegate(QW.QStyledItemDelegate):
+    skip = 10
+
+    def __init__(self):
+        super().__init__()
+        self.fontHeight = QW.QPushButton().fontMetrics().height()
+
+    def paint(self, painter, option, index):
+        name = index.data()
+        pixmap = TilePaletteButtons(TILE_PALETTES[name]).grab()
+        style = option.widget.style()
+        style.drawControl(QW.QStyle.CE_ItemViewItem, option, painter)
+        textRect = style.subElementRect(QW.QStyle.SE_ItemViewItemText, option)
+        drawRect = QC.QRect(
+            textRect.x(), self.fontHeight + textRect.y() + self.skip/2,
+            textRect.width(), textRect.height() - self.skip - self.fontHeight
+        )
+        painter.drawText(textRect, 0, name)
+        painter.drawPixmap(drawRect, pixmap)
+
+    def sizeHint(self, option, index):
+        return QC.QSize(4*BUTTON_SIZE,
+                        BUTTON_SIZE + self.skip + self.fontHeight)
+
+
+class PaletteComboBox(QW.QComboBox):
+
+    def __init__(self):
+        super().__init__()
+        self.setItemDelegate(PaletteDropdownItemDelegate())
+
+        for name, tile_palette in TILE_PALETTES.items():
+            self.addItem(name)
+
+    def showPopup(self):
+        super().showPopup()
+        popup = self.findChild(QW.QFrame)
+        popup.move(popup.x() + self.width(), popup.y())
+        popup.setFixedWidth(4*BUTTON_SIZE)
+
+    def sizeHint(self):
+        return QC.QSize(23, BUTTON_SIZE)
+
+    def minimumSizeHint(self):
+        return self.sizeHint()
+
+
 class ROMDockable(QW.QDockWidget):
 
     def __init__(self, filename, filetype, tile_picker):
@@ -360,6 +407,10 @@
         palette_layout.setSpacing(0)
         palette_layout.setSizeConstraint(QW.QLayout.SetFixedSize)
 
+        combo = PaletteComboBox()
+        combo.currentIndexChanged[str].connect(self.change_palette)
+        palette_layout.addWidget(combo)
+
         self.rom_palette_buttons = TilePaletteButtons(
             Tile.default_palette,
             callback=self.pick_palette_colour