Beispiel #1
0
 def __init__(self, touchpad, parent=None):
     QStateMachine.__init__(self, parent)
     self.touchpad = touchpad
     self._touchpad_wrapper = TouchpadQtWrapper(self.touchpad, self)
     # setup monitoring objects
     self._monitors = {"mouses": MouseDevicesManager(self), "keyboard": create_keyboard_monitor(self)}
     self._enabled_monitors = set()
     # setup the states:
     self.states = {}
     for name, touchpad_off in self._STATE_NAMES.iteritems():
         state = QState(self)
         state.setObjectName(name)
         state.assignProperty(self._touchpad_wrapper, "off", touchpad_off)
         self.states[name] = state
     # setup the initial state
     self.setInitialState(self.states["on"])
     # setup the transitions
     self.transitions = defaultdict(list)
     # mouse management transitions
     for state in ("on", "temporarily_off"):
         self._add_transition(state, "off", self._monitors["mouses"].firstMousePlugged)
     self._add_transition("off", "on", self._monitors["mouses"].lastMouseUnplugged)
     # keyboard management transitions
     self._add_transition("on", "temporarily_off", self._monitors["keyboard"].typingStarted)
     self._add_transition("temporarily_off", "on", self._monitors["keyboard"].typingStopped)
     # start monitors
     self.initialState().entered.connect(self._start_stop_monitors)
     # stop monitors if the state machine is stopped
     self.stopped.connect(self._stop_all_monitors)
Beispiel #2
0
 def __init__(self, touchpad, parent=None):
     QStateMachine.__init__(self, parent)
     self.touchpad = touchpad
     self._touchpad_wrapper = TouchpadQtWrapper(self.touchpad, self)
     # setup monitoring objects
     self._monitors = {'mouses': MouseDevicesManager(self),
                       'keyboard': create_keyboard_monitor(self)}
     self._enabled_monitors = set()
     # setup the states:
     self.states = {}
     for name, touchpad_off in self._STATE_NAMES.iteritems():
         state = QState(self)
         state.setObjectName(name)
         state.assignProperty(self._touchpad_wrapper, 'off', touchpad_off)
         self.states[name] = state
     # setup the initial state
     self.setInitialState(self.states['on'])
     # setup the transitions
     self.transitions = defaultdict(list)
     # mouse management transitions
     for state in ('on', 'temporarily_off'):
         self._add_transition(
             state, 'off', self._monitors['mouses'].firstMousePlugged)
     self._add_transition(
         'off', 'on', self._monitors['mouses'].lastMouseUnplugged)
     # keyboard management transitions
     self._add_transition('on', 'temporarily_off',
                          self._monitors['keyboard'].typingStarted)
     self._add_transition('temporarily_off', 'on',
                          self._monitors['keyboard'].typingStopped)
     # start monitors
     self.initialState().entered.connect(self._start_stop_monitors)
     # stop monitors if the state machine is stopped
     self.stopped.connect(self._stop_all_monitors)
def main():
    app = QApplication(sys.argv)

    window = QMainWindow()
    central_widget = QWidget(window)

    start_action = QAction('Start', window)
    stop_action = QAction('Stop', window)
    toolbar = window.addToolBar('Monitor')
    toolbar.addAction(start_action)
    toolbar.addAction(stop_action)

    central_layout = QVBoxLayout(central_widget)

    monitor_name = QLabel(central_widget)
    central_layout.addWidget(monitor_name)

    state_label = QLabel(central_widget)
    central_layout.addWidget(state_label)

    combo_box = QComboBox(central_widget)
    items = [('No keys', AbstractKeyboardMonitor.IGNORE_NO_KEYS),
             ('Modifiers', AbstractKeyboardMonitor.IGNORE_MODIFIER_KEYS),
             ('Modifier combos',
              AbstractKeyboardMonitor.IGNORE_MODIFIER_COMBOS)]
    for label, userdata in items:
        combo_box.addItem(label, userdata)

    def _update_ignore_keys(index):
        monitor.keys_to_ignore = combo_box.itemData(index).toPyObject()

    combo_box.currentIndexChanged[int].connect(_update_ignore_keys)
    central_layout.addWidget(combo_box)

    central_widget.setLayout(central_layout)
    window.setCentralWidget(central_widget)

    monitor = create_keyboard_monitor(window)
    monitor_name.setText('Using monitor class {0}'.format(
        monitor.__class__.__name__))
    monitor.typingStarted.connect(partial(state_label.setText, 'typing'))
    monitor.typingStopped.connect(partial(state_label.setText, 'not typing'))
    start_action.triggered.connect(monitor.start)
    stop_action.triggered.connect(monitor.stop)
    stop_action.setEnabled(False)
    monitor.started.connect(partial(start_action.setEnabled, False))
    monitor.started.connect(partial(stop_action.setEnabled, True))
    monitor.stopped.connect(partial(start_action.setEnabled, True))
    monitor.stopped.connect(partial(stop_action.setEnabled, False))
    window.show()
    app.exec_()
def main():
    app = QApplication(sys.argv)

    window = QMainWindow()
    central_widget = QWidget(window)

    start_action = QAction('Start', window)
    stop_action = QAction('Stop', window)
    toolbar = window.addToolBar('Monitor')
    toolbar.addAction(start_action)
    toolbar.addAction(stop_action)

    central_layout = QVBoxLayout(central_widget)

    monitor_name = QLabel(central_widget)
    central_layout.addWidget(monitor_name)

    state_label = QLabel(central_widget)
    central_layout.addWidget(state_label)

    combo_box = QComboBox(central_widget)
    items = [
        ('No keys', AbstractKeyboardMonitor.IGNORE_NO_KEYS),
        ('Modifiers', AbstractKeyboardMonitor.IGNORE_MODIFIER_KEYS),
        ('Modifier combos', AbstractKeyboardMonitor.IGNORE_MODIFIER_COMBOS)]
    for label, userdata in items:
        combo_box.addItem(label, userdata)

    def _update_ignore_keys(index):
        monitor.keys_to_ignore = combo_box.itemData(index).toPyObject()

    combo_box.currentIndexChanged[int].connect(_update_ignore_keys)
    central_layout.addWidget(combo_box)

    central_widget.setLayout(central_layout)
    window.setCentralWidget(central_widget)

    monitor = create_keyboard_monitor(window)
    monitor_name.setText('Using monitor class {0}'.format(
        monitor.__class__.__name__))
    monitor.typingStarted.connect(partial(state_label.setText, 'typing'))
    monitor.typingStopped.connect(partial(state_label.setText, 'not typing'))
    start_action.triggered.connect(monitor.start)
    stop_action.triggered.connect(monitor.stop)
    stop_action.setEnabled(False)
    monitor.started.connect(partial(start_action.setEnabled, False))
    monitor.started.connect(partial(stop_action.setEnabled, True))
    monitor.stopped.connect(partial(start_action.setEnabled, True))
    monitor.stopped.connect(partial(stop_action.setEnabled, False))
    window.show()
    app.exec_()