more game list classes

This commit is contained in:
georgemoralis 2023-03-08 14:05:16 +02:00
parent 380fb710b7
commit 0c5380d78d
8 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,68 @@
#include "game_list_grid.h"
#include "game_list_grid_delegate.h"
#include <QHeaderView>
#include <QScrollBar>
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;
}

View File

@ -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;
};

View File

@ -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<QPixmap>(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;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <QPainter>
#include <QStyledItemDelegate>
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;
};

View File

@ -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);
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <QTableWidget>
#include <QMouseEvent>
#include "../emulator/gameInfo.h"
struct gui_game_info
{
GameInfo info{};
};
typedef std::shared_ptr<gui_game_info> game_info;
Q_DECLARE_METATYPE(game_info)
class game_list_table : public QTableWidget
{
public:
void clear_list();
protected:
void mousePressEvent(QMouseEvent* event) override;
};

View File

@ -17,6 +17,9 @@
<ClCompile Include="emulator\Loader.cpp" />
<ClCompile Include="gui\custom_table_widget_item.cpp" />
<ClCompile Include="gui\GameListViewer.cpp" />
<ClCompile Include="gui\game_list_grid.cpp" />
<ClCompile Include="gui\game_list_grid_delegate.cpp" />
<ClCompile Include="gui\game_list_table.cpp" />
<ClCompile Include="gui\gui_settings.cpp" />
<ClCompile Include="gui\shadps4gui.cpp" />
<ClCompile Include="main.cpp" />
@ -37,6 +40,9 @@
<ClInclude Include="emulator\gameInfo.h" />
<ClInclude Include="emulator\Loader.h" />
<ClInclude Include="gui\custom_table_widget_item.h" />
<QtMoc Include="gui\game_list_grid.h" />
<ClInclude Include="gui\game_list_grid_delegate.h" />
<ClInclude Include="gui\game_list_table.h" />
<ClInclude Include="gui\gui_settings.h" />
<ClInclude Include="Types.h" />
</ItemGroup>

View File

@ -59,6 +59,18 @@
<ClCompile Include="gui\gui_settings.cpp">
<Filter>gui</Filter>
</ClCompile>
<ClCompile Include="gui\custom_table_widget_item.cpp">
<Filter>gui</Filter>
</ClCompile>
<ClCompile Include="gui\game_list_table.cpp">
<Filter>gui</Filter>
</ClCompile>
<ClCompile Include="gui\game_list_grid_delegate.cpp">
<Filter>gui</Filter>
</ClCompile>
<ClCompile Include="gui\game_list_grid.cpp">
<Filter>gui</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtUic Include="gui\shadps4gui.ui">
@ -72,6 +84,9 @@
<QtMoc Include="gui\GameListViewer.h">
<Filter>gui</Filter>
</QtMoc>
<QtMoc Include="gui\game_list_grid.h">
<Filter>gui</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Types.h">
@ -92,5 +107,17 @@
<ClInclude Include="gui\gui_settings.h">
<Filter>gui</Filter>
</ClInclude>
<ClInclude Include="emulator\gameInfo.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="gui\custom_table_widget_item.h">
<Filter>gui</Filter>
</ClInclude>
<ClInclude Include="gui\game_list_table.h">
<Filter>gui</Filter>
</ClInclude>
<ClInclude Include="gui\game_list_grid_delegate.h">
<Filter>gui</Filter>
</ClInclude>
</ItemGroup>
</Project>