KDEUI
kled.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1998 Jörg Habenicht (j.habenicht@europemail.com) 00003 Copyright (C) 2010 Christoph Feck <christoph@maxiom.de> 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 "kled.h" 00022 00023 #include <kcolorutils.h> 00024 00025 #include <QtGui/QPainter> 00026 #include <QtGui/QImage> 00027 #include <QtGui/QStyle> 00028 #include <QtGui/QStyleOption> 00029 00030 class KLed::Private 00031 { 00032 public: 00033 Private() 00034 : darkFactor( 300 ), 00035 state( On ), look( Raised ), shape( Circular ) 00036 { 00037 } 00038 00039 int darkFactor; 00040 QColor color; 00041 State state; 00042 Look look; 00043 Shape shape; 00044 00045 QPixmap cachedPixmap[2]; // for both states 00046 QStyle::ControlElement ce_indicatorLedCircular; 00047 QStyle::ControlElement ce_indicatorLedRectangular; 00048 }; 00049 00050 00051 00052 KLed::KLed( QWidget *parent ) 00053 : QWidget( parent ), 00054 d( new Private ) 00055 { 00056 setColor( Qt::green ); 00057 } 00058 00059 00060 KLed::KLed( const QColor& color, QWidget *parent ) 00061 : QWidget( parent ), 00062 d( new Private ) 00063 { 00064 setColor( color ); 00065 } 00066 00067 KLed::KLed( const QColor& color, State state, Look look, Shape shape, 00068 QWidget *parent ) 00069 : QWidget( parent ), 00070 d( new Private ) 00071 { 00072 d->state = (state == Off ? Off : On); 00073 d->look = look; 00074 d->shape = shape; 00075 00076 setColor( color ); 00077 } 00078 00079 KLed::~KLed() 00080 { 00081 delete d; 00082 } 00083 00084 void KLed::paintEvent( QPaintEvent* ) 00085 { 00086 switch( d->shape ) { 00087 case Rectangular: 00088 switch ( d->look ) { 00089 case Sunken: 00090 paintRectFrame( false ); 00091 break; 00092 case Raised: 00093 paintRectFrame( true ); 00094 break; 00095 case Flat: 00096 paintRect(); 00097 break; 00098 } 00099 break; 00100 case Circular: 00101 switch ( d->look ) { 00102 case Flat: 00103 paintFlat(); 00104 break; 00105 case Raised: 00106 paintRaised(); 00107 break; 00108 case Sunken: 00109 paintSunken(); 00110 break; 00111 } 00112 break; 00113 } 00114 } 00115 00116 int KLed::ledWidth() const 00117 { 00118 // Make sure the LED is round! 00119 int size = qMin(width(), height()); 00120 00121 // leave one pixel border 00122 size -= 2; 00123 00124 return qMax(0, size); 00125 } 00126 00127 bool KLed::paintCachedPixmap() 00128 { 00129 if (d->cachedPixmap[d->state].isNull()) { 00130 return false; 00131 } 00132 QPainter painter(this); 00133 painter.drawPixmap(1, 1, d->cachedPixmap[d->state]); 00134 return true; 00135 } 00136 00137 void KLed::paintFlat() 00138 { 00139 paintLed(Circular, Flat); 00140 } 00141 00142 void KLed::paintRaised() 00143 { 00144 paintLed(Circular, Raised); 00145 } 00146 00147 void KLed::paintSunken() 00148 { 00149 paintLed(Circular, Sunken); 00150 } 00151 00152 void KLed::paintRect() 00153 { 00154 paintLed(Rectangular, Flat); 00155 } 00156 00157 void KLed::paintRectFrame( bool raised ) 00158 { 00159 paintLed(Rectangular, raised ? Raised : Sunken); 00160 } 00161 00162 KLed::State KLed::state() const 00163 { 00164 return d->state; 00165 } 00166 00167 KLed::Shape KLed::shape() const 00168 { 00169 return d->shape; 00170 } 00171 00172 QColor KLed::color() const 00173 { 00174 return d->color; 00175 } 00176 00177 KLed::Look KLed::look() const 00178 { 00179 return d->look; 00180 } 00181 00182 void KLed::setState( State state ) 00183 { 00184 if ( d->state == state) 00185 return; 00186 00187 d->state = (state == Off ? Off : On); 00188 updateCachedPixmap(); 00189 } 00190 00191 void KLed::setShape( Shape shape ) 00192 { 00193 if ( d->shape == shape ) 00194 return; 00195 00196 d->shape = shape; 00197 updateCachedPixmap(); 00198 } 00199 00200 void KLed::setColor( const QColor &color ) 00201 { 00202 if ( d->color == color ) 00203 return; 00204 00205 d->color = color; 00206 updateCachedPixmap(); 00207 } 00208 00209 void KLed::setDarkFactor( int darkFactor ) 00210 { 00211 if ( d->darkFactor == darkFactor ) 00212 return; 00213 00214 d->darkFactor = darkFactor; 00215 updateCachedPixmap(); 00216 } 00217 00218 int KLed::darkFactor() const 00219 { 00220 return d->darkFactor; 00221 } 00222 00223 void KLed::setLook( Look look ) 00224 { 00225 if ( d->look == look) 00226 return; 00227 00228 d->look = look; 00229 updateCachedPixmap(); 00230 } 00231 00232 void KLed::toggle() 00233 { 00234 d->state = (d->state == On ? Off : On); 00235 updateCachedPixmap(); 00236 } 00237 00238 void KLed::on() 00239 { 00240 setState( On ); 00241 } 00242 00243 void KLed::off() 00244 { 00245 setState( Off ); 00246 } 00247 00248 void KLed::resizeEvent( QResizeEvent * ) 00249 { 00250 updateCachedPixmap(); 00251 } 00252 00253 QSize KLed::sizeHint() const 00254 { 00255 QStyleOption option; 00256 option.initFrom(this); 00257 int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, &option, this); 00258 return QSize( iconSize, iconSize ); 00259 } 00260 00261 QSize KLed::minimumSizeHint() const 00262 { 00263 return QSize( 16, 16 ); 00264 } 00265 00266 void KLed::updateCachedPixmap() 00267 { 00268 d->cachedPixmap[Off] = QPixmap(); 00269 d->cachedPixmap[On] = QPixmap(); 00270 update(); 00271 } 00272 00273 void KLed::paintLed(Shape shape, Look look) 00274 { 00275 if (paintCachedPixmap()) { 00276 return; 00277 } 00278 00279 QSize size(width() - 2, height() - 2); 00280 if (shape == Circular) { 00281 const int width = ledWidth(); 00282 size = QSize(width, width); 00283 } 00284 QPointF center(size.width() / 2.0, size.height() / 2.0); 00285 const int smallestSize = qMin(size.width(), size.height()); 00286 QPainter painter; 00287 00288 QImage image(size, QImage::Format_ARGB32_Premultiplied); 00289 image.fill(0); 00290 00291 QRadialGradient fillGradient(center, smallestSize / 2.0, QPointF(center.x(), size.height() / 3.0)); 00292 const QColor fillColor = d->state != Off ? d->color : d->color.dark(d->darkFactor); 00293 fillGradient.setColorAt(0.0, fillColor.light(250)); 00294 fillGradient.setColorAt(0.5, fillColor.light(130)); 00295 fillGradient.setColorAt(1.0, fillColor); 00296 00297 QConicalGradient borderGradient(center, look == Sunken ? 90 : -90); 00298 QColor borderColor = palette().color(QPalette::Dark); 00299 if (d->state == On) { 00300 QColor glowOverlay = fillColor; 00301 glowOverlay.setAlpha(80); 00302 borderColor = KColorUtils::overlayColors(borderColor, glowOverlay); 00303 } 00304 borderGradient.setColorAt(0.2, borderColor); 00305 borderGradient.setColorAt(0.5, palette().color(QPalette::Light)); 00306 borderGradient.setColorAt(0.8, borderColor); 00307 00308 painter.begin(&image); 00309 painter.setRenderHint(QPainter::Antialiasing); 00310 painter.setBrush(look == Flat ? QBrush(fillColor) : QBrush(fillGradient)); 00311 const QBrush penBrush = (look == Flat) ? QBrush(borderColor) : QBrush(borderGradient); 00312 const qreal penWidth = smallestSize / 8.0; 00313 painter.setPen(QPen(penBrush, penWidth)); 00314 QRectF r(penWidth / 2.0, penWidth / 2.0, size.width() - penWidth, size.height() - penWidth); 00315 if (shape == Rectangular) { 00316 painter.drawRect(r); 00317 } else { 00318 painter.drawEllipse(r); 00319 } 00320 painter.end(); 00321 00322 d->cachedPixmap[d->state] = QPixmap::fromImage(image); 00323 painter.begin(this); 00324 painter.drawPixmap(1, 1, d->cachedPixmap[d->state]); 00325 painter.end(); 00326 } 00327 00328 #include "kled.moc"
KDE 4.6 API Reference