Skip to content

Commit

Permalink
Properly scale images
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardomv committed Jan 15, 2016
1 parent 6e8f92d commit cb6069c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions modules/Material/Icon.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Item {

/*!
The name of the icon to display.
\sa source
*/
property string name
Expand Down Expand Up @@ -64,7 +64,8 @@ Item {
id: image

visible: !colorize

sourceSize.width: size
sourceSize.height: size
source: icon.source
}

Expand All @@ -77,5 +78,5 @@ Item {
cached: true
visible: image.source != "" && colorize
opacity: icon.color.a
}
}
}
27 changes: 20 additions & 7 deletions plugins/materialiconprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,38 @@
#include <qquickimageprovider.h>
#include <QImage>
#include <QPainter>
#include <QSvgRenderer>

#include <QDebug>

class MaterialIconProvider : public QQuickImageProvider
{
public:
MaterialIconProvider()
: QQuickImageProvider(QQuickImageProvider::Pixmap)
: QQuickImageProvider(QQuickImageProvider::Image)
{
}

QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
int width = 100;
int height = 50;
QImage icon = QImage("/lib/qt/qml/Material/icons/" + id + ".svg");
QString iconPath = "/lib/qt/qml/Material/icons/" + id + ".svg";

QSvgRenderer renderer;
if (!renderer.load(iconPath))
qWarning() << "Unable to load image:" << iconPath;

QImage image(requestedSize.width() > 0 ? requestedSize.width() : renderer.defaultSize().width(),
requestedSize.height() > 0 ? requestedSize.height() : renderer.defaultSize().height(),
QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);

if (size)
*size = QSize(width, height);
*size = image.size();

QPainter p(&image);
renderer.render(&p);

return QPixmap::fromImage(icon);
return image;
}
};

Expand Down
2 changes: 1 addition & 1 deletion plugins/plugins.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TEMPLATE = lib
CONFIG += plugin
QT += qml quick
QT += qml quick svg

TARGET = materialiconprovider

Expand Down

0 comments on commit cb6069c

Please sign in to comment.