libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / lib / paintutils.cpp
blob4a7de9dfe232de9579fcb305913e434a845fec7d
1 /*
2 Gwenview: an image viewer
3 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "paintutils.h"
22 #include <math.h>
24 // Qt
25 #include <QPainter>
26 #include <QPainterPath>
27 #include <QPixmap>
28 #include <QRectF>
30 namespace Gwenview {
32 namespace PaintUtils {
35 // Copied from KFileItemDelegate
36 QPainterPath roundedRectangle(const QRectF &rect, qreal radius) {
37 QPainterPath path(QPointF(rect.left(), rect.top() + radius));
38 path.quadTo(rect.left(), rect.top(), rect.left() + radius, rect.top()); // Top left corner
39 path.lineTo(rect.right() - radius, rect.top()); // Top side
40 path.quadTo(rect.right(), rect.top(), rect.right(), rect.top() + radius); // Top right corner
41 path.lineTo(rect.right(), rect.bottom() - radius); // Right side
42 path.quadTo(rect.right(), rect.bottom(), rect.right() - radius, rect.bottom()); // Bottom right corner
43 path.lineTo(rect.left() + radius, rect.bottom()); // Bottom side
44 path.quadTo(rect.left(), rect.bottom(), rect.left(), rect.bottom() - radius); // Bottom left corner
45 path.closeSubpath();
47 return path;
51 QPixmap generateFuzzyRect(const QSize& size, const QColor& color, int radius) {
52 QPixmap pix(size);
53 const QColor transparent(0, 0, 0, 0);
54 pix.fill(transparent);
56 QPainter painter(&pix);
57 painter.setRenderHint(QPainter::Antialiasing, true);
59 // Fill middle
60 painter.fillRect(pix.rect().adjusted(radius, radius, -radius, -radius), color);
62 // Corners
63 QRadialGradient gradient;
64 gradient.setColorAt(0, color);
65 gradient.setColorAt(1, transparent);
66 gradient.setRadius(radius);
67 QPoint center;
69 // Top Left
70 center = QPoint(radius, radius);
71 gradient.setCenter(center);
72 gradient.setFocalPoint(center);
73 painter.fillRect(0, 0, radius, radius, gradient);
75 // Top right
76 center = QPoint(size.width() - radius, radius);
77 gradient.setCenter(center);
78 gradient.setFocalPoint(center);
79 painter.fillRect(center.x(), 0, radius, radius, gradient);
81 // Bottom left
82 center = QPoint(radius, size.height() - radius);
83 gradient.setCenter(center);
84 gradient.setFocalPoint(center);
85 painter.fillRect(0, center.y(), radius, radius, gradient);
87 // Bottom right
88 center = QPoint(size.width() - radius, size.height() - radius);
89 gradient.setCenter(center);
90 gradient.setFocalPoint(center);
91 painter.fillRect(center.x(), center.y(), radius, radius, gradient);
93 // Borders
94 QLinearGradient linearGradient;
95 linearGradient.setColorAt(0, color);
96 linearGradient.setColorAt(1, transparent);
98 // Top
99 linearGradient.setStart(0, radius);
100 linearGradient.setFinalStop(0, 0);
101 painter.fillRect(radius, 0, size.width() - 2*radius, radius, linearGradient);
103 // Bottom
104 linearGradient.setStart(0, size.height() - radius);
105 linearGradient.setFinalStop(0, size.height());
106 painter.fillRect(radius, int(linearGradient.start().y()), size.width() - 2*radius, radius, linearGradient);
108 // Left
109 linearGradient.setStart(radius, 0);
110 linearGradient.setFinalStop(0, 0);
111 painter.fillRect(0, radius, radius, size.height() - 2*radius, linearGradient);
113 // Right
114 linearGradient.setStart(size.width() - radius, 0);
115 linearGradient.setFinalStop(size.width(), 0);
116 painter.fillRect(int(linearGradient.start().x()), radius, radius, size.height() - 2*radius, linearGradient);
117 return pix;
121 QColor adjustedHsv(const QColor& color, int deltaH, int deltaS, int deltaV) {
122 int hue, saturation, value;
123 color.getHsv(&hue, &saturation, &value);
124 return QColor::fromHsv(
125 qBound(0, hue + deltaH, 359),
126 qBound(0, saturation + deltaS, 255),
127 qBound(0, value + deltaV, 255)
132 QColor alphaAdjustedF(const QColor& color, qreal alphaF) {
133 QColor tmp = color;
134 tmp.setAlphaF(alphaF);
135 return tmp;
138 QRect containingRect(const QRectF& rectF) {
139 return QRect(
140 QPoint(
141 qRound(floor(rectF.left())),
142 qRound(floor(rectF.top()))
144 QPoint(
145 qRound(ceil(rectF.right() - 1.)),
146 qRound(ceil(rectF.bottom() - 1.))
149 // Note: QRect::right = left + width - 1, while QRectF::right = left + width
152 } // namespace
153 } // namespace