Main Page | Class List | File List | Class Members | File Members | Related Pages

wsystray.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Bojan D.                                        *
00003  *   bojan_d@users.sourceforge.net                                         *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020  
00026 #include <kpopupmenu.h>
00027 #include <kiconloader.h>
00028 #include <kaboutapplication.h>
00029 #include <kaction.h>
00030 #include <klocale.h>
00031 #include <kglobalsettings.h>
00032 #include <kiconeffect.h>
00033 #include <kdebug.h>
00034 
00035 #include <qpainter.h>
00036 #include <qimage.h>
00037 #include <qstring.h>
00038 #include <qtimer.h>
00039 #include <qbitmap.h>
00040 #include <qtooltip.h>
00041 
00042 #include "wsystray.h"
00043 #include "kwebsnap.h"
00044 
00045 WSysTray::WSysTray( QWidget *parent, const char *name )
00046  : KSystemTray( parent, name ),
00047    m_startId(-1),
00048    m_stopId(-1),
00049    m_configId(-1)
00050 {
00051         m_defaultIcon = KSystemTray::loadIcon("kwebsnap");
00052         setPixmap(m_defaultIcon);
00053         
00054         m_parent = reinterpret_cast<KWebSnap*> (parent);
00055                 
00056         m_menu = new KPopupMenu();
00057     
00058     KPopupMenu* popupmenu = contextMenu(); 
00059     
00060         popupmenu->changeTitle( popupmenu->idAt( 0 ), loadIcon("kwebsnap"), "KWebSnap");
00061         popupmenu->insertItem(SmallIcon("ksnapshot"), i18n("Snapshot"), m_parent, SLOT(slotSnapshot()));
00062         popupmenu->insertSeparator();
00063         m_startId = popupmenu->insertItem(SmallIcon("run"), i18n("Start"), m_parent, SLOT(slotStart()));
00064         m_stopId = popupmenu->insertItem(SmallIcon("stop"), i18n("Stop"), m_parent, SLOT(slotStop()));
00065         popupmenu->insertSeparator();
00066         m_configId = popupmenu->insertItem(SmallIcon("configure"), i18n("Configure..."), m_parent, SLOT(slotConfig()));
00067         popupmenu->insertSeparator();
00068         popupmenu->insertItem(SmallIcon("help"), i18n("Help"), m_parent, SLOT(slotHelp()));
00069         popupmenu->insertItem(SmallIcon("kwebsnap"), i18n("About KWebSnap"), m_parent, SLOT(slotAbout()));
00070         
00071         connect(this, SIGNAL(quitSelected()), m_parent, SLOT(slotQuit()));
00072         
00073         QToolTip::add(this, i18n("KWebSnap"));
00074             
00075         show();
00076 }
00077 
00078 WSysTray::~WSysTray()
00079 {
00080         delete m_menu;
00081     m_menu = 0;
00082 }
00083 
00084 // writes the integer n over the system tray's icon
00085 void WSysTray::setNumber(int n)
00086 {
00087         QString string;
00088         string = string.setNum(n); 
00089 
00090         setText(string);
00091         QToolTip::add(this, string);
00092 }
00093 
00094 // writes the string txt over the system tray's icon
00095 void WSysTray::setText(const QString & text)
00096 {
00097         int oldW = pixmap()->size().width();
00098         int oldH = pixmap()->size().height();
00099 
00100         QFont f = KGlobalSettings::generalFont();
00101         f.setBold(true);
00102         float pointSize = f.pointSizeFloat();
00103         QFontMetrics fm(f);
00104         int w = fm.width(text);
00105         if( w > (oldW) )
00106         {
00107                 pointSize *= float(oldW) / float(w);
00108                 f.setPointSizeFloat(pointSize);
00109         }
00110 
00111         QPixmap pix(oldW, oldH);
00112         pix.fill(Qt::white);
00113         QPainter p(&pix);
00114         p.setFont(f);
00115         p.setPen(Qt::red);
00116         p.drawText(pix.rect(), Qt::AlignCenter, text);
00117 
00118         pix.setMask(pix.createHeuristicMask());
00119         QImage img = pix.convertToImage();
00120 
00121         // overlay
00122         drawImage(img);
00123 }
00124 
00125 void WSysTray::drawFlash()
00126 {
00127         QPixmap flash(KGlobal::iconLoader()->loadIcon("ksnapshot",KIcon::Small,0,false));
00128         
00129         drawPixmap(flash);
00130         //QToolTip::add(this, i18n("Taking snapshot..."));
00131 }
00132 
00133 void WSysTray::drawSave()
00134 {
00135         QPixmap saveIcon(KGlobal::iconLoader()->loadIcon("filesave",KIcon::Small,0,false));
00136         
00137         drawPixmap(saveIcon);
00138         //QToolTip::add(this, i18n("Saving files..."));
00139 }
00140 
00141 void WSysTray::drawSend()
00142 {
00143         QPixmap send(KGlobal::iconLoader()->loadIcon("forward",KIcon::Small,0,false));
00144                 
00145         drawPixmap(send);
00146         //QToolTip::add(this, i18n("Transfering files..."));
00147 }
00148 
00149 
00150 void WSysTray::drawPixmap(const QPixmap& pixmap)
00151 {
00152         blendOverlay(pixmap, m_defaultIcon);    
00153 }
00154 
00155 
00156 void WSysTray::blendOverlay(const QPixmap& overlay, const QPixmap& sourcePixmap)
00157 {
00158         /*      This is straight from amaroK's system tray class.  
00159                 Thanks a lot guys. :)   
00160                 
00161                 (C) 2002-2003, Mark Kretschmann
00162                 (C) 2003-2005, The amaroK Development Squad     */      
00163         
00164         // here comes the tricky part.. no kdefx functions are helping here.. :-(
00165     // we have to blend pixmaps with different sizes (blending will be done in
00166     // the bottom-left corner of source pixmap with a smaller overlay pixmap)
00167     int opW = overlay.width(),
00168         opH = overlay.height(),
00169         opX = 1,
00170         opY = sourcePixmap.height() - opH;
00171 
00172     // get the rectangle where blending will take place
00173     QPixmap sourceCropped( opW, opH, sourcePixmap.depth() );
00174     copyBlt( &sourceCropped, 0,0, &sourcePixmap, opX,opY, opW,opH );
00175 
00176     //speculative fix for a bactrace we received
00177     //crash was in covertToImage() somewhere in this function
00178     if( sourceCropped.isNull() )
00179         return setPixmap( sourcePixmap );
00180 
00181     // blend the overlay image over the cropped rectangle
00182     QImage blendedImage = sourceCropped.convertToImage();
00183     QImage overlayImage = overlay.convertToImage();
00184     KIconEffect::overlay( blendedImage, overlayImage );
00185     sourceCropped.convertFromImage( blendedImage );
00186 
00187     // put back the blended rectangle to the original image
00188     QPixmap sourcePixmapCopy = sourcePixmap;
00189     copyBlt( &sourcePixmapCopy, opX,opY, &sourceCropped, 0,0, opW,opH );
00190 
00191     setPixmap( sourcePixmapCopy );
00192 }
00193 
00194 
00195 void WSysTray::drawImage(const QImage& image)
00196 {
00197         drawPixmap(QPixmap(image));
00198 }
00199 
00200 // resets the icon to the original icon
00201 void WSysTray::resetIcon()
00202 {
00203         setPixmap(m_defaultIcon);
00204         QToolTip::add(this, i18n("KWebSnap"));
00205 }
00206 
00207 void WSysTray::disableMenues()
00208 {
00209         setEnableMenues(false);
00210 }
00211 
00212 void WSysTray::enableMenues()
00213 {
00214         setEnableMenues(true);
00215 }
00216 
00217 void WSysTray::setEnableMenues(bool enable)
00218 {
00219         KPopupMenu* popupmenu = contextMenu(); 
00220         
00221         popupmenu->setItemEnabled(m_startId, enable);
00222         popupmenu->setItemEnabled(m_configId, enable);
00223         popupmenu->setItemEnabled(m_stopId, !enable);
00224 }

Generated on Mon Sep 5 12:52:34 2005 for kwebsnap.kdevelop by  doxygen 1.3.9.1