コード例 #1
0
 def mousePressEvent(self, me):
     if self.pressedDown or me.button() ^ me.buttons():
         QFrame.mousePressEvent(self, me)
         return
     if me.button() == Qt.LeftButton:
         self.pressedDown = True
         self.setFrameShadow(QFrame.Sunken)
         me.accept()
         return
     QFrame.mousePressEvent(self, me)
     pass
コード例 #2
0
 def mousePressEvent(self, mouseEvent):
     # first check whether the click position is on
     # the bevel and not a cell and ignore it in that case
     if self._clicked_on_bevel(mouseEvent.pos()):
         print("clicked on bevel")
         return
     # adjust position by bevel size
     pos = mouseEvent.pos() - QPoint(self.lineWidth(), self.lineWidth())
     i = pos.x() // GameController.CELL_SIZE
     j = pos.y() // GameController.CELL_SIZE
     # due to imperfect bevel size there might be one pixel on and off
     # that causes the index go beyond the limit. we simply treat it as clicked on bevel
     if i == GameController.CELL_COUNT or j == GameController.CELL_COUNT:
         print("clicked on bevel")
         return
     cell = self.game.cells[i][j]
     # check if clicked on already opened cell
     if cell.open:
         return
     if mouseEvent.button() == Qt.LeftButton:
         if not cell.flag:  # if flag is set, don't do anything
             if cell.mine:
                 cell.open = True
                 cell.current = True
                 print("boom!")
                 self._open_all_mines()
                 self.game.stop_game(False)
             else:
                 # recursively open cells around current one
                 self.game.open_cells_recursively(i, j)
                 cell.open = True
     else:
         if not cell.open:
             if cell.flag:  # if flag already set, remote it
                 cell.flag = False
                 self.game.set_flags_count(self.game.flags + 1)
             else:  # otherwise set the flag
                 if self.game.flags > 0:
                     cell.flag = True
                     self.game.set_flags_count(self.game.flags - 1)
     # call parent widget mouse click method
     QFrame.mousePressEvent(self, mouseEvent)
     # repaint the widget
     self.update()