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 <kapplication.h>
29 #include <kwin.h>
30 #include <kiconeffect.h>
31 #include <kdebug.h>
32 #include <klocale.h>
33 #include <kglobalsettings.h>
34 #include <dcopclient.h>
35 #include <dcopref.h>
36 #include <kpopupmenu.h>
37 #include <kiconloader.h>
38 
39 #include <tqbitmap.h>
40 #include <tqpainter.h>
41 #include <tqfont.h>
42 #include <tqtooltip.h>
43 
44 
45 namespace Akregator {
46 
47 TrayIcon* TrayIcon::m_instance = 0;
48 
49 TrayIcon* TrayIcon::getInstance()
50 {
51  return m_instance;
52 }
53 
54 void TrayIcon::setInstance(TrayIcon* trayIcon)
55 {
56  m_instance = trayIcon;
57 }
58 
59 
60 TrayIcon::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  KIconEffect::deSaturate(m_lightIconImage, 0.60);
67  setPixmap(m_defaultIcon);
68  TQToolTip::add(this, i18n("Akregator - RSS Feed Reader"));
69 }
70 
71 
72 TrayIcon::~TrayIcon()
73 {}
74 
75 
76 void TrayIcon::mousePressEvent(TQMouseEvent *e) {
77  if (e->button() == Qt::LeftButton) {
78  emit showPart();
79  }
80 
81  KSystemTray::mousePressEvent(e);
82 }
83 
84 
85 TQPixmap TrayIcon::takeScreenshot() const
86 {
87  TQPoint g = mapToGlobal(pos());
88  int desktopWidth = kapp->desktop()->width();
89  int desktopHeight = kapp->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(qt_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/*KApplication::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(KApplication::palette().active().foreground());
120  painter.begin(&finalShot);
121  painter.drawPixmap(BORDER, BORDER, shot);
122  painter.end();
123  return shot; // not finalShot?? -fo
124 }
125 
126 void TrayIcon::slotSetUnread(int unread)
127 {
128  if (unread==m_unread)
129  return;
130 
131  m_unread=unread;
132 
133  TQToolTip::remove(this);
134  TQToolTip::add(this, i18n("Akregator - 1 unread article", "Akregator - %n unread articles", unread > 0 ? unread : 0));
135 
136  if (unread <= 0)
137  {
138  setPixmap(m_defaultIcon);
139  }
140  else
141  {
142  // from KMSystemTray
143  int oldW = pixmap()->size().width();
144  int oldH = pixmap()->size().height();
145 
146  TQString uStr=TQString::number( unread );
147  TQFont f=KGlobalSettings::generalFont();
148  f.setBold(true);
149  float pointSize=f.pointSizeFloat();
150  TQFontMetrics fm(f);
151  int w=fm.width(uStr);
152  if( w > (oldW) )
153  {
154  pointSize *= float(oldW) / float(w);
155  f.setPointSizeFloat(pointSize);
156  }
157 
158  TQPixmap pix(oldW, oldH);
159  pix.fill(TQt::white);
160  TQPainter p(&pix);
161  p.setFont(f);
162  p.setPen(TQt::blue);
163  p.drawText(pix.rect(), TQt::AlignCenter, uStr);
164 
165  pix.setMask(pix.createHeuristicMask());
166  TQImage img=pix.convertToImage();
167 
168  // overlay
169  TQImage overlayImg=m_lightIconImage.copy();
170  KIconEffect::overlay(overlayImg, img);
171 
172  TQPixmap icon;
173  icon.convertFromImage(overlayImg);
174  setPixmap(icon);
175  }
176 }
177 
178 void TrayIcon::viewButtonClicked()
179 {
180  TQWidget *p=TQT_TQWIDGET(parent());
181  KWin::forceActiveWindow(p->winId());
182 }
183 
184 void TrayIcon::settingsChanged()
185 {
186  if ( Settings::showTrayIcon() )
187  show();
188  else
189  hide();
190 }
191 }
192 #include "trayicon.moc"