1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-26 12:42:41 +01:00

Qt: update table item delegates

This commit is contained in:
Megamouse 2018-06-29 04:25:14 +02:00 committed by Ivan
parent ed4caf0fbe
commit 20d653b58a
4 changed files with 51 additions and 19 deletions

View File

@ -9,6 +9,8 @@
game_list_grid::game_list_grid(const QSize& icon_size, const QColor& icon_color, const qreal& margin_factor, const qreal& text_factor, const bool& showText)
: game_list(), m_icon_size(icon_size), m_icon_color(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)
{
@ -19,11 +21,7 @@ game_list_grid::game_list_grid(const QSize& icon_size, const QColor& icon_color,
item_size = m_icon_size + m_icon_size * m_margin_factor * 2;
}
// font by stylesheet
QFont font = gui::utils::get_label_font("gamegrid_font");
QColor font_color = gui::utils::get_label_color("gamegrid_font");
grid_item_delegate = new game_list_grid_delegate(item_size, m_margin_factor, m_text_factor, font, font_color, this);
grid_item_delegate = new game_list_grid_delegate(item_size, m_margin_factor, m_text_factor, this);
setItemDelegate(grid_item_delegate);
setSelectionBehavior(QAbstractItemView::SelectItems);
setSelectionMode(QAbstractItemView::SingleSelection);

View File

@ -1,7 +1,7 @@
#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, const QFont& font, const QColor& font_color, QObject *parent)
: QStyledItemDelegate(parent), m_size(size), m_margin_factor(margin_factor), m_text_factor(text_factor), m_font(font), m_font_color(font_color)
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)
{
}
@ -9,6 +9,15 @@ game_list_grid_delegate::~game_list_grid_delegate()
{
}
void game_list_grid_delegate::initStyleOption(QStyleOptionViewItem * option, const QModelIndex & index) const
{
// 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
{
QRect r = option.rect;
@ -19,16 +28,13 @@ void game_list_grid_delegate::paint(QPainter * painter, const QStyleOptionViewIt
QPixmap image = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole));
QString title = index.data(Qt::DisplayRole).toString();
// Paint from our stylesheet
QStyledItemDelegate::paint(painter, option, index);
// image
if (image.isNull() == false)
{
painter->drawPixmap(r, image);
}
// Add selection overlay
if (option.state & QStyle::State_Selected)
{
painter->fillRect(r, QColor(20, 138, 255, 128));
painter->drawPixmap(option.rect, image);
}
int h = r.height() / (1 + m_margin_factor + m_margin_factor*m_text_factor);
@ -36,8 +42,16 @@ void game_list_grid_delegate::paint(QPainter * painter, const QStyleOptionViewIt
int top = r.bottom() - height;
// title
painter->setPen(QPen(m_font_color, 1, Qt::SolidLine));
painter->setFont(m_font);
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);
}

View File

@ -6,8 +6,9 @@
class game_list_grid_delegate : public QStyledItemDelegate
{
public:
game_list_grid_delegate(const QSize& imageSize, const qreal& margin_factor, const qreal& margin_ratio, const QFont& font, const QColor& font_color, QObject *parent = 0);
game_list_grid_delegate(const QSize& imageSize, const qreal& margin_factor, const qreal& margin_ratio, QObject *parent = 0);
virtual 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);
@ -16,6 +17,4 @@ private:
QSize m_size;
qreal m_margin_factor;
qreal m_text_factor;
QFont m_font;
QColor m_font_color;
};

View File

@ -1,15 +1,36 @@
#pragma once
#include <QStyledItemDelegate>
#include <QPainter>
/** This class is used to get rid of somewhat ugly item focus rectangles. You could change the rectangle instead of omiting it if you wanted */
class table_item_delegate : public QStyledItemDelegate
{
public:
explicit table_item_delegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}
virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
{
// Remove the focus frame around selected items
option->state &= ~QStyle::State_HasFocus;
if (index.column() == 0)
{
// Don't highlight icons
option->state &= ~QStyle::State_Selected;
}
QStyledItemDelegate::initStyleOption(option, index);
}
virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const override
{
if (index.column() == 0 && option.state & QStyle::State_Selected)
{
// Add background highlight color to icons
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
QStyledItemDelegate::paint(painter, option, index);
}
};