rpcsx/rpcs3/rpcs3qt/richtext_item_delegate.h
Megamouse d63a712cc5 Qt: fix richtext_item_delegate palette
We have to use the styled palette instead of the default palette in order to fix the style of lists as seen the pkg installer.
2021-10-24 10:38:50 +02:00

65 lines
1.7 KiB
C++

#pragma once
#include <QAbstractTextDocumentLayout>
#include <QApplication>
#include <QStyledItemDelegate>
#include <QTextDocument>
class richtext_item_delegate : public QStyledItemDelegate
{
private:
QTextDocument* m_document;
public:
explicit richtext_item_delegate(QObject* parent = nullptr)
: QStyledItemDelegate(parent)
, m_document(new QTextDocument(this))
{
}
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
{
painter->save();
// Initialize style option
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
// Setup the QTextDocument with our HTML
m_document->setHtml(opt.text);
opt.text.clear();
// Get the available style
QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
// Draw our widget if available
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
// Adjust our painter parameters with some magic that looks good.
// This is necessary so that we don't draw on top of the optional widget.
const int margin = (option.rect.height() - opt.fontMetrics.height() - 4);
QRect text_rect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, nullptr);
if (index.column() != 0)
{
text_rect.adjust(5, 0, 0, 0);
}
text_rect.setTop(text_rect.top() + margin);
painter->translate(text_rect.topLeft());
painter->setClipRect(text_rect.translated(-text_rect.topLeft()));
// Create a context for our painter
QAbstractTextDocumentLayout::PaintContext context;
// Apply the styled palette
context.palette = opt.palette;
// Draw the text
m_document->documentLayout()->draw(painter, context);
painter->restore();
}
};