File: enablestate.cpp

package info (click to toggle)
boats 202008-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,572 kB
  • sloc: cpp: 11,129; xml: 10; makefile: 9
file content (100 lines) | stat: -rw-r--r-- 2,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// C++ Implementation: enablestate
//
// Description:
//
//
// Author: Thibaut GRIDEL <tgridel@free.fr>
//
// Copyright (c) 2014-2015 Thibaut GRIDEL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//
#include "enablestate.h"

#include "commontypes.h"

#include <QStateMachine>
#include <QAbstractTransition>
#include <QAbstractState>

#include <iostream>

extern int debugLevel;

EnableState::EnableState(QState *parent) :
    QState(parent),
    m_active(false),
    m_enabled(false)
{
}

bool EnableState::isActive() {
    return m_active;
}

bool EnableState::isEnabled() {
    return m_enabled;
}

bool EnableState::isToggable() {
    return m_toggable;
}

void EnableState::onEntry(QEvent *event) {
    Q_UNUSED(event);
    if(!m_active) {
        m_active = true;
        if (debugLevel & 1 << STATE) std::cout << "State Entry " << this->objectName().toStdString() << std::endl;
        emit activeChanged(m_active);
        foreach (QAbstractTransition* transition, transitions()) {
            EnableState *state = static_cast<EnableState*> (transition->targetState());
            state->setEnabled(true);
        }
        if (m_toggable) {
            setEnabled(true);
        }
    }
}

void EnableState::onExit(QEvent *event) {
    Q_UNUSED(event);
    if(m_active) {
        m_active = false;
        if (debugLevel & 1 << STATE) std::cout << "State Exit " << this->objectName().toStdString() << std::endl;
        emit activeChanged(m_active);
        foreach (QAbstractTransition* transition, transitions()) {
            EnableState *state = static_cast<EnableState*> (transition->targetState());
            state->setEnabled(false);
        }
        if (m_toggable) {
            setEnabled(false);
        }
    }
}

void EnableState::setEnabled(bool enabled) {
    if (m_enabled != enabled) {
        m_enabled = enabled;
        emit enabledChanged(m_enabled);
    }
}

void EnableState::setToggable(bool toggable) {
    if (m_toggable != toggable) {
        m_toggable = toggable;
        emit toggableChanged(m_toggable);
    }
}