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>
34 struct GradientCacheEntry
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
)
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
));
67 static QCache
<int, GradientCacheEntry
> cache(65636);
71 using namespace Keramik
;
73 void GradientPainter::releaseCache()
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
;
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));
108 QPixmap
* pix
= new QPixmap(18, height
);
112 QImage gr
= Blitz::gradient(QSize(4,height
), c
.light(93), ColorUtil::lighten(c
,109), Blitz::VerticalGradient
);
113 QPixmap
grT(QPixmap::fromImage(gr
));
115 p2
.drawTiledPixmap(0,0, 18, height
, grT
);
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
));
130 p2
.drawTiledPixmap(0, 0, 18, h1
, topT
);
131 p2
.drawTiledPixmap(0, h1
, 18, h2
, botT
);
135 entry
.m_pixmap
= pix
;
139 QPixmap
* pix
= new QPixmap(width
, 18);
141 int h1
= 3 * width
/4;
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
));
151 p2
.drawTiledPixmap(0, 0, h1
, 18, topT
);
152 p2
.drawTiledPixmap(h1
, 0, h2
, 18, botT
);
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