This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
IndoorMap/UIHelper.h
2018-10-25 12:19:36 +02:00

105 lines
2.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* © 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