karm Library API Documentation

tray.cpp

00001 /*
00002 * KTray.
00003 *
00004 * This implements the functionality of the little icon in the kpanel
00005 * tray. Among which are tool tips and the running clock animated icon
00006 *
00007 * Distributed under the GPL.
00008 */
00009 
00010 
00011 // #include <qkeycode.h>
00012 // #include <qlayout.h>
00013 #include <qpixmap.h>
00014 #include <qptrlist.h>
00015 #include <qstring.h>
00016 #include <qtimer.h>
00017 #include <qtooltip.h>
00018 
00019 #include <kaction.h>            // actionPreferences()
00020 #include <kglobal.h>
00021 #include <kglobalsettings.h>
00022 #include <kiconloader.h>        // UserIcon
00023 #include <klocale.h>            // i18n
00024 #include <kpopupmenu.h>         // plug()
00025 #include <ksystemtray.h>
00026 
00027 #include "mainwindow.h"
00028 #include "task.h"
00029 #include "tray.h"
00030 
00031 QPtrVector<QPixmap> *KarmTray::icons = 0;
00032 
00033 KarmTray::KarmTray(MainWindow* parent)
00034   : KSystemTray(parent, "Karm Tray")
00035 {
00036   // the timer that updates the "running" icon in the tray
00037   _taskActiveTimer = new QTimer(this);
00038   connect( _taskActiveTimer, SIGNAL( timeout() ), this,
00039                              SLOT( advanceClock()) );
00040 
00041   if (icons == 0) {
00042     icons = new QPtrVector<QPixmap>(8);
00043     for (int i=0; i<8; i++) {
00044       QPixmap *icon = new QPixmap();
00045       QString name;
00046       name.sprintf("active-icon-%d.xpm",i);
00047       *icon = UserIcon(name);
00048       icons->insert(i,icon);
00049     }
00050   }
00051 
00052   parent->actionPreferences->plug( contextMenu() ); 
00053   parent->actionStopAll->plug( contextMenu() );
00054 
00055   resetClock();
00056   initToolTip();
00057 
00058   // start of a kind of menu for the tray
00059   // this are experiments/tests
00060   /*
00061   for (int i=0; i<30; i++)
00062     _tray->insertTitle(i18n("bla ").arg(i));
00063   for (int i=0; i<30; i++)
00064     _tray->insertTitle2(i18n("bli ").arg(i));
00065   */
00066   // experimenting with menus for the tray
00067   /*
00068   trayPopupMenu = contextMenu();
00069   trayPopupMenu2 = new QPopupMenu();
00070   trayPopupMenu->insertItem(i18n("Submenu"), *trayPopupMenu2);
00071   */
00072 }
00073 
00074 KarmTray::~KarmTray()
00075 {
00076 }
00077 
00078 
00079 // experiment
00080 /*
00081 void KarmTray::insertTitle(QString title)
00082 {
00083   trayPopupMenu->insertTitle(title);
00084 }
00085 */
00086 
00087 void KarmTray::startClock()
00088 {
00089   _taskActiveTimer->start(1000);
00090   setPixmap( *(*icons)[_activeIcon] );
00091   show();
00092 }
00093 
00094 void KarmTray::stopClock()
00095 {
00096   _taskActiveTimer->stop();
00097   show();
00098 }
00099 
00100 void KarmTray::advanceClock()
00101 {
00102   _activeIcon = (_activeIcon+1) % 8;
00103   setPixmap( *(*icons)[_activeIcon]);
00104 }
00105 
00106 void KarmTray::resetClock()
00107 {
00108   _activeIcon = 0;
00109   setPixmap( *(*icons)[_activeIcon]);
00110   show();
00111 }
00112 
00113 void KarmTray::initToolTip()
00114 {
00115   updateToolTip(QPtrList<Task> ());
00116 }
00117 
00118 void KarmTray::updateToolTip(QPtrList<Task> activeTasks)
00119 {
00120   if ( activeTasks.isEmpty() ) {
00121     QToolTip::add( this, i18n("No active tasks") );
00122     return;
00123   }
00124 
00125   QFontMetrics fm( QToolTip::font() );
00126   const QString continued = i18n( ", ..." );
00127   const int buffer = fm.boundingRect( continued ).width();
00128   const int desktopWidth = KGlobalSettings::desktopGeometry(this).width();
00129   const int maxWidth = desktopWidth - buffer;
00130 
00131   QString qTip;
00132   QString s;
00133 
00134   // Build the tool tip with all of the names of the active tasks.
00135   // If at any time the width of the tool tip is larger than the desktop,
00136   // stop building it.
00137   QPtrListIterator<Task> item( activeTasks );
00138   for ( int i = 0; item.current(); ++item, ++i ) {
00139     Task* task = item.current();
00140     if ( i > 0 )
00141       s += i18n( ", " ) + task->name();
00142     else
00143       s += task->name();
00144     int width = fm.boundingRect( s ).width();
00145     if ( width > maxWidth ) {
00146       qTip += continued;
00147       break;
00148     }
00149     qTip = s;
00150   }
00151 
00152   QToolTip::add( this, qTip );
00153 }
00154 
00155 #include "tray.moc"
KDE Logo
This file is part of the documentation for karm Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003