-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasksModel.cpp
More file actions
executable file
·162 lines (129 loc) · 4.39 KB
/
tasksModel.cpp
File metadata and controls
executable file
·162 lines (129 loc) · 4.39 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "tasksModel.h"
#include <iostream>
#include <QThread>
#include <QTimer>
//public:
TasksModel::TasksModel(QObject *parent) : QAbstractListModel(parent)
{
}
void TasksModel::addTask(std::unique_ptr<ITask> task)
{
std::lock_guard<std::mutex> lock(mutexTasks);
beginInsertRows(QModelIndex(), tasks.size(), tasks.size());
tasks.push_back(std::move(task));
sortTasksByStatus();
emit tasksChanged();
emit progressChanged(getOverallProgress());
endInsertRows();
}
void TasksModel::deleteTask(int taskId)
{
auto it = std::find_if(tasks.begin(), tasks.end(), [taskId](const auto& task) {
return task->getId() == taskId; });
if (it != tasks.end()) {
int index = std::distance(tasks.begin(), it);
std::lock_guard<std::mutex> lock(mutexTasks);
beginRemoveRows(QModelIndex(), index, index);
tasks.erase(it);
endRemoveRows();
emit tasksChanged();
emit progressChanged(getOverallProgress());
}
}
int TasksModel::getOverallProgress() const
{
if (tasks.empty()) return 0;
int totalProgress = 0;
for (const auto& task : tasks)
totalProgress += task->getProgress();
return std::min(100, static_cast<int>(totalProgress / tasks.size()));;
}
void TasksModel::updateTask(int taskId)
{
for (int i = 0; i < tasks.size(); ++i) {
if (tasks[i]->getId() == taskId) {
emit dataChanged(index(i), index(i));
emit progressChanged(getOverallProgress());
emit tasksChanged();
break;
}
}
}
void TasksModel::sortTasksByStatus()
{
//TODO вернись потом если время останется
// std::sort(tasks.begin(), tasks.end(), [](const std::unique_ptr<ITask> &a, const std::unique_ptr<ITask> &b) {
// const std::string statusA = a->getStatus();
// const std::string statusB = b->getStatus();
// auto getStatusPriority = [](const std::string &status) {
// if (status.find("Выполняет исполнитель") != std::string::npos)
// return 0;
// if (status == "Ожидает")
// return 1;
// if (status == "Завершено")
// return 2;
// return 3; // На случай неизвестного статуса
// };
// int priorityA = getStatusPriority(statusA);
// int priorityB = getStatusPriority(statusB);
// if (priorityA != priorityB)
// return priorityA < priorityB;
// return a->getId() < b->getId();
// });
// emit dataChanged(index(0), index(tasks.size() - 1));
}
ITask* TasksModel::getFreeTask()
{
std::lock_guard<std::mutex> lock(mutexTasks);
for (const auto& task : tasks)
if (task->getStatus() == "Ожидает")
return task.get();
return nullptr;
}
ITask* TasksModel::getTaskByTaskId (int taskId)
{
std::lock_guard<std::mutex> lock(mutexTasks);
for (const auto& task : tasks)
if (task->getId() == taskId)
return task.get();
return nullptr;
}
int TasksModel::countTasksByStatus(const std::string &status) const
{
return std::count_if(tasks.begin(), tasks.end(), [&](const auto& task) {
return task->getStatus().find(status) != std::string::npos;
});
}
std::vector<ITask*> TasksModel::getAllTasks() const
{
std::vector<ITask*> allTasks;
for (const auto& task : tasks)
allTasks.push_back(task.get());
return allTasks;
}
//protected:
QHash<int, QByteArray> TasksModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TaskIdRole] = "taskId";
roles[ProgressRole] = "progress";
roles[StatusRole] = "status";
roles[TypeRole] = "type";
return roles;
}
int TasksModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : tasks.size();
}
QVariant TasksModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= tasks.size()) return QVariant();
const auto& task = tasks[index.row()];
switch (role) {
case TaskIdRole: return task->getId(); break;
case ProgressRole: return task->getProgress(); break;
case StatusRole: return QString::fromStdString(task->getStatus()); break;
case TypeRole: return QString::fromStdString(task->getType()); break;
}
return QVariant();
}