File indexing completed on 2025-02-16 03:30:06
0001 /* 0002 0003 SPDX-FileCopyrightText: 2010 Nokia Corporation and /or its subsidiary(-ies) <qt-info@nokia.com> 0004 0005 This file is part of the QtCore module of the Qt Toolkit. 0006 0007 SPDX-License-Identifier: BSD-3-Clause 0008 0009 */ 0010 0011 #ifndef STATEMACHINE_H 0012 #define STATEMACHINE_H 0013 0014 #include <QAbstractAnimation> 0015 #include <QAbstractTransition> 0016 #include <QEvent> 0017 #include <QState> 0018 #include <QStateMachine> 0019 0020 class StateSwitchEvent : public QEvent 0021 { 0022 public: 0023 StateSwitchEvent() 0024 : QEvent(Type(StateSwitchType)) 0025 { 0026 } 0027 0028 StateSwitchEvent(int id) 0029 : QEvent(Type(StateSwitchType)) 0030 , m_id(id) 0031 { 0032 } 0033 0034 enum { StateSwitchType = QEvent::User + 256 }; 0035 0036 int id() const 0037 { 0038 return m_id; 0039 } 0040 0041 private: 0042 int m_id; 0043 }; 0044 0045 class StateSwitchTransition : public QAbstractTransition 0046 { 0047 public: 0048 StateSwitchTransition(int id) 0049 : QAbstractTransition() 0050 , m_id(id) 0051 { 0052 } 0053 0054 protected: 0055 bool eventTest(QEvent *event) override 0056 { 0057 return (event->type() == QEvent::Type(StateSwitchEvent::StateSwitchType)) && (static_cast<StateSwitchEvent *>(event)->id() == m_id); 0058 } 0059 0060 void onTransition(QEvent *) override 0061 { 0062 } 0063 0064 private: 0065 int m_id; 0066 }; 0067 0068 class StateSwitcher : public QState 0069 { 0070 public: 0071 explicit StateSwitcher(QStateMachine *machine); 0072 0073 void addState(QState *state, QAbstractAnimation *animation, int id); 0074 0075 void switchToState(int n); 0076 }; 0077 0078 #endif // STATEMACHINE_H