Beispiel #1
0
    def __init__(self, ChildMode=QState.ExclusiveStates, new_timeline=False):
        # Must fix the following hack
        try:
            exp
            no_exp = False
        except:
            no_exp = True

        if no_exp:
            self._saved_parent = None
        else:
            self._saved_parent = exp._current_parent

        QState.__init__(self, ChildMode, parent=self._saved_parent)

        # connect to previous state if needed
        if not no_exp:
            exp._add_transition_if_needed(self)

        # add in a timeline
        if new_timeline:
            self.timeline = Timeline()
        else:
            # grab from parent
            self.timeline = self._saved_parent.timeline

        # grab the starting offset for this child
        if no_exp:
            self.cur_event_time = 0
        else:
            self.cur_event_time = self._saved_parent.cur_event_time

        # save amount shifted
        self.amount_advanced = 0
Beispiel #2
0
    def __init__(self, ChildMode=QState.ExclusiveStates, new_timeline=False):
        # Must fix the following hack
        try:
            exp
            no_exp = False
        except:
            no_exp = True

        if no_exp:
            self._saved_parent = None
        else:
            self._saved_parent = exp._current_parent
            
        QState.__init__(self, ChildMode,
                        parent=self._saved_parent)        

        # connect to previous state if needed
        if not no_exp:
            exp._add_transition_if_needed(self)

        # add in a timeline
        if new_timeline:
            self.timeline = Timeline()
        else:
            # grab from parent
            self.timeline = self._saved_parent.timeline

        # grab the starting offset for this child
        if no_exp:
            self.cur_event_time = 0
        else:
            self.cur_event_time = self._saved_parent.cur_event_time

        # save amount shifted
        self.amount_advanced = 0
Beispiel #3
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 #4
0
class ExpState(QState):
    """
    Base experimental state.
    """
    def __init__(self, parent=None, event_time=None):
        if parent is None:
            # grab from current exp
            parent = exp._current_parent
        # can't have children other than those we define, so must be
        # set up as ExclusiveStates
        QState.__init__(self, QState.ExclusiveStates, parent=parent)
        # set up dummy initial and final child states
        self._iState = QState(parent=self)
        self._fState = QFinalState(parent=self)
        self.setInitialState(self._iState)
        self._iState.addTransition(self._iState.finished,self._fState)

        # add to a timeline and save time
        self.parent = parent
        if event_time is None:
            event_time = self.parent.cur_event_time
        self._event_time = event_time
        parent.timeline.add_state(self, self._event_time)
        
    def _finalize(self):
        self._iState.finished.emit()

    def _run(self):
        raise NotImplementedError("Subclasses must implement this.")

    def _get_wait_time(self):
        # use the state parent's timeline and the specific offset to
        # determine the desired event time of the state
        desired_time = self.parent.timeline.start_time + self._event_time/1000.
        
        # compare that with now and return the wait_time in ms
        wait_time = long((desired_time - now())*1000)
        if wait_time < 0:
            wait_time = 0
        return wait_time
            
    def onEntry(self, ev):
        """
        Default onEntry for experiment states.  Schedules when it will
        run based on the timeline.
        """
        # figure out how long to wait
        wait_time = self._get_wait_time()

        # schedule the run
        QTimer.singleShot(wait_time,self._run)
Beispiel #5
0
class ExpState(QState):
    """
    Base experimental state.
    """
    def __init__(self, parent=None, event_time=None):
        if parent is None:
            # grab from current exp
            parent = exp._current_parent
        # can't have children other than those we define, so must be
        # set up as ExclusiveStates
        QState.__init__(self, QState.ExclusiveStates, parent=parent)
        # set up dummy initial and final child states
        self._iState = QState(parent=self)
        self._fState = QFinalState(parent=self)
        self.setInitialState(self._iState)
        self._iState.addTransition(self._iState.finished, self._fState)

        # add to a timeline and save time
        self.parent = parent
        if event_time is None:
            event_time = self.parent.cur_event_time
        self._event_time = event_time
        parent.timeline.add_state(self, self._event_time)

    def _finalize(self):
        self._iState.finished.emit()

    def _run(self):
        raise NotImplementedError("Subclasses must implement this.")

    def _get_wait_time(self):
        # use the state parent's timeline and the specific offset to
        # determine the desired event time of the state
        desired_time = self.parent.timeline.start_time + self._event_time / 1000.

        # compare that with now and return the wait_time in ms
        wait_time = long((desired_time - now()) * 1000)
        if wait_time < 0:
            wait_time = 0
        return wait_time

    def onEntry(self, ev):
        """
        Default onEntry for experiment states.  Schedules when it will
        run based on the timeline.
        """
        # figure out how long to wait
        wait_time = self._get_wait_time()

        # schedule the run
        QTimer.singleShot(wait_time, self._run)
Beispiel #6
0
    def __init__(self, parent=None, event_time=None):
        if parent is None:
            # grab from current exp
            parent = exp._current_parent
        # can't have children other than those we define, so must be
        # set up as ExclusiveStates
        QState.__init__(self, QState.ExclusiveStates, parent=parent)
        # set up dummy initial and final child states
        self._iState = QState(parent=self)
        self._fState = QFinalState(parent=self)
        self.setInitialState(self._iState)
        self._iState.addTransition(self._iState.finished,self._fState)

        # add to a timeline and save time
        self.parent = parent
        if event_time is None:
            event_time = self.parent.cur_event_time
        self._event_time = event_time
        parent.timeline.add_state(self, self._event_time)
Beispiel #7
0
    def __init__(self, parent=None, event_time=None):
        if parent is None:
            # grab from current exp
            parent = exp._current_parent
        # can't have children other than those we define, so must be
        # set up as ExclusiveStates
        QState.__init__(self, QState.ExclusiveStates, parent=parent)
        # set up dummy initial and final child states
        self._iState = QState(parent=self)
        self._fState = QFinalState(parent=self)
        self.setInitialState(self._iState)
        self._iState.addTransition(self._iState.finished, self._fState)

        # add to a timeline and save time
        self.parent = parent
        if event_time is None:
            event_time = self.parent.cur_event_time
        self._event_time = event_time
        parent.timeline.add_state(self, self._event_time)
Beispiel #8
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 #9
0
    def _add_state(self, state_name, state_action=_action_widget_textEdit):
        """

        :param state_name:
        :param state_action:
        :return:
        """
        id_state = self._generate_id_for_state(state_name)
        state = self._states[id_state] = QState()
        state.setObjectName(id_state)
        #
        state_action(self, state_name)
        # Add State to Machine
        self._qsm.addState(state)