diff --git a/data/imagetojigsaw.py b/data/imagetojigsaw.py index 42e193e..745e7fd 100644 --- a/data/imagetojigsaw.py +++ b/data/imagetojigsaw.py @@ -5,53 +5,109 @@ from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtCore import Qt from PIL import Image import random +from data.settingswindow import SettingsWindow class JigsawPiece(QGraphicsPixmapItem): number = 0 + allPieces = [] - def __init__(self, pixmap, correctPos): + def __init__(self, pixmap, startPos, resultRow, resultCol): super().__init__() + self.setPixmap(pixmap) - self.correctPos = correctPos + + if(startPos != (0,0)): + self.startPos = startPos + else: + self.startPos = (1,1) self.setFlags( QGraphicsPixmapItem.ItemIsMovable | QGraphicsPixmapItem.ItemIsSelectable ) - self.setPos(0, 0) + JigsawPiece.number += 1 self.number = JigsawPiece.number + + self.row = resultRow + self.col = resultCol self.cluster = {self} + + JigsawPiece.allPieces.append(self) + def mousePressEvent(self, event): if event.button() == Qt.LeftButton: - self.dragPosition = event.pos() - print(event.pos(), self.correctPos, " ", self.number) + + print("Momentane Position: {", self.getPositionX() , " - ", self.getPositionY(), "} Piece number: ", self.number, " Row: ", self.row, " Col: ", self.col, " Starting pos:", self.startPos) def mouseMoveEvent(self, event): - if event.buttons() == Qt.LeftButton and self.dragPosition: - delta = self.mapToParent(event.pos()) - self.dragPosition - self.pos() - for piece in self.cluster: - piece.setPos(piece.pos() + delta) + if event.buttons() == Qt.LeftButton and self.pos(): + delta = self.mapToParent(event.pos()) - self.pos() + for self in self.cluster: + self.setPos(self.pos() + delta) def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: - firstRow = 0 - lastRow = (PuzzleWindow.getRows) - print(firstRow, lastRow) - if (self.number): - None + neighbors = self.getDistanceToNeighborPieces() + print(len(JigsawPiece.allPieces)) + for item in neighbors: + print(f"Neighbor {item[0]} {item[3]}: distance x{item[1]}. y{item[2]}") + self.checkPieceCluster() + def getPositionX(self): + return self.pos().x() + + def getPositionY(self): + return self.pos().y() + + def getNeighbors(self): + neighbors = [] + for piece in JigsawPiece.allPieces: + if piece == self: + continue + if((piece.row - self.row) == 0 and (piece.col - self.col) == 1): + neighbors.append((piece, "right")) + if((piece.row - self.row) == 1 and (piece.col - self.col) == 0): + neighbors.append((piece, "down")) + if((piece.row - self.row) == 0 and (piece.col - self.col) == -1): + neighbors.append((piece, "left")) + if((piece.row - self.row) == -1 and (piece.col - self.col) == 0): + neighbors.append((piece, "up")) + return neighbors + def getDistanceToNeighborPieces(self): + neighbors = self.getNeighbors() + neighborDistances = [[neighbor, neighbor.getPositionX() - self.getPositionX(), neighbor.getPositionY() - self.getPositionY(), direction] for neighbor, direction in neighbors] + return neighborDistances + + def checkPieceCluster(self): + distanceCheck = self.getDistanceToNeighborPieces() + for neighbor, dx, dy, direction in distanceCheck: + if direction == "right" and dx <= 150 and dx >= 110 and dy >= -5 and dy <= 5: + self.setPieceCluster(neighbor) + if(direction == "down" and dy <= 165 and dy >= 150 and dx >= -5 and dx <= 5): + self.setPieceCluster(neighbor) + if(direction == "left" and dx >= -125 and dx <= -100 and dy >= -5 and dy <= 5): + self.setPieceCluster(neighbor) + if(direction == "up" and dy <= -150 and dy >= -160 and dx >= -5 and dx <= 5): + self.setPieceCluster(neighbor) + + + def setPieceCluster(self, neighbor): + newCluster = self.cluster | neighbor.cluster + for cluster in newCluster: + cluster.cluster = newCluster + class PuzzleWindow(QGraphicsScene): def __init__(self, filePath, rows, cols): super().__init__() self.filePath = filePath self.rows = rows - print(self.rows-1) + print("ROWS: ", self.rows-1) self.cols = cols - print(self.cols-1) + print("COLS: ", self.cols-1) if (filePath != None): self.createPuzzle(rows, cols) @@ -74,7 +130,7 @@ class PuzzleWindow(QGraphicsScene): else: - for i in range(rows): + for i in range(self.rows): for j in range(cols): box = (j*pieceW, i*pieceH, (j+1)*pieceW, (i+1)*pieceH) @@ -83,17 +139,10 @@ class PuzzleWindow(QGraphicsScene): data = pieceImg.tobytes("raw", "RGB") qimage = QImage(data, width, height, 3 * width, QImage.Format_RGB888) pixmap = QPixmap.fromImage(qimage) - piece = JigsawPiece(pixmap, (j*pieceW, i*pieceH)) + piece = JigsawPiece(pixmap, (j*pieceW, i*pieceH), i, j) print(f"Piecenumber: {piece.number} | Row: {i} Col: {j} | H: {pieceImg.height} W: {pieceImg.width}") piece.setPos(random.randint(0, w-pieceW), random.randint(0, h-pieceH)) self.addItem(piece) - piece.setPos(random.randint(0, int(self.width()-pieceW)), random.randint(0, int(self.height()-pieceH))) - print("1111") - - def getRows(self): - return self.rows - - def getCols(self): - return self.cols + piece.setPos(random.randint(1, int(self.width()-pieceW)), random.randint(1, int(self.height()-pieceH))) \ No newline at end of file diff --git a/data/jsonhandler.py b/data/jsonhandler.py index 384baa6..9e8f107 100644 --- a/data/jsonhandler.py +++ b/data/jsonhandler.py @@ -2,9 +2,9 @@ import os, json class JSONHandling: def __init__(self): - if os.path.exists("settings.json"): - self.settingsFile = "settings.json" - else: + self.settingsFile = "settings.json" + + if not os.path.exists(self.settingsFile): data = {"color": "Blue", "puzzlePieces": 50} with open("settings.json", "w") as f: json.dump(data, f) @@ -13,13 +13,11 @@ class JSONHandling: return self.settingsFile def getDataFull(self): - if os.path.exists("settings.json"): try: - with open(self.settingsFile) as f: - rValue = json.load(f) - return rValue + with open(self.settingsFile, "r") as f: + return json.load(f) except Exception: - pass + return {} def setData(self, attribute, parameter): data = self.getDataFull() @@ -29,4 +27,4 @@ class JSONHandling: def getData(self, attribute): data = self.getDataFull() - return data[attribute] \ No newline at end of file + return data.get(attribute) \ No newline at end of file diff --git a/data/mainwindow.py b/data/mainwindow.py index 9247eb6..51ebcc8 100644 --- a/data/mainwindow.py +++ b/data/mainwindow.py @@ -25,7 +25,7 @@ class MainWindow(QMainWindow): leftLayout.setContentsMargins(0, 0, 0, 0) leftLayout.setSpacing(5) - self.scene = PuzzleWindow(None, self.settingsWindow.piecesToGrid(self.settings.getData("puzzlePieces"))[0], self.settingsWindow.piecesToGrid(self.settings.getData("puzzlePieces"))[1]) + self.scene = PuzzleWindow(None, self.settingsWindow.getGridRows(), self.settingsWindow.getGridCols()) self.view = QGraphicsView(self.scene) self.view.setStyleSheet(f"background-color: {self.settings.getData("color")}; border: 1px solid black;") leftLayout.addWidget(self.view) @@ -51,8 +51,8 @@ class MainWindow(QMainWindow): addImageAction = puzzleMenu.addAction("Add Image") selectedImage = addImageAction.triggered.connect(lambda: self.handlerInput("image")) - addFolderAction = puzzleMenu.addAction("Add Folder") - selectedFolder = addFolderAction.triggered.connect(lambda: self.handlerInput("folder")) + #addFolderAction = puzzleMenu.addAction("Add Folder") + #selectedFolder = addFolderAction.triggered.connect(lambda: self.handlerInput("folder")) puzzleAction.setMenu(puzzleMenu) @@ -71,13 +71,12 @@ class MainWindow(QMainWindow): selectedImage = self.imageConversion.openImageFile() if selectedImage: self.constructPuzzle(selectedImage) - elif typeInput == "folder": - selectedFolder = self.imageConversion.openFolder() - if selectedFolder: - self.constructPuzzle(selectedFolder) - + #elif typeInput == "folder": + # selectedFolder = self.imageConversion.openFolder() + # if selectedFolder: + # self.constructPuzzle(selectedFolder) def constructPuzzle(self, input): - print(input) - self.scene = PuzzleWindow(input, self.settingsWindow.piecesToGrid(self.settings.getData("puzzlePieces"))[0], self.settingsWindow.piecesToGrid(self.settings.getData("puzzlePieces"))[1]) + self.scene = PuzzleWindow(input, self.settingsWindow.getGridRows(), self.settingsWindow.getGridCols()) self.view.setScene(self.scene) + \ No newline at end of file diff --git a/data/settingswindow.py b/data/settingswindow.py index d92ab6f..a030c95 100644 --- a/data/settingswindow.py +++ b/data/settingswindow.py @@ -72,4 +72,10 @@ class SettingsWindow(QDialog): def piecesToGrid(self, totalPieces): cols = math.ceil(math.sqrt(totalPieces)) rows = math.ceil(totalPieces / cols) - return rows, cols \ No newline at end of file + return rows, cols + + def getGridRows(self): + return self.piecesToGrid(self.settings.getData("puzzlePieces"))[0] + + def getGridCols(self): + return self.piecesToGrid(self.settings.getData("puzzlePieces"))[1] \ No newline at end of file