libplasma
paintutils.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
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());
00104 path.lineTo(rect.right() - radius, rect.top());
00105 path.quadTo(rect.right(), rect.top(), rect.right(), rect.top() + radius);
00106 path.lineTo(rect.right(), rect.bottom() - radius);
00107 path.quadTo(rect.right(), rect.bottom(), rect.right() - radius, rect.bottom());
00108 path.lineTo(rect.left() + radius, rect.bottom());
00109 path.quadTo(rect.left(), rect.bottom(), rect.left(), rect.bottom() - radius);
00110 path.closeSubpath();
00111
00112 return path;
00113 }
00114
00115 }
00116
00117 }
00118