Document menu_graphics

master
En Yi 2018-07-24 21:50:43 +08:00 committed by En Yi
parent 089c779d82
commit 4d24dadb98
2 changed files with 119 additions and 13 deletions

View File

@ -182,7 +182,6 @@ class GameBoard(BoxBoard):
self.numring.toggle_anim(True)
self.numring.scribbling = scribbling
def select_ring_number(self, val, scribbling):
"""Get the selected number from the Ring and pass into the grid
@ -310,6 +309,7 @@ class MenuBoard(BoxBoard):
self.score_display.show_board(True)
def return_to_normal(self):
"""Reenable the difficulty and high score buttons. Used after setting the high scores"""
"""Re-enable the difficulty and high score buttons. Used after setting the high scores
"""
self.diff_display.set_disabled(False)
self.score_display.set_disabled(False)

View File

@ -17,12 +17,18 @@ else:
from . import scoreboard as scb
class TimerDisplayer(QGraphicsWidget):
"""The widget to display the elapsed time. Unit of time is a tenth of a second.
"""
def __init__(self, parent=None):
"""Set up the box to draw and the time string with the QTimer
Parameters
----------
parent: object
Passed into QGraphicsWidget init method
"""
super().__init__(parent=parent)
#self.setParent(parent)
self.width = 100
self.height = 50
@ -42,48 +48,78 @@ class TimerDisplayer(QGraphicsWidget):
self.atenth_seconds = 0
self.timer = QTimer()
self.timer.setInterval(100)
self.timer.timeout.connect(self.increase_time)
self.timer.timeout.connect(self._increase_time)
self.timer.start()
def increase_time(self):
def _increase_time(self):
"""Increase the time. Connected to the QTimer.
"""
self.atenth_seconds += 1
self.update()
def reset_time(self):
"""Reset the time to 0 and start the QTimer
"""
self.atenth_seconds = 0
self.timer.start()
def get_time(self):
"""Get the time formatted as such: (minutes):(seconds):(A tenth of a second)
Returns
-------
str: the formatted time string
"""
return "{:02d}:{:02d}:{:1d}".format(int(self.atenth_seconds/600),
int(self.atenth_seconds/10) % 60,
self.atenth_seconds % 10)
def paint(self, painter, style, widget=None):
"""Reimplemented from QGraphicsWidget. Draw the box and the timer.
"""
box = self.timer_box
painter.setPen(self.box_pen)
painter.drawRect(box)
painter.drawText(box, Qt.AlignCenter,
"{:02d}:{:02d}:{:1d}".format(int(self.atenth_seconds/600),
int(self.atenth_seconds/10) % 60,
self.atenth_seconds % 10))
painter.drawText(box, Qt.AlignCenter, self.get_time())
class DifficultyDisplayer(QGraphicsWidget):
<<<<<<< HEAD
notFocus = Signal()
difficultySelected = Signal(str)
=======
"""Display the current difficulty. Clicking on it displays the difficulty menu.
Attributes
----------
notFocus: pyqtSignal
Emitted when it loses focus
difficultySelected = pyqtSignal(str)
Emitted when a difficulty is selected. Emits the selected difficulty
"""
notFocus = pyqtSignal()
difficultySelected = pyqtSignal(str)
>>>>>>> Document menu_graphics
def __init__(self, parent=None):
"""Create the box and the text.
Parameters
----------
parent: object
Passed into QGraphicsWidget init method
"""
super().__init__(parent=parent)
self.width = 100
self.height = 50
self.text = "None"
self.diff_menu = DifficultyMenu(self.width, self.height, self)
self.diff_menu.setY(-self.diff_menu.height)
self.diff_menu.setVisible(False)
self.text = "None"
self.box_pen = QPen()
self.box_pen.setColor(Qt.white)
self.pen_width = 3
@ -105,18 +141,29 @@ class DifficultyDisplayer(QGraphicsWidget):
self.diff_menu.loseFocus.connect(self.notFocus.emit)
def set_disabled(self, state):
"""Set to disable mouse events.
Parameters
----------
state: bool
True to disable, False otherwise
"""
if state:
self.setAcceptedMouseButtons(Qt.NoButton)
else:
self.setAcceptedMouseButtons(Qt.LeftButton)
def paint(self, painter, style, widget=None):
"""Reimplemented from QGraphicsWidget. Draw the box and the difficulty text.
"""
painter.setPen(self.box_pen)
painter.drawRect(self.diff_box)
painter.drawText(self.diff_box, Qt.AlignCenter, self.text)
painter.drawRect(self.boundingRect())
def mousePressEvent(self, event):
"""Reimplemented from QGraphicsWidget. Toggle the difficulty menu on click.
"""
if not self.diff_menu.isVisible():
self.diff_menu.setFocus()
self.diff_menu.setVisible(True)
@ -125,24 +172,57 @@ class DifficultyDisplayer(QGraphicsWidget):
self.notFocus.emit()
def selected_difficulty(self, string):
"""Hide the difficulty menu once a difficulty is selected.
Parameters
----------
string: str
The difficulty selected
"""
self.diff_menu.setVisible(False)
self.set_text(string)
self.notFocus.emit()
def set_text(self, string):
"""Set the text to be displayed. Should be one of the difficulty options.
Parameters
----------
string: str
String to be set
"""
self.text = string
self.update()
def boundingRect(self):
"""Reimplemented from QGraphicsWidget.
"""
return QRectF(0, 0, self.width, self.height)
class DifficultyMenu(QGraphicsWidget):
"""The menu to select the difficulty.
Attributes
----------
menuClicked: pyqtSignal(str)
Emitted when a difficulty is selected. Emits the difficulty string.
loseFocus: pyqtSignal
Emitted when the menu loses focus.
"""
menuClicked = Signal(str)
loseFocus = Signal()
def __init__(self, width, height, parent=None):
"""Creates the five difficulty buttons
Parameters
----------
parent: object
Passed into QGraphicsWidget init method
"""
super().__init__(parent=parent)
self.setParent(parent)
@ -162,12 +242,22 @@ class DifficultyMenu(QGraphicsWidget):
self.setFocusPolicy(Qt.ClickFocus)
def boundingRect(self):
"""Reimplemented from QGraphicsWidget.
"""
return QRectF(0, 0, self.width, self.height)
def clicked_on(self, string):
"""Emits the menuCLicked signal with the selected difficulty, when one of the buttons is pressed.
Parameters
----------
string: The difficulty string from the buttons
"""
self.menuClicked.emit(string)
def focusOutEvent(self, event):
"""Reimplemented from QGraphicsWidget. Check that no buttons are pressed before losing focus.
"""
if not any(btn.isUnderMouse() for btn in self.diff_buttons) and not self.parent().isUnderMouse():
self.loseFocus.emit()
self.setVisible(False)
@ -176,6 +266,13 @@ class DifficultyMenu(QGraphicsWidget):
class HighScoreDisplayer(QGraphicsObject):
def __init__(self, parent=None):
"""Creates the five difficulty buttons
Parameters
----------
parent: object
Passed into QGraphicsWidget init method
"""
super().__init__(parent=parent)
self.size = 25
@ -209,9 +306,14 @@ class HighScoreDisplayer(QGraphicsObject):
self.size = self.icon_size
def boundingRect(self):
"""Reimplemented from QGraphicsWidget.
"""
return QRectF(-self.size, -self.size, self.size, self.size)
def paint(self, painter, style, widget=None):
"""Reimplemented from QGraphicsWidget. Paint the bounding rect as the border. Additionally,
draw a white rectangle when not selected.
"""
painter.setPen(self.box_pen)
painter.drawRect(self.boundingRect())
if not self.selected:
@ -219,9 +321,13 @@ class HighScoreDisplayer(QGraphicsObject):
-self.icon_size/2, -self.icon_size/2, Qt.white)
def hoverEnterEvent(self, ev):
"""Reimplemented from QGraphicsWidget. Show the score board.
"""
self.show_board(True)
def hoverLeaveEvent(self, ev):
"""Reimplemented from QGraphicsWidget. Hide the score board.
"""
self.show_board(False)