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 <tqkeycode.h> 00012 // #include <tqlayout.h> 00013 #include <tqpixmap.h> 00014 #include <tqptrlist.h> 00015 #include <tqstring.h> 00016 #include <tqtimer.h> 00017 #include <tqtooltip.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 TQPtrVector<TQPixmap> *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 TQTimer(this); 00038 connect( _taskActiveTimer, TQT_SIGNAL( timeout() ), this, 00039 TQT_SLOT( advanceClock()) ); 00040 00041 if (icons == 0) { 00042 icons = new TQPtrVector<TQPixmap>(8); 00043 for (int i=0; i<8; i++) { 00044 TQPixmap *icon = new TQPixmap(); 00045 TQString 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(i 18n("bla ").arg(i)); 00063 for (int i=0; i<30; i++) 00064 _tray->insertTitle2(i 18n("bli ").arg(i)); 00065 */ 00066 // experimenting with menus for the tray 00067 /* 00068 trayPopupMenu = contextMenu(); 00069 trayPopupMenu2 = new TQPopupMenu(); 00070 trayPopupMenu->insertItem(i18n("Submenu"), *trayPopupMenu2); 00071 */ 00072 } 00073 00074 KarmTray::KarmTray(karmPart * parent) 00075 : KSystemTray( 0 , "Karm Tray") 00076 { 00077 // it is not convenient if every kpart gets an icon in the systray. 00078 _taskActiveTimer = 0; 00079 } 00080 00081 KarmTray::~KarmTray() 00082 { 00083 } 00084 00085 00086 // experiment 00087 /* 00088 void KarmTray::insertTitle(TQString title) 00089 { 00090 trayPopupMenu->insertTitle(title); 00091 } 00092 */ 00093 00094 void KarmTray::startClock() 00095 { 00096 if ( _taskActiveTimer ) 00097 { 00098 _taskActiveTimer->start(1000); 00099 setPixmap( *(*icons)[_activeIcon] ); 00100 show(); 00101 } 00102 } 00103 00104 void KarmTray::stopClock() 00105 { 00106 if ( _taskActiveTimer ) 00107 { 00108 _taskActiveTimer->stop(); 00109 show(); 00110 } 00111 } 00112 00113 void KarmTray::advanceClock() 00114 { 00115 _activeIcon = (_activeIcon+1) % 8; 00116 setPixmap( *(*icons)[_activeIcon]); 00117 } 00118 00119 void KarmTray::resetClock() 00120 { 00121 _activeIcon = 0; 00122 setPixmap( *(*icons)[_activeIcon]); 00123 show(); 00124 } 00125 00126 void KarmTray::initToolTip() 00127 { 00128 updateToolTip(TQPtrList<Task> ()); 00129 } 00130 00131 void KarmTray::updateToolTip(TQPtrList<Task> activeTasks) 00132 { 00133 if ( activeTasks.isEmpty() ) { 00134 TQToolTip::add( this, i18n("No active tasks") ); 00135 return; 00136 } 00137 00138 TQFontMetrics fm( TQToolTip::font() ); 00139 const TQString continued = i18n( ", ..." ); 00140 const int buffer = fm.boundingRect( continued ).width(); 00141 const int desktopWidth = KGlobalSettings::desktopGeometry(this).width(); 00142 const int maxWidth = desktopWidth - buffer; 00143 00144 TQString qTip; 00145 TQString s; 00146 00147 // Build the tool tip with all of the names of the active tasks. 00148 // If at any time the width of the tool tip is larger than the desktop, 00149 // stop building it. 00150 TQPtrListIterator<Task> item( activeTasks ); 00151 for ( int i = 0; item.current(); ++item, ++i ) { 00152 Task* task = item.current(); 00153 if ( i > 0 ) 00154 s += i18n( ", " ) + task->name(); 00155 else 00156 s += task->name(); 00157 int width = fm.boundingRect( s ).width(); 00158 if ( width > maxWidth ) { 00159 qTip += continued; 00160 break; 00161 } 00162 qTip = s; 00163 } 00164 00165 TQToolTip::add( this, qTip ); 00166 } 00167 00168 #include "tray.moc"