akregator/src

trayicon.cpp
1/*
2 This file is part of Akregator.
3
4 Copyright (C) 2004 Stanislav Karchebny <Stanislav.Karchebny@kdemail.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include "akregatorconfig.h"
26#include "trayicon.h"
27
28#include <tdeapplication.h>
29#include <twin.h>
30#include <kiconeffect.h>
31#include <kdebug.h>
32#include <tdelocale.h>
33#include <tdeglobalsettings.h>
34#include <dcopclient.h>
35#include <dcopref.h>
36#include <tdepopupmenu.h>
37#include <kiconloader.h>
38
39#include <tqbitmap.h>
40#include <tqpainter.h>
41#include <tqfont.h>
42#include <tqtooltip.h>
43
44
45namespace Akregator {
46
47TrayIcon* TrayIcon::m_instance = 0;
48
49TrayIcon* TrayIcon::getInstance()
50{
51 return m_instance;
52}
53
54void TrayIcon::setInstance(TrayIcon* trayIcon)
55{
56 m_instance = trayIcon;
57}
58
59
60TrayIcon::TrayIcon(TQWidget *parent, const char *name)
61 : KSystemTray(parent, name), m_unread(0)
62{
63 m_defaultIcon=KSystemTray::loadIcon("akregator");
64 TQPixmap m_unreadIcon=KSystemTray::loadIcon("akregator_empty");
65 m_lightIconImage=m_unreadIcon.convertToImage();
66 TDEIconEffect::deSaturate(m_lightIconImage, 0.60);
67 setPixmap(m_defaultIcon);
68 TQToolTip::add(this, i18n("Akregator - RSS Feed Reader"));
69}
70
71
72TrayIcon::~TrayIcon()
73{}
74
75
76void TrayIcon::mousePressEvent(TQMouseEvent *e) {
77 if (e->button() == TQt::LeftButton) {
78 emit showPart();
79 }
80
81 KSystemTray::mousePressEvent(e);
82}
83
84
85TQPixmap TrayIcon::takeScreenshot() const
86{
87 TQPoint g = mapToGlobal(pos());
88 int desktopWidth = tdeApp->desktop()->width();
89 int desktopHeight = tdeApp->desktop()->height();
90 int tw = width();
91 int th = height();
92 int w = desktopWidth / 4;
93 int h = desktopHeight / 9;
94 int x = g.x() + tw/2 - w/2; // Center the rectange in the systray icon
95 int y = g.y() + th/2 - h/2;
96 if (x < 0)
97 x = 0; // Move the rectangle to stay in the desktop limits
98 if (y < 0)
99 y = 0;
100 if (x + w > desktopWidth)
101 x = desktopWidth - w;
102 if (y + h > desktopHeight)
103 y = desktopHeight - h;
104
105 // Grab the desktop and draw a circle arround the icon:
106 TQPixmap shot = TQPixmap::grabWindow(tqt_xrootwin(), x, y, w, h);
107 TQPainter painter(&shot);
108 const int MARGINS = 6;
109 const int WIDTH = 3;
110 int ax = g.x() - x - MARGINS -1;
111 int ay = g.y() - y - MARGINS -1;
112 painter.setPen( TQPen(TQt::red/*TDEApplication::palette().active().highlight()*/, WIDTH) );
113 painter.drawArc(ax, ay, tw + 2*MARGINS, th + 2*MARGINS, 0, 16*360);
114 painter.end();
115
116 // Paint the border
117 const int BORDER = 1;
118 TQPixmap finalShot(w + 2*BORDER, h + 2*BORDER);
119 finalShot.fill(TDEApplication::palette().active().foreground());
120 painter.begin(&finalShot);
121 painter.drawPixmap(BORDER, BORDER, shot);
122 painter.end();
123 return shot; // not finalShot?? -fo
124}
125
126void TrayIcon::resizeEvent(TQResizeEvent *)
127{
128 setUnread(m_unread, true);
129}
130
131void TrayIcon::slotSetUnread(int unread)
132{
133 setUnread(unread, false);
134}
135
136void TrayIcon::setUnread(int unread, bool force)
137{
138 if ((unread==m_unread) && (!force))
139 {
140 return;
141 }
142
143 m_unread=unread;
144
145 TQToolTip::remove(this);
146 TQToolTip::add(this, i18n("Akregator - 1 unread article", "Akregator - %n unread articles", unread > 0 ? unread : 0));
147
148 if (unread <= 0)
149 {
150 setPixmap(m_defaultIcon);
151 }
152 else
153 {
154 // from KMSystemTray
155 int oldW = pixmap()->size().width();
156 int oldH = pixmap()->size().height();
157
158 TQString uStr=TQString::number( unread );
159 TQFont f=TDEGlobalSettings::generalFont();
160 f.setBold(true);
161
162 // increase the size of the font for the number of unread messages if the
163 // icon size is less than 22 pixels
164 // see bug 1251
165 int realIconHeight = height();
166 if (realIconHeight < 22) {
167 f.setPointSizeFloat( f.pointSizeFloat() * 2.0 );
168 }
169
170 // decrease the size of the font for the number of unread articles if the
171 // number doesn't fit into the available space
172 float pointSize=f.pointSizeFloat();
173 TQFontMetrics fm(f);
174 int w=fm.width(uStr);
175 if( w > (oldW) )
176 {
177 pointSize *= float(oldW) / float(w);
178 f.setPointSizeFloat(pointSize);
179 }
180
181 TQPixmap pix(oldW, oldH);
182 pix.fill(TQt::white);
183 TQPainter p(&pix);
184 p.setFont(f);
185 p.setPen(Settings::unreadTextColor());
186 p.drawText(pix.rect(), TQt::AlignCenter, uStr);
187
188 pix.setMask(pix.createHeuristicMask());
189 TQImage img=pix.convertToImage();
190
191 // overlay
192 TQImage overlayImg=m_lightIconImage.copy();
193 TDEIconEffect::overlay(overlayImg, img);
194
195 TQPixmap icon;
196 icon.convertFromImage(overlayImg);
197 setPixmap(icon);
198 }
199}
200
201void TrayIcon::viewButtonClicked()
202{
203 TQWidget *p=static_cast<TQWidget*>(parent());
204 KWin::forceActiveWindow(p->winId());
205}
206
207void TrayIcon::settingsChanged()
208{
209 if ( Settings::showTrayIcon() ) {
210 show();
211 setUnread(m_unread, true);
212 }
213 else {
214 hide();
215 }
216}
217}
218#include "trayicon.moc"