-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupgradeablemodel.cpp
126 lines (107 loc) · 3.2 KB
/
upgradeablemodel.cpp
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
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Kate Leet <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "upgradeablemodel.h"
static QString formatByteSize(double size, int precision)
{
int unit = 0;
double multiplier = 1024.0;
while (qAbs(size) >= multiplier && unit < int(8)) {
size /= multiplier;
++unit;
}
if (unit == 0) {
precision = 0;
}
QString numString = QString::number(size, 'f', precision);
switch (unit) {
case 0:
return QString("%1 B").arg(numString);
case 1:
return QString("%1 KB").arg(numString);
case 2:
return QString("%1 MB").arg(numString);
case 3:
return QString("%1 GB").arg(numString);
case 4:
return QString("%1 TB").arg(numString);
case 5:
return QString("%1 PB").arg(numString);
case 6:
return QString("%1 EB").arg(numString);
case 7:
return QString("%1 ZB").arg(numString);
case 8:
return QString("%1 YB").arg(numString);
default:
return QString();
}
return QString();
}
UpgradeableModel *UpgradeableModel::self()
{
static UpgradeableModel s;
return &s;
}
UpgradeableModel::UpgradeableModel(QObject *parent)
: QAbstractListModel(parent)
{
}
QHash<int, QByteArray> UpgradeableModel::roleNames() const
{
static QHash<int, QByteArray> s_roles;
if (s_roles.isEmpty()) {
s_roles.insert(UpgradeableModel::NameRole, QByteArrayLiteral("name"));
s_roles.insert(UpgradeableModel::VersionRole, QByteArrayLiteral("version"));
s_roles.insert(UpgradeableModel::DownloadSize, QByteArrayLiteral("downloadSize"));
}
return s_roles;
}
int UpgradeableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_packages.count();
}
QVariant UpgradeableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
PackageInfo info = m_packages.at(index.row());
switch (role) {
case UpgradeableModel::NameRole:
return info.name;
case UpgradeableModel::VersionRole:
return info.version;
case UpgradeableModel::DownloadSize:
return formatByteSize(info.downloadSize, 1);
default:
break;
}
// FIXME: Implement me!
return QVariant();
}
void UpgradeableModel::addPackage(const QString &name, const QString &version, quint64 downloadSize)
{
beginInsertRows(QModelIndex(), 0, 0);
PackageInfo info;
info.name = name;
info.version = version;
info.downloadSize = downloadSize;
m_packages.prepend(info);
endInsertRows();
}