akregator/src

trayicon.cpp
00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2004 Stanislav Karchebny <Stanislav.Karchebny@kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include "akregatorconfig.h"
00026 #include "trayicon.h"
00027 
00028 #include <tdeapplication.h>
00029 #include <twin.h>
00030 #include <kiconeffect.h>
00031 #include <kdebug.h>
00032 #include <tdelocale.h>
00033 #include <tdeglobalsettings.h>
00034 #include <dcopclient.h>
00035 #include <dcopref.h>
00036 #include <tdepopupmenu.h>
00037 #include <kiconloader.h>
00038 
00039 #include <tqbitmap.h>
00040 #include <tqpainter.h>
00041 #include <tqfont.h>
00042 #include <tqtooltip.h>
00043 
00044 
00045 namespace Akregator {
00046 
00047 TrayIcon* TrayIcon::m_instance = 0;
00048 
00049 TrayIcon* TrayIcon::getInstance()
00050 {
00051     return m_instance;
00052 }
00053 
00054 void TrayIcon::setInstance(TrayIcon* trayIcon)
00055 {
00056     m_instance = trayIcon;
00057 }
00058 
00059 
00060 TrayIcon::TrayIcon(TQWidget *parent, const char *name)
00061         : KSystemTray(parent, name), m_unread(0)
00062 {
00063     m_defaultIcon=KSystemTray::loadIcon("akregator");
00064     TQPixmap m_unreadIcon=KSystemTray::loadIcon("akregator_empty");
00065     m_lightIconImage=m_unreadIcon.convertToImage();
00066     TDEIconEffect::deSaturate(m_lightIconImage, 0.60);
00067     setPixmap(m_defaultIcon);
00068     TQToolTip::add(this, i18n("Akregator - RSS Feed Reader"));
00069 }
00070 
00071 
00072 TrayIcon::~TrayIcon()
00073 {}
00074 
00075 
00076 void TrayIcon::mousePressEvent(TQMouseEvent *e) {
00077     if (e->button() == Qt::LeftButton) {
00078         emit showPart();
00079     }
00080 
00081     KSystemTray::mousePressEvent(e);
00082 }
00083 
00084 
00085 TQPixmap TrayIcon::takeScreenshot() const
00086 {
00087     TQPoint g = mapToGlobal(pos());
00088     int desktopWidth  = kapp->desktop()->width();
00089     int desktopHeight = kapp->desktop()->height();
00090     int tw = width();
00091     int th = height();
00092     int w = desktopWidth / 4;
00093     int h = desktopHeight / 9;
00094     int x = g.x() + tw/2 - w/2; // Center the rectange in the systray icon
00095     int y = g.y() + th/2 - h/2;
00096     if (x < 0)
00097         x = 0; // Move the rectangle to stay in the desktop limits
00098     if (y < 0)
00099         y = 0;
00100     if (x + w > desktopWidth)
00101         x = desktopWidth - w;
00102     if (y + h > desktopHeight)
00103         y = desktopHeight - h;
00104 
00105         // Grab the desktop and draw a circle arround the icon:
00106     TQPixmap shot = TQPixmap::grabWindow(tqt_xrootwin(), x, y, w, h);
00107     TQPainter painter(&shot);
00108     const int MARGINS = 6;
00109     const int WIDTH   = 3;
00110     int ax = g.x() - x - MARGINS -1;
00111     int ay = g.y() - y - MARGINS -1;
00112     painter.setPen( TQPen(TQt::red/*TDEApplication::palette().active().highlight()*/, WIDTH) );
00113     painter.drawArc(ax, ay, tw + 2*MARGINS, th + 2*MARGINS, 0, 16*360);
00114     painter.end();
00115 
00116     // Paint the border
00117     const int BORDER = 1;
00118     TQPixmap finalShot(w + 2*BORDER, h + 2*BORDER);
00119     finalShot.fill(TDEApplication::palette().active().foreground());
00120     painter.begin(&finalShot);
00121     painter.drawPixmap(BORDER, BORDER, shot);
00122     painter.end();
00123     return shot; // not finalShot?? -fo
00124 }
00125 
00126 void TrayIcon::resizeEvent(TQResizeEvent *)
00127 {
00128     setUnread(m_unread, true);
00129 }
00130 
00131 void TrayIcon::slotSetUnread(int unread)
00132 {
00133     setUnread(unread, false);
00134 }
00135 
00136 void TrayIcon::setUnread(int unread, bool force)
00137 {
00138     if ((unread==m_unread) && (!force))
00139     {
00140         return;
00141     }
00142 
00143     m_unread=unread;
00144 
00145     TQToolTip::remove(this);
00146     TQToolTip::add(this, i18n("Akregator - 1 unread article", "Akregator - %n unread articles", unread > 0 ? unread : 0));
00147 
00148     if (unread <= 0)
00149     {
00150         setPixmap(m_defaultIcon);
00151     }
00152     else
00153     {
00154         // from KMSystemTray
00155         int oldW = pixmap()->size().width();
00156         int oldH = pixmap()->size().height();
00157 
00158         TQString uStr=TQString::number( unread );
00159         TQFont f=TDEGlobalSettings::generalFont();
00160         f.setBold(true);
00161 
00162         // increase the size of the font for the number of unread messages if the
00163         // icon size is less than 22 pixels
00164         // see bug 1251
00165         int realIconHeight = height();
00166         if (realIconHeight < 22) {
00167             f.setPointSizeFloat( f.pointSizeFloat() * 2.0 );
00168         }
00169 
00170         // decrease the size of the font for the number of unread articles if the
00171         // number doesn't fit into the available space
00172         float pointSize=f.pointSizeFloat();
00173         TQFontMetrics fm(f);
00174         int w=fm.width(uStr);
00175         if( w > (oldW) )
00176         {
00177             pointSize *= float(oldW) / float(w);
00178             f.setPointSizeFloat(pointSize);
00179         }
00180 
00181         TQPixmap pix(oldW, oldH);
00182         pix.fill(TQt::white);
00183         TQPainter p(&pix);
00184         p.setFont(f);
00185         p.setPen(Settings::unreadTextColor());
00186         p.drawText(pix.rect(), TQt::AlignCenter, uStr);
00187 
00188         pix.setMask(pix.createHeuristicMask());
00189         TQImage img=pix.convertToImage();
00190 
00191         // overlay
00192         TQImage overlayImg=m_lightIconImage.copy();
00193         TDEIconEffect::overlay(overlayImg, img);
00194 
00195         TQPixmap icon;
00196         icon.convertFromImage(overlayImg);
00197         setPixmap(icon);
00198     }
00199 }
00200 
00201 void TrayIcon::viewButtonClicked()
00202 {
00203     TQWidget *p=TQT_TQWIDGET(parent());
00204     KWin::forceActiveWindow(p->winId());
00205 }
00206 
00207 void TrayIcon::settingsChanged()
00208 {
00209     if ( Settings::showTrayIcon() ) {
00210         show();
00211         setUnread(m_unread, true);
00212     }
00213     else {
00214         hide();
00215     }
00216 }
00217 }
00218 #include "trayicon.moc"