changeset 72:74ebf2f4e801

refactor: import Qt models as QC, QG, and QW. I got tired of typing them out and I don't want to do `from QtThing import *`.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 10 Sep 2019 12:35:52 -0400
parents 281bf29af098
children 88305fa62d7e
files colors.py tilerswift
diffstat 2 files changed, 36 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/colors.py
+++ b/colors.py
@@ -1,4 +1,4 @@
-from PyQt5 import QtGui
+from PyQt5 import QtGui as QG, QtCore as QC
 
 NES_PALETTE = [
     "#7C7C7C",  # 0
@@ -70,4 +70,4 @@
     "#000000",  # 63
 ]
 
-QT_NES_PALETTE = [QtGui.QColor(color).rgb() for color in NES_PALETTE]
+QT_NES_PALETTE = [QG.QColor(color).rgb() for color in NES_PALETTE]
--- a/tilerswift
+++ b/tilerswift
@@ -1,7 +1,7 @@
 #!/usr/bin/python3
 import sys
 
-from PyQt5 import QtCore, QtGui, QtWidgets
+from PyQt5 import QtCore as QC, QtGui as QG, QtWidgets as QW
 
 from colors import NES_PALETTE
 
@@ -56,7 +56,7 @@
         ) + "\n" + "-"*10
 
     def palette_to_qt(self):
-        return [QtGui.QColor(NES_PALETTE[color_idx]).rgb() for color_idx in self.palette]
+        return [QG.QColor(NES_PALETTE[color_idx]).rgb() for color_idx in self.palette]
 
     def set_palette(self, new_palette):
         self.palette = new_palette
@@ -64,9 +64,9 @@
 
     def update_pixmap(self):
         img_data = bytes(sum(self.tile, []))
-        image = QtGui.QImage(img_data, 8, 8, QtGui.QImage.Format_Indexed8)
+        image = QG.QImage(img_data, 8, 8, QG.QImage.Format_Indexed8)
         image.setColorTable(self.palette_to_qt())
-        self.pixmap = QtGui.QPixmap(image)
+        self.pixmap = QG.QPixmap(image)
 
     def get_tile(self):
         if getattr(self, "_tile", None) is None:
@@ -144,7 +144,7 @@
         self.numcols = numcols
 
 
-class TileGrid(QtWidgets.QWidget):
+class TileGrid(QW.QWidget):
 
     def __init__(self, numrows, numcols, scalefactor, spacing):
         super().__init__()
@@ -157,15 +157,15 @@
         self.setStyleSheet(self.CSS)
 
     def paintEvent(self, event):
-        painter = QtGui.QPainter(self)
+        painter = QG.QPainter(self)
 
         # I don't really know what this block of code means, but it's
         # what I have to do in order to get this widget to obey CSS
         # styles.
-        opt = QtWidgets.QStyleOption()
+        opt = QW.QStyleOption()
         opt.initFrom(self)
         style = self.style()
-        style.drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)
+        style.drawPrimitive(QW.QStyle.PE_Widget, opt, painter, self)
 
         # Let's be a little economical and only repaint the visible region
         x_start, y_start, x_end, y_end = event.rect().getCoords()
@@ -176,7 +176,7 @@
             for j in range(j_start, j_end+1):
                 tile = self.tiles[i, j]
                 if tile:
-                    rect = QtCore.QRect(
+                    rect = QC.QRect(
                         self.spacing + j*self.tilesize,
                         self.spacing + i*self.tilesize,
                         self.tilesize - self.spacing,
@@ -185,7 +185,7 @@
                     flipx, flipy = self.flips[i, j]
                     painter.drawPixmap(
                         rect,
-                        tile.pixmap.transformed(QtGui.QTransform().scale(flipx, flipy))
+                        tile.pixmap.transformed(QG.QTransform().scale(flipx, flipy))
                     )
 
     def coordsToTileIndices(self, x, y):
@@ -197,7 +197,7 @@
 
     def resize(self):
         self.tilesize = self.spacing + self.scalefactor*8
-        self.setFixedSize(QtCore.QSize(
+        self.setFixedSize(QC.QSize(
             self.tilesize*self.numcols + self.spacing,
             self.tilesize*self.numrows + self.spacing,
         ))
@@ -296,12 +296,12 @@
     def mousePressEvent(self, event):
         i, j = self.coordsToTileIndices(event.x(), event.y())
 
-        if event.button() == QtCore.Qt.LeftButton:
+        if event.button() == QC.Qt.LeftButton:
             self.tiles[i, j] = self.rom_canvas.picked_tile
             self.flips[i, j] = (1, 1)
-        elif event.button() == QtCore.Qt.RightButton:
+        elif event.button() == QC.Qt.RightButton:
             self.tiles[i, j] = None
-        elif event.button() == QtCore.Qt.MidButton:
+        elif event.button() == QC.Qt.MidButton:
             flipx, flipy = self.flips[i, j]
             if flipx == 1:
                 if flipy == 1:
@@ -317,13 +317,13 @@
         self.update()
 
 
-class ColourPicker(QtWidgets.QDialog):
+class ColourPicker(QW.QDialog):
 
     def __init__(self):
         super().__init__()
         self.setWindowTitle("Pick a colour")
 
-        layout = QtWidgets.QGridLayout()
+        layout = QW.QGridLayout()
         layout.setSpacing(0)
 
         for i in range(4):
@@ -339,24 +339,24 @@
         self.accept()
 
 
-class ColourButton(QtWidgets.QPushButton):
+class ColourButton(QW.QPushButton):
 
     def __init__(self, colour_idx):
         super().__init__()
-        self.setFixedSize(QtCore.QSize(24, 24))
+        self.setFixedSize(QC.QSize(24, 24))
         self.colour_idx = colour_idx
         self.set_colour(colour_idx)
 
     def set_colour(self, colour_idx):
         self.colour_idx = colour_idx
         self.setText(f"{colour_idx:0{2}X}")
-
         bgcolour = NES_PALETTE[colour_idx]
-        qt_colour = QtGui.QColor(bgcolour)
+        qt_colour = QG.QColor(bgcolour)
         r, g, b = qt_colour.red(), qt_colour.green(), qt_colour.blue()
         luminance = (0.2126*r + 0.7152*g + 0.0722*b)/256
         textcolour = 'white' if luminance < 0.5 else 'black'
         bordercolour = "#444444" if r == g == b == 0 else qt_colour.darker().name()
+
         self.setStyleSheet(
             f'''
             QPushButton {{
@@ -378,11 +378,11 @@
         self.button_idx = button_idx
 
 
-class MainWindow(QtWidgets.QMainWindow):
+class MainWindow(QW.QMainWindow):
     def __init__(self):
         super().__init__()
 
-        filename = QtWidgets.QFileDialog.getOpenFileName(
+        filename = QW.QFileDialog.getOpenFileName(
             self,
             "Open ROM", "", "NES Files (*.nes)"
         )
@@ -392,43 +392,43 @@
 
         self.tile_picker = TilePicker(self.grid_widget)
 
-        scroll1 = QtWidgets.QScrollArea()
+        scroll1 = QW.QScrollArea()
         scroll1.setWidget(self.grid_widget)
 
-        scroll2 = QtWidgets.QScrollArea()
+        scroll2 = QW.QScrollArea()
         scroll2.setWidget(self.tile_picker)
 
-        horz = QtWidgets.QHBoxLayout()
+        horz = QW.QHBoxLayout()
         horz.addWidget(scroll1)
         horz.addWidget(scroll2)
 
-        scrolls = QtWidgets.QWidget()
+        scrolls = QW.QWidget()
         scrolls.setLayout(horz)
 
-        vert = QtWidgets.QVBoxLayout()
+        vert = QW.QVBoxLayout()
         vert.addWidget(scrolls)
 
         self.colour_picker = ColourPicker()
 
-        self.palette = QtWidgets.QHBoxLayout()
+        self.palette = QW.QHBoxLayout()
         for button_idx, color_idx in enumerate(Tile.default_palette):
             button = PaletteButton(button_idx, color_idx)
             button.pressed.connect(lambda idx=button_idx: self.pick_palette_colour(idx))
             self.palette.addWidget(button)
 
-        zoomInButton = QtWidgets.QPushButton("+")
-        zoomInButton.setFixedSize(QtCore.QSize(24, 24))
+        zoomInButton = QW.QPushButton("+")
+        zoomInButton.setFixedSize(QC.QSize(24, 24))
         zoomInButton.pressed.connect(self.grid_widget.zoomIn)
         self.palette.addWidget(zoomInButton)
 
-        zoomOutButton = QtWidgets.QPushButton("-")
-        zoomOutButton.setFixedSize(QtCore.QSize(24, 24))
+        zoomOutButton = QW.QPushButton("-")
+        zoomOutButton.setFixedSize(QC.QSize(24, 24))
         zoomOutButton.pressed.connect(self.grid_widget.zoomOut)
         self.palette.addWidget(zoomOutButton)
 
         vert.addLayout(self.palette)
 
-        main_widget = QtWidgets.QWidget()
+        main_widget = QW.QWidget()
         main_widget.setLayout(vert)
 
         self.setCentralWidget(main_widget)
@@ -444,7 +444,7 @@
             self.grid_widget.update()
 
 
-app = QtWidgets.QApplication(sys.argv)
+app = QW.QApplication(sys.argv)
 window = MainWindow()
 window.show()
 app.exec_()