| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/GameList/GridProxyModel.h" |
| 5 | |
| 6 | #include <QImage> |
| 7 | #include <QPainter> |
| 8 | #include <QPixmap> |
| 9 | #include <QSize> |
| 10 | |
| 11 | #include "DolphinQt/GameList/GameListModel.h" |
| 12 | |
| 13 | #include "Core/Config/UISettings.h" |
| 14 | |
| 15 | #include "UICommon/GameFile.h" |
| 16 | |
| 17 | const QSize LARGE_BANNER_SIZE(144, 48); |
| 18 | |
| 19 | GridProxyModel::GridProxyModel(QObject* parent) : QSortFilterProxyModel(parent) |
| 20 | { |
| 21 | setDynamicSortFilter(true); |
| 22 | } |
| 23 | |
| 24 | QVariant GridProxyModel::data(const QModelIndex& i, int role) const |
| 25 | { |
| 26 | QModelIndex source_index = mapToSource(i); |
| 27 | if (role == Qt::DisplayRole) |
| 28 | { |
| 29 | return sourceModel()->data( |
| 30 | sourceModel()->index(source_index.row(), static_cast<int>(GameListModel::Column::Title)), |
| 31 | Qt::DisplayRole); |
| 32 | } |
| 33 | else if (role == Qt::DecorationRole) |
| 34 | { |
| 35 | auto* model = static_cast<GameListModel*>(sourceModel()); |
| 36 | |
| 37 | const auto& buffer = model->GetGameFile(source_index.row())->GetCoverImage().buffer; |
| 38 | |
| 39 | QSize size = Config::Get(Config::MAIN_USE_GAME_COVERS) ? QSize(160, 224) : LARGE_BANNER_SIZE; |
| 40 | QPixmap pixmap(size * model->GetScale() * QPixmap().devicePixelRatio()); |
| 41 | |
| 42 | if (buffer.empty() || !Config::Get(Config::MAIN_USE_GAME_COVERS)) |
| 43 | { |
| 44 | QPixmap banner = model |
| 45 | ->data(model->index(source_index.row(), |
| 46 | static_cast<int>(GameListModel::Column::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 | |
| 73 | 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 | |
| 79 | bool GridProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const |
| 80 | { |
| 81 | if (left.data(GameListModel::SORT_ROLE) != right.data(GameListModel::SORT_ROLE)) |
| 82 | return QSortFilterProxyModel::lessThan(left, right); |
| 83 | |
| 84 | // If two items are otherwise equal, compare them by their title |
| 85 | const auto right_title = sourceModel() |
| 86 | ->index(right.row(), static_cast<int>(GameListModel::Column::Title)) |
| 87 | .data() |
| 88 | .toString(); |
| 89 | const auto left_title = sourceModel() |
| 90 | ->index(left.row(), static_cast<int>(GameListModel::Column::Title)) |
| 91 | .data() |
| 92 | .toString(); |
| 93 | |
| 94 | if (sortOrder() == Qt::AscendingOrder) |
| 95 | return left_title < right_title; |
| 96 | |
| 97 | return right_title < left_title; |
| 98 | } |