2022-04-09 07:10:29 +02:00
|
|
|
|
#include "catalog/delegates/IconColumnDelegate.h"
|
2017-12-30 12:57:55 +01:00
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
|
|
|
|
IconColumnDelegate::IconColumnDelegate(QWidget *parent)
|
|
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IconColumnDelegate::~IconColumnDelegate()
|
|
|
|
|
|
{
|
|
|
|
|
|
clearCache();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IconColumnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
|
|
|
|
const QModelIndex &index) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index.data().canConvert<QString>()) {
|
2018-12-16 15:38:32 +01:00
|
|
|
|
auto icon_name = qvariant_cast<QString>(index.data());
|
2017-12-30 12:57:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QIcon* icon = IconColumnDelegate::getIcon(icon_name);
|
|
|
|
|
|
if (icon) {
|
|
|
|
|
|
if (option.state & QStyle::State_Selected)
|
|
|
|
|
|
painter->fillRect(option.rect, option.palette.highlight());
|
|
|
|
|
|
icon->paint(painter, option.rect);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-09 18:52:32 +02:00
|
|
|
|
QSize IconColumnDelegate::sizeHint(const QStyleOptionViewItem &,
|
|
|
|
|
|
const QModelIndex &) const
|
2017-12-30 12:57:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
return QSize(16, 16);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void IconColumnDelegate::clearCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto &e : m_Icons)
|
2018-10-21 13:46:58 +02:00
|
|
|
|
delete e.second;
|
2017-12-30 12:57:55 +01:00
|
|
|
|
m_Icons.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QIcon* IconColumnDelegate::getIcon(const QString &name) const
|
|
|
|
|
|
{
|
|
|
|
|
|
auto fr = m_Icons.find(name);
|
|
|
|
|
|
if (fr == m_Icons.end()) {
|
|
|
|
|
|
// load and insert icon
|
2018-12-16 15:38:32 +01:00
|
|
|
|
auto icon = new QIcon(name);
|
2017-12-30 12:57:55 +01:00
|
|
|
|
fr = m_Icons.emplace(name, icon).first;
|
|
|
|
|
|
}
|
|
|
|
|
|
return fr->second;
|
|
|
|
|
|
}
|