Ejemplo n.º 1
0
class ChessScene(QtGui.QGraphicsScene):

    def __init__(self, game_engine):

        super(ChessScene, self).__init__()

        #board
        self.board = BoardWidget()
        self.addItem(self.board)

        #moves
        self.moves = MovesWidget(self.board, game_engine)
        self.board.boardChanged.connect(self.moves.resetEngine)
        self.addItem(self.moves)

        #fen
        self.addItem(self.board.fen)

        self.moves.setPos(0, 0)
        size = self.moves.preferredSize()
        self.board.setPos(0, size.height())

        #opening_table
        #self.opening_table = OpeningTable()
        #self.move_table.moveMade.connect(self.opening_table.onMoveMade)

        #game engine
        self.setEngine(game_engine)
        self.newGame()

        #actions
        actions = [
            Action(self, "Play", settings.keys['mode_play'], tr("play game"), self.onShowMoves, 'media-playback-start'),
            Action(self, "Edit", settings.keys['mode_edit'], tr("set board position"), self.onShowPallete, 'document-open')
        ]
        #FIXME
        #self.addActions(actions)

        play, edit = [a.graphics_button for a in actions]
        self.addItem(play)
        self.addItem(edit)
        rect = self.board.squares.boundingRect()
        play.setPos(rect.width(), 0)
        edit.setPos(rect.width() + play.size().width(), 0)
        self.onShowMoves()

    def onShowMoves(self):
        self.board.setEditable(False)
        self.moves.setEnabled(True)

    def onShowPallete(self):
        self.board.setEditable(True)
        self.moves.setEnabled(False)

    def moveCursor(self, direction):
        self.board.moveCursor(direction)

    def cursorSelect(self):
        self.board.cursorSelect()

    def loadGame(self, moves):
        position = self.moves.loadGame(moves)
        self.moves.first()

    def newGame(self):
        self.moves.newGame()

    def setEngine(self, game_engine):
        self.moves.setEngine(game_engine)
    

    def onVariationSelected(self, from_square, to_square, subject, target):
        
        # go back on move
        last_move = self.moves_table.previousMove()
        self.setMove(last_move)
        self.moves_table.onMoveMade(last_move)

        # set the new move
        self.onNewMove(from_square, to_square, subject, target)
Ejemplo n.º 2
0
class ChessGameWidget(GraphicsWidget):
    def __init__(self):

        super(ChessGameWidget, self).__init__()

        # board
        self.board = BoardWidget()
        self.board.setParentItem(self)

        # moves
        game_engine = ChessLibGameEngine()
        self.moves = MovesWidget(self.board, game_engine)
        self.moves.newGame()

        self.fen = self.board.fen.text
        self.fen.setParentItem(self.board)
        self.fen.setPos(0, -40)

        # actions
        actions = [
            Action(self, "Play", settings.keys["mode_play"], tr("play game"), self.onShowMoves, "media-playback-start"),
            Action(
                self, "Edit", settings.keys["mode_edit"], tr("set board position"), self.onShowPallete, "document-edit"
            ),
            Action(
                self,
                "Analyze",
                settings.keys["mode_analyze"],
                tr("analyze position"),
                self.onShowAnalyze,
                "games-solve",
            ),
        ]
        self._buttons = [b.graphics_button for b in actions]
        for button in self._buttons:
            button._checkable = True
            button._radio = True
        self.addActions(actions)

        size = self.moves.preferredSize()
        x, y = self.board.squares.size().width() + self.board.spacing, size.height()
        self._pack_actions(self.moves.actions(), x + 10, y + 10, False)

        self.onShowMoves()

        layout = QtGui.QGraphicsLinearLayout()
        buttons = self.getGraphicsButtonLayout()
        layout.addItem(buttons)
        layout.setAlignment(buttons, QtCore.Qt.AlignRight)
        layout.addItem(self.moves)
        layout.addItem(self.board)
        layout.setOrientation(QtCore.Qt.Vertical)
        self.setLayout(layout)

    def _pack_actions(self, actions, x, y, is_horizontal):

        # set moves actions
        for idx, action in enumerate(actions):
            action.graphics_button.setParentItem(self)
            if is_horizontal:
                offset = (action.graphics_button.preferredSize().width() + 5) * idx
                action.graphics_button.setPos(x + offset, y)
            else:
                offset = (action.graphics_button.preferredSize().height() + 5) * idx
                action.graphics_button.setPos(x, y + offset)

    def onShowMoves(self):
        self.board.editable = False
        self.moves.setEnabled(True)
        self.fen.fadeOut(settings.animation_duration)
        self._buttons[1].checked = False
        self._buttons[2].checked = False

    def onShowPallete(self):
        self.board.editable = True
        self.moves.setEnabled(False)
        self.fen.fadeIn(settings.animation_duration)
        self._buttons[0].checked = False
        self._buttons[2].checked = False

    def onShowAnalyze(self):
        self._buttons[0].checked = False
        self._buttons[1].checked = False