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

KStyles

scrollbar.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
00003  * Copyright 2007 Casper Boemann <cbr@boemann.dk>
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 version 2 as published by the Free Software Foundation.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "scrollbar.h"
00021 
00022 #include <KColorUtils>
00023 #include <KColorScheme>
00024 
00025 #include <QtGui/QPainter>
00026 #include <QtGui/QLinearGradient>
00027 
00028 inline QColor alphaColor(QColor color, double alpha)
00029 {
00030     color.setAlphaF(alpha);
00031     return color;
00032 }
00033 
00034 OxygenScrollbar::OxygenScrollbar(const QColor &c, double contrast) : color(c),
00035     light(KColorScheme::shade(c, KColorScheme::LightShade, contrast - 0.5)),
00036     mid(KColorScheme::shade(c, KColorScheme::MidShade, contrast)),
00037     dark(KColorScheme::shade(c, KColorScheme::DarkShade, contrast - 0.5)),
00038     shadow(KColorScheme::shade(c, KColorScheme::ShadowShade, contrast)),
00039     highlight(Qt::white)
00040 {
00041     double y = KColorUtils::luma(color);
00042     if (y > KColorUtils::luma(light)) {
00043         light = Qt::white;
00044         dark = KColorScheme::shade(c, KColorScheme::DarkShade, contrast);
00045     }
00046 }
00047 
00048 void OxygenScrollbar::mask(QPainter &p, const QRectF &rect) const
00049 {
00050     double w = rect.width();
00051     double h = rect.height();
00052 
00053     // drawRoundRect is too bloody hard to control to get the corners perfectly
00054     // square (i.e. circles not ellipses), so draw the mask in parts with real
00055     // circles
00056     p.setBrush(Qt::black); // color doesn't matter
00057     p.drawRect(rect.adjusted(7,0,-7,0));
00058     p.drawRect(rect.adjusted(0,7,0,-7));
00059     p.drawEllipse(QRectF(0,0,14,14));
00060     p.drawEllipse(QRectF(w-14,0,14,14));
00061     p.drawEllipse(QRectF(0,h-14,14,14));
00062     p.drawEllipse(QRectF(w-14,h-14,14,14));
00063 
00064     // never draw outside the mask
00065     p.setCompositionMode(QPainter::CompositionMode_SourceAtop);
00066 }
00067 
00068 QLinearGradient OxygenScrollbar::baseGradient(double width, Qt::Orientation orient) const
00069 {
00070     double x = 0.0, y1 = width, y2 = width;
00071     if (orient == Qt::Vertical)
00072         x = width * 0.6;
00073     else
00074         y2 = width * 0.4;
00075 
00076     QLinearGradient gradient(0, y1, x, y2);
00077     gradient.setColorAt(0.0, color);
00078     gradient.setColorAt(1.0, mid);
00079 
00080     return gradient;
00081 }
00082 
00083 QLinearGradient OxygenScrollbar::shineGradient(double width, Qt::Orientation orient) const
00084 {
00085     double x = 0.0, y1 = -width, y2 = -width;
00086     if (orient == Qt::Vertical)
00087         x = width * 2.0;
00088     else
00089         y1 = width;
00090 
00091     QLinearGradient gradient(0, y1, x, y2);
00092     gradient.setColorAt(0.0, light);
00093     gradient.setColorAt(0.5, alphaColor(color, 0.5));
00094     gradient.setColorAt(1.0, color);
00095 
00096     return gradient;
00097 }
00098 
00099 QLinearGradient OxygenScrollbar::shimmerGradient(double offset, Qt::Orientation orient) const
00100 {
00101     double x = 0.0, y = 0.0, xo = 0.0, yo = 0.0;
00102     if (orient == Qt::Vertical) {
00103         yo = offset;
00104         x = 14.4/2.0;
00105         y = 43.2/2.0;
00106     } else {
00107         xo = offset;
00108         x = 43.2/2.0;
00109         y = -14.4/2.0;
00110     }
00111 
00112     // should tile every 48 units, with 1:3 slope
00113     QLinearGradient gradient(xo, yo, x+xo, y+yo);
00114     gradient.setSpread(QGradient::ReflectSpread);
00115     gradient.setColorAt(0.0, alphaColor(dark, 0.40));
00116     gradient.setColorAt(0.6, alphaColor(dark, 0.10));
00117     gradient.setColorAt(1.0, alphaColor(dark, 0.00));
00118 
00119     return gradient;
00120 }
00121 
00122 QLinearGradient OxygenScrollbar::dimGradient(Qt::Orientation orient) const
00123 {
00124     int x = 0, y = 0;
00125     if (orient == Qt::Vertical)
00126         y = 3*22;
00127     else
00128         x = 3*22;
00129 
00130     QLinearGradient gradient(0, 0, x, y);
00131     gradient.setSpread(QGradient::ReflectSpread);
00132     gradient.setColorAt(0.00, alphaColor(dark, 1.0));
00133     gradient.setColorAt(0.19, alphaColor(dark, 0.3));
00134     gradient.setColorAt(0.27, alphaColor(dark, 0.0));
00135 
00136     return gradient;
00137 }
00138 
00139 QPixmap OxygenScrollbar::bevel(int width, int height, double w, double h, int rx, int ry) const
00140 {
00141     QPixmap pixmap(width, height);
00142     pixmap.fill(Qt::transparent);
00143 
00144     QPainter p(&pixmap);
00145     p.setRenderHint(QPainter::Antialiasing);
00146     p.setPen(Qt::NoPen);
00147     p.setWindow(0, 0, int(w), int(h));
00148 
00149     QRectF rect(0, 0, w, h);
00150 
00151     // anti-highlight
00152     QLinearGradient ahGradient(0, 0, 0, 8);
00153     ahGradient.setColorAt(0.0, dark);
00154     ahGradient.setColorAt(0.9, dark);
00155     ahGradient.setColorAt(1.0, shadow);
00156     p.setBrush(ahGradient);
00157     p.drawRect(rect);
00158 
00159     // anti-highlight mask
00160     p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
00161     p.setBrush(Qt::black);
00162     p.drawRoundRect(rect.adjusted(0, 1, 0, -1), rx, ry);
00163 
00164     // bevel
00165     QLinearGradient bevelGradient(0, 0, 0, 8);
00166     bevelGradient.setColorAt(0.0, alphaColor(highlight, 0.4));
00167     bevelGradient.setColorAt(0.6, alphaColor(highlight, 0.4));
00168     bevelGradient.setColorAt(1.0, alphaColor(shadow, 0.8));
00169     p.setBrush(bevelGradient);
00170     p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
00171     p.drawRect(rect);
00172 
00173     // mask
00174     p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
00175     p.setBrush(Qt::black);
00176     p.drawRoundRect(rect.adjusted(1, 2.5, -1, -1.4), rx, ry);
00177 
00178     p.end();
00179     return pixmap;
00180 }
00181 
00182 TileSet* OxygenScrollbar::vertical(int size, int width, int offset) const
00183 {
00184     int s = size/2;
00185     int length = s*22;
00186     double w = 12.0 * double(width)/double(s*2);
00187     double o = -12.0 * double(offset) / double(size);
00188     const int h = 6*22;
00189 
00190     QPixmap pixmap(width, length);
00191     pixmap.fill(Qt::transparent);
00192 
00193     QPainter p(&pixmap);
00194     p.setRenderHint(QPainter::Antialiasing);
00195     p.setPen(Qt::NoPen);
00196     p.setWindow(0, 0, int(w), h);
00197     QRectF rect(0, 0, w, h);
00198 
00199     // mask; never draw outside this, hence mask() sets SourceAtop
00200     mask(p, rect);
00201 
00202     // base
00203     p.setBrush(baseGradient(w, Qt::Vertical));
00204     p.drawRect(rect);
00205 
00206     // shine
00207     p.setBrush(shineGradient(w, Qt::Vertical));
00208     p.drawRoundRect(QRectF(w- int(w*0.45)-0.5, 0, int(w*0.45), h), int(2000.0/w), 12);
00209     p.setClipping(false);
00210 
00211     // shimmer
00212     p.setBrush(shimmerGradient(o, Qt::Vertical));
00213     p.drawRect(rect);
00214 
00215     // dim edges
00216     p.setBrush(dimGradient(Qt::Vertical));
00217     p.drawRect(rect);
00218 
00219     // highlight
00220     p.setBrush(alphaColor(highlight, 0.2));
00221     p.drawRoundRect(QRectF(w-3, 7, 1.5, h-14), 100, 5);
00222     p.drawRoundRect(QRectF(1.5, 7, 1.5, h-14), 100, 5);
00223 
00224     // bevel
00225     p.setWindow(0, 0, width, length);
00226     p.drawPixmap(0, 0, bevel(width, length, w, h, int(1400.0/w), 9));
00227 
00228     return new TileSet(pixmap, 1, s*3, width-2, s*16);
00229 }
00230 
00231 TileSet* OxygenScrollbar::horizontal(int size, int width, int offset) const
00232 {
00233     int s = size/2;
00234     int length = s*22;
00235     double h = 12.0 * double(width)/double(s*2);
00236     double o = -12.0 * double(offset) / double(size);
00237     const int w = 6*22;
00238 
00239     QPixmap pixmap(length, width);
00240     pixmap.fill(Qt::transparent);
00241 
00242     QPainter p(&pixmap);
00243     p.setRenderHint(QPainter::Antialiasing);
00244     p.setPen(Qt::NoPen);
00245     p.setWindow(0, 0, w, int(h));
00246     QRectF rect(0, 0, w, h);
00247 
00248     // mask; never draw outside this, hence mask() sets SourceAtop
00249     mask(p, rect);
00250 
00251     // base
00252     p.setBrush(baseGradient(h, Qt::Horizontal));
00253     p.drawRect(rect);
00254 
00255     // shine
00256     p.setBrush(shineGradient(h, Qt::Horizontal));
00257     p.drawRoundRect(QRectF(0, 0.5, w, int(h*0.45)), 12, int(2000.0/h));
00258     p.setClipping(false);
00259 
00260     // shimmer
00261     p.setBrush(shimmerGradient(o, Qt::Horizontal));
00262     p.drawRect(rect);
00263 
00264     // dim edges
00265     p.setBrush(dimGradient(Qt::Horizontal));
00266     p.drawRect(rect);
00267 
00268     // bevel
00269     p.setWindow(0, 0, length, width);
00270     p.drawPixmap(0, 0, bevel(length, width, w, h, 9, int(1400.0/h)));
00271 
00272     return new TileSet(pixmap, s*3, 1, s*16, width-2);
00273 }

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.4
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