105 lines
2.3 KiB
C++
105 lines
2.3 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef UIHELPER_H
|
||
#define UIHELPER_H
|
||
|
||
#include "fixC11.h"
|
||
|
||
#include <QImage>
|
||
#include <QPainter>
|
||
#include <QRgb>
|
||
#include <QIcon>
|
||
|
||
#include <QtSvg/QSvgRenderer>
|
||
|
||
#include <unordered_map>
|
||
|
||
class UIHelper {
|
||
|
||
public:
|
||
|
||
// static QIcon getIcon(const std::string& name) {
|
||
// const std::string file = "://res/icons/" + name + "16.png";
|
||
// QPixmap img(file.c_str());
|
||
// return QIcon(img);
|
||
// }
|
||
|
||
static const QPixmap& getPixmap(const std::string& name, const int size = 32) {
|
||
|
||
// caching
|
||
static std::unordered_map<std::string, QPixmap> cache;
|
||
|
||
// try to get the image from the cache
|
||
const std::string cacheKey = std::to_string(size) + name;
|
||
auto it = cache.find(cacheKey);
|
||
|
||
// not in cache?
|
||
if (it == cache.end()) {
|
||
|
||
// build
|
||
const QColor fill = Qt::transparent;
|
||
const std::string file = "://res/icons/" + name + ".svg";
|
||
QSvgRenderer renderer(QString(file.c_str()));
|
||
QPixmap pm(size, size);
|
||
pm.fill(fill);
|
||
QPainter painter(&pm);
|
||
renderer.render(&painter, pm.rect());
|
||
|
||
// add to cache
|
||
cache[cacheKey] = pm;
|
||
|
||
}
|
||
|
||
// done
|
||
return cache[cacheKey];
|
||
|
||
}
|
||
|
||
static const QPixmap& getPixmapColored(const std::string& name, const QColor color, const int size = 32) {
|
||
|
||
// caching
|
||
static std::unordered_map<std::string, QPixmap> cache;
|
||
|
||
// try to get the image from the cache
|
||
const QString hex = color.name();
|
||
const std::string cacheKey = hex.toStdString() + "_" + std::to_string(size) + "_" + name;
|
||
auto it = cache.find(cacheKey);
|
||
|
||
// not in cache?
|
||
if (it == cache.end()) {
|
||
|
||
// copy
|
||
QPixmap colored = getPixmap(name, size);
|
||
QPainter painter(&colored);
|
||
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
||
painter.fillRect(colored.rect(), color);
|
||
painter.end();
|
||
|
||
// add to cache
|
||
cache[cacheKey] = colored;
|
||
|
||
}
|
||
|
||
// done
|
||
return cache[cacheKey];
|
||
|
||
}
|
||
|
||
static QIcon getIcon(const std::string& name, const int size = 32) {
|
||
|
||
return QIcon(getPixmap(name, size));
|
||
|
||
}
|
||
|
||
};
|
||
|
||
#endif // UIHELPER_H
|