From 3815df70bf023ace7702f922cbe3a2f8741544af Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 21 Jul 2018 19:50:38 +0800 Subject: [PATCH] Subclassing buttons --- graphic_components/board.py | 1 + graphic_components/buttons.py | 81 +++++++++++++++++---------- graphic_components/menu_graphics.py | 2 +- graphic_components/sudoku_graphics.py | 12 ++-- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/graphic_components/board.py b/graphic_components/board.py index f697f6a..33cd850 100644 --- a/graphic_components/board.py +++ b/graphic_components/board.py @@ -127,6 +127,7 @@ class GameBoard(BoxBoard): self.numring.setFocus() self.numring.toggle_anim(True) self.numring.scribbling = scribbling + self.numring.set_buttons_transparent(False) def select_ring_number(self, val, scribbling): if val == 'X': diff --git a/graphic_components/buttons.py b/graphic_components/buttons.py index 8b2658f..95ae345 100644 --- a/graphic_components/buttons.py +++ b/graphic_components/buttons.py @@ -19,16 +19,15 @@ class AnimBox(QGraphicsObject): # Prepare the signal hoverEnter = pyqtSignal() hoverExit = pyqtSignal() - buttonClicked = pyqtSignal(str) # Initialisation - def __init__(self, x, y, width, height, text, parent=None): + def __init__(self, x, y, width, height, parent=None): super().__init__(parent=parent) self.x = x self.y = y self.width = width self.height = height - self.text = text + #self.text = text self.circumference = 2*(width+height) # Set up pens for drawing @@ -60,19 +59,6 @@ class AnimBox(QGraphicsObject): for t in range(1, 10): self.anim.setKeyValueAt(t / 10, self.logistic_func(t / 10)) self.anim.setEndValue(self.circumference) - self.animText = AnimatedText(self.text, parent=self) - self.transparent = False - - def set_transparent(self, state): - self.transparent = state - col = self.default_pen.color() - if state: - col.setAlphaF(0.2) - else: - col.setAlphaF(1) - self.default_pen.setColor(col) - self.animText.set_transparent(state) - self.update() def set_freeze(self, freeze): if freeze: @@ -106,10 +92,6 @@ class AnimBox(QGraphicsObject): if line.length() > 1: painter.drawLine(line) painter.setPen(self.default_pen) - if self.transparent: - painter.fillRect(self.btn_rect, QColor(255, 255, 255, 0.1)) - else: - painter.fillRect(self.btn_rect, Qt.black) painter.drawRect(self.btn_rect) # Defining the length to be drawn as a pyqtProperty @@ -166,6 +148,56 @@ class AnimBox(QGraphicsObject): self.toggle_anim(False) super().hoverLeaveEvent(event) + +class RingButton(AnimBox): + # Prepare the signal + buttonClicked = pyqtSignal(str) + + # Initialisation + def __init__(self, x, y, width, height, text, parent=None): + super().__init__(x, y, width, height, parent=parent) + self.text = text + self.transparent = False + + def set_transparent(self, state): + self.transparent = state + col = self.default_pen.color() + if state: + col.setAlphaF(0.2) + else: + col.setAlphaF(1) + self.default_pen.setColor(col) + self.update() + + # Reimplemented paint + def paint(self, painter, style, widget=None): + super().paint(painter, style, widget) + painter.setPen(self.default_pen) + if self.transparent: + painter.fillRect(self.btn_rect, QColor(255, 255, 255, 0.1)) + else: + painter.fillRect(self.btn_rect, Qt.black) + painter.drawText(self.boundingRect(), Qt.AlignCenter, self.text) + + def mousePressEvent(self, event): + self.toggle_anim(False) + self.buttonClicked.emit(self.text) + + +class MenuButton(AnimBox): + # Prepare the signal + buttonClicked = pyqtSignal(str) + + # Initialisation + def __init__(self, x, y, width, height, text, parent=None): + super().__init__(x, y, width, height, parent=parent) + self.text = text + self.animText = AnimatedText(text, parent=self) + + def paint(self, painter, style, widget=None): + super().paint(painter, style, widget) + painter.fillRect(self.btn_rect, Qt.black) + def mousePressEvent(self, event): self.toggle_anim(False) self.buttonClicked.emit(self.text) @@ -196,15 +228,6 @@ class AnimatedText(QGraphicsObject): self.anim.setEndValue(len(self.actual_text) + self.delay) self.visibleChanged.connect(self.show_text) - def set_transparent(self, state): - col = self.default_pen.color() - if state: - col.setAlphaF(0.2) - else: - col.setAlphaF(1) - self.default_pen.setColor(col) - self.update() - def show_text(self): if self.isVisible(): self.toggle_anim(True) diff --git a/graphic_components/menu_graphics.py b/graphic_components/menu_graphics.py index 0dcee05..edc60e6 100644 --- a/graphic_components/menu_graphics.py +++ b/graphic_components/menu_graphics.py @@ -158,7 +158,7 @@ class DifficultyMenu(QGraphicsWidget): self.width = self.btn_width for i in range(5): - btn = buttons.AnimBox(0, (self.btn_height + 10) * i, + btn = buttons.MenuButton(0, (self.btn_height + 10) * i, self.btn_width, self.btn_height, DIFFICULTIES[i], parent=self) btn.buttonClicked.connect(self.clicked_on) self.diff_buttons.append(btn) diff --git a/graphic_components/sudoku_graphics.py b/graphic_components/sudoku_graphics.py index 521eff2..f84e19e 100644 --- a/graphic_components/sudoku_graphics.py +++ b/graphic_components/sudoku_graphics.py @@ -283,8 +283,8 @@ class NumberRing(BaseSudokuItem): cell_string = 'X' else: cell_string = str(i) - btn = buttons.AnimBox(0, 0, self.cell_width, - self.cell_height, cell_string, parent=self) + btn = buttons.RingButton(0, 0, self.cell_width, self.cell_height, + cell_string, parent=self) btn.buttonClicked.connect(self.send_button_press) self.cell_buttons.append(btn) @@ -376,12 +376,14 @@ class NumberRing(BaseSudokuItem): self.scribbling = False def hoverEnterEvent(self, event): - for btn in self.cell_buttons: - btn.set_transparent(False) + self.set_buttons_transparent(False) def hoverLeaveEvent(self, event): + self.set_buttons_transparent(True) + + def set_buttons_transparent(self, state): for btn in self.cell_buttons: - btn.set_transparent(True) + btn.set_transparent(state) # Defining the length to be drawn as a pyqtProperty @pyqtProperty(float)