Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kstyles / keramik / gradients.cpp
blobc7e75b841c5f9a53f87cf1a0632603b3a8a2041c
1 /* Keramik Style for KDE4, gradient routines..
2 Copyright (c) 2002 Malte Starostik <malte@kde.org>
3 (c) 2002, 2005 Maksim Orlovich <maksim@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "gradients.h"
22 #include "colorutil.h"
24 #include <QtGui/QPainter>
25 #include <QtCore/QRect>
26 #include <QtGui/QColor>
28 #include <QtGui/QImage>
29 #include <QtCore/QCache>
30 #include <qimageblitz.h>
32 namespace
34 struct GradientCacheEntry
36 QPixmap* m_pixmap;
37 QRgb m_color;
38 bool m_menu;
39 int m_width;
40 int m_height;
42 GradientCacheEntry(int width, int height, const QColor& color, bool menu):
43 m_pixmap(0), m_color(color.rgb()), m_menu(menu), m_width(width), m_height(height)
46 int key()
48 return (int)m_menu ^ m_width ^ (m_height << 16) ^ ((m_color)<<8);
51 bool operator == (const GradientCacheEntry& other)
53 return ((m_width == other.m_width) &&
54 (m_height == other.m_height) &&
55 (m_menu == other.m_menu) &&
56 (m_color == other.m_color));
59 ~GradientCacheEntry()
61 delete m_pixmap;
67 static QCache<int, GradientCacheEntry> cache(65636);
71 using namespace Keramik;
73 void GradientPainter::releaseCache()
75 cache.clear();
78 void GradientPainter::renderGradient( QPainter* p, const QRect& r, const QColor& c,
79 bool horizontal, bool menu, int px, int py,
80 int pwidth, int pheight)
82 int width = r.width(), height = r.height();
83 if (pwidth != -1) width = pwidth;
84 if (pheight != -1) height = pheight;
86 if (horizontal)
87 width = 18;
88 else
89 height = 18;
91 GradientCacheEntry entry (width, height, c, menu);
92 GradientCacheEntry* cacheEntry = 0;
94 int key = entry.key();
96 if ((cacheEntry = cache.take(key)))
98 if (entry == *cacheEntry)
100 p->drawTiledPixmap(r, *cacheEntry->m_pixmap, horizontal? QPoint(0,py): QPoint(px,0));
101 return;
106 if (horizontal)
108 QPixmap* pix = new QPixmap(18, height);
110 if (menu)
112 QImage gr = Blitz::gradient(QSize(4,height), c.light(93), ColorUtil::lighten(c,109), Blitz::VerticalGradient );
113 QPixmap grT(QPixmap::fromImage(gr));
114 QPainter p2(pix);
115 p2.drawTiledPixmap(0,0, 18, height, grT);
116 p2.end();
118 else
120 int h1 = 3 * height/4;
121 int h2 = height - h1;
123 QImage top = Blitz::gradient(QSize(4,h1), ColorUtil::lighten(c,110), c.light(94), Blitz::VerticalGradient );
124 QImage bot = Blitz::gradient(QSize(4,h2), c.light(94), ColorUtil::lighten(c,109), Blitz::VerticalGradient );
126 QPixmap topT(QPixmap::fromImage(top));
127 QPixmap botT(QPixmap::fromImage(bot));
129 QPainter p2(pix);
130 p2.drawTiledPixmap(0, 0, 18, h1, topT);
131 p2.drawTiledPixmap(0, h1, 18, h2, botT);
132 p2.end();
135 entry.m_pixmap = pix;
137 else
139 QPixmap* pix = new QPixmap(width, 18);
141 int h1 = 3 * width/4;
142 int h2 = width - h1;
144 QImage top = Blitz::gradient(QSize(h1,4), ColorUtil::lighten(c,110), c.light(94), Blitz::HorizontalGradient );
145 QImage bot = Blitz::gradient(QSize(h2,4), c.light(94), ColorUtil::lighten(c,109), Blitz::HorizontalGradient );
147 QPixmap topT(QPixmap::fromImage(top));
148 QPixmap botT(QPixmap::fromImage(bot));
150 QPainter p2(pix);
151 p2.drawTiledPixmap(0, 0, h1, 18, topT);
152 p2.drawTiledPixmap(h1, 0, h2, 18, botT);
153 p2.end();
155 entry.m_pixmap = pix;
160 GradientCacheEntry* imgToAdd = new GradientCacheEntry(entry);
161 cache.insert(imgToAdd->key(), imgToAdd,
162 imgToAdd->m_pixmap->width() * imgToAdd->m_pixmap->height()*
163 imgToAdd->m_pixmap->depth()/8);
165 p->drawTiledPixmap(r, *imgToAdd->m_pixmap, horizontal? QPoint(0,py): QPoint(px,0));
167 entry.m_pixmap = 0;//Don't free too early..
170 // vim: ts=4 sw=4 noet