summaryrefslogtreecommitdiffstats
path: root/amarok/src/systray.cpp
blob: 4b2f44b9d3b42691a5649741a16c1906f7c75f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// AmarokSystray
//
// Contributors: Stanislav Karchebny <berkus@users.sf.net>, (C) 2003
//               berkus, mxcl, eros, eean
//               Timothy Pearson <kb9vqf@pearsoncomputing.net> (c) 2010
//
// Copyright: like rest of Amarok
//

#include "amarok.h"
#include "amarokconfig.h"
#include "enginecontroller.h"
#include "systray.h"

#include <tqevent.h>
#include <tqimage.h>
#include <tdeaction.h>
#include <tdeapplication.h>
#include <tdepopupmenu.h>
#include <kiconeffect.h>
#include <kstandarddirs.h>

namespace Amarok
{
    static TQPixmap
    loadOverlay( const char *iconName, int iconWidth )
    {
        return TQImage( locate( "data", TQString( "amarok/images/b_%1.png" ).arg( iconName ) ), "PNG" ).smoothScale( ((iconWidth/2)-(iconWidth/20)), ((iconWidth/2)-(iconWidth/20)) );
    }
}


Amarok::TrayIcon::TrayIcon( TQWidget *playerWidget )
        : KSystemTray( playerWidget )
        , EngineObserver( EngineController::instance() )
        , trackLength( 0 )
        , mergeLevel( -1 )
        , overlay( 0 )
        , blinkTimerID( 0 )
        , overlayVisible( false )
        , m_lastFmMode( false )
{
    TDEActionCollection* const ac = Amarok::actionCollection();

    setAcceptDrops( true );

    ac->action( "prev"        )->plug( contextMenu() );
    ac->action( "play_pause"  )->plug( contextMenu() );
    ac->action( "stop"        )->plug( contextMenu() );
    ac->action( "next"        )->plug( contextMenu() );

    //seems to be necessary
    TDEAction *quit = actionCollection()->action( "file_quit" );
    quit->disconnect();
    connect( quit, TQT_SIGNAL(activated()), kapp, TQT_SLOT(quit()) );

    baseIcon     = KSystemTray::loadIcon( "amarok" );
    playOverlay  = Amarok::loadOverlay( "play", baseIcon.width() );
    pauseOverlay = Amarok::loadOverlay( "pause", baseIcon.width() );
    overlayVisible = false;

    //paintIcon();
    setPixmap( baseIcon );
}

bool
Amarok::TrayIcon::event( TQEvent *e )
{
    switch( e->type() )
    {
    case TQEvent::Drop:
    case TQEvent::Wheel:
    case TQEvent::DragEnter:
        return Amarok::genericEventHandler( this, e );

    case TQEvent::Timer:
        if( TQT_TQTIMEREVENT(e)->timerId() != blinkTimerID )
            return KSystemTray::event( e );

        // if we're playing, blink icon
        if ( overlay == &playOverlay )
        {
            overlayVisible = !overlayVisible;
            paintIcon( mergeLevel, true );
        }

        return true;

    case TQEvent::MouseButtonPress:
        if( TQT_TQMOUSEEVENT(e)->button() == Qt::MidButton )
        {
            EngineController::instance()->playPause();

            return true;
        }

        //else FALL THROUGH

    default:
        return KSystemTray::event( e );
    }
}

void Amarok::TrayIcon::resizeTrayIcon ()
{
	// Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
	baseIcon = KSystemTray::loadSizedIcon( "amarok", width() );
	if (overlay == &pauseOverlay) {
		pauseOverlay = Amarok::loadOverlay( "pause", width() );
		overlay = &pauseOverlay;
	}
	if (overlay == &playOverlay) {
		playOverlay = Amarok::loadOverlay( "play", width() );
		overlay = &playOverlay;
	}
	playOverlay  = Amarok::loadOverlay( "play", width() );
	pauseOverlay = Amarok::loadOverlay( "pause", width() );
	grayedIcon = TQPixmap();
	alternateIcon = TQPixmap();
	paintIcon( -1, true );
}

void Amarok::TrayIcon::resizeEvent ( TQResizeEvent * )
{
	resizeTrayIcon();
}

void Amarok::TrayIcon::showEvent ( TQShowEvent * )
{
	resizeTrayIcon();
}

void
Amarok::TrayIcon::engineStateChanged( Engine::State state, Engine::State /*oldState*/ )
{
    // stop timer
    if ( blinkTimerID )
    {
        killTimer( blinkTimerID );
        blinkTimerID = 0;
    }
    // draw overlay
    overlayVisible = true;

    // draw the right overlay for each state
    switch( state )
    {
    case Engine::Paused:
        overlay = &pauseOverlay;
        paintIcon( mergeLevel, true );
        break;

    case Engine::Playing:
        overlay = &playOverlay;
        if( AmarokConfig::animateTrayIcon() )
           blinkTimerID = startTimer( 1500 );  // start 'blink' timer

        paintIcon( mergeLevel, true ); // repaint the icon
        break;

    case Engine::Empty:
        overlayVisible = false;
        paintIcon( -1, true ); // repaint the icon
                               // fall through to default:
    default:
        setLastFm( false );
    }
}

void
Amarok::TrayIcon::engineNewMetaData( const MetaBundle &bundle, bool /*trackChanged*/ )
{
    trackLength = bundle.length() * 1000;
    setLastFm( bundle.url().protocol() == "lastfm" );
}

void
Amarok::TrayIcon::engineTrackPositionChanged( long position, bool /*userSeek*/ )
{
    mergeLevel = trackLength ? ((baseIcon.height() + 1) * position) / trackLength : -1;
    paintIcon( mergeLevel );
}

void
Amarok::TrayIcon::paletteChange( const TQPalette & op )
{
    if ( palette().active().highlight() == op.active().highlight() || alternateIcon.isNull() )
        return;

    alternateIcon.resize( 0, 0 );
    paintIcon( mergeLevel, true );
}

void
Amarok::TrayIcon::paintIcon( int mergePixels, bool force )
{
    // skip redrawing the same pixmap
    static int mergePixelsCache = 0;
    if ( mergePixels == mergePixelsCache && !force ) {
        return;
    }
    mergePixelsCache = mergePixels;

    if ( mergePixels < 0 ) {
        return blendOverlay( baseIcon );
    }

    // make up the grayed icon
    if ( grayedIcon.isNull() )
    {
        TQImage tmpTrayIcon = baseIcon.convertToImage();
        TDEIconEffect::semiTransparent( tmpTrayIcon );
        grayedIcon = tmpTrayIcon;
    }

    // make up the alternate icon (use highlight color but more saturated)
    if ( alternateIcon.isNull() )
    {
        TQImage tmpTrayIcon = baseIcon.convertToImage();
        // eros: this looks cool with dark red blue or green but sucks with
        // other colors (such as kde default's pale pink..). maybe the effect
        // or the blended color has to be changed..
        TQColor saturatedColor = palette().active().highlight();
        int hue, sat, value;
        saturatedColor.getHsv( &hue, &sat, &value );
        saturatedColor.setHsv( hue, sat > 200 ? 200 : sat, value < 100 ? 100 : value );
        TDEIconEffect::colorize( tmpTrayIcon, saturatedColor/* TQt::blue */, 0.9 );
        alternateIcon = tmpTrayIcon;
    }

    if ( mergePixels >= alternateIcon.height() ) {
        return blendOverlay( grayedIcon );
    }
    if ( mergePixels == 0 ) {
        return blendOverlay( alternateIcon );
    }

    // mix [ grayed <-> colored ] icons
    TQPixmap tmpTrayPixmap = alternateIcon;
    copyBlt( &tmpTrayPixmap, 0,0, &grayedIcon, 0,0,
            alternateIcon.width(), mergePixels>0 ? mergePixels-1 : 0 );
    blendOverlay( tmpTrayPixmap );
}

void
Amarok::TrayIcon::blendOverlay( TQPixmap &sourcePixmap )
{
    if ( !overlayVisible || !overlay || overlay->isNull() ) {
        return setPixmap( sourcePixmap ); // @since 3.2
    }

    // here comes the tricky part.. no tdefx functions are helping here.. :-(
    // we have to blend pixmaps with different sizes (blending will be done in
    // the bottom-left corner of source pixmap with a smaller overlay pixmap)
    int opW = overlay->width(),
        opH = overlay->height(),
        opX = 1,
        opY = sourcePixmap.height() - opH;

    // get the rectangle where blending will take place
    TQPixmap sourceCropped( opW, opH, sourcePixmap.depth() );
    copyBlt( &sourceCropped, 0,0, &sourcePixmap, opX,opY, opW,opH );

    //speculative fix for a bactrace we received
    //crash was in convertToImage() somewhere in this function
    if( sourceCropped.isNull() ) {
        return setPixmap( sourcePixmap );
    }

    // blend the overlay image over the cropped rectangle
    TQImage blendedImage = sourceCropped.convertToImage();
    TQImage overlayImage = overlay->convertToImage();
    TDEIconEffect::overlay( blendedImage, overlayImage );
    sourceCropped.convertFromImage( blendedImage );

    // put back the blended rectangle to the original image
    TQPixmap sourcePixmapCopy = sourcePixmap;
    copyBlt( &sourcePixmapCopy, opX,opY, &sourceCropped, 0,0, opW,opH );

    setPixmap( sourcePixmapCopy ); // @since 3.2
}

void
Amarok::TrayIcon::setLastFm( bool lastFmActive )
{
    if( lastFmActive == m_lastFmMode ) return;

    static int separatorId = 0;

    TDEActionCollection* const ac = Amarok::actionCollection();
    if( ac->action( "ban" ) == 0 ) return; //if the LastFm::Controller doesn't exist yet

    if( lastFmActive )
    {
        ac->action( "play_pause" )->unplug( contextMenu() );
        // items are inserted in reverse order!
        ac->action( "ban" ) ->plug( contextMenu(), 4 );
        ac->action( "love" )->plug( contextMenu(), 4 );
        ac->action( "skip" )->plug( contextMenu(), 4 );
        separatorId = contextMenu()->insertSeparator( 4 );

        m_lastFmMode = true;
    }
    else
    {
        ac->action( "play_pause" )->plug( contextMenu(), 2 );
        ac->action( "ban" ) ->unplug( contextMenu() );
        ac->action( "love" )->unplug( contextMenu() );
        ac->action( "skip" )->unplug( contextMenu() );

        if( separatorId != 0 )
            contextMenu()->removeItem( separatorId ); // kill separator
        m_lastFmMode = false;
   }
}