From 0c5380d78d5a13d0f4dd67db6323f3643e7432ae Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Wed, 8 Mar 2023 14:05:16 +0200 Subject: [PATCH] more game list classes --- shadPS4/gui/game_list_grid.cpp | 68 +++++++++++++++++++++++++ shadPS4/gui/game_list_grid.h | 29 +++++++++++ shadPS4/gui/game_list_grid_delegate.cpp | 67 ++++++++++++++++++++++++ shadPS4/gui/game_list_grid_delegate.h | 19 +++++++ shadPS4/gui/game_list_table.cpp | 17 +++++++ shadPS4/gui/game_list_table.h | 22 ++++++++ shadPS4/shadPS4.vcxproj | 6 +++ shadPS4/shadPS4.vcxproj.filters | 27 ++++++++++ 8 files changed, 255 insertions(+) create mode 100644 shadPS4/gui/game_list_grid.cpp create mode 100644 shadPS4/gui/game_list_grid.h create mode 100644 shadPS4/gui/game_list_grid_delegate.cpp create mode 100644 shadPS4/gui/game_list_grid_delegate.h create mode 100644 shadPS4/gui/game_list_table.cpp create mode 100644 shadPS4/gui/game_list_table.h diff --git a/shadPS4/gui/game_list_grid.cpp b/shadPS4/gui/game_list_grid.cpp new file mode 100644 index 00000000..02d48816 --- /dev/null +++ b/shadPS4/gui/game_list_grid.cpp @@ -0,0 +1,68 @@ +#include "game_list_grid.h" +#include "game_list_grid_delegate.h" + +#include +#include + +game_list_grid::game_list_grid(const QSize& icon_size, QColor icon_color, const qreal& margin_factor, const qreal& text_factor, const bool& showText) + : game_list_table() + , m_icon_size(icon_size) + , m_icon_color(std::move(icon_color)) + , m_margin_factor(margin_factor) + , m_text_factor(text_factor) + , m_text_enabled(showText) +{ + setObjectName("game_grid"); + + QSize item_size; + if (m_text_enabled) + { + item_size = m_icon_size + QSize(m_icon_size.width() * m_margin_factor * 2, m_icon_size.height() * m_margin_factor * (m_text_factor + 1)); + } + else + { + item_size = m_icon_size + m_icon_size * m_margin_factor * 2; + } + + grid_item_delegate = new game_list_grid_delegate(item_size, m_margin_factor, m_text_factor, this); + setItemDelegate(grid_item_delegate); + setEditTriggers(QAbstractItemView::NoEditTriggers); + setSelectionBehavior(QAbstractItemView::SelectItems); + setSelectionMode(QAbstractItemView::SingleSelection); + setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); + setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); + verticalScrollBar()->setSingleStep(20); + horizontalScrollBar()->setSingleStep(20); + setContextMenuPolicy(Qt::CustomContextMenu); + verticalHeader()->setVisible(false); + horizontalHeader()->setVisible(false); + setShowGrid(false); + setMouseTracking(true); +} + +void game_list_grid::enableText(const bool& enabled) +{ + m_text_enabled = enabled; +} + +void game_list_grid::setIconSize(const QSize& size) const +{ + if (m_text_enabled) + { + grid_item_delegate->setItemSize(size + QSize(size.width() * m_margin_factor * 2, size.height() * m_margin_factor * (m_text_factor + 1))); + } + else + { + grid_item_delegate->setItemSize(size + size * m_margin_factor * 2); + } +} + +QTableWidgetItem* game_list_grid::addItem(const game_info& app, const QString& name, const QString& movie_path, const int& row, const int& col) +{ + throw std::exception("TODO"); +} + +qreal game_list_grid::getMarginFactor() const +{ + return m_margin_factor; +} diff --git a/shadPS4/gui/game_list_grid.h b/shadPS4/gui/game_list_grid.h new file mode 100644 index 00000000..179c2cf7 --- /dev/null +++ b/shadPS4/gui/game_list_grid.h @@ -0,0 +1,29 @@ +#pragma once + +#include "game_list_table.h" + +class game_list_grid_delegate; + +class game_list_grid : public game_list_table +{ + Q_OBJECT + + QSize m_icon_size; + QColor m_icon_color; + qreal m_margin_factor; + qreal m_text_factor; + bool m_text_enabled = true; + +public: + explicit game_list_grid(const QSize& icon_size, QColor icon_color, const qreal& margin_factor, const qreal& text_factor, const bool& showText); + + void enableText(const bool& enabled); + void setIconSize(const QSize& size) const; + QTableWidgetItem* addItem(const game_info& app, const QString& name, const QString& movie_path, const int& row, const int& col); + + [[nodiscard]] qreal getMarginFactor() const; + +private: + game_list_grid_delegate* grid_item_delegate; +}; + diff --git a/shadPS4/gui/game_list_grid_delegate.cpp b/shadPS4/gui/game_list_grid_delegate.cpp new file mode 100644 index 00000000..1f086907 --- /dev/null +++ b/shadPS4/gui/game_list_grid_delegate.cpp @@ -0,0 +1,67 @@ +#include "game_list_grid_delegate.h" + +game_list_grid_delegate::game_list_grid_delegate(const QSize& size, const qreal& margin_factor, const qreal& text_factor, QObject* parent) + : QStyledItemDelegate(parent), m_size(size), m_margin_factor(margin_factor), m_text_factor(text_factor) +{ +} + +void game_list_grid_delegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const +{ + Q_UNUSED(index) + + // Remove the focus frame around selected items + option->state &= ~QStyle::State_HasFocus; + + // Call initStyleOption without a model index, since we want to paint the relevant data ourselves + QStyledItemDelegate::initStyleOption(option, QModelIndex()); +} + +void game_list_grid_delegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + const QRect r = option.rect; + + painter->setRenderHints(QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform); + painter->eraseRect(r); + + // Get title and image + const QPixmap image = qvariant_cast(index.data(Qt::DecorationRole)); + const QString title = index.data(Qt::DisplayRole).toString(); + + // Paint from our stylesheet + QStyledItemDelegate::paint(painter, option, index); + + // image + if (image.isNull() == false) + { + painter->drawPixmap(option.rect, image); + } + + const int h = r.height() / (1 + m_margin_factor + m_margin_factor * m_text_factor); + const int height = r.height() - h - h * m_margin_factor; + const int top = r.bottom() - height; + + // title + if (option.state & QStyle::State_Selected) + { + painter->setPen(QPen(option.palette.color(QPalette::HighlightedText), 1, Qt::SolidLine)); + } + else + { + painter->setPen(QPen(option.palette.color(QPalette::WindowText), 1, Qt::SolidLine)); + } + + painter->setFont(option.font); + painter->drawText(QRect(r.left(), top, r.width(), height), +Qt::TextWordWrap | +Qt::AlignCenter, title); +} + +QSize game_list_grid_delegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + Q_UNUSED(option) + Q_UNUSED(index) + return m_size; +} + +void game_list_grid_delegate::setItemSize(const QSize& size) +{ + m_size = size; +} diff --git a/shadPS4/gui/game_list_grid_delegate.h b/shadPS4/gui/game_list_grid_delegate.h new file mode 100644 index 00000000..e2e68306 --- /dev/null +++ b/shadPS4/gui/game_list_grid_delegate.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +class game_list_grid_delegate : public QStyledItemDelegate +{ +public: + game_list_grid_delegate(const QSize& imageSize, const qreal& margin_factor, const qreal& margin_ratio, QObject* parent = nullptr); + + void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const override; + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override; + void setItemSize(const QSize& size); +private: + QSize m_size; + qreal m_margin_factor; + qreal m_text_factor; +}; diff --git a/shadPS4/gui/game_list_table.cpp b/shadPS4/gui/game_list_table.cpp new file mode 100644 index 00000000..b9a733f1 --- /dev/null +++ b/shadPS4/gui/game_list_table.cpp @@ -0,0 +1,17 @@ +#include "game_list_table.h" + +void game_list_table ::clear_list() +{ + clearSelection(); + clearContents(); +} + +void game_list_table::mousePressEvent(QMouseEvent* event) +{ + if (QTableWidgetItem* item = itemAt(event->pos()); !item || !item->data(Qt::UserRole).isValid()) + { + clearSelection(); + setCurrentItem(nullptr); // Needed for currentItemChanged + } + QTableWidget::mousePressEvent(event); +} \ No newline at end of file diff --git a/shadPS4/gui/game_list_table.h b/shadPS4/gui/game_list_table.h new file mode 100644 index 00000000..db0d7296 --- /dev/null +++ b/shadPS4/gui/game_list_table.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include "../emulator/gameInfo.h" + +struct gui_game_info +{ + GameInfo info{}; +}; + +typedef std::shared_ptr game_info; +Q_DECLARE_METATYPE(game_info) + +class game_list_table : public QTableWidget +{ +public: + void clear_list(); + +protected: + void mousePressEvent(QMouseEvent* event) override; +}; \ No newline at end of file diff --git a/shadPS4/shadPS4.vcxproj b/shadPS4/shadPS4.vcxproj index c847d5f9..6249c93e 100644 --- a/shadPS4/shadPS4.vcxproj +++ b/shadPS4/shadPS4.vcxproj @@ -17,6 +17,9 @@ + + + @@ -37,6 +40,9 @@ + + + diff --git a/shadPS4/shadPS4.vcxproj.filters b/shadPS4/shadPS4.vcxproj.filters index 9b81844f..072d8376 100644 --- a/shadPS4/shadPS4.vcxproj.filters +++ b/shadPS4/shadPS4.vcxproj.filters @@ -59,6 +59,18 @@ gui + + gui + + + gui + + + gui + + + gui + @@ -72,6 +84,9 @@ gui + + gui + @@ -92,5 +107,17 @@ gui + + Header Files + + + gui + + + gui + + + gui + \ No newline at end of file