summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/audiowidget.cpp
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/audiowidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/audiowidget.cpp277
1 files changed, 277 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/audiowidget.cpp b/core/multimedia/opieplayer/audiowidget.cpp
new file mode 100644
index 0000000..243c58c
--- a/dev/null
+++ b/core/multimedia/opieplayer/audiowidget.cpp
@@ -0,0 +1,277 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include <qwidget.h>
21#include <qpixmap.h>
22#include <qbutton.h>
23#include <qpainter.h>
24#include <qframe.h>
25#include <qpe/resource.h>
26#include "audiowidget.h"
27#include "mediaplayerstate.h"
28
29
30extern MediaPlayerState *mediaPlayerState;
31
32
33static const int xo = -2; // movable x offset
34static const int yo = 22; // movable y offset
35
36
37struct MediaButton {
38 int xPos, yPos;
39 int color;
40 bool isToggle, isBig, isHeld, isDown;
41};
42
43
44// Layout information for the audioButtons (and if it is a toggle button or not)
45MediaButton audioButtons[] = {
46 { 3*30-15+xo, 3*30-13+yo, 0, TRUE, TRUE, FALSE, FALSE }, // play
47 { 1*30+xo, 5*30+yo, 2, FALSE, FALSE, FALSE, FALSE }, // stop
48 { 5*30+xo, 5*30+yo, 2, TRUE, FALSE, FALSE, FALSE }, // pause
49 { 6*30-5+xo, 3*30+yo, 1, FALSE, FALSE, FALSE, FALSE }, // next
50 { 0*30+5+xo, 3*30+yo, 1, FALSE, FALSE, FALSE, FALSE }, // previous
51 { 3*30+xo, 0*30+5+yo, 3, FALSE, FALSE, FALSE, FALSE }, // volume up
52 { 3*30+xo, 6*30-5+yo, 3, FALSE, FALSE, FALSE, FALSE }, // volume down
53 { 5*30+xo, 1*30+yo, 0, TRUE, FALSE, FALSE, FALSE }, // repeat/loop
54 { 1*30+xo, 1*30+yo, 0, FALSE, FALSE, FALSE, FALSE } // playlist
55};
56
57
58static const int numButtons = (sizeof(audioButtons)/sizeof(MediaButton));
59
60
61AudioWidget::AudioWidget(QWidget* parent, const char* name, WFlags f) :
62 QWidget( parent, name, f )
63{
64 setCaption( tr("MediaPlayer") );
65 setBackgroundPixmap( Resource::loadPixmap( "mpegplayer/metalFinish" ) );
66 pixmaps[0] = new QPixmap( Resource::loadPixmap( "mpegplayer/mediaButtonsAll" ) );
67 pixmaps[1] = new QPixmap( Resource::loadPixmap( "mpegplayer/mediaButtonsBig" ) );
68 pixmaps[2] = new QPixmap( Resource::loadPixmap( "mpegplayer/mediaControls" ) );
69 pixmaps[3] = new QPixmap( Resource::loadPixmap( "mpegplayer/animatedButton" ) );
70
71 songInfo = new Ticker( this );
72 songInfo->setFocusPolicy( QWidget::NoFocus );
73 songInfo->setGeometry( QRect( 7, 3, 220, 20 ) );
74
75 slider = new QSlider( Qt::Horizontal, this );
76 slider->setFixedWidth( 220 );
77 slider->setFixedHeight( 20 );
78 slider->setMinValue( 0 );
79 slider->setMaxValue( 1 );
80 slider->setBackgroundPixmap( Resource::loadPixmap( "mpegplayer/metalFinish" ) );
81 slider->setFocusPolicy( QWidget::NoFocus );
82 slider->setGeometry( QRect( 7, 262, 220, 20 ) );
83
84 connect( slider, SIGNAL( sliderPressed() ), this, SLOT( sliderPressed() ) );
85 connect( slider, SIGNAL( sliderReleased() ), this, SLOT( sliderReleased() ) );
86
87 connect( mediaPlayerState, SIGNAL( lengthChanged(long) ), this, SLOT( setLength(long) ) );
88 connect( mediaPlayerState, SIGNAL( positionChanged(long) ),this, SLOT( setPosition(long) ) );
89 connect( mediaPlayerState, SIGNAL( positionUpdated(long) ),this, SLOT( setPosition(long) ) );
90 connect( mediaPlayerState, SIGNAL( viewChanged(char) ), this, SLOT( setView(char) ) );
91 connect( mediaPlayerState, SIGNAL( loopingToggled(bool) ), this, SLOT( setLooping(bool) ) );
92 connect( mediaPlayerState, SIGNAL( pausedToggled(bool) ), this, SLOT( setPaused(bool) ) );
93 connect( mediaPlayerState, SIGNAL( playingToggled(bool) ), this, SLOT( setPlaying(bool) ) );
94
95 // Intialise state
96 setLength( mediaPlayerState->length() );
97 setPosition( mediaPlayerState->position() );
98 setLooping( mediaPlayerState->fullscreen() );
99 setPaused( mediaPlayerState->paused() );
100 setPlaying( mediaPlayerState->playing() );
101
102}
103
104
105AudioWidget::~AudioWidget() {
106 for ( int i = 0; i < 4; i++ )
107 delete pixmaps[i];
108}
109
110
111static bool audioSliderBeingMoved = FALSE;
112
113
114void AudioWidget::sliderPressed() {
115 audioSliderBeingMoved = TRUE;
116}
117
118
119void AudioWidget::sliderReleased() {
120 audioSliderBeingMoved = FALSE;
121 if ( slider->width() == 0 )
122 return;
123 long val = long((double)slider->value() * mediaPlayerState->length() / slider->width());
124 mediaPlayerState->setPosition( val );
125}
126
127
128void AudioWidget::setPosition( long i ) {
129 updateSlider( i, mediaPlayerState->length() );
130}
131
132
133void AudioWidget::setLength( long max ) {
134 updateSlider( mediaPlayerState->position(), max );
135}
136
137
138void AudioWidget::setView( char view ) {
139 if ( view == 'a' ) {
140 startTimer( 150 );
141 showMaximized();
142 } else {
143 killTimers();
144 hide();
145 }
146}
147
148
149void AudioWidget::updateSlider( long i, long max ) {
150 if ( max == 0 )
151 return;
152 // Will flicker too much if we don't do this
153 // Scale to something reasonable
154 int width = slider->width();
155 int val = int((double)i * width / max);
156 if ( !audioSliderBeingMoved ) {
157 if ( slider->value() != val )
158 slider->setValue( val );
159 if ( slider->maxValue() != width )
160 slider->setMaxValue( width );
161 }
162}
163
164
165void AudioWidget::setToggleButton( int i, bool down ) {
166 if ( down != audioButtons[i].isDown )
167 toggleButton( i );
168}
169
170
171void AudioWidget::toggleButton( int i ) {
172 audioButtons[i].isDown = !audioButtons[i].isDown;
173 QPainter p(this);
174 paintButton ( &p, i );
175}
176
177
178void AudioWidget::paintButton( QPainter *p, int i ) {
179 int x = audioButtons[i].xPos;
180 int y = audioButtons[i].yPos;
181 int offset = 22 + 14 * audioButtons[i].isBig + audioButtons[i].isDown;
182 int buttonSize = 64 + audioButtons[i].isBig * (90 - 64);
183 p->drawPixmap( x, y, *pixmaps[audioButtons[i].isBig], buttonSize * (audioButtons[i].isDown + 2 * audioButtons[i].color), 0, buttonSize, buttonSize );
184 p->drawPixmap( x + offset, y + offset, *pixmaps[2], 18 * i, 0, 18, 18 );
185}
186
187
188void AudioWidget::timerEvent( QTimerEvent * ) {
189 static int frame = 0;
190 if ( !mediaPlayerState->paused() && audioButtons[ AudioPlay ].isDown ) {
191 frame = frame >= 7 ? 0 : frame + 1;
192 int x = audioButtons[AudioPlay].xPos;
193 int y = audioButtons[AudioPlay].yPos;
194 QPainter p( this );
195 // Optimize to only draw the little bit of the changing images which is different
196 p.drawPixmap( x + 14, y + 8, *pixmaps[3], 32 * frame, 0, 32, 32 );
197 p.drawPixmap( x + 37, y + 37, *pixmaps[2], 18 * AudioPlay, 0, 6, 3 );
198 }
199}
200
201
202void AudioWidget::mouseMoveEvent( QMouseEvent *event ) {
203 for ( int i = 0; i < numButtons; i++ ) {
204 int size = audioButtons[i].isBig;
205 int x = audioButtons[i].xPos;
206 int y = audioButtons[i].yPos;
207 if ( event->state() == QMouseEvent::LeftButton ) {
208 // The test to see if the mouse click is inside the circular button or not
209 // (compared with the radius squared to avoid a square-root of our distance)
210 int radius = 32 + 13 * size;
211 QPoint center = QPoint( x + radius, y + radius );
212 QPoint dXY = center - event->pos();
213 int dist = dXY.x() * dXY.x() + dXY.y() * dXY.y();
214 bool isOnButton = dist <= (radius * radius);
215 // QRect r( x, y, 64 + 22*size, 64 + 22*size );
216 // bool isOnButton = r.contains( event->pos() ); // Rectangular Button code
217 if ( isOnButton && !audioButtons[i].isHeld ) {
218 audioButtons[i].isHeld = TRUE;
219 toggleButton(i);
220 switch (i) {
221 case AudioVolumeUp: emit moreClicked(); return;
222 case AudioVolumeDown: emit lessClicked(); return;
223 }
224 } else if ( !isOnButton && audioButtons[i].isHeld ) {
225 audioButtons[i].isHeld = FALSE;
226 toggleButton(i);
227 }
228 } else {
229 if ( audioButtons[i].isHeld ) {
230 audioButtons[i].isHeld = FALSE;
231 if ( !audioButtons[i].isToggle )
232 setToggleButton( i, FALSE );
233 switch (i) {
234 case AudioPlay: mediaPlayerState->setPlaying(audioButtons[i].isDown); return;
235 case AudioStop: mediaPlayerState->setPlaying(FALSE); return;
236 case AudioPause: mediaPlayerState->setPaused(audioButtons[i].isDown); return;
237 case AudioNext: mediaPlayerState->setNext(); return;
238 case AudioPrevious: mediaPlayerState->setPrev(); return;
239 case AudioLoop: mediaPlayerState->setLooping(audioButtons[i].isDown); return;
240 case AudioVolumeUp: emit moreReleased(); return;
241 case AudioVolumeDown: emit lessReleased(); return;
242 case AudioPlayList: mediaPlayerState->setList(); return;
243 }
244 }
245 }
246 }
247}
248
249
250void AudioWidget::mousePressEvent( QMouseEvent *event ) {
251 mouseMoveEvent( event );
252}
253
254
255void AudioWidget::mouseReleaseEvent( QMouseEvent *event ) {
256 mouseMoveEvent( event );
257}
258
259
260void AudioWidget::showEvent( QShowEvent* ) {
261 QMouseEvent event( QEvent::MouseMove, QPoint( 0, 0 ), 0, 0 );
262 mouseMoveEvent( &event );
263}
264
265
266void AudioWidget::closeEvent( QCloseEvent* ) {
267 mediaPlayerState->setList();
268}
269
270
271void AudioWidget::paintEvent( QPaintEvent * ) {
272 QPainter p( this );
273 for ( int i = 0; i < numButtons; i++ )
274 paintButton( &p, i );
275}
276
277