Subclassing buttons

master
En Yi 2018-07-21 19:50:38 +08:00
parent 912eafc564
commit 3815df70bf
4 changed files with 61 additions and 35 deletions

View File

@ -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':

View File

@ -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)

View File

@ -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)

View File

@ -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)