Subclassing buttons
parent
912eafc564
commit
3815df70bf
|
@ -127,6 +127,7 @@ class GameBoard(BoxBoard):
|
||||||
self.numring.setFocus()
|
self.numring.setFocus()
|
||||||
self.numring.toggle_anim(True)
|
self.numring.toggle_anim(True)
|
||||||
self.numring.scribbling = scribbling
|
self.numring.scribbling = scribbling
|
||||||
|
self.numring.set_buttons_transparent(False)
|
||||||
|
|
||||||
def select_ring_number(self, val, scribbling):
|
def select_ring_number(self, val, scribbling):
|
||||||
if val == 'X':
|
if val == 'X':
|
||||||
|
|
|
@ -19,16 +19,15 @@ class AnimBox(QGraphicsObject):
|
||||||
# Prepare the signal
|
# Prepare the signal
|
||||||
hoverEnter = pyqtSignal()
|
hoverEnter = pyqtSignal()
|
||||||
hoverExit = pyqtSignal()
|
hoverExit = pyqtSignal()
|
||||||
buttonClicked = pyqtSignal(str)
|
|
||||||
|
|
||||||
# Initialisation
|
# Initialisation
|
||||||
def __init__(self, x, y, width, height, text, parent=None):
|
def __init__(self, x, y, width, height, parent=None):
|
||||||
super().__init__(parent=parent)
|
super().__init__(parent=parent)
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.text = text
|
#self.text = text
|
||||||
self.circumference = 2*(width+height)
|
self.circumference = 2*(width+height)
|
||||||
|
|
||||||
# Set up pens for drawing
|
# Set up pens for drawing
|
||||||
|
@ -60,19 +59,6 @@ class AnimBox(QGraphicsObject):
|
||||||
for t in range(1, 10):
|
for t in range(1, 10):
|
||||||
self.anim.setKeyValueAt(t / 10, self.logistic_func(t / 10))
|
self.anim.setKeyValueAt(t / 10, self.logistic_func(t / 10))
|
||||||
self.anim.setEndValue(self.circumference)
|
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):
|
def set_freeze(self, freeze):
|
||||||
if freeze:
|
if freeze:
|
||||||
|
@ -106,10 +92,6 @@ class AnimBox(QGraphicsObject):
|
||||||
if line.length() > 1:
|
if line.length() > 1:
|
||||||
painter.drawLine(line)
|
painter.drawLine(line)
|
||||||
painter.setPen(self.default_pen)
|
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)
|
painter.drawRect(self.btn_rect)
|
||||||
|
|
||||||
# Defining the length to be drawn as a pyqtProperty
|
# Defining the length to be drawn as a pyqtProperty
|
||||||
|
@ -166,6 +148,56 @@ class AnimBox(QGraphicsObject):
|
||||||
self.toggle_anim(False)
|
self.toggle_anim(False)
|
||||||
super().hoverLeaveEvent(event)
|
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):
|
def mousePressEvent(self, event):
|
||||||
self.toggle_anim(False)
|
self.toggle_anim(False)
|
||||||
self.buttonClicked.emit(self.text)
|
self.buttonClicked.emit(self.text)
|
||||||
|
@ -196,15 +228,6 @@ class AnimatedText(QGraphicsObject):
|
||||||
self.anim.setEndValue(len(self.actual_text) + self.delay)
|
self.anim.setEndValue(len(self.actual_text) + self.delay)
|
||||||
self.visibleChanged.connect(self.show_text)
|
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):
|
def show_text(self):
|
||||||
if self.isVisible():
|
if self.isVisible():
|
||||||
self.toggle_anim(True)
|
self.toggle_anim(True)
|
||||||
|
|
|
@ -158,7 +158,7 @@ class DifficultyMenu(QGraphicsWidget):
|
||||||
self.width = self.btn_width
|
self.width = self.btn_width
|
||||||
|
|
||||||
for i in range(5):
|
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)
|
self.btn_width, self.btn_height, DIFFICULTIES[i], parent=self)
|
||||||
btn.buttonClicked.connect(self.clicked_on)
|
btn.buttonClicked.connect(self.clicked_on)
|
||||||
self.diff_buttons.append(btn)
|
self.diff_buttons.append(btn)
|
||||||
|
|
|
@ -283,8 +283,8 @@ class NumberRing(BaseSudokuItem):
|
||||||
cell_string = 'X'
|
cell_string = 'X'
|
||||||
else:
|
else:
|
||||||
cell_string = str(i)
|
cell_string = str(i)
|
||||||
btn = buttons.AnimBox(0, 0, self.cell_width,
|
btn = buttons.RingButton(0, 0, self.cell_width, self.cell_height,
|
||||||
self.cell_height, cell_string, parent=self)
|
cell_string, parent=self)
|
||||||
btn.buttonClicked.connect(self.send_button_press)
|
btn.buttonClicked.connect(self.send_button_press)
|
||||||
self.cell_buttons.append(btn)
|
self.cell_buttons.append(btn)
|
||||||
|
|
||||||
|
@ -376,12 +376,14 @@ class NumberRing(BaseSudokuItem):
|
||||||
self.scribbling = False
|
self.scribbling = False
|
||||||
|
|
||||||
def hoverEnterEvent(self, event):
|
def hoverEnterEvent(self, event):
|
||||||
for btn in self.cell_buttons:
|
self.set_buttons_transparent(False)
|
||||||
btn.set_transparent(False)
|
|
||||||
|
|
||||||
def hoverLeaveEvent(self, event):
|
def hoverLeaveEvent(self, event):
|
||||||
|
self.set_buttons_transparent(True)
|
||||||
|
|
||||||
|
def set_buttons_transparent(self, state):
|
||||||
for btn in self.cell_buttons:
|
for btn in self.cell_buttons:
|
||||||
btn.set_transparent(True)
|
btn.set_transparent(state)
|
||||||
|
|
||||||
# Defining the length to be drawn as a pyqtProperty
|
# Defining the length to be drawn as a pyqtProperty
|
||||||
@pyqtProperty(float)
|
@pyqtProperty(float)
|
||||||
|
|
Loading…
Reference in New Issue