1 // Copyright 2015 Dolphin Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4
5 #include "DolphinQt/GameList/GridProxyModel.h"
6
7 #include <QImage>
8 #include <QPainter>
9 #include <QPixmap>
10 #include <QSize>
11
12 #include "DolphinQt/GameList/GameListModel.h"
13
14 #include "Core/Config/UISettings.h"
15
16 #include "UICommon/GameFile.h"
17
18 const QSize LARGE_BANNER_SIZE(144, 48);
19
GridProxyModel(QObject * parent)20 GridProxyModel::GridProxyModel(QObject* parent) : QSortFilterProxyModel(parent)
21 {
22 setSortCaseSensitivity(Qt::CaseInsensitive);
23 sort(GameListModel::COL_TITLE);
24 }
25
data(const QModelIndex & i,int role) const26 QVariant GridProxyModel::data(const QModelIndex& i, int role) const
27 {
28 QModelIndex source_index = mapToSource(i);
29 if (role == Qt::DisplayRole)
30 {
31 return sourceModel()->data(sourceModel()->index(source_index.row(), GameListModel::COL_TITLE),
32 Qt::DisplayRole);
33 }
34 else if (role == Qt::DecorationRole)
35 {
36 auto* model = static_cast<GameListModel*>(sourceModel());
37
38 const auto& buffer = model->GetGameFile(source_index.row())->GetCoverImage().buffer;
39
40 QSize size = Config::Get(Config::MAIN_USE_GAME_COVERS) ? QSize(160, 224) : LARGE_BANNER_SIZE;
41 QPixmap pixmap(size * model->GetScale() * QPixmap().devicePixelRatio());
42
43 if (buffer.empty() || !Config::Get(Config::MAIN_USE_GAME_COVERS))
44 {
45 QPixmap banner = model
46 ->data(model->index(source_index.row(), GameListModel::COL_BANNER),
47 Qt::DecorationRole)
48 .value<QPixmap>();
49
50 banner = banner.scaled(pixmap.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
51
52 pixmap.fill();
53
54 QPainter painter(&pixmap);
55
56 painter.drawPixmap(0, pixmap.height() / 2 - banner.height() / 2, banner.width(),
57 banner.height(), banner);
58
59 return pixmap;
60 }
61 else
62 {
63 pixmap = QPixmap::fromImage(QImage::fromData(
64 reinterpret_cast<const unsigned char*>(&buffer[0]), static_cast<int>(buffer.size())));
65
66 return pixmap.scaled(QSize(160, 224) * model->GetScale() * pixmap.devicePixelRatio(),
67 Qt::KeepAspectRatio, Qt::SmoothTransformation);
68 }
69 }
70 return QVariant();
71 }
72
filterAcceptsRow(int source_row,const QModelIndex & source_parent) const73 bool GridProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
74 {
75 GameListModel* glm = qobject_cast<GameListModel*>(sourceModel());
76 return glm->ShouldDisplayGameListItem(source_row);
77 }
78