-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (117 loc) · 4.02 KB
/
main.cpp
File metadata and controls
143 lines (117 loc) · 4.02 KB
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
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <progressworker.h>
#include <progressobject.h>
#include <QQmlContext>
#include "general.h"
#include <QTimer>
#include <bridge/qbridge.h>
#include <vector>
QObject * booty_bp_invoke = nullptr;
void named_pipe_progress_handler(ProgressWorker *ptr_pw)
{
show_thread();
if (ptr_pw->isFinishing())
{
return;
}
if (!ptr_pw->isPipeExist())
{
bool result = ptr_pw->createPipe();
if (!result)
{
qDebug() << "error : named pipe wasn't created" << endl;
return;
}
}
QString line = ptr_pw->readPipe();
if (line.compare("finished") == 0)
{
ptr_pw->endJob();
return;
}
progressState state = ptr_pw->parseForProgress(line);
if (state.match)
{
if (ptr_pw->m_current != state.current)
{
ptr_pw->m_current = state.current;
emit ptr_pw->currentStepChanged(state.current);
}
if (ptr_pw->m_total_step != state.total)
{
ptr_pw->m_total_step = state.total;
emit ptr_pw->totalStepChanged(state.total);
}
if (ptr_pw->m_progress != state.progress)
{
ptr_pw->m_progress = state.progress;
emit ptr_pw->progressChanged(state.progress);
//Direct call and set
if(booty_bp_invoke != nullptr)
{
booty_bp_invoke->setProperty("value",state.progress);
QMetaObject::invokeMethod(booty_bp_invoke,"invokeExample",Qt::QueuedConnection,Q_ARG(QVariant,(state.progress + 0.25)));
}
}
if(ptr_pw->m_progress >= 1.0)
{
emit ptr_pw->finished();
}
}
qDebug() << "read content from named piped file: " << line << endl;
QTimer::singleShot(1000, ptr_pw, &ProgressWorker::startJob);
}
int main(int argc, char *argv[])
{
set_thread_name("main thread");
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QTranslator qtTranslator;
qtTranslator.load(":/i18n/strings_en_US", ".");
app.installTranslator(&qtTranslator);
QQmlApplicationEngine engine;
QString path = QGuiApplication::applicationDirPath();
engine.addImportPath("qrc:/");
engine.addImportPath("qrc:/imports");
const QUrl url(QStringLiteral("qrc:/main.qml"));
ProgressObject prog_obj;
prog_obj.initJob(named_pipe_progress_handler, "fifo_pipe");
prog_obj.prepareJob();
// Bridge C++ to qml connection
QBridge qbridge;
engine.rootContext()->setContextProperty("qbridge", &qbridge);
// End of Bridge C++ to qml connection
engine.rootContext()->setContextProperty("ProgressObj", &prog_obj);
qRegisterMetaType<Status>("Status");
qmlRegisterUncreatableType<ProgressStatus>("thundernet.general", 1, 0, "Status", "Not creatable generic status enum");
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreated,
&app,
[url,&prog_obj](QObject *obj, const QUrl &objUrl)
{
if (!obj && (url == objUrl))
QCoreApplication::exit(-1);
if(url == objUrl)
{
booty_bp_invoke = obj->findChild<QObject*>("booty_pb_invoke");
//Starting reading FIFO
prog_obj.startJob();
}
},
Qt::QueuedConnection
);
engine.load(url);
show_thread();
QObject::connect(&app, &QGuiApplication::aboutToQuit,[&prog_obj](){
if(prog_obj.isFinished()) return;
InterfaceNamedPipe * pipeWriter = Factory::createNamedPipeObject(prog_obj.getFileName());
pipeWriter->pipeWrite("finished\n");
delete pipeWriter;
} );
return app.exec();
}