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 Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "akregatorconfig.h"
00026 #include "trayicon.h"
00027 
00028 #include <kapplication.h>
00029 #include <kwin.h>
00030 #include <kiconeffect.h>
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 #include <kglobalsettings.h>
00034 #include <dcopclient.h>
00035 #include <dcopref.h>
00036 #include <kpopupmenu.h>
00037 #include <kiconloader.h>
00038 
00039 #include <qbitmap.h>
00040 #include <qpainter.h>
00041 #include <qfont.h>
00042 #include <qtooltip.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(QWidget *parent, const char *name)
00061         : KSystemTray(parent, name), m_unread(0)
00062 {
00063     m_defaultIcon=KSystemTray::loadIcon("akregator");
00064     QPixmap m_unreadIcon=KSystemTray::loadIcon("akregator_empty");
00065     m_lightIconImage=m_unreadIcon.convertToImage();
00066     KIconEffect::deSaturate(m_lightIconImage, 0.60);
00067     setPixmap(m_defaultIcon);
00068     QToolTip::add(this, i18n("Akregator - RSS Feed Reader"));
00069 }
00070 
00071 
00072 TrayIcon::~TrayIcon()
00073 {}
00074 
00075 
00076 void TrayIcon::mousePressEvent(QMouseEvent *e) {
00077     if (e->button() == LeftButton) {
00078         emit showPart();
00079     }
00080 
00081     KSystemTray::mousePressEvent(e);
00082 }
00083 
00084 
00085 QPixmap TrayIcon::takeScreenshot() const
00086 {
00087     QPoint 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     QPixmap shot = QPixmap::grabWindow(qt_xrootwin(), x, y, w, h);
00107     QPainter 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( QPen(Qt::red/*KApplication::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     QPixmap finalShot(w + 2*BORDER, h + 2*BORDER);
00119     finalShot.fill(KApplication::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::slotSetUnread(int unread)
00127 {
00128     if (unread==m_unread)
00129         return;
00130     
00131     m_unread=unread;
00132     
00133     QToolTip::remove(this);
00134     QToolTip::add(this, i18n("Akregator - 1 unread article", "Akregator - %n unread articles", unread > 0 ? unread : 0));
00135     
00136     if (unread <= 0)
00137     {    
00138         setPixmap(m_defaultIcon);
00139     }
00140     else
00141     {               
00142         // from KMSystemTray
00143         int oldW = pixmap()->size().width();
00144         int oldH = pixmap()->size().height();
00145 
00146         QString uStr=QString::number( unread );
00147         QFont f=KGlobalSettings::generalFont();
00148         f.setBold(true);
00149         float pointSize=f.pointSizeFloat();
00150         QFontMetrics fm(f);
00151         int w=fm.width(uStr);
00152         if( w > (oldW) )
00153         {
00154             pointSize *= float(oldW) / float(w);
00155             f.setPointSizeFloat(pointSize);
00156         }
00157 
00158         QPixmap pix(oldW, oldH);
00159         pix.fill(Qt::white);
00160         QPainter p(&pix);
00161         p.setFont(f);
00162         p.setPen(Qt::blue);
00163         p.drawText(pix.rect(), Qt::AlignCenter, uStr);
00164 
00165         pix.setMask(pix.createHeuristicMask());
00166         QImage img=pix.convertToImage();
00167 
00168         // overlay
00169         QImage overlayImg=m_lightIconImage.copy();
00170         KIconEffect::overlay(overlayImg, img);
00171 
00172         QPixmap icon;
00173         icon.convertFromImage(overlayImg);
00174         setPixmap(icon);
00175     }
00176 }
00177 
00178 void TrayIcon::viewButtonClicked()
00179 {
00180     QWidget *p=static_cast<QWidget*>(parent());
00181     KWin::forceActiveWindow(p->winId());
00182 }
00183 
00184 void TrayIcon::settingsChanged()
00185 {
00186     if ( Settings::showTrayIcon() )
00187         show();
00188     else
00189         hide();
00190 }
00191 }
00192 #include "trayicon.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys