-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
144 lines (108 loc) · 5.61 KB
/
main.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <QApplication>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
//#include <QtQML>
// file and directory includes
#include <QFile>
#include <QDir>
// thread includes
#include <QThread>
#include <QThreadPool>
#include <QRunnable>
// CAN BUS includes
#include <QCanBus>
#include <QCanBusDeviceInfo>
#include <QCanBusDevice>
#include <QCanBusFrame>
// Byte and bit includes
#include <QList>
#include <QBitArray>
#include <QByteArray>
#include <QByteArrayList>
#include <windows.h>
// Class file includes
#include "FrameHandler.hpp"
#include "GNCThread.hpp"
#include "CommandState.hpp"
#include "DirHelper.hpp"
#define WIN1000 // How do I set flags
//make a function to create can bus objects instead????
// another function to connect?
int main(int argc, char *argv[])
{
//QGuiApplication app(argc, argv);
QApplication app(argc, argv);
qInfo() << "Hello";
QQmlApplicationEngine engine;
/////////////////////////////////////////////////////////////////////////////////////////////////////
QDir appDir {QGuiApplication::applicationDirPath()};
QThreadPool* pool {QThreadPool::globalInstance()};
QThread::currentThread()->setObjectName("Main Event Thread");
pool->setMaxThreadCount(pool->maxThreadCount());
qInfo() << appDir.absolutePath();
//////////////////////////////////////////////////////////////
FrameHandler *frameHandler {new FrameHandler(&app)};
GNCThread *GNC {new GNCThread(&app)};
frameHandler->setAutoDelete(false); //hmmmmmmmmmmmmmm might crash
GNC->setAutoDelete(false); // might crash
///////////////////////////////////////////////////////////////////////////////////////////////
// Look into lambda functions for signals and slots.
////////////////////////////////////////////////////////////////////////////////////////////////
engine.rootContext()->setContextProperty("appDir", appDir.absolutePath());
//engine.rootContext()->setContextProperty("monitorWidth", GetSystemMetrics(SM_CXSCREEN));
//engine.rootContext()->setContextProperty("monitorHeight", GetSystemMetrics(SM_CYSCREEN));
qInfo() << GetSystemMetrics(SM_CXSCREEN);
qInfo() << GetSystemMetrics(SM_CYSCREEN);
// Expose objects to the QML engine
engine.rootContext()->setContextProperty("frameHandler", frameHandler);
engine.rootContext()->setContextProperty("GNC", GNC);
engine.rootContext()->setContextProperty("logger", frameHandler->logger()); // may not need
engine.rootContext()->setContextProperty("qmlEngine", &engine);
//engine.rootContext()->setContextProperty("frameHandlerSensors", frameHandler->sensors());
qRegisterMetaType<FrameHandler>();
qRegisterMetaType<Sensor>();
qRegisterMetaType<HPSensor>();
qRegisterMetaType<Valve>();
qRegisterMetaType<Autosequence>();
qRegisterMetaType<TankPressController>();
qRegisterMetaType<Node>();
qRegisterMetaType<EngineController>();
qRegisterMetaType<Logger>();
// Also expose sensors and valves with setContextProperty too using the foreach loop
qmlRegisterUncreatableType<FrameHandler>("FrameHandlerEnums", 1, 0, "FrameHandlerEnums", "C++ instantiation only");
qmlRegisterUncreatableType<Sensor>("SensorEnums", 1, 0, "SensorEnums", "C++ instantiation only");
qmlRegisterUncreatableType<HPSensor>("HPSensorEnums", 1, 0, "HPSensorEnums", "C++ instantiation only");
qmlRegisterUncreatableType<Valve>("ValveEnums", 1, 0, "ValveEnums", "C++ instantiation only");
qmlRegisterUncreatableType<Node>("NodeIDEnums", 1, 0, "NodeIDEnums", "C++ instantiation only");
qmlRegisterUncreatableType<Autosequence>("AutosequenceEnums", 1, 0, "AutosequenceEnums", "C++ instantiation only");
qmlRegisterUncreatableType<TankPressController>("TankPressControllerEnums", 1, 0, "TankPressControllerEnums", "C++ instantiation only");
qmlRegisterUncreatableType<EngineController>("EngineControllerEnums", 1, 0, "EngineControllerEnums", "C++ instantiation only");
qmlRegisterUncreatableType<Logger>("LoggerEnums", 1, 0, "LoggerEnums", "C++ instantiation only");
// Register C++ objects to QML objects and vice versa. (expose c++ data to QML as a property)
// also register actionable items in QML and use signals and slots to connect
// signals emitted by those QML items to the C++ objects.
// QQmlComponent component(&engine, QUrl("qrc:/BLT-GUI-Maker/MyItem.qml));
// Create an instance of the component
// QObject* qmlObject {component.create()};
const QUrl url(u"file:///C:/CodeStuff/Beach Launch Team/BLT-Theseus-GUI/Main.qml"_qs);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
&app, []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
// How to load multiple windows
engine.load(url);
//engine.load(url);
//engine.load(u"file:///C:/CodeStuff/Beach Launch Team/BLT-Theseus-GUI/GraphQML.qml"_qs);
//engine.load(url); // for each call to load, another object is created. This is ok to do when the frontend ONLY grabs data from the backend
//engine.load(url); // as a result, each page should ideally do something completely different for optimization
// Starting threads, where the application begins running:
pool->start(frameHandler);
pool->start(GNC);
// Important
// Gracefully exits the theads (lol I don't know if this is the correctway)
QObject::connect(&app, &QGuiApplication::aboutToQuit, frameHandler, &FrameHandler::setLoopToFalse);
//QObject::connect(&app, &QGuiApplication::aboutToQuit, GNC, &GNC::setLoopToFalse);
qInfo() << QThread::currentThread();
qInfo() << " before return app.exec()";
return app.exec();
}