2 * Copyright (C) 2003 by Scott Monachello <smonach@cox.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the
16 * Free Software Foundation, Inc.
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301 USA.
25 * This implements the functionality of the little icon in the kpanel
26 * tray. Among which are tool tips and the running clock animated icon
38 #include <KGlobalSettings>
41 #include "mainwindow.h"
43 #include "timetrackerwidget.h"
45 QVector
<QPixmap
*> *TrayIcon::icons
= 0;
47 TrayIcon::TrayIcon(MainWindow
* parent
)
48 : KStatusNotifierItem(parent
)
50 setObjectName( "Ktimetracker Tray" );
51 // the timer that updates the "running" icon in the tray
52 _taskActiveTimer
= new QTimer(this);
53 connect( _taskActiveTimer
, SIGNAL(timeout()), this,
54 SLOT(advanceClock()) );
58 icons
= new QVector
<QPixmap
*>(8);
59 for (int i
=0; i
<8; i
++)
61 QPixmap
*icon
= new QPixmap();
63 name
.sprintf("active-icon-%d.xpm",i
);
64 *icon
= UserIcon(name
);
65 icons
->insert(i
,icon
);
68 TimetrackerWidget
*timetrackerWidget
= static_cast< TimetrackerWidget
* >( parent
->centralWidget() );
69 if ( timetrackerWidget
)
71 KAction
*action
= timetrackerWidget
->action( "configure_ktimetracker" );
72 if ( action
) contextMenu()->addAction( action
);
73 action
= timetrackerWidget
->action( "stopAll" );
74 if ( action
) contextMenu()->addAction( action
);
80 TrayIcon::TrayIcon(ktimetrackerpart
*)
81 : KStatusNotifierItem( 0 )
83 setObjectName( "Ktimetracker Tray" );
84 // it is not convenient if every kpart gets an icon in the systray.
89 : KStatusNotifierItem( 0 )
90 // will display nothing at all
92 setObjectName( "Ktimetracker Tray" );
100 void TrayIcon::startClock()
102 kDebug(5970) << "Entering function";
103 if ( _taskActiveTimer
)
105 _taskActiveTimer
->start(1000);
106 setIconByPixmap( *(*icons
)[_activeIcon
] );
108 kDebug(5970) << "Leaving function";
111 void TrayIcon::stopClock()
113 kDebug(5970) << "Entering function";
114 if ( _taskActiveTimer
)
116 _taskActiveTimer
->stop();
118 kDebug(5970) << "Leaving function";
121 void TrayIcon::advanceClock()
123 _activeIcon
= (_activeIcon
+1) % 8;
124 setIconByPixmap( *(*icons
)[_activeIcon
]);
127 void TrayIcon::resetClock()
130 setIconByPixmap( *(*icons
)[_activeIcon
]);
133 void TrayIcon::initToolTip()
135 updateToolTip(QList
<Task
*> ());
138 void TrayIcon::updateToolTip(QList
<Task
*> activeTasks
)
140 if ( activeTasks
.isEmpty() )
142 this->setToolTip( "ktimetracker", "ktimetracker", i18n("No active tasks") );
146 QFontMetrics
fm( QToolTip::font() );
147 const QString continued
= i18n( ", ..." );
148 const int buffer
= fm
.boundingRect( continued
).width();
149 const int desktopWidth
= KGlobalSettings::desktopGeometry(associatedWidget()).width();
150 const int maxWidth
= desktopWidth
- buffer
;
155 // Build the tool tip with all of the names of the active tasks.
156 // If at any time the width of the tool tip is larger than the desktop,
159 for ( int i
= 0; i
< activeTasks
.count(); ++i
)
161 Task
* task
= activeTasks
.at( i
);
163 s
+= i18n( ", " ) + task
->name();
166 int width
= fm
.boundingRect( s
).width();
167 if ( width
> maxWidth
)
174 this->setToolTip( "ktimetracker", "ktimetracker", qTip
);