-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressworker.cpp
More file actions
146 lines (122 loc) · 2.84 KB
/
progressworker.cpp
File metadata and controls
146 lines (122 loc) · 2.84 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
144
145
146
#include "progressworker.h"
#include "general.h"
#include <QTimer>
#include <QCoreApplication>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
ProgressWorker::ProgressWorker(QObject *parent)
: QObject{parent}
{
}
ProgressWorker::~ProgressWorker()
{
if (m_ptr_named_pipe != nullptr)
{
delete m_ptr_named_pipe;
}
}
void ProgressWorker::setInputFileName(const QString file_name, bool named)
{
this->m_file_name = file_name;
this->m_named = named;
}
void ProgressWorker::setHandler(progress_handler handler)
{
m_handler = handler;
}
bool ProgressWorker::isBlocked()
{
return m_blocked;
}
bool ProgressWorker::isPipeExist()
{
if (this->m_ptr_named_pipe == nullptr)
{
return false;
}
return this->m_ptr_named_pipe->isExist();
}
bool ProgressWorker::createPipe()
{
if (this->m_ptr_named_pipe != nullptr)
{
delete this->m_ptr_named_pipe;
}
this->m_ptr_named_pipe = Factory::createNamedPipeObject(this->m_file_name);
if (this->m_ptr_named_pipe != nullptr)
{
m_ptr_named_pipe->setName(m_file_name);
return m_ptr_named_pipe->create();
}
return false;
}
QString ProgressWorker::readPipe()
{
if (!isPipeExist())
{
return QString("");
}
m_blocked = true;
emit beforeBlock(m_file_name);
// this will block the thread.
QString result = m_ptr_named_pipe->pipeRead();
emit afterBlock(m_file_name);
m_blocked = false;
return result;
}
bool ProgressWorker::deletePipe()
{
if (!isPipeExist())
{
return true;
}
return m_ptr_named_pipe->deletePipe();
}
void ProgressWorker::startJob()
{
emit started();
set_thread_name(m_file_name + " thread");
if (m_handler == nullptr)
{
return;
}
m_handler(this);
}
progressState ProgressWorker::parseForProgress(const QString &message)
{
progressState progress_state;
QRegularExpression re("^Step (\\d+) of (\\d+)$");
QRegularExpressionMatch match = re.match(message);
bool hasMatch = match.hasMatch(); // true
if (!hasMatch)
{
progress_state.match = false;
return progress_state;
}
progress_state.current = match.captured(1).toInt();
progress_state.total = match.captured(2).toInt();
if (progress_state.total == 0)
{
progress_state.match = false;
return progress_state;
}
progress_state.progress = float(progress_state.current) / float(progress_state.total);
return progress_state;
}
bool ProgressWorker::isFinishing()
{
return m_finishing;
}
void ProgressWorker::endJob()
{
qDebug() << "ending job" << endl;
m_finishing = true;
deletePipe();
if (m_ptr_named_pipe != nullptr)
{
delete m_ptr_named_pipe;
m_ptr_named_pipe = nullptr;
}
QThread::currentThread()->quit();
emit finished();
}