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

KStyles

progressbar.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 "progressbar.h"
00021 
00022 #include <KColorUtils>
00023 #include <KColorScheme>
00024 #include <QDebug>
00025 
00026 #include <QtGui/QPainter>
00027 #include <QtGui/QLinearGradient>
00028 
00029 #include <math.h>
00030 
00031 inline QColor alphaColor(QColor color, double alpha)
00032 {
00033     color.setAlphaF(alpha);
00034     return color;
00035 }
00036 
00037 OxygenProgressBar::OxygenProgressBar(const QColor &c, double contrast) : color(c),
00038     light(KColorScheme::shade(c, KColorScheme::LightShade, contrast - 0.5)),
00039     mid(KColorScheme::shade(c, KColorScheme::MidShade, contrast)),
00040     dark(KColorScheme::shade(c, KColorScheme::DarkShade, contrast - 0.5)),
00041     shadow(KColorScheme::shade(c, KColorScheme::ShadowShade, contrast)),
00042     highlight(Qt::white)
00043 {
00044     double y = KColorUtils::luma(color);
00045     if (y > KColorUtils::luma(light)) {
00046         light = Qt::white;
00047         dark = KColorScheme::shade(c, KColorScheme::DarkShade, contrast);
00048     }
00049 }
00050 
00051 void OxygenProgressBar::mask(QPainter &p, const QRectF &rect) const
00052 {
00053     p.setBrush(alphaColor(mid,0.8)); // color does matter
00054     p.drawRoundedRect(rect,8,8);
00055 
00056     // never draw outside the mask
00057     p.setCompositionMode(QPainter::CompositionMode_SourceAtop);
00058 }
00059 
00060 QLinearGradient OxygenProgressBar::baseGradient(double width, Qt::Orientation orient) const
00061 {
00062     int x = width, y = width;
00063     if (orient == Qt::Horizontal)
00064         x = 0;
00065     else
00066         y = 0;
00067 
00068     QLinearGradient gradient(0, 0, x, y);
00069     gradient.setColorAt(0.0, mid);
00070     gradient.setColorAt(1.0, alphaColor(light,0.8));
00071 
00072     return gradient;
00073 }
00074 
00075 QLinearGradient OxygenProgressBar::shineGradient(double width, Qt::Orientation orient) const
00076 {
00077     int x = width, y = width;
00078     if (orient == Qt::Horizontal)
00079         x = 0;
00080     else
00081         y = 0;
00082 
00083     QLinearGradient gradient(0, 0, x, y);
00084     gradient.setColorAt(0.0, light);
00085     gradient.setColorAt(0.45, alphaColor(light,0.5));
00086     gradient.setColorAt(0.5, Qt::transparent);
00087 
00088     return gradient;
00089 }
00090 
00091 TileSet* OxygenProgressBar::horizontal(int size, int width) const
00092 {
00093     int s = size/2;
00094     int length = s*22;
00095     double h = 12.0 * double(width)/double(s*2);
00096     const int w = 6*22;
00097 
00098     QPixmap pixmap(length, width);
00099     pixmap.fill(Qt::transparent);
00100 
00101     QPainter p(&pixmap);
00102     p.setRenderHint(QPainter::Antialiasing);
00103     p.setPen(Qt::NoPen);
00104     p.setWindow(0, 0, w, int(h));
00105     QRectF rect(0, 0, w, h);
00106 
00107     // mask; never draw outside this, hence mask() sets SourceAtop
00108     mask(p, rect);
00109 
00110     // shadow
00111     p.setBrush(alphaColor(dark,0.2));
00112     p.drawRoundedRect(rect,6,6);
00113     rect.adjust(0,0,0,-1);
00114 
00115     // base gradient
00116     p.setBrush(baseGradient(h, Qt::Horizontal));
00117     p.drawRoundedRect(rect,6,6);
00118 
00119     // shine gradient
00120     p.setBrush(shineGradient(h, Qt::Horizontal));
00121     p.drawRoundedRect(rect.adjusted(1,1,-1,-2),6,6);
00122 
00123     // dim layer
00124     p.fillRect(rect.adjusted(2,2,-2,-2),alphaColor(dark,0.3));
00125 
00126     // shine and border
00127     p.setBrush(Qt::NoBrush);
00128     p.setPen( QPen(dark, 1.2)  );
00129     p.drawRoundedRect(rect.adjusted(0,0,0,-1),6,6);
00130 
00131     p.setPen( QPen(alphaColor(light,0.4), 1.0) );
00132     p.drawRoundedRect(rect.adjusted(2,2,-2,-3),6,6);
00133     p.drawRoundedRect(rect.adjusted(2,2,-2,-2.5),6,6);
00134 
00135 
00136     return new TileSet(pixmap, 5, 2, pixmap.width()-10, pixmap.height()-5);
00137 }
00138 
00139 
00140 TileSet* OxygenProgressBar::vertical(int size, int width) const
00141 {
00142     int s = size/2;
00143     int length = s*22;
00144     double w = 12.0 * double(width)/double(s*2);
00145     const int h = 6*22;
00146 
00147     QPixmap pixmap(width, length);
00148     pixmap.fill(Qt::transparent);
00149 
00150     QPainter p(&pixmap);
00151     p.setRenderHint(QPainter::Antialiasing);
00152     p.setPen(Qt::NoPen);
00153     p.setWindow(0, 0, int(w), h);
00154     QRectF rect(0, 0, w, h);
00155 
00156     // mask; never draw outside this, hence mask() sets SourceAtop
00157     mask(p, rect);
00158 
00159     // shadow
00160     p.setBrush(alphaColor(dark,0.2));
00161     p.drawRoundedRect(rect,6,6);
00162     rect.adjust(0,0,0,-1);
00163 
00164     // base gradient
00165     p.setBrush(baseGradient(w, Qt::Vertical));
00166     p.drawRoundedRect(rect,6,6);
00167 
00168     // shine gradient
00169     p.setBrush(shineGradient(w, Qt::Vertical));
00170     p.drawRoundedRect(rect.adjusted(1,1,-1,-2),6,6);
00171 
00172     // dim layer
00173     p.fillRect(rect.adjusted(2,2,-2,-2),alphaColor(dark,0.3));
00174 
00175     // shine and border
00176     p.setBrush(Qt::NoBrush);
00177     p.setPen(QPen(dark,1));
00178     p.drawRoundedRect(rect.adjusted(0,0,0,-1),6,6);
00179 
00180     p.setPen(alphaColor(light,0.4));
00181     p.drawRoundedRect(rect.adjusted(2,2,-2,-2),6,6);
00182     p.drawRoundedRect(rect.adjusted(2,2,-2,-2.5),6,6);
00183 
00184     return new TileSet(pixmap, 1, s*3, width-2, s*16);
00185 }

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