~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kstyles/keramik/gradients.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include "gradients.h"
 
22
#include "colorutil.h"
 
23
 
 
24
#include <QtGui/QPainter>
 
25
#include <QtCore/QRect>
 
26
#include <QtGui/QColor>
 
27
 
 
28
#include <QtGui/QImage>
 
29
#include <QtCore/QCache>
 
30
#include <qimageblitz.h>
 
31
 
 
32
namespace
 
33
{
 
34
        struct GradientCacheEntry
 
35
        {
 
36
                QPixmap* m_pixmap;
 
37
                QRgb     m_color;
 
38
                bool     m_menu;
 
39
                int      m_width;
 
40
                int      m_height;
 
41
                
 
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)
 
44
                {}
 
45
                
 
46
                int key()
 
47
                {
 
48
                        return (int)m_menu ^ m_width ^ (m_height << 16) ^ ((m_color)<<8);
 
49
                }
 
50
                
 
51
                bool operator == (const GradientCacheEntry& other)
 
52
                {
 
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));
 
57
                }
 
58
                
 
59
                ~GradientCacheEntry()
 
60
                {
 
61
                        delete m_pixmap;
 
62
                }
 
63
                
 
64
        };
 
65
        
 
66
        
 
67
        static QCache<int, GradientCacheEntry> cache(65636);
 
68
        
 
69
}
 
70
 
 
71
using namespace Keramik;
 
72
 
 
73
void GradientPainter::releaseCache()
 
74
{
 
75
        cache.clear();
 
76
}
 
77
 
 
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)
 
81
{
 
82
        int width = r.width(), height = r.height();
 
83
        if (pwidth != -1) width = pwidth;
 
84
        if (pheight != -1) height = pheight;
 
85
        
 
86
        if (horizontal)
 
87
                width  = 18;
 
88
        else
 
89
                height = 18;
 
90
        
 
91
        GradientCacheEntry entry (width, height, c, menu);
 
92
        GradientCacheEntry* cacheEntry = 0;
 
93
        
 
94
        int key = entry.key();
 
95
        
 
96
        if ((cacheEntry = cache.take(key)))
 
97
        {
 
98
                if (entry == *cacheEntry)
 
99
                {
 
100
                        p->drawTiledPixmap(r, *cacheEntry->m_pixmap, horizontal? QPoint(0,py): QPoint(px,0));
 
101
                        return;
 
102
                }
 
103
        }
 
104
        
 
105
        
 
106
        if (horizontal)
 
107
        {
 
108
                QPixmap* pix = new QPixmap(18, height);
 
109
                
 
110
                if (menu)
 
111
                {
 
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();
 
117
                }
 
118
                else
 
119
                {
 
120
                        int h1 = 3 * height/4;
 
121
                        int h2 = height - h1;
 
122
 
 
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 );
 
125
                        
 
126
                        QPixmap topT(QPixmap::fromImage(top));
 
127
                        QPixmap botT(QPixmap::fromImage(bot));
 
128
                        
 
129
                        QPainter p2(pix);
 
130
                        p2.drawTiledPixmap(0, 0, 18, h1, topT);
 
131
                        p2.drawTiledPixmap(0, h1, 18, h2, botT);
 
132
                        p2.end();
 
133
                }
 
134
                
 
135
                entry.m_pixmap = pix;
 
136
        }
 
137
        else
 
138
        {
 
139
                QPixmap* pix = new QPixmap(width, 18);
 
140
                
 
141
                int h1 = 3 * width/4;
 
142
                int h2 = width - h1;
 
143
                
 
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 );
 
146
                
 
147
                QPixmap topT(QPixmap::fromImage(top));
 
148
                QPixmap botT(QPixmap::fromImage(bot));
 
149
 
 
150
                QPainter p2(pix);
 
151
                p2.drawTiledPixmap(0,  0, h1, 18, topT);
 
152
                p2.drawTiledPixmap(h1, 0, h2, 18, botT);
 
153
                p2.end();
 
154
                
 
155
                entry.m_pixmap = pix;
 
156
 
 
157
        }
 
158
                
 
159
        
 
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);
 
164
                
 
165
        p->drawTiledPixmap(r, *imgToAdd->m_pixmap, horizontal? QPoint(0,py): QPoint(px,0));
 
166
        
 
167
        entry.m_pixmap = 0;//Don't free too early..
 
168
}
 
169
 
 
170
// vim: ts=4 sw=4 noet