summaryrefslogtreecommitdiffstats
path: root/application/VersionProxyModel.cpp
blob: 990b949e8999475563e94fe9decd4e55fae80a3a (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "VersionProxyModel.h"
#include "MultiMC.h"
#include <QSortFilterProxyModel>
#include <QPixmapCache>
#include <Version.h>
#include <meta/VersionList.h>

class VersionFilterModel : public QSortFilterProxyModel
{
	Q_OBJECT
public:
	VersionFilterModel(VersionProxyModel *parent) : QSortFilterProxyModel(parent)
	{
		m_parent = parent;
		setSortRole(BaseVersionList::SortRole);
		sort(0, Qt::DescendingOrder);
	}

	bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
	{
		const auto &filters = m_parent->filters();
		for (auto it = filters.begin(); it != filters.end(); ++it)
		{
			auto idx = sourceModel()->index(source_row, 0, source_parent);
			auto data = sourceModel()->data(idx, it.key());
			auto match = data.toString();
			if(!it.value()->accepts(match))
			{
				return false;
			}
		}
		return true;
	}

	void filterChanged()
	{
		invalidateFilter();
	}
private:
	VersionProxyModel *m_parent;
};

VersionProxyModel::VersionProxyModel(QObject *parent) : QAbstractProxyModel(parent)
{
	filterModel = new VersionFilterModel(this);
	connect(filterModel, &QAbstractItemModel::dataChanged, this, &VersionProxyModel::sourceDataChanged);
	connect(filterModel, &QAbstractItemModel::rowsAboutToBeInserted, this, &VersionProxyModel::sourceRowsAboutToBeInserted);
	connect(filterModel, &QAbstractItemModel::rowsInserted, this, &VersionProxyModel::sourceRowsInserted);
	connect(filterModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &VersionProxyModel::sourceRowsAboutToBeRemoved);
	connect(filterModel, &QAbstractItemModel::rowsRemoved, this, &VersionProxyModel::sourceRowsRemoved);
	// FIXME: implement when needed
	/*
	connect(replacing, &QAbstractItemModel::rowsAboutToBeMoved, this, &VersionProxyModel::sourceRowsAboutToBeMoved);
	connect(replacing, &QAbstractItemModel::rowsMoved, this, &VersionProxyModel::sourceRowsMoved);
	connect(replacing, &QAbstractItemModel::layoutAboutToBeChanged, this, &VersionProxyModel::sourceLayoutAboutToBeChanged);
	connect(replacing, &QAbstractItemModel::layoutChanged, this, &VersionProxyModel::sourceLayoutChanged);
	*/
	connect(filterModel, &QAbstractItemModel::modelAboutToBeReset, this, &VersionProxyModel::sourceAboutToBeReset);
	connect(filterModel, &QAbstractItemModel::modelReset, this, &VersionProxyModel::sourceReset);

	QAbstractProxyModel::setSourceModel(filterModel);
}

QVariant VersionProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if(section < 0 || section >= m_columns.size())
		return QVariant();
	if(orientation != Qt::Horizontal)
		return QVariant();
	auto column = m_columns[section];
	if(role == Qt::DisplayRole)
	{
		switch(column)
		{
			case Name:
				return tr("Version");
			case ParentVersion:
				return tr("Minecraft"); //FIXME: this should come from metadata
			case Branch:
				return tr("Branch");
			case Type:
				return tr("Type");
			case Architecture:
				return tr("Architecture");
			case Path:
				return tr("Path");
			case Time:
				return tr("Released");
		}
	}
	else if(role == Qt::ToolTipRole)
	{
		switch(column)
		{
			case Name:
				return tr("The name of the version.");
			case ParentVersion:
				return tr("Minecraft version"); //FIXME: this should come from metadata
			case Branch:
				return tr("The version's branch");
			case Type:
				return tr("The version's type");
			case Architecture:
				return tr("CPU Architecture");
			case Path:
				return tr("Filesystem path to this version");
			case Time:
				return tr("Release date of this version");
		}
	}
	return QVariant();
}

QVariant VersionProxyModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())
	{
		return QVariant();
	}
	auto column = m_columns[index.column()];
	auto parentIndex = mapToSource(index);
	switch(role)
	{
		case Qt::DisplayRole:
		{
			switch(column)
			{
				case Name:
					return sourceModel()->data(parentIndex, BaseVersionList::VersionRole);
				case ParentVersion:
					return sourceModel()->data(parentIndex, BaseVersionList::ParentVersionRole);
				case Branch:
					return sourceModel()->data(parentIndex, BaseVersionList::BranchRole);
				case Type:
					return sourceModel()->data(parentIndex, BaseVersionList::TypeRole);
				case Architecture:
					return sourceModel()->data(parentIndex, BaseVersionList::ArchitectureRole);
				case Path:
					return sourceModel()->data(parentIndex, BaseVersionList::PathRole);
				case Time:
					return sourceModel()->data(parentIndex, Meta::VersionList::TimeRole).toDate();
				default:
					return QVariant();
			}
		}
		case Qt::ToolTipRole:
		{
			switch(column)
			{
				case Name:
				{
					if(hasRecommended)
					{
						auto value = sourceModel()->data(parentIndex, BaseVersionList::RecommendedRole);
						if(value.toBool())
						{
							return tr("Recommended");
						}
						else if(hasLatest)
						{
							auto value = sourceModel()->data(parentIndex, BaseVersionList::LatestRole);
							if(value.toBool())
							{
								return tr("Latest");
							}
						}
						else if(index.row() == 0)
						{
							return tr("Latest");
						}
					}
				}
				default:
				{
					return sourceModel()->data(parentIndex, BaseVersionList::VersionIdRole);
				}
			}
		}
		case Qt::DecorationRole:
		{
			switch(column)
			{
				case Name:
				{
					if(hasRecommended)
					{
						auto value = sourceModel()->data(parentIndex, BaseVersionList::RecommendedRole);
						if(value.toBool())
						{
							return MMC->getThemedIcon("star");
						}
						else if(hasLatest)
						{
							auto value = sourceModel()->data(parentIndex, BaseVersionList::LatestRole);
							if(value.toBool())
							{
								return MMC->getThemedIcon("bug");
							}
						}
						else if(index.row() == 0)
						{
							return MMC->getThemedIcon("bug");
						}
						auto pixmap = QPixmapCache::find("placeholder");
						if(!pixmap)
						{
							QPixmap px(16,16);
							px.fill(Qt::transparent);
							QPixmapCache::insert("placeholder", px);
							return px;
						}
						return *pixmap;
					}
				}
				default:
				{
					return QVariant();
				}
			}
		}
		default:
		{
			if(roles.contains((BaseVersionList::ModelRoles)role))
			{
				return sourceModel()->data(parentIndex, role);
			}
			return QVariant();
		}
	}
}

QModelIndex VersionProxyModel::parent(const QModelIndex &child) const
{
	return QModelIndex();
}

QModelIndex VersionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
	if(sourceIndex.isValid())
	{
		return index(sourceIndex.row(), 0);
	}
	return QModelIndex();
}

QModelIndex VersionProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
	if(proxyIndex.isValid())
	{
		return sourceModel()->index(proxyIndex.row(), 0);
	}
	return QModelIndex();
}

QModelIndex VersionProxyModel::index(int row, int column, const QModelIndex &parent) const
{
	// no trees here... shoo
	if(parent.isValid())
	{
		return QModelIndex();
	}
	if(row < 0 || row >= sourceModel()->rowCount())
		return QModelIndex();
	if(column < 0 || column >= columnCount())
		return QModelIndex();
	return QAbstractItemModel::createIndex(row, column);
}

int VersionProxyModel::columnCount(const QModelIndex &parent) const
{
	return m_columns.size();
}

int VersionProxyModel::rowCount(const QModelIndex &parent) const
{
	if(sourceModel())
	{
		return sourceModel()->rowCount();
	}
	return 0;
}

void VersionProxyModel::sourceDataChanged(const QModelIndex &source_top_left,
										  const QModelIndex &source_bottom_right)
{
	if(source_top_left.parent() != source_bottom_right.parent())
		return;

	// whole row is getting changed
	auto topLeft = createIndex(source_top_left.row(), 0);
	auto bottomRight = createIndex(source_bottom_right.row(), columnCount() - 1);
	emit dataChanged(topLeft, bottomRight);
}

void VersionProxyModel::setSourceModel(QAbstractItemModel *replacingRaw)
{
	auto replacing = dynamic_cast<BaseVersionList *>(replacingRaw);
	beginResetModel();

	m_columns.clear();
	if(!replacing)
	{
		roles.clear();
		filterModel->setSourceModel(replacing);
		return;
	}

	roles = replacing->providesRoles();
	if(roles.contains(BaseVersionList::VersionRole))
	{
		m_columns.push_back(Name);
	}
	/*
	if(roles.contains(BaseVersionList::ParentVersionRole))
	{
		m_columns.push_back(ParentVersion);
	}
	*/
	if(roles.contains(BaseVersionList::ArchitectureRole))
	{
		m_columns.push_back(Architecture);
	}
	if(roles.contains(BaseVersionList::PathRole))
	{
		m_columns.push_back(Path);
	}
	if(roles.contains(Meta::VersionList::TimeRole))
	{
		m_columns.push_back(Time);
	}
	if(roles.contains(BaseVersionList::BranchRole))
	{
		m_columns.push_back(Branch);
	}
	if(roles.contains(BaseVersionList::TypeRole))
	{
		m_columns.push_back(Type);
	}
	if(roles.contains(BaseVersionList::RecommendedRole))
	{
		hasRecommended = true;
	}
	if(roles.contains(BaseVersionList::LatestRole))
	{
		hasLatest = true;
	}
	filterModel->setSourceModel(replacing);

	endResetModel();
}

QModelIndex VersionProxyModel::getRecommended() const
{
	if(!roles.contains(BaseVersionList::RecommendedRole))
	{
		return index(0, 0);
	}
	int recommended = 0;
	for (int i = 0; i < rowCount(); i++)
	{
		auto value = sourceModel()->data(mapToSource(index(i, 0)), BaseVersionList::RecommendedRole);
		if (value.toBool())
		{
			recommended = i;
		}
	}
	return index(recommended, 0);
}

QModelIndex VersionProxyModel::getVersion(const QString& version) const
{
	int found = -1;
	for (int i = 0; i < rowCount(); i++)
	{
		auto value = sourceModel()->data(mapToSource(index(i, 0)), BaseVersionList::VersionRole);
		if (value.toString() == version)
		{
			found = i;
		}
	}
	if(found == -1)
	{
		return QModelIndex();
	}
	return index(found, 0);
}

void VersionProxyModel::clearFilters()
{
	m_filters.clear();
	filterModel->filterChanged();
}

void VersionProxyModel::setFilter(const BaseVersionList::ModelRoles column, Filter * f)
{
	m_filters[column].reset(f);
	filterModel->filterChanged();
}

const VersionProxyModel::FilterMap &VersionProxyModel::filters() const
{
	return m_filters;
}

void VersionProxyModel::sourceAboutToBeReset()
{
	beginResetModel();
}

void VersionProxyModel::sourceReset()
{
	endResetModel();
}

void VersionProxyModel::sourceRowsAboutToBeInserted(const QModelIndex& parent, int first, int last)
{
	beginInsertRows(parent, first, last);
}

void VersionProxyModel::sourceRowsInserted(const QModelIndex& parent, int first, int last)
{
	endInsertRows();
}

void VersionProxyModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent, int first, int last)
{
	beginRemoveRows(parent, first, last);
}

void VersionProxyModel::sourceRowsRemoved(const QModelIndex& parent, int first, int last)
{
	endRemoveRows();
}


#include "VersionProxyModel.moc"