a lot of work on th map-creator
This commit is contained in:
79
UIHelper.h
79
UIHelper.h
@@ -3,8 +3,12 @@
|
||||
|
||||
#include <QImage>
|
||||
#include <QPainter>
|
||||
#include <QRgb>
|
||||
|
||||
#include <QtSvg/QSvgRenderer>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
class UIHelper {
|
||||
|
||||
public:
|
||||
@@ -15,16 +19,71 @@ public:
|
||||
// return QIcon(img);
|
||||
// }
|
||||
|
||||
static QIcon getIcon(const std::string& name) {
|
||||
const int size = 32;
|
||||
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());
|
||||
return QIcon(pm);
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user