changeset 52:406899afca32

TileGrid: only repaint the visible region I found part of the Qt API that tells you what area of your widget is touched by the paint event, so I can redraw just that part. Another optimisation so that we're still worthy of the Swift surname.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 08 Sep 2019 18:47:21 -0400
parents f2b46bee6c50
children 4a4c74d920bc
files tilerswift
diffstat 1 files changed, 12 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/tilerswift
+++ b/tilerswift
@@ -120,8 +120,13 @@
         style = self.style()
         style.drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)
 
-        for i in range(self.numrows):
-            for j in range(self.numcols):
+        # Let's be a little economical and only repaint the visible region
+        x_start, y_start, x_end, y_end = event.rect().getCoords()
+        i_start, j_start = self.coordsToTileIndices(x_start, y_start)
+        i_end, j_end = self.coordsToTileIndices(x_end, y_end)
+
+        for i in range(i_start, i_end+1):
+            for j in range(j_start, j_end+1):
                 tile = self.tiles[i, j]
                 if tile:
                     rect = QtCore.QRect(
@@ -136,10 +141,10 @@
                         tile.pixmap.transformed(QtGui.QTransform().scale(flipx, flipy))
                     )
 
-    def mouseClickToIJ(self, event):
+    def coordsToTileIndices(self, x, y):
         j, i = (
-            (event.x() - self.spacing)//self.tilesize,
-            (event.y() - self.spacing)//self.tilesize,
+            (x - self.spacing)//self.tilesize,
+            (y - self.spacing)//self.tilesize,
         )
         return (i, j)
 
@@ -193,7 +198,7 @@
         self.resize()
 
     def mousePressEvent(self, event):
-        i, j = self.mouseClickToIJ(event)
+        i, j = self.coordsToTileIndices(event.x(), event.y())
         tile = self.tiles[i, j]
 
         self.picked_tile = tile
@@ -220,7 +225,7 @@
         self.resize()
 
     def mousePressEvent(self, event):
-        i, j = self.mouseClickToIJ(event)
+        i, j = self.coordsToTileIndices(event.x(), event.y())
 
         if event.button() == QtCore.Qt.LeftButton:
             self.tiles[i, j] = self.rom_canvas.picked_tile