~jbicha/unity-2d/fix-launcher-quicklist-naming-lp949636

« back to all changes in this revision

Viewing changes to libunity-2d-private/src/colorizeeffect.cpp

. Fixes: . Approved by .

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2012 Canonical, Ltd.
3
 
 *
4
 
 * Authors:
5
 
 *  Florian Boucault <florian.boucault@canonical.com>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; version 3.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include "colorizeeffect.h"
21
 
 
22
 
#include <QPainter>
23
 
 
24
 
ColorizeEffect::ColorizeEffect(QObject *parent) :
25
 
    QGraphicsEffect(parent),
26
 
    m_color("black"),
27
 
    m_saturation(1.0)
28
 
{
29
 
}
30
 
 
31
 
QColor ColorizeEffect::color() const
32
 
{
33
 
    return m_color;
34
 
}
35
 
 
36
 
void ColorizeEffect::setColor(const QColor &color)
37
 
{
38
 
    if (color == m_color) return;
39
 
 
40
 
    m_color = color;
41
 
    m_tintedPixmap = QPixmap();
42
 
    update();
43
 
    Q_EMIT colorChanged(color);
44
 
}
45
 
 
46
 
qreal ColorizeEffect::saturation() const
47
 
{
48
 
    return m_saturation;
49
 
}
50
 
 
51
 
void ColorizeEffect::setSaturation(qreal saturation)
52
 
{
53
 
    if (saturation == m_saturation) return;
54
 
 
55
 
    m_saturation = saturation;
56
 
    m_tintedPixmap = QPixmap();
57
 
    update();
58
 
    Q_EMIT saturationChanged(saturation);
59
 
}
60
 
 
61
 
void ColorizeEffect::draw(QPainter *painter)
62
 
{
63
 
    QPoint offset;
64
 
    const QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset, QGraphicsEffect::NoPad);
65
 
 
66
 
    if (m_tintedPixmap.isNull()) {
67
 
        /* Compute the tinted pixmap by composing the source pixmap and a tinted rectangle */
68
 
        m_tintedPixmap = pixmap.copy();
69
 
        QPainter tintedPainter(&m_tintedPixmap);
70
 
        tintedPainter.setRenderHints(painter->renderHints());
71
 
        tintedPainter.setCompositionMode(QPainter::CompositionMode_Overlay);
72
 
        QColor color = m_color;
73
 
        color.setAlphaF(m_saturation);
74
 
        tintedPainter.fillRect(m_tintedPixmap.rect(), color);
75
 
        /* Apply alpha channel of the source pixmap to the tinted pixmap */
76
 
        tintedPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
77
 
        tintedPainter.drawPixmap(0, 0, pixmap);
78
 
        tintedPainter.end();
79
 
    }
80
 
 
81
 
    QTransform previousTransform = painter->worldTransform();
82
 
    painter->setWorldTransform(QTransform());
83
 
    painter->drawPixmap(offset, m_tintedPixmap);
84
 
    painter->setWorldTransform(previousTransform);
85
 
}
86
 
 
87
 
void ColorizeEffect::sourceChanged(ChangeFlags flags)
88
 
{
89
 
    m_tintedPixmap = QPixmap();
90
 
}
91
 
 
92
 
#include "colorizeeffect.moc"