# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1567982841 14400 # Node ID 406899afca32675413c53fad886dd84917ac0f62 # Parent f2b46bee6c502684734340bd6d01b5bccdda2310 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. diff --git a/tilerswift b/tilerswift --- 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