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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "androidmanager.h"
#include "androidsdkmanager.h"
#include "androidsdkmodel.h"
#include "androidtr.h"
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/utilsicons.h>
#include <QIcon>
#include <QLoggingCategory>
namespace {
static Q_LOGGING_CATEGORY(androidSdkModelLog, "qtc.android.sdkmodel", QtWarningMsg)
}
namespace Android {
namespace Internal {
const int packageColCount = 3;
AndroidSdkModel::AndroidSdkModel(const AndroidConfig &config, AndroidSdkManager *sdkManager,
QObject *parent)
: QAbstractItemModel(parent),
m_config(config),
m_sdkManager(sdkManager)
{
QTC_CHECK(m_sdkManager);
connect(m_sdkManager, &AndroidSdkManager::packageReloadBegin, this, [this] {
clearContainers();
beginResetModel();
});
connect(m_sdkManager, &AndroidSdkManager::packageReloadFinished, this, [this] {
refreshData();
endResetModel();
});
}
QVariant AndroidSdkModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation)
QVariant data;
if (role == Qt::DisplayRole) {
switch (section) {
case packageNameColumn:
data = Tr::tr("Package");
break;
case packageRevisionColumn:
data = Tr::tr("Revision");
break;
case apiLevelColumn:
data = Tr::tr("API");
break;
default:
break;
}
}
return data;
}
QModelIndex AndroidSdkModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid()) {
// Packages under top items.
if (parent.row() == 0) {
// Tools packages
if (row < m_tools.count())
return createIndex(row, column, const_cast<AndroidSdkPackage *>(m_tools.at(row)));
} else if (parent.row() < m_sdkPlatforms.count() + 1) {
// Platform packages
const SdkPlatform *sdkPlatform = m_sdkPlatforms.at(parent.row() - 1);
SystemImageList images = sdkPlatform->systemImages(AndroidSdkPackage::AnyValidState);
if (row < images.count() + 1) {
if (row == 0)
return createIndex(row, column, const_cast<SdkPlatform *>(sdkPlatform));
else
return createIndex(row, column, images.at(row - 1));
}
}
} else if (row < m_sdkPlatforms.count() + 1) {
return createIndex(row, column); // Top level items (Tools & platform)
}
return QModelIndex();
}
QModelIndex AndroidSdkModel::parent(const QModelIndex &index) const
{
void *ip = index.internalPointer();
if (!ip)
return QModelIndex();
auto package = static_cast<const AndroidSdkPackage *>(ip);
if (package->type() == AndroidSdkPackage::SystemImagePackage) {
auto image = static_cast<const SystemImage *>(package);
int row = m_sdkPlatforms.indexOf(const_cast<SdkPlatform *>(image->platform()));
if (row > -1)
return createIndex(row + 1, 0);
} else if (package->type() == AndroidSdkPackage::SdkPlatformPackage) {
int row = m_sdkPlatforms.indexOf(static_cast<const SdkPlatform *>(package));
if (row > -1)
return createIndex(row + 1, 0);
} else {
return createIndex(0, 0); // Tools
}
return QModelIndex();
}
int AndroidSdkModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return m_sdkPlatforms.count() + 1;
if (!parent.internalPointer()) {
if (parent.row() == 0) // Tools
return m_tools.count();
if (parent.row() <= m_sdkPlatforms.count()) {
const SdkPlatform * platform = m_sdkPlatforms.at(parent.row() - 1);
return platform->systemImages(AndroidSdkPackage::AnyValidState).count() + 1;
}
}
return 0;
}
int AndroidSdkModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return packageColCount;
}
QVariant AndroidSdkModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (!index.parent().isValid()) {
// Top level tools
if (index.row() == 0) {
return role == Qt::DisplayRole && index.column() == packageNameColumn ?
QVariant(Tr::tr("Tools")) : QVariant();
}
// Top level platforms
const SdkPlatform *platform = m_sdkPlatforms.at(index.row() - 1);
if (role == Qt::DisplayRole) {
if (index.column() == packageNameColumn) {
QString androidName = AndroidManager::androidNameForApiLevel(platform->apiLevel());
if (androidName.startsWith("Android"))
return androidName;
else
return platform->displayText();
} else if (index.column() == apiLevelColumn) {
return platform->apiLevel();
}
}
return QVariant();
}
auto p = static_cast<const AndroidSdkPackage *>(index.internalPointer());
QString apiLevelStr;
if (p->type() == AndroidSdkPackage::SdkPlatformPackage)
apiLevelStr = QString::number(static_cast<const SdkPlatform *>(p)->apiLevel());
if (p->type() == AndroidSdkPackage::SystemImagePackage)
apiLevelStr = QString::number(static_cast<const SystemImage *>(p)->platform()->apiLevel());
if (role == Qt::DisplayRole) {
switch (index.column()) {
case packageNameColumn:
return p->type() == AndroidSdkPackage::SdkPlatformPackage ?
Tr::tr("SDK Platform") : p->displayText();
case packageRevisionColumn:
return p->revision().toString();
case apiLevelColumn:
return apiLevelStr;
default:
break;
}
}
if (index.column() == packageNameColumn) {
if (role == Qt::CheckStateRole) {
if (p->state() == AndroidSdkPackage::Installed)
return m_changeState.contains(p) ? Qt::Unchecked : Qt::Checked;
else
return m_changeState.contains(p) ? Qt::Checked : Qt::Unchecked;
}
if (role == Qt::FontRole) {
QFont font;
if (m_changeState.contains(p))
font.setBold(true);
return font;
}
}
if (role == Qt::TextAlignmentRole && index.column() == packageRevisionColumn)
return Qt::AlignRight;
if (role == Qt::ToolTipRole)
return QString("%1 - (%2)").arg(p->descriptionText()).arg(p->sdkStylePath());
if (role == PackageTypeRole)
return p->type();
if (role == PackageStateRole)
return p->state();
return QVariant();
}
QHash<int, QByteArray> AndroidSdkModel::roleNames() const
{
QHash <int, QByteArray> roles;
roles[PackageTypeRole] = "PackageRole";
roles[PackageStateRole] = "PackageState";
return roles;
}
Qt::ItemFlags AndroidSdkModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags f = QAbstractItemModel::flags(index);
if (index.column() == packageNameColumn)
f |= Qt::ItemIsUserCheckable;
void *ip = index.internalPointer();
if (ip && index.column() == packageNameColumn) {
auto package = static_cast<const AndroidSdkPackage *>(ip);
if (package->state() == AndroidSdkPackage::Installed
&& package->type() == AndroidSdkPackage::SdkToolsPackage) {
f &= ~Qt::ItemIsEnabled;
}
}
return f;
}
bool AndroidSdkModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
void *ip = index.internalPointer();
if (ip && role == Qt::CheckStateRole) {
auto package = static_cast<const AndroidSdkPackage *>(ip);
if (value.toInt() == Qt::Checked && package->state() != AndroidSdkPackage::Installed) {
m_changeState << package;
emit dataChanged(index, index, {Qt::CheckStateRole});
} else if (m_changeState.remove(package)) {
emit dataChanged(index, index, {Qt::CheckStateRole});
} else if (value.toInt() == Qt::Unchecked) {
m_changeState.insert(package);
emit dataChanged(index, index, {Qt::CheckStateRole});
}
return true;
}
return false;
}
void AndroidSdkModel::selectMissingEssentials()
{
resetSelection();
QStringList pendingPkgs(m_config.allEssentials());
auto addTool = [this](QList<const AndroidSdkPackage *>::const_iterator itr) {
if ((*itr)->installedLocation().isEmpty()) {
m_changeState << *itr;
auto i = index(std::distance(m_tools.cbegin(), itr), 0, index(0, 0));
emit dataChanged(i, i, {Qt::CheckStateRole});
}
};
for (auto tool = m_tools.cbegin(); tool != m_tools.cend(); ++tool) {
if (!pendingPkgs.contains((*tool)->sdkStylePath()))
continue;
addTool(tool);
pendingPkgs.removeOne((*tool)->sdkStylePath());
if (pendingPkgs.isEmpty())
break;
}
// Select SDK platform
for (const SdkPlatform *platform : std::as_const(m_sdkPlatforms)) {
if (!platform->installedLocation().isEmpty()) {
pendingPkgs.removeOne(platform->sdkStylePath());
} else if (pendingPkgs.contains(platform->sdkStylePath()) &&
platform->installedLocation().isEmpty()) {
auto i = index(0, 0, index(1, 0));
m_changeState << platform;
emit dataChanged(i, i, {Qt::CheckStateRole});
pendingPkgs.removeOne(platform->sdkStylePath());
}
if (pendingPkgs.isEmpty())
break;
}
m_missingEssentials = pendingPkgs;
if (!m_missingEssentials.isEmpty())
qCDebug(androidSdkModelLog) << "Couldn't find some essential packages:" << m_missingEssentials;
}
QList<const AndroidSdkPackage *> AndroidSdkModel::userSelection() const
{
return Utils::toList(m_changeState);
}
void AndroidSdkModel::resetSelection()
{
beginResetModel();
m_changeState.clear();
endResetModel();
}
void AndroidSdkModel::clearContainers()
{
m_sdkPlatforms.clear();
m_tools.clear();
m_changeState.clear();
}
void AndroidSdkModel::refreshData()
{
clearContainers();
for (AndroidSdkPackage *p : m_sdkManager->allSdkPackages()) {
if (p->type() == AndroidSdkPackage::SdkPlatformPackage)
m_sdkPlatforms << static_cast<SdkPlatform *>(p);
else
m_tools << p;
}
Utils::sort(m_sdkPlatforms, [](const SdkPlatform *p1, const SdkPlatform *p2) {
return p1->apiLevel() > p2->apiLevel();
});
Utils::sort(m_tools, [](const AndroidSdkPackage *p1, const AndroidSdkPackage *p2) {
if (p1->state() == p2->state()) {
if (p1->type() == p2->type())
return p1->revision() > p2->revision();
else
return p1->type() > p2->type();
} else {
return p1->state() < p2->state();
}
});
}
} // namespace Internal
} // namespace Android
|