2
* Copyright (C) 2012 Canonical, Ltd.
5
* Florian Boucault <florian.boucault@canonical.com>
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.
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.
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/>.
20
#include "colorizeeffect.h"
24
ColorizeEffect::ColorizeEffect(QObject *parent) :
25
QGraphicsEffect(parent),
31
QColor ColorizeEffect::color() const
36
void ColorizeEffect::setColor(const QColor &color)
38
if (color == m_color) return;
41
m_tintedPixmap = QPixmap();
43
Q_EMIT colorChanged(color);
46
qreal ColorizeEffect::saturation() const
51
void ColorizeEffect::setSaturation(qreal saturation)
53
if (saturation == m_saturation) return;
55
m_saturation = saturation;
56
m_tintedPixmap = QPixmap();
58
Q_EMIT saturationChanged(saturation);
61
void ColorizeEffect::draw(QPainter *painter)
64
const QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset, QGraphicsEffect::NoPad);
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);
81
QTransform previousTransform = painter->worldTransform();
82
painter->setWorldTransform(QTransform());
83
painter->drawPixmap(offset, m_tintedPixmap);
84
painter->setWorldTransform(previousTransform);
87
void ColorizeEffect::sourceChanged(ChangeFlags flags)
89
m_tintedPixmap = QPixmap();
92
#include "colorizeeffect.moc"