00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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));
00054 p.drawRoundedRect(rect,8,8);
00055
00056
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
00108 mask(p, rect);
00109
00110
00111 p.setBrush(alphaColor(dark,0.2));
00112 p.drawRoundedRect(rect,6,6);
00113 rect.adjust(0,0,0,-1);
00114
00115
00116 p.setBrush(baseGradient(h, Qt::Horizontal));
00117 p.drawRoundedRect(rect,6,6);
00118
00119
00120 p.setBrush(shineGradient(h, Qt::Horizontal));
00121 p.drawRoundedRect(rect.adjusted(1,1,-1,-2),6,6);
00122
00123
00124 p.fillRect(rect.adjusted(2,2,-2,-2),alphaColor(dark,0.3));
00125
00126
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
00157 mask(p, rect);
00158
00159
00160 p.setBrush(alphaColor(dark,0.2));
00161 p.drawRoundedRect(rect,6,6);
00162 rect.adjust(0,0,0,-1);
00163
00164
00165 p.setBrush(baseGradient(w, Qt::Vertical));
00166 p.drawRoundedRect(rect,6,6);
00167
00168
00169 p.setBrush(shineGradient(w, Qt::Vertical));
00170 p.drawRoundedRect(rect.adjusted(1,1,-1,-2),6,6);
00171
00172
00173 p.fillRect(rect.adjusted(2,2,-2,-2),alphaColor(dark,0.3));
00174
00175
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 }