Plasma::ImageEffects namespace. Add shadowBlur and shadowText functions.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=827065
This commit is contained in:
Andrew Lake 2008-07-02 08:02:27 +00:00
parent 21b148b963
commit 4675e518b5
3 changed files with 144 additions and 0 deletions

View File

@ -39,6 +39,7 @@ set(plasma_LIB_SRCS
dialog.cpp
#FOR FUTURE
#layouts/borderlayout.cpp
imageeffects.cpp
packages.cpp
panelsvg.cpp
paneltoolbox.cpp
@ -127,6 +128,7 @@ set(plasma_LIB_INCLUDES
dataenginemanager.h
delegate.h
dialog.h
imageeffects.h
panelsvg.h
plasma.h
plasma_export.h
@ -200,6 +202,7 @@ includes/Dialog
includes/Flash
includes/GroupBox
includes/Icon
includes/ImageEffects
includes/Label
includes/LineEdit
includes/Meter

82
imageeffects.cpp Normal file
View File

@ -0,0 +1,82 @@
/*
* 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 <imageeffects.h>
#include "effects/blur.cpp"
#include <QImage>
#include <QPainter>
#include <QPixmap>
namespace Plasma
{
namespace ImageEffects
{
void shadowBlur(QImage &image, int radius, const QColor &color)
{
if (radius < 1)
return;
expblur<16, 7>(image, radius);
QPainter p(&image);
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
p.fillRect(image.rect(), color);
}
QPixmap shadowText(QString text, QColor textColor, QColor shadowColor, QPoint offset, int radius)
{
// Draw text
QPixmap pixmap(10,10);
QPainter p(&pixmap);
QRect textRect = p.fontMetrics().boundingRect(text);
p.end();
QPixmap textPixmap(textRect.size());
textPixmap.fill(Qt::transparent);
p.begin(&textPixmap);
p.setPen(textColor);
p.drawText(textPixmap.rect(), Qt::AlignLeft, text);
p.end();
//Draw blurred shadow
QImage img(textRect.size() + QSize(radius * 2, radius * 2),
QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
p.begin(&img);
p.drawImage(QPoint(0,0), textPixmap.toImage());
p.end();
shadowBlur(img, radius, shadowColor);
//Compose text and shadow
QPixmap finalPixmap(img.size() + QSize(offset.x(), offset.y()));
finalPixmap.fill(Qt::transparent);
p.begin(&finalPixmap);
p.drawImage(finalPixmap.rect().topLeft() + QPoint(offset.x(), offset.y()), img);
p.drawPixmap(finalPixmap.rect().topLeft(), textPixmap);
p.end();
return finalPixmap;
}
} //ImageEffects namespace
} // Plasma namespace

59
imageeffects.h Normal file
View File

@ -0,0 +1,59 @@
/*
* 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.
*/
#ifndef IMAGEEFFECTS_H
#define IMAGEEFFECTS_H
#include <QtGui/QGraphicsItem>
#include <QtGui/QPainterPath>
#include <plasma/plasma_export.h>
#include "theme.h"
namespace Plasma
{
/**
* Namespace for all Image Effects specific to Plasma
**/
namespace ImageEffects
{
/**
* Creates a blurred shadow of the supplied image.
*/
PLASMA_EXPORT void shadowBlur(QImage &image, int radius, const QColor &color);
/**
* Returns a pixmap containing text with blurred shadow.
* Text and shadow colors default to Plasma::Theme colors.
*/
PLASMA_EXPORT QPixmap shadowText(QString text,
QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor),
QColor shadowColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor),
QPoint offset = QPoint(1,1),
int radius = 2);
} // ImageEffects namespace
} // Plasma namespace
#endif