Relicense all GPLv2 only code to GPLv2+.
[kdenetwork.git] / kget / ui / tray.cpp
blob03b8d32075a4a97b42e39bf5b5d2acb34ecaa0a5
1 /* This file is part of the KDE project
3 Copyright (C) 2002 by Patrick Charbonnier <pch@freeshell.org>
4 Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
12 #include "ui/tray.h"
14 #include "core/kget.h"
15 #include "mainwindow.h"
17 #include <kaboutdata.h>
18 #include <kactioncollection.h>
19 #include <kapplication.h>
20 #include <kiconloader.h>
21 #include <kiconeffect.h>
22 #include <kmenu.h>
23 #include <kdebug.h>
25 #include <QTimer>
26 #include <QClipboard>
27 #include <QPainter>
28 #include <QLabel>
30 /** class Tray
31 * Reimplmentation of the system tray class adding drag/drop
32 * capabilities and the quit action.
34 Tray::Tray(MainWindow * parent)
35 : KSystemTrayIcon(parent),
36 blinkTimer( 0 ),
37 grayedIcon( 0 ),
38 alternateIcon( 0 ),
39 overlay( 0 ),
40 overlayVisible( false )
42 baseIcon = new QPixmap( KSystemTrayIcon::loadIcon("kget").pixmap(22) );
43 playOverlay = new QPixmap( SmallIcon( "media-playback-start" ) );
44 stopOverlay = new QPixmap( SmallIcon( "media-playback-pause" ) );
46 paintIcon();
48 // add preferences action to the context menu
49 QMenu * cm = contextMenu();
50 cm->addAction( parent->actionCollection()->action("new_download") );
51 cm->addSeparator();
52 cm->addAction( parent->actionCollection()->action("start_all_download") );
53 cm->addAction( parent->actionCollection()->action("stop_all_download") );
54 cm->addSeparator();
55 cm->addAction( parent->actionCollection()->action("preferences") );
56 cm->addAction( parent->actionCollection()->action("konqueror_integration") );
57 cm->addAction( parent->actionCollection()->action("show_drop_target") );
59 // add tooltip telling "I'm kget"
60 setToolTip( KGlobal::mainComponent().aboutData()->shortDescription() );
62 // connecting the "Exit" menu item to the quit() of our app
63 connect( this, SIGNAL( quitSelected() ), kapp, SLOT(quit()));
64 connect( this, SIGNAL( activated( QSystemTrayIcon::ActivationReason ) ),
65 SLOT( slotActivated( QSystemTrayIcon::ActivationReason ) ) );
68 // dtor: delete internal classes
69 Tray::~Tray()
71 delete blinkTimer;
72 delete baseIcon;
73 delete grayedIcon;
74 delete alternateIcon;
75 delete playOverlay;
76 delete stopOverlay;
77 // delete overlay; // deleting overlay is wrong - it's either playOverlay or stopOverlay
80 // filter middle mouse clicks to ask scheduler to paste URL
81 void Tray::slotActivated( QSystemTrayIcon::ActivationReason reason )
83 if ( reason == QSystemTrayIcon::MiddleClick )
85 //Here we paste the transfer
86 QString newtransfer = QApplication::clipboard()->text();
87 newtransfer = newtransfer.trimmed();
89 if(!newtransfer.isEmpty())
90 KGet::addTransfer(KUrl(newtransfer), QString(), QString(), true);
94 // display blinking icon when downloading
95 void Tray::setDownloading( bool running )
97 kDebug(5001) << "Tray::setDownloading";
99 if(!blinkTimer)
101 blinkTimer = new QTimer;
102 connect( blinkTimer, SIGNAL( timeout() ), this, SLOT( slotTimeout() ) );
105 overlayVisible = true;
107 if(running)
109 overlay = playOverlay;
110 blinkTimer->start( 1500 ); // start 'blink' timer
111 paintIcon( 50, true );
113 else
115 overlay = stopOverlay;
116 blinkTimer->start( 1500 ); // start 'hide' timer
117 paintIcon( 50, true );
121 // slot executed every 1s: toggle icon pixmap
122 void Tray::slotTimeout()
124 if ( overlay == playOverlay )
126 overlayVisible = !overlayVisible;
127 paintIcon( 50/*mergeLevel*/, true );
129 else if( overlay == stopOverlay )
131 overlay = 0;
132 blinkTimer->stop();
133 paintIcon( -1, true );
134 overlayVisible = false;
138 void Tray::paintIcon( int mergePixels, bool force )
140 // skip redrawing the same pixmap
141 static int mergePixelsCache = 0;
142 if ( mergePixels == mergePixelsCache && !force )
143 return;
144 mergePixelsCache = mergePixels;
146 if ( mergePixels < 0 )
147 return blendOverlay( baseIcon );
149 // make up the grayed icon
150 if ( !grayedIcon )
152 QImage tmpTrayIcon = baseIcon->toImage();
153 KIconEffect::semiTransparent( tmpTrayIcon );
154 grayedIcon = new QPixmap( QPixmap::fromImage( tmpTrayIcon ) );
156 if ( mergePixels == 0 )
157 return blendOverlay( grayedIcon );
159 // make up the alternate icon (use hilight color but more saturated)
160 if ( !alternateIcon )
162 QImage tmpTrayIcon = baseIcon->toImage();
163 // eros: this looks cool with dark red blue or green but sucks with
164 // other colors (such as kde default's pale pink..). maybe the effect
165 // or the blended color has to be changed..
166 QLabel label; //just a hack to get the palette
167 QColor saturatedColor = label.palette().color(QPalette::Active, QPalette::Highlight);
168 int hue, sat, value;
169 saturatedColor.getHsv( &hue, &sat, &value );
170 saturatedColor.setHsv( hue, (sat + 510) / 3, value );
171 KIconEffect::colorize( tmpTrayIcon, saturatedColor/* Qt::blue */, 0.9 );
172 alternateIcon = new QPixmap( QPixmap::fromImage( tmpTrayIcon ) );
174 if ( mergePixels >= alternateIcon->height() )
175 return blendOverlay( alternateIcon );
177 // mix [ grayed <-> colored ] icons
178 QPixmap tmpTrayPixmap( *alternateIcon );
179 QPainter paint;
180 paint.begin( &tmpTrayPixmap );
181 paint.drawPixmap( 0, 0, *grayedIcon, 0, 0,
182 alternateIcon->width(), alternateIcon->height() - mergePixels );
183 paint.end();
185 blendOverlay( &tmpTrayPixmap );
188 void Tray::blendOverlay( QPixmap * sourcePixmap )
190 if ( !overlayVisible || !overlay || overlay->isNull() )
191 return setIcon( *sourcePixmap );
193 // here comes the tricky part.. no kdefx functions are helping here.. :-(
194 // we have to blend pixmaps with different sizes (blending will be done in
195 // the bottom-left corner of source pixmap with a smaller overlay pixmap)
196 int opW = overlay->width(),
197 opH = overlay->height(),
198 opX = 1,
199 opY = sourcePixmap->height() - opH;
201 // get the rectangle where blending will take place
202 QPixmap sourceCropped( opW, opH );
203 sourceCropped.fill(Qt::transparent);
204 QPainter paint;
205 paint.begin( &sourceCropped );
206 paint.drawPixmap( 0, 0, *sourcePixmap, opX, opY, opW,opH );
207 paint.end();
209 // blend the overlay image over the cropped rectangle
210 QImage blendedImage = sourceCropped.toImage();
211 QImage overlayImage = overlay->toImage();
212 KIconEffect::overlay( blendedImage, overlayImage );
213 sourceCropped = QPixmap().fromImage( blendedImage );
215 // put back the blended rectangle to the original image
216 QPixmap sourcePixmapCopy = *sourcePixmap;
217 paint.begin( &sourcePixmapCopy );
218 paint.drawPixmap( opX, opY, sourceCropped, 0, 0, opW,opH );
219 paint.end();
221 setIcon( sourcePixmapCopy );
224 #include "tray.moc"