Fixed crash bug and fixed jigsaw piece snapping when changing piece number(piece size)

This commit is contained in:
serialexperiments0815 2025-12-06 16:43:15 +01:00
parent b7aba483f7
commit 67035f5bc0
2 changed files with 31 additions and 11 deletions

View file

@ -12,6 +12,7 @@ class JigsawPiece(QGraphicsPixmapItem):
number = 0 number = 0
allPieces = [] allPieces = []
def __init__(self, pixmap, startPos, resultRow, resultCol): def __init__(self, pixmap, startPos, resultRow, resultCol):
super().__init__() super().__init__()
@ -29,6 +30,9 @@ class JigsawPiece(QGraphicsPixmapItem):
JigsawPiece.number += 1 JigsawPiece.number += 1
self.number = JigsawPiece.number self.number = JigsawPiece.number
self.width = self.pixmap().width()
self.height = self.pixmap().height()
self.row = resultRow self.row = resultRow
self.col = resultCol self.col = resultCol
self.cluster = {self} self.cluster = {self}
@ -79,20 +83,26 @@ class JigsawPiece(QGraphicsPixmapItem):
def getDistanceToNeighborPieces(self): def getDistanceToNeighborPieces(self):
neighbors = self.getNeighbors() neighbors = self.getNeighbors()
neighborDistances = [[neighbor, neighbor.getPositionX() - self.getPositionX(), neighbor.getPositionY() - self.getPositionY(), direction] for neighbor, direction in neighbors] neighborDistances = [[neighbor, neighbor.getPositionX() - self.getPositionX(), neighbor.getPositionY() - self.getPositionY(), direction] for neighbor, direction in neighbors]
return neighborDistances return neighborDistances
def checkPieceCluster(self): def checkPieceCluster(self):
snapTolerance = 10
distanceCheck = self.getDistanceToNeighborPieces() distanceCheck = self.getDistanceToNeighborPieces()
for neighbor, dx, dy, direction in distanceCheck: for neighbor, dx, dy, direction in distanceCheck:
if direction == "right" and dx <= 150 and dx >= 110 and dy >= -5 and dy <= 5: if direction == "right":
self.setPieceCluster(neighbor) if abs(dx - self.width) <= snapTolerance and abs(dy) <= snapTolerance:
if(direction == "down" and dy <= 165 and dy >= 150 and dx >= -5 and dx <= 5): self.setPieceCluster(neighbor)
self.setPieceCluster(neighbor) elif direction == "left":
if(direction == "left" and dx >= -125 and dx <= -100 and dy >= -5 and dy <= 5): if abs(dx + self.width) <= snapTolerance and abs(dy) <= snapTolerance:
self.setPieceCluster(neighbor) self.setPieceCluster(neighbor)
if(direction == "up" and dy <= -150 and dy >= -160 and dx >= -5 and dx <= 5): elif direction == "down":
self.setPieceCluster(neighbor) if abs(dy - self.height) <= snapTolerance and abs(dx) <= snapTolerance:
self.setPieceCluster(neighbor)
elif direction == "up":
if abs(dy + self.height) <= snapTolerance and abs(dx) <= snapTolerance:
self.setPieceCluster(neighbor)
def setPieceCluster(self, neighbor): def setPieceCluster(self, neighbor):
@ -103,6 +113,13 @@ class JigsawPiece(QGraphicsPixmapItem):
class PuzzleWindow(QGraphicsScene): class PuzzleWindow(QGraphicsScene):
def __init__(self, filePath, rows, cols): def __init__(self, filePath, rows, cols):
super().__init__() super().__init__()
for piece in JigsawPiece.allPieces:
if piece.scene() is not None:
piece.scene().removeItem(piece)
JigsawPiece.allPieces.clear()
JigsawPiece.number = 0
self.filePath = filePath self.filePath = filePath
self.rows = rows self.rows = rows
print("ROWS: ", self.rows-1) print("ROWS: ", self.rows-1)

View file

@ -8,6 +8,9 @@ from data.imageconversion import ImageConversion
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def __init__(self): def __init__(self):
self.settings = JSONHandling() self.settings = JSONHandling()
self.settingsWindow = SettingsWindow() self.settingsWindow = SettingsWindow()
self.imageConversion = ImageConversion() self.imageConversion = ImageConversion()