From 4aed2c8219774f5d797760606b8489a92ddc5163 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kicker/extensions/kasbar/kasloaditem.cpp | 130 +++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 kicker/extensions/kasbar/kasloaditem.cpp (limited to 'kicker/extensions/kasbar/kasloaditem.cpp') diff --git a/kicker/extensions/kasbar/kasloaditem.cpp b/kicker/extensions/kasbar/kasloaditem.cpp new file mode 100644 index 000000000..b0f9c23e3 --- /dev/null +++ b/kicker/extensions/kasbar/kasloaditem.cpp @@ -0,0 +1,130 @@ +#include +#include + +#include +#ifdef HAVE_SYS_LOADAVG_H +#include // e.g. Solaris +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "kaspopup.h" +#include "kastasker.h" + +#include "kasloaditem.h" +#include "kasloaditem.moc" + +KasLoadItem::KasLoadItem( KasBar *parent ) + : KasItem( parent ) +{ + QTimer *t = new QTimer( this ); + connect( t, SIGNAL( timeout() ), SLOT( updateDisplay() ) ); + t->start( 1000 ); + updateDisplay(); + + connect( this, SIGNAL(rightButtonClicked(QMouseEvent *)), SLOT(showMenuAt(QMouseEvent *) ) ); +} + +KasLoadItem::~KasLoadItem() +{ +} + +void KasLoadItem::updateDisplay() +{ + double load[3]; + + int ret = getloadavg( load, 3 ); + if ( ret == -1 ) + return; + + valuesOne.append( load[0] ); + valuesFive.append( load[1] ); + valuesFifteen.append( load[2] ); + + if ( valuesOne.count() > 2/*(extent()-2)*/ ) { + valuesOne.pop_front(); + valuesFive.pop_front(); + valuesFifteen.pop_front(); + } + + setText( QString("%1").arg( valuesOne.last(), 3, 'f', 2 ) ); +} + +void KasLoadItem::paint( QPainter *p ) +{ + double val = valuesOne.last(); + double maxValue = 1.0; + double scaleVal = QMAX( val, valuesFive.last() ); + + if ( scaleVal >= maxValue ) + maxValue = 2.0; + if ( scaleVal >= maxValue ) + maxValue = 5.0; + if ( scaleVal >= maxValue ) + maxValue = 10.0; + if ( scaleVal >= maxValue ) + maxValue = 20.0; + if ( scaleVal >= maxValue ) + maxValue = 50.0; + if ( scaleVal >= maxValue ) + maxValue = 100.0; + + double dh = extent()-16; + dh = dh / maxValue; + + int h = (int) floor( dh * val ); + int w = extent()-4; + h = (h > 0) ? h : 1; + w = (w > 0) ? w : 1; + + KasItem::paint( p ); + + QColor light = kasbar()->colorGroup().highlight(); + QColor dark = light.dark(); + + KPixmap pix; + pix.resize( w, h ); + KPixmapEffect::gradient( pix, light, dark, KPixmapEffect::DiagonalGradient ); + p->drawPixmap( 2, extent()-2-h, pix ); + + p->setPen( kasbar()->colorGroup().mid() ); + for ( double pos = 0.2 ; pos < 1.0 ; pos += 0.2 ) { + int ypos = (int) floor((extent()-2) - (dh*maxValue*pos)); + p->drawLine( 2, ypos, extent()-3, ypos ); + } +} + +void KasLoadItem::showMenuAt( QMouseEvent *ev ) +{ + hidePopup(); + showMenuAt( ev->globalPos() ); +} + +void KasLoadItem::showMenuAt( QPoint p ) +{ + mouseLeave(); + kasbar()->updateMouseOver(); + + KasTasker *bar = dynamic_cast (KasItem::kasbar()); + if ( !bar ) + return; + + KPopupMenu *menu = bar->contextMenu(); + menu->exec( p ); +} -- cgit v1.2.3