Esempio n. 1
0
    def setup_statemachine(self):
        """Setup and start state machine"""

        machine = QtCore.QStateMachine()

        #  _______________
        # |               |
        # |               |
        # |               |
        # |_______________|
        #
        group = util.QState("group", QtCore.QState.ParallelStates, machine)

        #  _______________
        # | ____     ____ |
        # ||    |---|    ||
        # ||____|---|____||
        # |_______________| - Parallell State
        #
        visibility = util.QState("visibility", group)

        hidden = util.QState("hidden", visibility)
        visible = util.QState("visible", visibility)

        #  _______________
        # | ____     ____ |
        # ||    |---|    ||
        # ||____|---|____||
        # |_______________| - Parallell State
        #
        operation = util.QState("operation", group)

        ready = util.QState("ready", operation)
        publishing = util.QState("publishing", operation)
        finished = util.QState("finished", operation)
        repairing = util.QState("repairing", operation)
        initialising = util.QState("initialising", operation)
        stopping = util.QState("stopping", operation)
        stopped = util.QState("stopped", operation)
        saving = util.QState("saving", operation)

        #  _______________
        # | ____     ____ |
        # ||    |---|    ||
        # ||____|---|____||
        # |_______________| - Parallell State
        #
        errored = util.QState("errored", group)

        clean = util.QState("clean", errored)
        dirty = util.QState("dirty", errored)

        #  _______________
        # | ____     ____ |
        # ||    |---|    ||
        # ||____|---|____||
        # |_______________| - Parallell State

        # States that block the underlying GUI
        suspended = util.QState("suspended", group)

        alive = util.QState("alive", suspended)
        acting = util.QState("acting", suspended)
        acted = QtCore.QHistoryState(operation)
        acted.setDefaultState(ready)

        #  _______________
        # | ____     ____ |
        # ||    |---|    ||
        # ||____|---|____||
        # |_______________|
        # | ____     ____ |
        # ||    |---|    ||
        # ||____|---|____||
        # |_______________|
        #

        hidden.addTransition(self.show, visible)
        visible.addTransition(self.hide, hidden)

        ready.addTransition(self.acting, acting)
        ready.addTransition(self.publishing, publishing)
        ready.addTransition(self.initialising, initialising)
        ready.addTransition(self.repairing, repairing)
        ready.addTransition(self.saving, saving)
        saving.addTransition(self.saved, ready)
        publishing.addTransition(self.stopping, stopping)
        publishing.addTransition(self.finished, finished)
        finished.addTransition(self.initialising, initialising)
        finished.addTransition(self.acting, acting)
        initialising.addTransition(self.initialised, ready)
        stopping.addTransition(self.acted, acted)
        stopping.addTransition(self.finished, finished)

        dirty.addTransition(self.initialising, clean)
        clean.addTransition(self.changed, dirty)

        alive.addTransition(self.acting, acting)
        acting.addTransition(self.acted, acted)

        # Set initial states
        for compound, state in {
                machine: group,
                visibility: hidden,
                operation: ready,
                errored: clean,
                suspended: alive
        }.items():
            compound.setInitialState(state)

        # Make connections
        for state in (hidden, visible, ready, publishing, finished, repairing,
                      initialising, stopping, saving, stopped, dirty, clean,
                      acting, alive, acted):
            state.entered.connect(
                lambda state=state: self.state_changed.emit(state.name))

        machine.start()
        return machine
Esempio n. 2
0
    def stateInit(self):
        """Initialize state machine"""
        self.machine = QtCore.QStateMachine(self)

        idle = QtCore.QState()
        idle_none = QtCore.QState(idle)
        idle_open = QtCore.QState(idle)
        idle_done = QtCore.QState(idle)
        idle_updated = QtCore.QState(idle)
        idle_last = QtCore.QHistoryState(idle)
        idle.setInitialState(idle_none)
        running = QtCore.QState()

        idle.setObjectName('STATE_IDLE')
        idle.assignProperty(self.paramWidget.container, 'enabled', True)
        idle.assignProperty(self.actionStop, 'visible', False)
        idle.assignProperty(self.actionRun, 'visible', True)

        def onEntry(event):
            self.treeConstraints.setItemsDisabled(False)
            self.treeResults.setItemsDisabled(False)
            self.setFocus()

        idle.onEntry = onEntry

        idle_none.setObjectName('STATE_IDLE_NONE')
        idle_none.assignProperty(self.searchWidget, 'enabled', False)
        idle_none.assignProperty(self.tabConstraints, 'enabled', False)
        idle_none.assignProperty(self.tabParams, 'enabled', False)
        idle_none.assignProperty(self.tabResults, 'enabled', False)
        idle_none.assignProperty(self.tabDiagram, 'enabled', False)
        idle_none.assignProperty(self.tabTable, 'enabled', False)
        idle_none.assignProperty(self.actionRun, 'enabled', False)
        idle_none.assignProperty(self.actionSave, 'enabled', False)
        idle_none.assignProperty(self.actionExport, 'enabled', False)
        idle_none.assignProperty(self.barFlags, 'enabled', False)
        idle_none.assignProperty(self.labelFlagInfo, 'text',
                                 'Nothing to display.')
        idle_none.assignProperty(self.labelFlagInfo, 'visible', True)
        idle_none.assignProperty(self.labelFlagWarn, 'visible', False)

        idle_open.setObjectName('STATE_IDLE_OPEN')
        idle_open.assignProperty(self.searchWidget, 'enabled', True)
        idle_open.assignProperty(self.tabConstraints, 'enabled', True)
        idle_open.assignProperty(self.tabParams, 'enabled', True)
        idle_open.assignProperty(self.tabResults, 'enabled', False)
        idle_open.assignProperty(self.tabDiagram, 'enabled', False)
        idle_open.assignProperty(self.tabTable, 'enabled', False)
        idle_open.assignProperty(self.actionRun, 'enabled', True)
        idle_open.assignProperty(self.actionSave, 'enabled', True)
        idle_open.assignProperty(self.actionExport, 'enabled', False)
        idle_open.assignProperty(self.barFlags, 'enabled', True)
        idle_open.assignProperty(self.labelFlagInfo, 'text',
                                 'Ready to run rate analysis.')
        idle_open.assignProperty(self.labelFlagInfo, 'visible', True)
        idle_open.assignProperty(self.labelFlagWarn, 'visible', False)

        def onEntry(event):
            self.tabContainerAnalysis.setCurrentIndex(0)
            self.setFocus()

        idle_open.onEntry = onEntry

        idle_done.setObjectName('STATE_IDLE_DONE')
        idle_done.assignProperty(self.searchWidget, 'enabled', True)
        idle_done.assignProperty(self.tabConstraints, 'enabled', True)
        idle_done.assignProperty(self.tabParams, 'enabled', True)
        idle_done.assignProperty(self.tabResults, 'enabled', True)
        idle_done.assignProperty(self.tabDiagram, 'enabled', True)
        idle_done.assignProperty(self.tabTable, 'enabled', True)
        idle_done.assignProperty(self.actionRun, 'enabled', True)
        idle_done.assignProperty(self.actionSave, 'enabled', True)
        idle_done.assignProperty(self.actionExport, 'enabled', True)
        idle_done.assignProperty(self.barFlags, 'enabled', True)
        idle_done.assignProperty(self.labelFlagInfo, 'text',
                                 'Analysis complete.')
        idle_done.assignProperty(self.labelFlagInfo, 'visible', False)
        idle_done.assignProperty(self.labelFlagWarn, 'visible', True)

        def onEntry(event):
            self.tabContainerAnalysis.setCurrentIndex(0)
            self.tabContainerResults.setCurrentIndex(0)

        idle_done.onEntry = onEntry

        running.setObjectName('STATE_RUNNING')
        running.assignProperty(self.actionRun, 'visible', False)
        running.assignProperty(self.actionStop, 'visible', True)
        running.assignProperty(self.paramWidget.container, 'enabled', False)
        running.assignProperty(self.barFlags, 'enabled', True)
        running.assignProperty(self.labelFlagInfo, 'text', 'Please wait...')
        running.assignProperty(self.labelFlagWarn, 'visible', False)

        def onEntry(event):
            self.tabContainerResults.setCurrentIndex(1)
            self.treeConstraints.setItemsDisabled(True)
            self.treeResults.setItemsDisabled(True)

        running.onEntry = onEntry

        idle_updated.assignProperty(self.barFlags, 'enabled', True)
        idle_updated.assignProperty(self.labelFlagInfo, 'visible', False)
        idle_updated.assignProperty(self.labelFlagWarn, 'visible', True)
        idle_updated.assignProperty(
            self.labelFlagWarn, 'text',
            'Parameters have changed, re-run analysis to update results.')

        transition = utility.NamedTransition('OPEN')

        def onTransition(event):
            fileInfo = QtCore.QFileInfo(event.kwargs['file'])
            labelText = fileInfo.baseName()
            treeName = self.analysis.tree.label
            if treeName is not None:
                if len(treeName) > 0 and treeName != labelText:
                    labelText += ': ' + treeName
            self.labelTree.setText(labelText)
            self.treeResults.clear()
            self.treeConstraints.clear()
            widgets.TreeWidgetNodeConstraints(self.treeConstraints,
                                              self.analysis.tree.seed_node)
            idealWidth = self.treeConstraints.idealWidth()
            width = min([self.width() / 2, idealWidth])
            self.splitter.setSizes([width, 1, self.width() / 2])
            if self.analysis.results is not None:
                self.machine.postEvent(utility.NamedEvent('LOAD'))

        transition.onTransition = onTransition
        transition.setTargetState(idle_open)
        idle.addTransition(transition)

        transition = utility.NamedTransition('LOAD')

        def onTransition(event):
            warning = self.analysis.results.flags['warning']
            self.labelFlagWarn.setText(warning)
            self.treeResults.clear()
            widgets.TreeWidgetNodeResults(self.treeResults,
                                          self.analysis.results.tree.seed_node)

        transition.onTransition = onTransition
        transition.setTargetState(idle_done)
        idle.addTransition(transition)

        transition = utility.NamedTransition('RUN')
        transition.setTargetState(running)
        idle.addTransition(transition)

        transition = utility.NamedTransition('DONE')

        def onTransition(event):
            warning = self.analysis.results.flags['warning']
            self.labelFlagWarn.setText(warning)
            self.treeResults.clear()
            widgets.TreeWidgetNodeResults(self.treeResults,
                                          self.analysis.results.tree.seed_node)
            self.closeMessages()
            msgBox = QtWidgets.QMessageBox(self)
            msgBox.setWindowTitle(self.windowTitle())
            msgBox.setIcon(QtWidgets.QMessageBox.Information)
            msgBox.setText('Analysis complete.')
            msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
            msgBox.setDefaultButton(QtWidgets.QMessageBox.Ok)
            msgBox.exec()

        transition.onTransition = onTransition
        transition.setTargetState(idle_done)
        running.addTransition(transition)

        transition = utility.NamedTransition('UPDATE')
        transition.setTargetState(idle_updated)
        idle_done.addTransition(transition)

        transition = utility.NamedTransition('FAIL')

        def onTransition(event):
            self.tabContainerResults.setCurrentIndex(1)
            self.fail(event.args[0])

        transition.onTransition = onTransition
        transition.setTargetState(idle_last)
        running.addTransition(transition)

        transition = utility.NamedTransition('CANCEL')
        transition.setTargetState(idle_last)
        running.addTransition(transition)

        self.machine.addState(idle)
        self.machine.addState(running)
        self.machine.setInitialState(idle)
        self.machine.start()