-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkersModel.cpp
More file actions
executable file
·107 lines (87 loc) · 2.57 KB
/
workersModel.cpp
File metadata and controls
executable file
·107 lines (87 loc) · 2.57 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
#include "workersModel.h"
//public:
WorkersModel::WorkersModel(QObject* parent)
: QAbstractListModel(parent)
{
}
void WorkersModel::addWorker(std::unique_ptr<Worker> worker)
{
connect(worker.get(), &Worker::taskFinished, this, [this](int workerId) {
updateWorker(workerId);
});
connect(worker.get(), &Worker::changeStatus, this, [this](int workerId) {
updateWorker(workerId);
});
std::lock_guard<std::mutex> lock(mutexWorkers);
beginInsertRows(QModelIndex(), workers.size(), workers.size());
workers.push_back(std::move(worker));
emit workersChanged();
endInsertRows();
}
void WorkersModel::updateWorker(int workerId)
{
for (int i = 0; i < workers.size(); ++i) {
if (workers[i]->getId() == workerId) {
emit dataChanged(index(i), index(i));
break;
}
}
}
Worker* WorkersModel::getFreeWorker()
{
std::lock_guard<std::mutex> lock(mutexWorkers);
for (const auto &worker : workers)
if (!worker->isRun())
return worker.get();
return nullptr;
}
std::vector<Worker*> WorkersModel::getAllWorkers() const
{
std::lock_guard<std::mutex> lock(mutexWorkers);
std::vector<Worker*> allWorkers;
for (const auto& worker : workers)
allWorkers.push_back(worker.get());
return allWorkers;
}
Worker* WorkersModel::searchWorkerByTaskId(int taskId)
{
std::lock_guard<std::mutex> lock(mutexWorkers);
for (const auto& worker : workers)
if (worker->getTaskId() == taskId)
return worker.get();
return nullptr;
}
int WorkersModel::countWorkersByStatus(const std::string &status) const
{
std::lock_guard<std::mutex> lock(mutexWorkers);
return std::count_if(workers.begin(), workers.end(), [&](const auto& worker) {
return worker->getStatus().find(status) != std::string::npos;
});
}
int WorkersModel::countWorkersAll() const
{
return workers.size();
}
//protected:
int WorkersModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return workers.size();
}
QVariant WorkersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= workers.size())
return QVariant();
const auto& worker = workers[index.row()];
switch (role) {
case IdRole: return worker->getId(); break;
case StatusRole: return QString::fromStdString(worker->getStatus()); break;
}
}
QHash<int, QByteArray> WorkersModel::roleNames() const
{
return {
{ IdRole, "workerId" },
{ StatusRole, "workerStatus" }
};
}