karm

tray.cpp
1/*
2* KTray.
3*
4* This implements the functionality of the little icon in the kpanel
5* tray. Among which are tool tips and the running clock animated icon
6*
7* Distributed under the GPL.
8*/
9
10
11// #include <tqkeycode.h>
12// #include <tqlayout.h>
13#include <tqpixmap.h>
14#include <tqptrlist.h>
15#include <tqstring.h>
16#include <tqtimer.h>
17#include <tqtooltip.h>
18
19#include <tdeaction.h> // actionPreferences()
20#include <tdeglobal.h>
21#include <tdeglobalsettings.h>
22#include <kiconloader.h> // UserIcon
23#include <tdelocale.h> // i18n
24#include <tdepopupmenu.h> // plug()
25#include <ksystemtray.h>
26
27#include "mainwindow.h"
28#include "task.h"
29#include "tray.h"
30
31TQPtrVector<TQPixmap> *KarmTray::icons = 0;
32
33KarmTray::KarmTray(MainWindow* parent)
34 : KSystemTray(parent, "Karm Tray")
35{
36 // the timer that updates the "running" icon in the tray
37 _taskActiveTimer = new TQTimer(this);
38 connect( _taskActiveTimer, TQ_SIGNAL( timeout() ), this,
39 TQ_SLOT( advanceClock()) );
40
41 if (icons == 0) {
42 icons = new TQPtrVector<TQPixmap>(8);
43 for (int i=0; i<8; i++) {
44 TQPixmap *icon = new TQPixmap();
45 TQString name;
46 name.sprintf("active-icon-%d.xpm",i);
47 *icon = UserIcon(name);
48 icons->insert(i,icon);
49 }
50 }
51
52 parent->actionPreferences->plug( contextMenu() );
53 parent->actionStopAll->plug( contextMenu() );
54
55 resetClock();
56 initToolTip();
57
58 // start of a kind of menu for the tray
59 // this are experiments/tests
60 /*
61 for (int i=0; i<30; i++)
62 _tray->insertTitle(i 18n("bla ").arg(i));
63 for (int i=0; i<30; i++)
64 _tray->insertTitle2(i 18n("bli ").arg(i));
65 */
66 // experimenting with menus for the tray
67 /*
68 trayPopupMenu = contextMenu();
69 trayPopupMenu2 = new TQPopupMenu();
70 trayPopupMenu->insertItem(i18n("Submenu"), *trayPopupMenu2);
71 */
72}
73
74KarmTray::KarmTray(karmPart * parent)
75 : KSystemTray( 0 , "Karm Tray")
76{
77// it is not convenient if every kpart gets an icon in the systray.
78 _taskActiveTimer = 0;
79}
80
81KarmTray::~KarmTray()
82{
83}
84
85
86// experiment
87/*
88void KarmTray::insertTitle(TQString title)
89{
90 trayPopupMenu->insertTitle(title);
91}
92*/
93
94void KarmTray::startClock()
95{
96 if ( _taskActiveTimer )
97 {
98 _taskActiveTimer->start(1000);
99 setPixmap( *(*icons)[_activeIcon] );
100 show();
101 }
102}
103
104void KarmTray::stopClock()
105{
106 if ( _taskActiveTimer )
107 {
108 _taskActiveTimer->stop();
109 show();
110 }
111}
112
113void KarmTray::advanceClock()
114{
115 _activeIcon = (_activeIcon+1) % 8;
116 setPixmap( *(*icons)[_activeIcon]);
117}
118
119void KarmTray::resetClock()
120{
121 _activeIcon = 0;
122 setPixmap( *(*icons)[_activeIcon]);
123 show();
124}
125
126void KarmTray::initToolTip()
127{
128 updateToolTip(TQPtrList<Task> ());
129}
130
131void KarmTray::updateToolTip(TQPtrList<Task> activeTasks)
132{
133 if ( activeTasks.isEmpty() ) {
134 TQToolTip::add( this, i18n("No active tasks") );
135 return;
136 }
137
138 TQFontMetrics fm( TQToolTip::font() );
139 const TQString continued = i18n( ", ..." );
140 const int buffer = fm.boundingRect( continued ).width();
141 const int desktopWidth = TDEGlobalSettings::desktopGeometry(this).width();
142 const int maxWidth = desktopWidth - buffer;
143
144 TQString qTip;
145 TQString s;
146
147 // Build the tool tip with all of the names of the active tasks.
148 // If at any time the width of the tool tip is larger than the desktop,
149 // stop building it.
150 TQPtrListIterator<Task> item( activeTasks );
151 for ( int i = 0; item.current(); ++item, ++i ) {
152 Task* task = item.current();
153 if ( i > 0 )
154 s += i18n( ", " ) + task->name();
155 else
156 s += task->name();
157 int width = fm.boundingRect( s ).width();
158 if ( width > maxWidth ) {
159 qTip += continued;
160 break;
161 }
162 qTip = s;
163 }
164
165 TQToolTip::add( this, qTip );
166}
167
168#include "tray.moc"
Main window to tie the application together.
Definition: mainwindow.h:27
A class representing a task.
Definition: task.h:42
TQString name() const
returns the name of this task.
Definition: task.h:162
This is a "Part".
Definition: karm_part.h:31