Fix focusing issue of game board

master
En Yi 2019-07-06 22:10:14 +08:00 committed by En Yi
parent de5af7cb44
commit 9ce1ae0ff7
4 changed files with 27 additions and 16 deletions

View File

@ -147,7 +147,7 @@ class GameBoard(BoxBoard):
self.numring = sdk_grap.NumberRing(parent=self) self.numring = sdk_grap.NumberRing(parent=self)
self.playmenu = sdk_grap.PlayMenu(parent=self) self.playmenu = sdk_grap.PlayMenu(parent=self)
self.gamegrid.setFocus(Qt.MouseFocusReason) #self.gamegrid.setFocus(Qt.MouseFocusReason)
self.show_grid(False) self.show_grid(False)
self.show_playmenu(False) self.show_playmenu(False)
@ -161,6 +161,10 @@ class GameBoard(BoxBoard):
self.anim.finished.connect(lambda: self.show_playmenu(True)) self.anim.finished.connect(lambda: self.show_playmenu(True))
self.toggle_anim(True) self.toggle_anim(True)
self.refocus_timer = QTimer()
self.refocus_timer.timeout.connect(self.game_refocus)
self.refocus_timer.setSingleShot(True)
def show_number_ring(self, x=0, y=0, scribbling=False): def show_number_ring(self, x=0, y=0, scribbling=False):
"""Display the Number Ring if it is not visible, while setting the focus to it """Display the Number Ring if it is not visible, while setting the focus to it
@ -174,6 +178,7 @@ class GameBoard(BoxBoard):
True to set Scribble mode, False otherwise True to set Scribble mode, False otherwise
""" """
if not self.numring.isVisible(): if not self.numring.isVisible():
self.game_unfocus()
self.numring.setPos(x, y) self.numring.setPos(x, y)
self.numring.setVisible(True) self.numring.setVisible(True)
self.numring.setFocus() self.numring.setFocus()
@ -201,9 +206,17 @@ class GameBoard(BoxBoard):
"""Enable the grid and give it grid focus """Enable the grid and give it grid focus
""" """
self.gamegrid.set_disabled(False) self.gamegrid.set_disabled(False)
self.gamegrid.setFocus() #self.gamegrid.setFocus()
self.gamegrid.scribbling = self.numring.scribbling # To update the grid scribbling mode self.gamegrid.scribbling = self.numring.scribbling # To update the grid scribbling mode
def game_unfocus(self):
"""Enable the grid and give it grid focus
"""
self.gamegrid.set_disabled(True)
#self.gamegrid.setFocus()
#self.gamegrid.scribbling = self.numring.scribbling # To update the grid scribbling mode
def show_grid(self, state): def show_grid(self, state):
"""Show the grid, if it is not; Hide the grid, if it is. """Show the grid, if it is not; Hide the grid, if it is.
Note: Animation only plays when showing the grid. Note: Animation only plays when showing the grid.

View File

@ -84,10 +84,6 @@ class TimerDisplayer(QGraphicsWidget):
class DifficultyDisplayer(QGraphicsWidget): class DifficultyDisplayer(QGraphicsWidget):
<<<<<<< HEAD
notFocus = Signal()
difficultySelected = Signal(str)
=======
"""Display the current difficulty. Clicking on it displays the difficulty menu. """Display the current difficulty. Clicking on it displays the difficulty menu.
Attributes Attributes
@ -98,9 +94,8 @@ class DifficultyDisplayer(QGraphicsWidget):
difficultySelected = pyqtSignal(str) difficultySelected = pyqtSignal(str)
Emitted when a difficulty is selected. Emits the selected difficulty Emitted when a difficulty is selected. Emits the selected difficulty
""" """
notFocus = pyqtSignal() notFocus = Signal()
difficultySelected = pyqtSignal(str) difficultySelected = Signal(str)
>>>>>>> Document menu_graphics
def __init__(self, parent=None): def __init__(self, parent=None):
"""Create the box and the text. """Create the box and the text.
@ -167,6 +162,7 @@ class DifficultyDisplayer(QGraphicsWidget):
if not self.diff_menu.isVisible(): if not self.diff_menu.isVisible():
self.diff_menu.setFocus() self.diff_menu.setFocus()
self.diff_menu.setVisible(True) self.diff_menu.setVisible(True)
self.clicked.emit()
else: else:
self.diff_menu.setVisible(False) self.diff_menu.setVisible(False)
self.notFocus.emit() self.notFocus.emit()
@ -247,7 +243,7 @@ class DifficultyMenu(QGraphicsWidget):
return QRectF(0, 0, self.width, self.height) return QRectF(0, 0, self.width, self.height)
def clicked_on(self, string): def clicked_on(self, string):
"""Emits the menuCLicked signal with the selected difficulty, when one of the buttons is pressed. """Emits the menuClicked signal with the selected difficulty, when one of the buttons is pressed.
Parameters Parameters
---------- ----------

View File

@ -192,7 +192,7 @@ class SudokuGrid(BaseSudokuItem):
self.setAcceptHoverEvents(True) self.setAcceptHoverEvents(True)
self.setAcceptedMouseButtons(Qt.LeftButton) self.setAcceptedMouseButtons(Qt.LeftButton)
self.setFlag(QGraphicsItem.ItemIsFocusable, True) #self.setFlag(QGraphicsItem.ItemIsFocusable, True)
self.set_disabled(False) self.set_disabled(False)
# Set up the animation # Set up the animation
@ -326,18 +326,19 @@ class SudokuGrid(BaseSudokuItem):
if not self.sudoku_grid.get_cell_status(self.mouse_h, self.mouse_w) == sdk.FIXED: if not self.sudoku_grid.get_cell_status(self.mouse_h, self.mouse_w) == sdk.FIXED:
self.buttonClicked.emit(w, h, self.scribbling) self.buttonClicked.emit(w, h, self.scribbling)
self.set_disabled(True)
else: else:
self.buttonClicked.emit(0, 0, self.scribbling) self.buttonClicked.emit(0, 0, self.scribbling)
def focusInEvent(self, event): #def focusInEvent(self, event):
"""Reimplemented from QGraphicsObject. Unfreeze the grid on focus """Reimplemented from QGraphicsObject. Unfreeze the grid on focus
""" """
self.set_disabled(False) #self.set_disabled(False)
def focusOutEvent(self, event): #def focusOutEvent(self, event):
"""Reimplemented from QGraphicsObject. Freeze the grid when out of focus """Reimplemented from QGraphicsObject. Freeze the grid when out of focus
""" """
self.set_disabled(True) #self.set_disabled(True)
def keyPressEvent(self, event): def keyPressEvent(self, event):
"""Reimplemented from QGraphicsObject. Check if scribble key is held, toggling on scribbling mode. """Reimplemented from QGraphicsObject. Check if scribble key is held, toggling on scribbling mode.

View File

@ -43,7 +43,8 @@ class SudokuWindow(QGraphicsView):
self.gameboard.gridDrawn.connect(lambda: self.menuboard.show_children(True)) self.gameboard.gridDrawn.connect(lambda: self.menuboard.show_children(True))
self.gameboard.newGameSelected.connect(self.menuboard.set_difficulty_text) self.gameboard.newGameSelected.connect(self.menuboard.set_difficulty_text)
self.gameboard.sudokuDone.connect(self.menuboard.finish_the_game) self.gameboard.sudokuDone.connect(self.menuboard.finish_the_game)
self.menuboard.diff_display.notFocus.connect(self.gameboard.game_refocus) self.menuboard.diff_display.clicked.connect(self.gameboard.game_unfocus)
self.menuboard.diff_display.notFocus.connect(lambda: self.gameboard.refocus_timer.start(10))
self.menuboard.diff_display.difficultySelected.connect(self.gameboard.new_game) self.menuboard.diff_display.difficultySelected.connect(self.gameboard.new_game)
def resizeEvent(self, event): def resizeEvent(self, event):