1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "taskmodel.h"
6
7#include <QFontMetrics>
8
9TaskModel::TaskModel(QObject *parent) : QAbstractItemModel(parent)
10{
11
12}
13
14QModelIndex TaskModel::index(int row, int column, const QModelIndex &parent) const
15{
16 if (parent.isValid())
17 return QModelIndex();
18 return createIndex(row, column);
19}
20
21QModelIndex TaskModel::parent(const QModelIndex &child) const
22{
23 Q_UNUSED(child)
24 return QModelIndex();
25}
26
27int TaskModel::rowCount(const QModelIndex &parent) const
28{
29 return parent.isValid() ? 0 : tasks.count();
30}
31
32int TaskModel::columnCount(const QModelIndex &parent) const
33{
34 return parent.isValid() ? 0 : 1;
35}
36
37QVariant TaskModel::data(const QModelIndex &index, int role) const
38{
39 int row = index.row();
40 if (!index.isValid() || row < 0 || row >= tasks.count() || index.column() != 0)
41 return QVariant();
42
43 if (role == TaskModel::File)
44 return tasks.at(index.row()).file.toString();
45 else if (role == TaskModel::Line)
46 return tasks.at(index.row()).line;
47 else if (role == TaskModel::MovedLine)
48 return tasks.at(index.row()).movedLine;
49 else if (role == TaskModel::Description)
50 return tasks.at(index.row()).description;
51 else if (role == TaskModel::Type)
52 return static_cast<int>(tasks.at(index.row()).type);
53 else if (role == TaskModel::Icon)
54 return tasks.at(index.row()).icon;
55 else if (role == TaskModel::Task_t)
56 return QVariant::fromValue(task(index));
57 return QVariant();
58}
59
60Task TaskModel::task(const QModelIndex &index) const
61{
62 int row = index.row();
63 if (!index.isValid() || row < 0 || row >= tasks.count())
64 return Task();
65 return tasks.at(row);
66}
67
68QList<Task> TaskModel::getTasks() const
69{
70 return tasks;
71}
72
73bool sortById(const Task &task, unsigned int id)
74{
75 return task.taskId < id;
76}
77
78void TaskModel::addTask(const Task &task)
79{
80 auto it = std::lower_bound(tasks.begin(), tasks.end(),task.taskId, sortById);
81 int i = it - tasks.begin();
82 beginInsertRows(QModelIndex(), i, i);
83 tasks.insert(it, task);
84 endInsertRows();
85}
86
87void TaskModel::removeTask(const Task &task)
88{
89 int index = tasks.indexOf(task);
90 if (index >= 0) {
91 beginRemoveRows(QModelIndex(), index, index);
92 tasks.removeAt(index);
93 endRemoveRows();
94 }
95}
96
97void TaskModel::clearTasks()
98{
99 if (tasks.isEmpty())
100 return;
101
102 beginRemoveRows(QModelIndex(), 0, tasks.count() -1);
103 tasks.clear();
104 endRemoveRows();
105}
106
107int TaskModel::taskCount()
108{
109 return tasks.count();
110}
111
112int TaskModel::sizeOfFile(const QFont &font)
113{
114 int count = tasks.count();
115 if (count == 0)
116 return 0;
117
118 if (maxSizeOfFileName > 0 && font == fileMeasurementFont && lastMaxSizeIndex == count - 1)
119 return maxSizeOfFileName;
120
121 QFontMetrics fm(font);
122 fileMeasurementFont = font;
123
124 for (int i = lastMaxSizeIndex; i < count; ++i) {
125 QString filename = tasks.at(i).file.toString();
126 const int pos = filename.lastIndexOf(QLatin1Char('/'));
127 if (pos != -1)
128 filename = filename.mid(pos +1);
129
130 maxSizeOfFileName = qMax(maxSizeOfFileName, fm.horizontalAdvance(filename));
131 }
132 lastMaxSizeIndex = count - 1;
133 return maxSizeOfFileName;
134}
135
136int TaskModel::getSizeOfLineNumber(const QFont &font)
137{
138 if (sizeOfLineNumber == 0 || font != lineMeasurementFont) {
139 QFontMetrics fm(font);
140 lineMeasurementFont = font;
141 const char *maxLineNumber = "99999";
142 sizeOfLineNumber = fm.horizontalAdvance(QLatin1String(maxLineNumber));
143 }
144 return sizeOfLineNumber;
145}
146