2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2005 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2008 by Andrew Lake <jamboarder@yahoo.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <paintutils.h>
|
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPaintEngine>
|
|
|
|
#include <QPixmap>
|
|
|
|
|
2009-09-03 22:40:22 +02:00
|
|
|
#include "private/effects/blur.cpp"
|
2009-11-26 22:57:36 +01:00
|
|
|
#include "private/effects/halopainter_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace PaintUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
void shadowBlur(QImage &image, int radius, const QColor &color)
|
|
|
|
{
|
|
|
|
if (radius < 1) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-29 02:04:19 +02:00
|
|
|
if (image.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
expblur<16, 7>(image, radius);
|
|
|
|
|
|
|
|
QPainter p(&image);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
p.fillRect(image.rect(), color);
|
|
|
|
p.end();
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:31:59 +01:00
|
|
|
//TODO: we should have shadowText methods that paint the results directly into a QPainter passed in
|
2008-11-04 00:08:39 +01:00
|
|
|
QPixmap shadowText(QString text, QColor textColor, QColor shadowColor, QPoint offset, int radius)
|
2008-12-23 19:31:59 +01:00
|
|
|
{
|
|
|
|
return shadowText(text, qApp->font(), textColor, shadowColor, offset, radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap shadowText(QString text, const QFont &font, QColor textColor, QColor shadowColor, QPoint offset, int radius)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2008-11-27 15:52:08 +01:00
|
|
|
//don't try to paint stuff on a future null pixmap because the text is empty
|
2008-11-27 16:33:30 +01:00
|
|
|
if (text.isEmpty()) {
|
2008-11-27 15:52:08 +01:00
|
|
|
return QPixmap();
|
|
|
|
}
|
2008-12-23 19:31:59 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
// Draw text
|
2008-12-23 19:31:59 +01:00
|
|
|
QFontMetrics fm(font);
|
2008-11-04 00:08:39 +01:00
|
|
|
QRect textRect = fm.boundingRect(text);
|
2008-12-23 19:31:59 +01:00
|
|
|
QPixmap textPixmap(textRect.width(), fm.height());
|
2008-11-04 00:08:39 +01:00
|
|
|
textPixmap.fill(Qt::transparent);
|
|
|
|
QPainter p(&textPixmap);
|
|
|
|
p.setPen(textColor);
|
2008-12-23 19:31:59 +01:00
|
|
|
p.setFont(font);
|
2008-12-29 01:59:22 +01:00
|
|
|
// FIXME: the center alignment here is odd: the rect should be the size needed by
|
|
|
|
// the text, but for some fonts and configurations this is off by a pixel or so
|
|
|
|
// and "centering" the text painting 'fixes' that. Need to research why
|
|
|
|
// this is the case and determine if we should be painting it differently here,
|
|
|
|
// doing soething different with the boundingRect call or if it's a problem
|
|
|
|
// in Qt itself
|
|
|
|
p.drawText(textPixmap.rect(), Qt::AlignCenter, text);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.end();
|
|
|
|
|
|
|
|
//Draw blurred shadow
|
2010-09-22 21:35:47 +02:00
|
|
|
QImage img(textRect.size() + QSize(radius * 2, radius * 2), QImage::Format_ARGB32_Premultiplied);
|
2009-01-09 17:50:27 +01:00
|
|
|
img.fill(0);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.begin(&img);
|
|
|
|
p.drawImage(QPoint(radius, radius), textPixmap.toImage());
|
|
|
|
p.end();
|
|
|
|
shadowBlur(img, radius, shadowColor);
|
|
|
|
|
|
|
|
//Compose text and shadow
|
2010-10-05 15:13:40 +02:00
|
|
|
int addSizeX = qMax(0, qAbs(offset.x()) - radius);
|
|
|
|
int addSizeY = qMax(0, qAbs(offset.y()) - radius);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QPixmap finalPixmap(img.size() + QSize(addSizeX, addSizeY));
|
|
|
|
finalPixmap.fill(Qt::transparent);
|
|
|
|
p.begin(&finalPixmap);
|
2010-10-05 15:13:40 +02:00
|
|
|
p.drawImage(qMax(0, offset.x()), qMax(0, offset.y()), img);
|
|
|
|
p.drawPixmap(radius + qMax(0, -offset.x()), radius + qMax(0, -offset.y()), textPixmap);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.end();
|
|
|
|
|
|
|
|
return finalPixmap;
|
|
|
|
}
|
|
|
|
|
2009-11-26 22:57:36 +01:00
|
|
|
void drawHalo(QPainter *painter, const QRectF &rect)
|
|
|
|
{
|
|
|
|
HaloPainter::instance()->drawHalo(painter, rect.toRect());
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QPainterPath roundedRectangle(const QRectF &rect, qreal radius)
|
|
|
|
{
|
|
|
|
QPainterPath path(QPointF(rect.left(), rect.top() + radius));
|
|
|
|
path.quadTo(rect.left(), rect.top(), rect.left() + radius, rect.top()); // Top left corner
|
|
|
|
path.lineTo(rect.right() - radius, rect.top()); // Top side
|
|
|
|
path.quadTo(rect.right(), rect.top(), rect.right(), rect.top() + radius); // Top right corner
|
|
|
|
path.lineTo(rect.right(), rect.bottom() - radius); // Right side
|
|
|
|
path.quadTo(rect.right(), rect.bottom(), rect.right() - radius, rect.bottom()); // Bottom right corner
|
|
|
|
path.lineTo(rect.left() + radius, rect.bottom()); // Bottom side
|
|
|
|
path.quadTo(rect.left(), rect.bottom(), rect.left(), rect.bottom() - radius); // Bottom left corner
|
|
|
|
path.closeSubpath();
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2010-01-22 00:13:56 +01:00
|
|
|
void centerPixmaps(QPixmap &from, QPixmap &to)
|
|
|
|
{
|
2010-06-01 23:58:05 +02:00
|
|
|
if (from.size() == to.size() && from.hasAlphaChannel() && to.hasAlphaChannel()) {
|
2010-01-22 00:13:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-09-22 21:35:47 +02:00
|
|
|
|
2010-01-22 00:13:56 +01:00
|
|
|
QRect fromRect(from.rect());
|
|
|
|
QRect toRect(to.rect());
|
|
|
|
|
|
|
|
QRect actualRect = QRect(QPoint(0,0), fromRect.size().expandedTo(toRect.size()));
|
|
|
|
fromRect.moveCenter(actualRect.center());
|
|
|
|
toRect.moveCenter(actualRect.center());
|
|
|
|
|
2010-06-01 23:21:15 +02:00
|
|
|
if (from.size() != actualRect.size() || !from.hasAlphaChannel()) {
|
2010-01-22 00:13:56 +01:00
|
|
|
QPixmap result(actualRect.size());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
QPainter p(&result);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
p.drawPixmap(fromRect.topLeft(), from);
|
|
|
|
p.end();
|
|
|
|
from = result;
|
|
|
|
}
|
|
|
|
|
2010-06-01 23:21:15 +02:00
|
|
|
if (to.size() != actualRect.size() || !to.hasAlphaChannel()) {
|
2010-01-22 00:13:56 +01:00
|
|
|
QPixmap result(actualRect.size());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
QPainter p(&result);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
p.drawPixmap(toRect.topLeft(), to);
|
|
|
|
p.end();
|
|
|
|
to = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QPixmap transition(const QPixmap &from, const QPixmap &to, qreal amount)
|
|
|
|
{
|
2010-04-14 22:53:45 +02:00
|
|
|
if (from.isNull() && to.isNull()) {
|
2009-06-24 23:53:18 +02:00
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
2010-09-22 21:35:47 +02:00
|
|
|
QRect startRect(from.rect());
|
|
|
|
QRect targetRect(to.rect());
|
|
|
|
QSize pixmapSize = startRect.size().expandedTo(targetRect.size());
|
|
|
|
QRect toRect = QRect(QPoint(0,0), pixmapSize);
|
|
|
|
targetRect.moveCenter(toRect.center());
|
|
|
|
startRect.moveCenter(toRect.center());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-01-22 00:13:56 +01:00
|
|
|
//paint to in the center of from
|
2008-11-04 00:08:39 +01:00
|
|
|
QColor color;
|
|
|
|
color.setAlphaF(amount);
|
|
|
|
|
|
|
|
// If the native paint engine supports Porter/Duff compositing and CompositionMode_Plus
|
2010-01-27 17:26:41 +01:00
|
|
|
QPaintEngine *paintEngine = from.paintEngine();
|
|
|
|
if (paintEngine &&
|
|
|
|
paintEngine->hasFeature(QPaintEngine::PorterDuff) &&
|
|
|
|
paintEngine->hasFeature(QPaintEngine::BlendModes)) {
|
2010-09-23 21:59:54 +02:00
|
|
|
QPixmap startPixmap(pixmapSize);
|
2010-09-23 21:51:39 +02:00
|
|
|
startPixmap.fill(Qt::transparent);
|
2010-09-23 21:59:54 +02:00
|
|
|
|
|
|
|
QPixmap targetPixmap(pixmapSize);
|
2010-09-23 21:51:39 +02:00
|
|
|
targetPixmap.fill(Qt::transparent);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QPainter p;
|
2010-01-22 00:13:56 +01:00
|
|
|
p.begin(&targetPixmap);
|
2010-09-23 21:59:54 +02:00
|
|
|
p.drawPixmap(targetRect, to);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
2010-09-22 21:35:47 +02:00
|
|
|
p.fillRect(targetRect, color);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.end();
|
|
|
|
|
2010-01-22 00:13:56 +01:00
|
|
|
p.begin(&startPixmap);
|
2010-09-22 21:35:47 +02:00
|
|
|
p.drawPixmap(startRect, from);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
|
2010-09-22 21:35:47 +02:00
|
|
|
p.fillRect(startRect, color);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Plus);
|
2010-09-23 21:59:54 +02:00
|
|
|
p.drawPixmap(targetRect, targetPixmap);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.end();
|
|
|
|
|
2010-01-22 00:13:56 +01:00
|
|
|
return startPixmap;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
#if defined(Q_WS_X11) && defined(HAVE_XRENDER)
|
|
|
|
// We have Xrender support
|
2010-01-27 17:26:41 +01:00
|
|
|
else if (paintEngine && paintEngine->hasFeature(QPaintEngine::PorterDuff)) {
|
2008-11-04 00:08:39 +01:00
|
|
|
// QX11PaintEngine doesn't implement CompositionMode_Plus in Qt 4.3,
|
|
|
|
// which we need to be able to do a transition from one pixmap to
|
|
|
|
// another.
|
|
|
|
//
|
|
|
|
// In order to avoid the overhead of converting the pixmaps to images
|
|
|
|
// and doing the operation entirely in software, this function has a
|
|
|
|
// specialized path for X11 that uses Xrender directly to do the
|
|
|
|
// transition. This operation can be fully accelerated in HW.
|
|
|
|
//
|
|
|
|
// This specialization can be removed when QX11PaintEngine supports
|
|
|
|
// CompositionMode_Plus.
|
2010-01-22 00:13:56 +01:00
|
|
|
QPixmap source(targetPixmap), destination(startPixmap);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
source.detach();
|
|
|
|
destination.detach();
|
|
|
|
|
|
|
|
Display *dpy = QX11Info::display();
|
|
|
|
|
|
|
|
XRenderPictFormat *format = XRenderFindStandardFormat(dpy, PictStandardA8);
|
|
|
|
XRenderPictureAttributes pa;
|
|
|
|
pa.repeat = 1; // RepeatNormal
|
|
|
|
|
|
|
|
// Create a 1x1 8 bit repeating alpha picture
|
|
|
|
Pixmap pixmap = XCreatePixmap(dpy, destination.handle(), 1, 1, 8);
|
|
|
|
Picture alpha = XRenderCreatePicture(dpy, pixmap, format, CPRepeat, &pa);
|
|
|
|
XFreePixmap(dpy, pixmap);
|
|
|
|
|
|
|
|
// Fill the alpha picture with the opacity value
|
|
|
|
XRenderColor xcolor;
|
|
|
|
xcolor.alpha = quint16(0xffff * amount);
|
|
|
|
XRenderFillRectangle(dpy, PictOpSrc, alpha, &xcolor, 0, 0, 1, 1);
|
|
|
|
|
|
|
|
// Reduce the alpha of the destination with 1 - opacity
|
|
|
|
XRenderComposite(dpy, PictOpOutReverse, alpha, None, destination.x11PictureHandle(),
|
|
|
|
0, 0, 0, 0, 0, 0, destination.width(), destination.height());
|
|
|
|
|
|
|
|
// Add source * opacity to the destination
|
|
|
|
XRenderComposite(dpy, PictOpAdd, source.x11PictureHandle(), alpha,
|
|
|
|
destination.x11PictureHandle(),
|
|
|
|
toRect.x(), toRect.y(), 0, 0, 0, 0, destination.width(), destination.height());
|
|
|
|
|
|
|
|
XRenderFreePicture(dpy, alpha);
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
// Fall back to using QRasterPaintEngine to do the transition.
|
2010-09-23 21:59:54 +02:00
|
|
|
QImage under(pixmapSize, QImage::Format_ARGB32_Premultiplied);
|
2010-09-22 21:35:47 +02:00
|
|
|
under.fill(Qt::transparent);
|
2010-09-23 21:59:54 +02:00
|
|
|
QImage over(pixmapSize, QImage::Format_ARGB32_Premultiplied);
|
2010-09-22 21:35:47 +02:00
|
|
|
over.fill(Qt::transparent);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QPainter p;
|
|
|
|
p.begin(&over);
|
2010-09-22 21:35:47 +02:00
|
|
|
p.drawPixmap(targetRect, to);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
p.fillRect(over.rect(), color);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
p.begin(&under);
|
2010-09-22 21:35:47 +02:00
|
|
|
p.drawPixmap(startRect, from);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
|
2010-09-22 21:35:47 +02:00
|
|
|
p.fillRect(startRect, color);
|
2008-11-04 00:08:39 +01:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Plus);
|
|
|
|
p.drawImage(toRect.topLeft(), over);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
return QPixmap::fromImage(under);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // PaintUtils namespace
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|