class BoardView(QWidget): def __init__(self, svg_board, **kwargs): super().__init__(**kwargs) self.svgWidget = QSvgWidget() layout = EvenLayout(self) layout.addWidget(self.svgWidget, 0, 0) self.press_pos = None self.refresh(svg_board) def refresh(self, svg_board): data = createQByteArray(svg_board) self.svgWidget.load(data) def mousePressEvent(self, event): self.press_pos = self.calculate_board_position(event) def mouseReleaseEvent(self, event): if self.press_pos: release_pos = self.calculate_board_position(event) if release_pos: piece = self.parent().game.get_piece_at_pos(self.press_pos) move = self.press_pos + release_pos if ((piece == 'P' and release_pos[1] == '8' and self.press_pos[1] == '7' and self.parent().game.board.turn) or (piece == 'p' and release_pos[1] == '1' and self.press_pos[1] == '2' and not self.parent().game.board.turn)): # Promotion # TODO: player input - await, asyncio move += 'q' elif self.press_pos == release_pos: move = '0000' # uci null move self.parent().execute_game_command(move) def mouseMoveEvent(self, event): # TODO: Implement drag piece-icon pass # Returns a tuple with the board position as uci and the piece at that position, # or None. def calculate_board_position(self, event): margin = self.svgWidget.size() * 0.05 + QSize(2, 2) square = (self.svgWidget.size() - 2 * margin) / 8 offset = (self.size() - self.svgWidget.size()) / 2. + margin x_coordinate = event.pos().x() - offset.width() y_coordinate = offset.height() + 8 * square.height() - event.pos().y() x_pos = int(1 + x_coordinate / square.width()) y_pos = int(1 + y_coordinate / square.height()) if (x_pos > 8 or x_pos < 1 or y_pos > 8 or y_pos < 1): return None else: return 'abcdefgh'[x_pos - 1] + str(y_pos)
class SourceWidget(QWidget): def __init__(self, parent=None): super(SourceWidget, self).__init__(parent) self.mimeData = None imageFile = QFile(':/images/example.svg') imageFile.open(QIODevice.ReadOnly) self.imageData = imageFile.readAll() imageFile.close() imageArea = QScrollArea() self.imageLabel = QSvgWidget() self.imageLabel.renderer().load(self.imageData) imageArea.setWidget(self.imageLabel) instructTopLabel = QLabel("This is an SVG drawing:") instructBottomLabel = QLabel( "Drag the icon to copy the drawing as a PNG file:") dragIcon = QPushButton("Export") dragIcon.setIcon(QIcon(':/images/drag.png')) dragIcon.pressed.connect(self.startDrag) layout = QGridLayout() layout.addWidget(instructTopLabel, 0, 0, 1, 2) layout.addWidget(imageArea, 1, 0, 2, 2) layout.addWidget(instructBottomLabel, 3, 0) layout.addWidget(dragIcon, 3, 1) self.setLayout(layout) self.setWindowTitle("Delayed Encoding") def createData(self, mimeType): if mimeType != 'image/png': return image = QImage(self.imageLabel.size(), QImage.Format_RGB32) painter = QPainter() painter.begin(image) self.imageLabel.renderer().render(painter) painter.end() data = QByteArray() buffer = QBuffer(data) buffer.open(QIODevice.WriteOnly) image.save(buffer, 'PNG') buffer.close() self.mimeData.setData('image/png', data) def startDrag(self): self.mimeData = MimeData() self.mimeData.dataRequested.connect(self.createData, Qt.DirectConnection) drag = QDrag(self) drag.setMimeData(self.mimeData) drag.setPixmap(QPixmap(':/images/drag.png')) drag.exec_(Qt.CopyAction)
class SourceWidget(QWidget): def __init__(self, parent=None): super(SourceWidget, self).__init__(parent) self.mimeData = None imageFile = QFile(':/images/example.svg') imageFile.open(QIODevice.ReadOnly) self.imageData = imageFile.readAll() imageFile.close() imageArea = QScrollArea() self.imageLabel = QSvgWidget() self.imageLabel.renderer().load(self.imageData) imageArea.setWidget(self.imageLabel) instructTopLabel = QLabel("This is an SVG drawing:") instructBottomLabel = QLabel("Drag the icon to copy the drawing as a PNG file:") dragIcon = QPushButton("Export") dragIcon.setIcon(QIcon(':/images/drag.png')) dragIcon.pressed.connect(self.startDrag) layout = QGridLayout() layout.addWidget(instructTopLabel, 0, 0, 1, 2) layout.addWidget(imageArea, 1, 0, 2, 2) layout.addWidget(instructBottomLabel, 3, 0) layout.addWidget(dragIcon, 3, 1) self.setLayout(layout) self.setWindowTitle("Delayed Encoding") def createData(self, mimeType): if mimeType != 'image/png': return image = QImage(self.imageLabel.size(), QImage.Format_RGB32) painter = QPainter() painter.begin(image) self.imageLabel.renderer().render(painter) painter.end() data = QByteArray() buffer = QBuffer(data) buffer.open(QIODevice.WriteOnly) image.save(buffer, 'PNG') buffer.close() self.mimeData.setData('image/png', data) def startDrag(self): self.mimeData = MimeData() self.mimeData.dataRequested.connect(self.createData, Qt.DirectConnection) drag = QDrag(self) drag.setMimeData(self.mimeData) drag.setPixmap(QPixmap(':/images/drag.png')) drag.exec_(Qt.CopyAction)
class LEDButton(QPushButton): # unset UNSET = -1 # normal OFF = 0 BLUE = 1 GREEN = 2 RED = 3 YELLOW = 4 # extra CALF = 5 def __init__(self, parent): QPushButton.__init__(self, parent) self.fLastColor = self.UNSET self.fColor = self.UNSET self.fImage = QSvgWidget() self.fImage.load(":/scalable/led_off.svg") self.fRect = QRectF(self.fImage.rect()) self.setCheckable(True) self.setText("") self.setColor(self.BLUE) def setColor(self, color): self.fColor = color if color == self.CALF: self.fLastColor = self.UNSET if self._loadImageNowIfNeeded(): if isinstance(self.fImage, QPixmap): size = self.fImage.width() else: size = self.fImage.sizeHint().width() self.fRect = QRectF(self.fImage.rect()) self.setFixedSize(self.fImage.size()) def paintEvent(self, event): painter = QPainter(self) event.accept() self._loadImageNowIfNeeded() if isinstance(self.fImage, QPixmap): size = self.fImage.width() rect = QRectF(0, 0, size, size) painter.drawPixmap(rect, self.fImage, rect) else: size = self.fImage.sizeHint().width() rect = QRectF(0, 0, size, size) self.fImage.renderer().render(painter, rect) def _loadImageNowIfNeeded(self): if self.isChecked(): if self.fLastColor == self.fColor: return if self.fColor == self.OFF: img = ":/scalable/led_off.svg" elif self.fColor == self.BLUE: img = ":/scalable/led_blue.svg" elif self.fColor == self.GREEN: img = ":/scalable/led_green.svg" elif self.fColor == self.RED: img = ":/scalable/led_red.svg" elif self.fColor == self.YELLOW: img = ":/scalable/led_yellow.svg" elif self.fColor == self.CALF: img = ":/bitmaps/led_calf_on.png" else: return False self.fLastColor = self.fColor elif self.fLastColor != self.OFF: img = ":/bitmaps/led_calf_off.png" if self.fColor == self.CALF else ":/scalable/led_off.svg" self.fLastColor = self.OFF else: return False if img.endswith(".png"): if not isinstance(self.fImage, QPixmap): self.fImage = QPixmap() else: if not isinstance(self.fImage, QSvgWidget): self.fImage = QSvgWidget() self.fImage.load(img) self.update() return True