Skip to content

Commit

Permalink
Use icon cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Sep 16, 2018
1 parent 9b448a5 commit 6bdd31f
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions src/gui/iconfactory.cpp
Expand Up @@ -36,6 +36,7 @@
#include <QPaintDevice>
#include <QPaintEngine>
#include <QPixmap>
#include <QPixmapCache>
#include <QPointer>
#include <QSvgRenderer>
#include <QVariant>
Expand Down Expand Up @@ -134,6 +135,17 @@ QPixmap pixmapFromBitmapFile(const QString &path, QSize size)

QPixmap pixmapFromFile(const QString &path, QSize size)
{
const auto cacheKey = QString("path:%1|%2x%3")
.arg(path)
.arg(size.width())
.arg(size.height());

{
QPixmap pixmap;
if ( QPixmapCache::find(cacheKey, &pixmap) )
return pixmap;
}

if ( !QFile::exists(path) )
return QPixmap();

Expand All @@ -149,6 +161,9 @@ QPixmap pixmapFromFile(const QString &path, QSize size)
pix.fill(Qt::transparent);
QPainter painter(&pix);
renderer.render(&painter, pix.rect());

QPixmapCache::insert(cacheKey, pix);

return pix;
}

Expand Down Expand Up @@ -201,11 +216,26 @@ void replaceColor(QPixmap *pix, const QString &iconSuffix, const QColor &targetC
p.drawPixmap(0, 0, pix2);
}

void drawFontIcon(QPixmap *pix, ushort id, int w, int h, const QColor &color)
QPixmap drawFontIcon(ushort id, int w, int h, const QColor &color)
{
const auto cacheKey = QString("id:%1|%2x%3|%4")
.arg(id)
.arg(w)
.arg(h)
.arg(color.name());

{
QPixmap pixmap;
if ( QPixmapCache::find(cacheKey, &pixmap) )
return pixmap;
}

QPixmap pixmap(w, h);
pixmap.fill(Qt::transparent);

id = fixIconId(id);

QPainter painter(pix);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setRenderHint(QPainter::Antialiasing);
const QFont font = iconFontFitSize(w, h);
Expand All @@ -230,6 +260,10 @@ void drawFontIcon(QPixmap *pix, ushort id, int w, int h, const QColor &color)

painter.setPen(color);
painter.drawText(pos, iconText);

QPixmapCache::insert(cacheKey, pixmap);

return pixmap;
}

QColor getDefaultIconColor(const QColor &color)
Expand Down Expand Up @@ -377,15 +411,13 @@ class FontIconEngine : public BaseIconEngine

QPixmap doCreatePixmap(QSize size, QIcon::Mode mode, QIcon::State, QPainter *painter) override
{
QPixmap pixmap(size);
pixmap.fill(Qt::transparent);

if (m_iconId == 0)
if (m_iconId == 0) {
QPixmap pixmap(size);
pixmap.fill(Qt::transparent);
return pixmap;
}

drawFontIcon( &pixmap, m_iconId, size.width(), size.height(), colorForMode(painter, mode) );

return pixmap;
return drawFontIcon( m_iconId, size.width(), size.height(), colorForMode(painter, mode) );
}

private:
Expand Down Expand Up @@ -576,12 +608,11 @@ QIcon iconFromFile(const QString &fileName, const QString &tag, const QColor &co

QPixmap createPixmap(unsigned short id, const QColor &color, int size)
{
QPixmap pixmap(size, size);
pixmap.fill(Qt::transparent);

if (loadIconFont())
drawFontIcon(&pixmap, id, size, size, color);
return drawFontIcon(id, size, size, color);

QPixmap pixmap(size, size);
pixmap.fill(Qt::transparent);
return pixmap;
}

Expand Down

0 comments on commit 6bdd31f

Please sign in to comment.