コード例 #1
0
ファイル: camera.py プロジェクト: teleshoes/n9-button-monitor
class TorchAutoShutOff(QWidget):
  def __init__(self, camera):
    super(TorchAutoShutOff, self).__init__()
    self.camera = camera
    self.timer = QBasicTimer()
  def schedule(self, time):
    self.timer.start(time, self)
  def cancel(self):
    self.timer.stop()
  def timerEvent(self, e):
    self.timer.stop()
    if self.camera.torchState == "on":
      print "auto shut-off"
      self.camera.torchOff()
コード例 #2
0
ファイル: game_engine.py プロジェクト: cloew/GalagaEsque
class GameEngine:
    """ Represents the engine for managing the game """
    Speed = 10

    def __init__(self):
        """ Instantiate the Game Engine """
        self.timer = QBasicTimer()
        
        self.window = None
        self.controller = None

        self.object = QObject()
        self.object.timerEvent = self.timerEvent

    def start(self, window, controller):
        """ Start the Game Engine """
        self.controller = controller
        self.window = window
        self.window.keyPressEvent = self.keyPressed
        self.window.keyReleaseEvent = self.keyReleased
        self.startTimer()
        
    def startTimer(self):
        """ Start the Game Timer """
        self.timer.start(GameEngine.Speed, self.object)
        
    def stop(self):
        """ Stop the game engine game timer """
        self.timer.stop()

    def timerEvent(self, event):
        """ Run the Game Timer Loop """
        self.controller.performGameCycle()

    def updateUI(self):
        """ Set the UI to update """
        self.window.update()

    def keyPressed(self, event):
        """ Called when the window has a key pressed """
        self.controller.keyPressed(event.key())

    def keyReleased(self, event):
        """ Called when the window has a key released """
        self.controller.keyReleased(event.key())
コード例 #3
0
class ClickTimer(QWidget):
  def __init__(self, key, config):
    super(ClickTimer, self).__init__()
    self.timer = QBasicTimer()
    self.key = key
    self.keyPressed = False
    self.config = config
    self.reset()
  def reset(self):
    self.timer.stop()
    self.presses = []
    self.releases = []
    self.longClickStarted = False
  def nowMs(self):
    return monotonic_time() * 1000
  def checkEvent(self):
    now = self.nowMs()
    self.timer.stop()
    if len(self.presses) == 0:
      if self.longClickStarted and len(self.releases) > 0:
        self.click("longClickStop")
      else:
        self.reset()
    elif len(self.presses) == 1:
      press = self.presses[0]
      if len(self.releases) == 0:
        if now - press > self.config.longClickDelayMs:
          self.click("longClickStart")
          self.longClickStarted = True
        else:
          self.schedule(self.config.longClickDelayMs - (now - press))
      else:
        if now - press > self.config.doubleClickDelayMs:
          self.click("singleClick")
        else:
          self.schedule(self.config.doubleClickDelayMs - (now - press))
    elif len(self.presses) == 2:
      press = self.presses[0]
      if now - press > self.config.trebleClickDelayMs:
        self.click("doubleClick")
      else:
        self.schedule(self.config.trebleClickDelayMs - (now - press))
    else:
      self.click("trebleClick")
  def schedule(self, time):
    self.timer.start(time, self)
  def click(self, clickType):
    self.reset()
    self.config.checkConfigFile()
    print >> sys.stderr, str(self.key) + ": " + clickType

    actionMaps = self.config.getActionMapSet()
    for a in actionMaps.getActionMapsForKey(self.key, clickType):
      a.maybeRun()
  def timerEvent(self, e):
    self.timer.stop()
    self.checkEvent()
  def receivePress(self):
    self.presses.append(self.nowMs())
    self.checkEvent()
  def receiveRelease(self):
    self.releases.append(self.nowMs())
    self.checkEvent()
  def keyEvent(self, state):
    if state == STATE_ON and not self.keyPressed:
      self.keyPressed = True
      self.receivePress()
    elif state == STATE_OFF:
      self.keyPressed = False
      self.receiveRelease()