File: TableDelegate.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,976 kB
  • ctags: 35,666
  • sloc: cpp: 213,139; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (94 lines) | stat: -rw-r--r-- 2,736 bytes parent folder | download | duplicates (2)
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
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <QPainter>

#include "DolphinQt2/Resources.h"
#include "DolphinQt2/GameList/GameListModel.h"
#include "DolphinQt2/GameList/TableDelegate.h"

static QSize NORMAL_BANNER_SIZE(96, 32);

// Convert an integer size to a friendly string representation.
static QString FormatSize(qint64 size)
{
	QStringList units{
		QStringLiteral("KB"),
		QStringLiteral("MB"),
		QStringLiteral("GB"),
		QStringLiteral("TB")
	};
	QStringListIterator i(units);
	QString unit = QStringLiteral("B");
	double num = (double) size;
	while (num > 1024.0 && i.hasNext())
	{
		unit = i.next();
		num /= 1024.0;
	}
	return QStringLiteral("%1 %2").arg(QString::number(num, 'f', 1)).arg(unit);
}

TableDelegate::TableDelegate(QWidget* parent) : QStyledItemDelegate(parent)
{
}

void TableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	QVariant data = index.data(Qt::DisplayRole);
	switch (index.column())
	{
	case GameListModel::COL_PLATFORM:
		DrawPixmap(painter, option.rect, Resources::GetPlatform(data.toInt()));
		break;
	case GameListModel::COL_COUNTRY:
		DrawPixmap(painter, option.rect, Resources::GetCountry(data.toInt()));
		break;
	case GameListModel::COL_RATING:
		DrawPixmap(painter, option.rect, Resources::GetRating(data.toInt()));
		break;
	case GameListModel::COL_BANNER:
		DrawPixmap(painter, option.rect, data.value<QPixmap>().scaled(
					NORMAL_BANNER_SIZE,
					Qt::KeepAspectRatio,
					Qt::SmoothTransformation));
		break;
	case GameListModel::COL_SIZE:
		painter->drawText(option.rect, Qt::AlignCenter, FormatSize(data.toULongLong()));
		break;
	// Fall through.
	case GameListModel::COL_ID:
	case GameListModel::COL_TITLE:
	case GameListModel::COL_DESCRIPTION:
	case GameListModel::COL_MAKER:
		painter->drawText(option.rect, Qt::AlignVCenter, data.toString());
		break;
	default: break;
	}
}

QSize TableDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	switch (index.column())
	{
	case GameListModel::COL_PLATFORM:
		return Resources::GetPlatform(0).size();
	case GameListModel::COL_COUNTRY:
		return Resources::GetCountry(0).size();
	case GameListModel::COL_RATING:
		return Resources::GetRating(0).size();
	case GameListModel::COL_BANNER:
		return NORMAL_BANNER_SIZE;
	default: return QSize(0, 0);
	}
}

void TableDelegate::DrawPixmap(QPainter* painter, const QRect& rect, const QPixmap& pixmap) const
{
	// We don't want to stretch the pixmap out, so center it in the rect.
	painter->drawPixmap(
			rect.left() + (rect.width() - pixmap.width()) / 2,
			rect.top() + (rect.height() - pixmap.height()) / 2,
			pixmap);
}