GradientImageProvider.cpp 1.4 KB
Newer Older
Michael Winkelmann's avatar
Michael Winkelmann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "GradientImageProvider.h"

#include <QPainter>
#include <QLinearGradient>
#include <QDebug>

GradientImageProvider::GradientImageProvider() :
    QQuickImageProvider (QQuickImageProvider::Image)
{

}

QImage GradientImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    QStringList colors = id.split("-");

    // We need at least two colors!
    if (colors.size() < 2)
        return QImage();

    qDebug() << "Generate Image: " << colors;

    if (!requestedSize.isValid())
        *size = QSize(96,96);
    else
        *size = requestedSize;

    // Make a new image
    QImage image(*size,QImage::Format_ARGB32);
    image.fill(0);

    QPainter p(&image);
    p.setRenderHints(QPainter::HighQualityAntialiasing |
                     QPainter::Antialiasing);

    // Setup gradient
    QLinearGradient gradient;
    gradient.setColorAt(0.0, QColor(colors[0]));
    gradient.setColorAt(1.0, QColor(colors[1]));
    gradient.setStart(0,0);
    gradient.setFinalStop(0,size->height());

    p.setBrush(gradient);
    p.setPen(Qt::NoPen);

    // Draw our ellipse
    QRect r(0,0,size->width(),size->height());
    p.drawEllipse(r.adjusted(1,1,-1,-1));

    // Draw border if there are more than 2 colors
    if (colors.size() > 2) {
        p.setBrush(Qt::NoBrush);
        p.setPen(QPen(QColor(colors[2]),10));
        p.drawEllipse(r.adjusted(5,5,-5,-5));
    }

    return image;
}