• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

paintutils.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2005 by Aaron Seigo <aseigo@kde.org>
00003  *   Copyright 2008 by Andrew Lake <jamboarder@yahoo.com>
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU Library General Public License as
00007  *   published by the Free Software Foundation; either version 2, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 #include <paintutils.h>
00022 
00023 #include <QImage>
00024 #include <QPainter>
00025 #include <QPixmap>
00026 
00027 #include "effects/blur.cpp"
00028 
00029 namespace Plasma
00030 {
00031 
00032 namespace PaintUtils
00033 {
00034 
00035 void shadowBlur(QImage &image, int radius, const QColor &color)
00036 {
00037     if (radius < 1)
00038         return;
00039 
00040     expblur<16, 7>(image, radius);
00041 
00042     QPainter p(&image);
00043     p.setCompositionMode(QPainter::CompositionMode_SourceIn);
00044     p.fillRect(image.rect(), color);
00045 }
00046 
00047 QPixmap shadowText(QString text, QColor textColor, QColor shadowColor, QPoint offset, int radius)
00048 {
00049     // Draw text 
00050     QFontMetrics fm(text);
00051     QRect textRect = fm.boundingRect(text);
00052     QPixmap textPixmap(textRect.size());
00053     textPixmap.fill(Qt::transparent);
00054     QPainter p(&textPixmap);
00055     p.setPen(textColor);
00056     p.drawText(textPixmap.rect(), Qt::AlignLeft, text);
00057     p.end();
00058 
00059     //Draw blurred shadow
00060     QImage img(textRect.size() + QSize(radius * 2, radius * 2),
00061     QImage::Format_ARGB32_Premultiplied);
00062     img.fill(Qt::transparent);
00063     p.begin(&img);
00064     p.drawImage(QPoint(radius,radius), textPixmap.toImage());
00065     p.end();
00066     shadowBlur(img, radius, shadowColor);
00067 
00068     //Compose text and shadow
00069     int addSizeX;
00070     int addSizeY;
00071     if (offset.x() > radius) {
00072         addSizeX = abs(offset.x()) - radius;
00073     } else {
00074         addSizeX = 0;
00075     }
00076     if (offset.y() > radius) {
00077         addSizeY = abs(offset.y()) - radius;
00078     } else {
00079         addSizeY = 0;
00080     }
00081 
00082     QPixmap finalPixmap(img.size() + QSize(addSizeX, addSizeY));
00083     finalPixmap.fill(Qt::transparent);
00084     p.begin(&finalPixmap);
00085     QPointF offsetF(offset);
00086     QPointF textTopLeft(finalPixmap.rect().topLeft() + 
00087             QPointF ((finalPixmap.width() - textPixmap.width()) / 2.0, (finalPixmap.height() - textPixmap.height()) / 2.0) -
00088             (offsetF / 2.0));     
00089     QPointF shadowTopLeft(finalPixmap.rect().topLeft() + 
00090             QPointF ((finalPixmap.width() - img.width()) / 2.0, (finalPixmap.height() - img.height()) / 2.0) + 
00091             (offsetF / 2.0));     
00092 
00093     p.drawImage(shadowTopLeft, img);
00094     p.drawPixmap(textTopLeft, textPixmap);
00095     p.end();
00096 
00097     return finalPixmap;
00098 }
00099 
00100 QPainterPath roundedRectangle(const QRectF& rect, qreal radius)
00101 {
00102     QPainterPath path(QPointF(rect.left(), rect.top() + radius));
00103     path.quadTo(rect.left(), rect.top(), rect.left() + radius, rect.top());         // Top left corner
00104     path.lineTo(rect.right() - radius, rect.top());                                 // Top side
00105     path.quadTo(rect.right(), rect.top(), rect.right(), rect.top() + radius);       // Top right corner
00106     path.lineTo(rect.right(), rect.bottom() - radius);                              // Right side
00107     path.quadTo(rect.right(), rect.bottom(), rect.right() - radius, rect.bottom()); // Bottom right corner
00108     path.lineTo(rect.left() + radius, rect.bottom());                               // Bottom side
00109     path.quadTo(rect.left(), rect.bottom(), rect.left(), rect.bottom() - radius);   // Bottom left corner
00110     path.closeSubpath();
00111 
00112     return path;
00113 }
00114 
00115 } // PaintUtils namespace
00116 
00117 } // Plasma namespace
00118 

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal