Reorganization of pgLab project

This commit is contained in:
eelke 2022-04-09 07:10:29 +02:00
parent 7300865c77
commit c71fdc4af7
78 changed files with 204 additions and 148 deletions

View file

@ -0,0 +1,58 @@
#include "catalog/delegates/IconColumnDelegate.h"
#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>()) {
auto icon_name = qvariant_cast<QString>(index.data());
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);
}
}
QSize IconColumnDelegate::sizeHint(const QStyleOptionViewItem &,
const QModelIndex &) const
{
return QSize(16, 16);
}
void IconColumnDelegate::clearCache()
{
for (auto &e : m_Icons)
delete e.second;
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
auto icon = new QIcon(name);
fr = m_Icons.emplace(name, icon).first;
}
return fr->second;
}

View file

@ -0,0 +1,29 @@
#ifndef ICONCOLUMNDELEGATE_H
#define ICONCOLUMNDELEGATE_H
#include <QStyledItemDelegate>
#include <map>
/** This class asumes that the string values supplies for the column data
* can be Resolved by QIcon to an actual icon. Icons are cached and reused.
*
*/
class IconColumnDelegate: public QStyledItemDelegate {
public:
IconColumnDelegate(QWidget *parent = nullptr);
~IconColumnDelegate() override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void clearCache();
private:
using t_IconCache = std::map<QString, QIcon*>;
mutable t_IconCache m_Icons;
QIcon* getIcon(const QString &name) const;
};
#endif // ICONCOLUMNDELEGATE_H