summaryrefslogtreecommitdiff
path: root/noncore/multimedia/opieplayer2/mediawidget.cpp
blob: f193001ca979ea6627628eb8fd0eec046ee5aa97 (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
/*
   Copyright (C) 2002 Simon Hausmann <simon@lst.de>
             (C) 2002 Max Reiss <harlekin@handhelds.org>
             (C) 2002 L. Potter <ljp@llornkcor.com>
             (C) 2002 Holger Freyther <zecke@handhelds.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <assert.h>

#include "mediawidget.h"
#include "playlistwidget.h"
#include "skin.h"

MediaWidget::MediaWidget( PlayListWidget &_playList, MediaPlayerState &_mediaPlayerState, QWidget *parent, const char *name )
    : QWidget( parent, name ), mediaPlayerState( _mediaPlayerState ), playList( _playList )
{
    connect( &mediaPlayerState, SIGNAL( displayTypeChanged( MediaPlayerState::DisplayType ) ),
             this, SLOT( setDisplayType( MediaPlayerState::DisplayType ) ) );
    connect( &mediaPlayerState, SIGNAL( lengthChanged( long ) ),
             this, SLOT( setLength( long ) ) );
    connect( &mediaPlayerState, SIGNAL( playingToggled( bool ) ),
             this, SLOT( setPlaying( bool ) ) );

    setBackgroundMode( NoBackground );
}

MediaWidget::~MediaWidget()
{
}

void MediaWidget::setupButtons( const SkinButtonInfo *skinInfo, uint buttonCount,
                                const Skin &skin )
{
    buttonMask = skin.buttonMask( skinInfo, buttonCount );

    buttons.clear();
    buttons.reserve( buttonCount );

    for ( uint i = 0; i < buttonCount; ++i ) {
        Button button = setupButton( skinInfo[ i ], skin );
        buttons.push_back( button );
    }
}

MediaWidget::Button MediaWidget::setupButton( const SkinButtonInfo &buttonInfo, const Skin &skin )
{
    Button button;
    button.command = buttonInfo.command;
    button.type = buttonInfo.type;
    button.mask = skin.buttonMaskImage( buttonInfo.fileName );

    return button;
}

void MediaWidget::loadDefaultSkin( const GUIInfo &guiInfo )
{
    Skin skin( guiInfo.fileNameInfix );
    skin.setCachable( false );
    loadSkin( guiInfo.buttonInfo, guiInfo.buttonCount, skin );
}

void MediaWidget::loadSkin( const SkinButtonInfo *skinInfo, uint buttonCount, const Skin &skin )
{
    backgroundPixmap = skin.backgroundPixmap();
    buttonUpImage = skin.buttonUpImage();
    buttonDownImage = skin.buttonDownImage();

    setupButtons( skinInfo, buttonCount, skin );
}

void MediaWidget::closeEvent( QCloseEvent * )
{
    mediaPlayerState.setList();
}

void MediaWidget::paintEvent( QPaintEvent *pe )
{
    QPainter p( this );

    if ( mediaPlayerState.isFullscreen() ) {
        // Clear the background
        p.setBrush( QBrush( Qt::black ) );
        return;
    }

    QPixmap buffer( size() );
    QPainter bufferedPainter( &buffer );
    bufferedPainter.drawTiledPixmap( rect(), backgroundPixmap, QPoint( 0, 0 ) );
    paintAllButtons( bufferedPainter );
    p.drawPixmap( 0, 0, buffer );
}

void MediaWidget::resizeEvent( QResizeEvent *e )
{
    QPixmap pixUp = combineImageWithBackground( buttonUpImage, backgroundPixmap, upperLeftOfButtonMask );
    QPixmap pixDn = combineImageWithBackground( buttonDownImage, backgroundPixmap, upperLeftOfButtonMask );

    for ( ButtonVector::iterator it = buttons.begin(); it != buttons.end(); ++it ) {
        Button &button = *it;

        if ( button.mask.isNull() )
            continue;
        button.pixUp = addMaskToPixmap( pixUp, button.mask );
        button.pixDown = addMaskToPixmap( pixDn, button.mask );
    }

    QWidget::resizeEvent( e );
}

MediaWidget::Button *MediaWidget::buttonAt( const QPoint &position )
{
    if ( position.x() <= 0 || position.y() <= 0 ||
         position.x() >= buttonMask.width() ||
         position.y() >= buttonMask.height() )
        return 0;

    int pixelIdx = buttonMask.pixelIndex( position.x(), position.y() );
    for ( ButtonVector::iterator it = buttons.begin(); it != buttons.end(); ++it )
        if ( it->command + 1 == pixelIdx )
            return &( *it );

    return 0;
}

void MediaWidget::mousePressEvent( QMouseEvent *event )
{
    Button *button = buttonAt( event->pos() - upperLeftOfButtonMask );

    if ( !button ) {
        QWidget::mousePressEvent( event );
        return;
    }

    switch ( button->command ) {
        case VolumeUp:   emit moreClicked(); return;
        case VolumeDown: emit lessClicked(); return;
        case Back:       emit backClicked(); return;
        case Forward:    emit forwardClicked(); return;
        default: break;
    }
}

void MediaWidget::mouseReleaseEvent( QMouseEvent *event )
{
    Button *button = buttonAt( event->pos() - upperLeftOfButtonMask );

    if ( !button ) {
        QWidget::mouseReleaseEvent( event );
        return;
    }

    if ( button->type == ToggleButton )
        toggleButton( *button );

    handleCommand( button->command, button->isDown );
}

void MediaWidget::makeVisible()
{
}

void MediaWidget::handleCommand( Command command, bool buttonDown )
{
    switch ( command ) {
        case Play:       mediaPlayerState.togglePaused(); return;
        case Stop:       mediaPlayerState.setPlaying(FALSE); return;
        case Next:       if( playList.currentTab() == PlayListWidget::CurrentPlayList ) mediaPlayerState.setNext(); return;
        case Previous:   if( playList.currentTab() == PlayListWidget::CurrentPlayList ) mediaPlayerState.setPrev(); return;
        case Loop:       mediaPlayerState.setLooping( buttonDown ); return;
        case VolumeUp:   emit moreReleased(); return;
        case VolumeDown: emit lessReleased(); return;
        case PlayList:   mediaPlayerState.setList();  return;
        case Forward:    emit forwardReleased(); return;
        case Back:       emit backReleased(); return;
        case FullScreen: mediaPlayerState.setFullscreen( true ); makeVisible(); return;
        default: assert( false );
    }
}

bool MediaWidget::isOverButton( const QPoint &position, int buttonId ) const
{
    return ( position.x() > 0 && position.y() > 0 && 
             position.x() < buttonMask.width() && 
             position.y() < buttonMask.height() && 
             buttonMask.pixelIndex( position.x(), position.y() ) == buttonId + 1 );
}

void MediaWidget::paintAllButtons( QPainter &p )
{
    for ( ButtonVector::const_iterator it = buttons.begin();
          it != buttons.end(); ++it )
        paintButton( p, *it );
}

void MediaWidget::paintButton( const Button &button )
{
    QPainter p( this );
    paintButton( p, button );
}

void MediaWidget::paintButton( QPainter &p, const Button &button )
{
    if ( button.isDown )
        p.drawPixmap( upperLeftOfButtonMask, button.pixDown );
    else
        p.drawPixmap( upperLeftOfButtonMask, button.pixUp );
}

void MediaWidget::setToggleButton( Command command, bool down )
{
    for ( ButtonVector::iterator it = buttons.begin(); it != buttons.end(); ++it )
        if ( it->command == command ) {
            setToggleButton( *it, down );
            return;
        }
}

void MediaWidget::setToggleButton( Button &button, bool down )
{
    if ( down != button.isDown )
        toggleButton( button );
}

void MediaWidget::toggleButton( Button &button )
{
    button.isDown = !button.isDown;

    paintButton( button );
}

QPixmap MediaWidget::combineImageWithBackground( const QImage &image, const QPixmap &background, const QPoint &offset )
{
    QPixmap pix( image.size() );
    QPainter p( &pix );
    p.drawTiledPixmap( pix.rect(), background, offset );
    p.drawImage( 0, 0, image );
    return pix;
}

QPixmap MediaWidget::addMaskToPixmap( const QPixmap &pix, const QBitmap &mask )
{
    QPixmap result( pix );
    result.setMask( mask );
    return result;
}

/* vim: et sw=4 ts=4
 */