-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.h
69 lines (65 loc) · 1.8 KB
/
controller.h
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
#pragma once
#include <QObject>
#include <QTimer>
#include <QQuickItem>
#include <epframebuffer.h>
/*!
* \brief This class serves as a simple interop between QML and C++
*/
class Controller : public QObject {
Q_OBJECT
public:
Controller(QObject* parent) : QObject(parent) {}
~Controller(){}
/*!
* \brief Method called by QML after it's loaded to allow C++ to start interacting with the UI
*/
Q_INVOKABLE void startup(){
qDebug() << "Running controller startup";
QTimer::singleShot(10, [this]{
setState("loaded");
});
}
/*!
* \brief Gets the current state in QML
* \return The current state
*/
QString state() {
if(stateControllerUI == nullptr && !getStateControllerUI()){
return "loading";
}
return stateControllerUI->property("state").toString();
}
/*!
* \brief Sets the current QML state
* \param state The state to use
*/
void setState(QString state){
if(stateControllerUI == nullptr && !getStateControllerUI()){
throw "Unable to find state controller";
}
stateControllerUI->setProperty("state", state);
}
/*!
* \brief Change the root QObject for the QML
* \param root The root QObject
*/
void setRoot(QObject* root){ this->root = root; }
private:
/*!
* \brief The root QObject for the QML
*/
QObject* root = nullptr;
/*!
* \brief The QML state controller
*/
QObject* stateControllerUI = nullptr;
/*!
* \brief Find the QML state controller and cache it's value
* \return The QML state controller QObject
*/
QObject* getStateControllerUI(){
stateControllerUI = root->findChild<QObject*>("stateController");
return stateControllerUI;
}
};