• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KStyles

gradients.cpp

Go to the documentation of this file.
00001 /* Keramik Style for KDE4, gradient routines..
00002    Copyright (c) 2002       Malte Starostik <malte@kde.org>
00003              (c) 2002, 2005 Maksim Orlovich <maksim@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "gradients.h"
00022 #include "colorutil.h"
00023 
00024 #include <QtGui/QPainter>
00025 #include <QtCore/QRect>
00026 #include <QtGui/QColor>
00027 
00028 #include <QtGui/QImage>
00029 #include <QtCore/QCache>
00030 #include <qimageblitz.h>
00031 
00032 namespace
00033 {
00034     struct GradientCacheEntry
00035     {
00036         QPixmap* m_pixmap;
00037         QRgb     m_color;
00038         bool     m_menu;
00039         int      m_width;
00040         int      m_height;
00041         
00042         GradientCacheEntry(int width, int height, const QColor& color, bool menu):
00043             m_pixmap(0), m_color(color.rgb()), m_menu(menu), m_width(width), m_height(height)
00044         {}
00045         
00046         int key()
00047         {
00048             return (int)m_menu ^ m_width ^ (m_height << 16) ^ ((m_color)<<8);
00049         }
00050         
00051         bool operator == (const GradientCacheEntry& other)
00052         {
00053             return ((m_width == other.m_width) &&
00054                     (m_height == other.m_height) &&
00055                     (m_menu == other.m_menu) &&
00056                     (m_color == other.m_color));
00057         }
00058         
00059         ~GradientCacheEntry()
00060         {
00061             delete m_pixmap;
00062         }
00063         
00064     };
00065     
00066     
00067     static QCache<int, GradientCacheEntry> cache(65636);
00068     
00069 }
00070 
00071 using namespace Keramik;
00072 
00073 void GradientPainter::releaseCache()
00074 {
00075     cache.clear();
00076 }
00077 
00078 void GradientPainter::renderGradient( QPainter* p, const QRect& r, const QColor& c,
00079                                         bool horizontal, bool menu, int px, int py,
00080                                         int pwidth, int pheight)
00081 {
00082     int width = r.width(), height = r.height();
00083     if (pwidth != -1) width = pwidth;
00084     if (pheight != -1) height = pheight;
00085     
00086     if (horizontal)
00087         width  = 18;
00088     else
00089         height = 18;
00090     
00091     GradientCacheEntry entry (width, height, c, menu);
00092     GradientCacheEntry* cacheEntry = 0;
00093     
00094     int key = entry.key();
00095     
00096     if ((cacheEntry = cache.take(key)))
00097     {
00098         if (entry == *cacheEntry)
00099         {
00100             p->drawTiledPixmap(r, *cacheEntry->m_pixmap, horizontal? QPoint(0,py): QPoint(px,0));
00101             return;
00102         }
00103     }
00104     
00105     
00106     if (horizontal)
00107     {
00108         QPixmap* pix = new QPixmap(18, height);
00109         
00110         if (menu)
00111         {
00112             QImage gr = Blitz::gradient(QSize(4,height), c.light(93), ColorUtil::lighten(c,109), Blitz::VerticalGradient );
00113             QPixmap grT(QPixmap::fromImage(gr));
00114             QPainter p2(pix);
00115             p2.drawTiledPixmap(0,0, 18, height, grT);
00116             p2.end();
00117         }
00118         else
00119         {
00120             int h1 = 3 * height/4;
00121             int h2 = height - h1;
00122 
00123             QImage top = Blitz::gradient(QSize(4,h1),  ColorUtil::lighten(c,110), c.light(94), Blitz::VerticalGradient );
00124             QImage bot = Blitz::gradient(QSize(4,h2), c.light(94), ColorUtil::lighten(c,109), Blitz::VerticalGradient );
00125             
00126             QPixmap topT(QPixmap::fromImage(top));
00127             QPixmap botT(QPixmap::fromImage(bot));
00128             
00129             QPainter p2(pix);
00130             p2.drawTiledPixmap(0, 0, 18, h1, topT);
00131             p2.drawTiledPixmap(0, h1, 18, h2, botT);
00132             p2.end();
00133         }
00134         
00135         entry.m_pixmap = pix;
00136     }
00137     else
00138     {
00139         QPixmap* pix = new QPixmap(width, 18);
00140         
00141         int h1 = 3 * width/4;
00142         int h2 = width - h1;
00143         
00144         QImage top = Blitz::gradient(QSize(h1,4), ColorUtil::lighten(c,110), c.light(94), Blitz::HorizontalGradient );
00145         QImage bot = Blitz::gradient(QSize(h2,4), c.light(94), ColorUtil::lighten(c,109), Blitz::HorizontalGradient );
00146         
00147         QPixmap topT(QPixmap::fromImage(top));
00148         QPixmap botT(QPixmap::fromImage(bot));
00149 
00150         QPainter p2(pix);
00151         p2.drawTiledPixmap(0,  0, h1, 18, topT);
00152         p2.drawTiledPixmap(h1, 0, h2, 18, botT);
00153         p2.end();
00154         
00155         entry.m_pixmap = pix;
00156 
00157     }
00158         
00159     
00160     GradientCacheEntry* imgToAdd = new GradientCacheEntry(entry);
00161     cache.insert(imgToAdd->key(), imgToAdd,
00162         imgToAdd->m_pixmap->width() * imgToAdd->m_pixmap->height()*
00163         imgToAdd->m_pixmap->depth()/8);
00164         
00165     p->drawTiledPixmap(r, *imgToAdd->m_pixmap, horizontal? QPoint(0,py): QPoint(px,0));
00166     
00167     entry.m_pixmap = 0;//Don't free too early..
00168 }
00169 
00170 // vim: ts=4 sw=4 noet

KStyles

Skip menu "KStyles"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal