00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <assert.h>
00024
00025 #include "mediawidget.h"
00026 #include "skin.h"
00027
00028 MediaWidget::MediaWidget( PlayListWidget &_playList, MediaPlayerState &_mediaPlayerState, QWidget *parent, const char *name )
00029 : QWidget( parent, name ), mediaPlayerState( _mediaPlayerState ), playList( _playList )
00030 {
00031 connect( &mediaPlayerState, SIGNAL( displayTypeChanged(MediaPlayerState::DisplayType) ),
00032 this, SLOT( setDisplayType(MediaPlayerState::DisplayType) ) );
00033 connect( &mediaPlayerState, SIGNAL( lengthChanged(long) ),
00034 this, SLOT( setLength(long) ) );
00035 connect( &mediaPlayerState, SIGNAL( playingToggled(bool) ),
00036 this, SLOT( setPlaying(bool) ) );
00037
00038 setBackgroundMode( NoBackground );
00039 }
00040
00041 MediaWidget::~MediaWidget()
00042 {
00043 }
00044
00045 void MediaWidget::setupButtons( const SkinButtonInfo *skinInfo, uint buttonCount,
00046 const Skin &skin )
00047 {
00048 buttonMask = skin.buttonMask( skinInfo, buttonCount );
00049
00050 buttons.clear();
00051
00052 for ( uint i = 0; i < buttonCount; ++i ) {
00053 Button button = setupButton( skinInfo[ i ], skin );
00054 buttons.append( button );
00055 }
00056 }
00057
00058 MediaWidget::Button MediaWidget::setupButton( const SkinButtonInfo &buttonInfo, const Skin &skin )
00059 {
00060 Button button;
00061 button.command = buttonInfo.command;
00062 button.type = buttonInfo.type;
00063 button.mask = skin.buttonMaskImage( buttonInfo.fileName );
00064
00065 return button;
00066 }
00067
00068 void MediaWidget::loadDefaultSkin( const GUIInfo &guiInfo )
00069 {
00070 Skin skin( guiInfo.fileNameInfix );
00071 skin.setCachable( false );
00072 loadSkin( guiInfo.buttonInfo, guiInfo.buttonCount, skin );
00073 }
00074
00075 void MediaWidget::loadSkin( const SkinButtonInfo *skinInfo, uint buttonCount, const Skin &skin )
00076 {
00077 backgroundPixmap = skin.backgroundPixmap();
00078 buttonUpImage = skin.buttonUpImage();
00079 buttonDownImage = skin.buttonDownImage();
00080
00081 setupButtons( skinInfo, buttonCount, skin );
00082 }
00083
00084 void MediaWidget::closeEvent( QCloseEvent * )
00085 {
00086 mediaPlayerState.setList();
00087 }
00088
00089 void MediaWidget::paintEvent( QPaintEvent * )
00090 {
00091 QPainter p( this );
00092
00093 QPixmap buffer( size() );
00094 QPainter bufferedPainter( &buffer );
00095 bufferedPainter.drawTiledPixmap( rect(), backgroundPixmap, QPoint( 0, 0 ) );
00096 paintAllButtons( bufferedPainter );
00097 p.drawPixmap( 0, 0, buffer );
00098 }
00099
00100 void MediaWidget::resizeEvent( QResizeEvent *e )
00101 {
00102 QPixmap pixUp = combineImageWithBackground( buttonUpImage, backgroundPixmap, upperLeftOfButtonMask );
00103 QPixmap pixDn = combineImageWithBackground( buttonDownImage, backgroundPixmap, upperLeftOfButtonMask );
00104
00105 for ( ButtonVector::Iterator it = buttons.begin(); it != buttons.end(); ++it ) {
00106 Button &button = *it;
00107
00108 if ( button.mask.isNull() )
00109 continue;
00110 button.pixUp = addMaskToPixmap( pixUp, button.mask );
00111 button.pixDown = addMaskToPixmap( pixDn, button.mask );
00112 }
00113
00114 QWidget::resizeEvent( e );
00115 }
00116
00117 MediaWidget::Button *MediaWidget::buttonAt( const QPoint &position )
00118 {
00119 if ( position.x() <= 0 || position.y() <= 0 ||
00120 position.x() >= buttonMask.width() ||
00121 position.y() >= buttonMask.height() )
00122 return 0;
00123
00124 int pixelIdx = buttonMask.pixelIndex( position.x(), position.y() );
00125 for ( ButtonVector::Iterator it = buttons.begin(); it != buttons.end(); ++it )
00126 if ( (*it).command + 1 == pixelIdx )
00127 return &( *it );
00128
00129 return 0;
00130 }
00131
00132 void MediaWidget::mousePressEvent( QMouseEvent *event )
00133 {
00134 Button *button = buttonAt( event->pos() - upperLeftOfButtonMask );
00135
00136 if ( !button ) {
00137 QWidget::mousePressEvent( event );
00138 return;
00139 }
00140
00141 switch ( button->command ) {
00142 case VolumeUp: emit moreClicked(); return;
00143 case VolumeDown: emit lessClicked(); return;
00144 case Back: emit backClicked(); return;
00145 case Forward: emit forwardClicked(); return;
00146 default: break;
00147 }
00148 }
00149
00150 void MediaWidget::mouseReleaseEvent( QMouseEvent *event )
00151 {
00152 Button *button = buttonAt( event->pos() - upperLeftOfButtonMask );
00153
00154 if ( !button ) {
00155 QWidget::mouseReleaseEvent( event );
00156 return;
00157 }
00158
00159 if ( button->type == ToggleButton )
00160 toggleButton( *button );
00161
00162 handleCommand( button->command, button->isDown );
00163 }
00164
00165 void MediaWidget::makeVisible()
00166 {
00167 }
00168
00169 void MediaWidget::handleCommand( Command command, bool buttonDown )
00170 {
00171 switch ( command ) {
00172 case Play: mediaPlayerState.togglePaused(); return;
00173 case Stop: mediaPlayerState.setPlaying(FALSE); return;
00174 case Next: if( playList.currentTab() == PlayListWidget::CurrentPlayList ) mediaPlayerState.setNext(); return;
00175 case Previous: if( playList.currentTab() == PlayListWidget::CurrentPlayList ) mediaPlayerState.setPrev(); return;
00176 case Loop: mediaPlayerState.setLooping( buttonDown ); return;
00177 case VolumeUp: emit moreReleased(); return;
00178 case VolumeDown: emit lessReleased(); return;
00179 case PlayList: mediaPlayerState.setList(); return;
00180 case Forward: emit forwardReleased(); return;
00181 case Back: emit backReleased(); return;
00182 case FullScreen: mediaPlayerState.setFullscreen( true ); makeVisible(); return;
00183 default: assert( false );
00184 }
00185 }
00186
00187 bool MediaWidget::isOverButton( const QPoint &position, int buttonId ) const
00188 {
00189 return ( position.x() > 0 && position.y() > 0 &&
00190 position.x() < buttonMask.width() &&
00191 position.y() < buttonMask.height() &&
00192 buttonMask.pixelIndex( position.x(), position.y() ) == buttonId + 1 );
00193 }
00194
00195 void MediaWidget::paintAllButtons( QPainter &p )
00196 {
00197 for ( ButtonVector::ConstIterator it = buttons.begin();
00198 it != buttons.end(); ++it )
00199 paintButton( p, *it );
00200 }
00201
00202 void MediaWidget::paintButton( const Button &button )
00203 {
00204 QPainter p( this );
00205 paintButton( p, button );
00206 }
00207
00208 void MediaWidget::paintButton( QPainter &p, const Button &button )
00209 {
00210 if ( button.isDown )
00211 p.drawPixmap( upperLeftOfButtonMask, button.pixDown );
00212 else
00213 p.drawPixmap( upperLeftOfButtonMask, button.pixUp );
00214 }
00215
00216 void MediaWidget::setToggleButton( Command command, bool down )
00217 {
00218 for ( ButtonVector::Iterator it = buttons.begin(); it != buttons.end(); ++it )
00219 if ( (*it).command == command ) {
00220 setToggleButton( *it, down );
00221 return;
00222 }
00223 }
00224
00225 void MediaWidget::setToggleButton( Button &button, bool down )
00226 {
00227 if ( down != button.isDown )
00228 toggleButton( button );
00229 }
00230
00231 void MediaWidget::toggleButton( Button &button )
00232 {
00233 button.isDown = !button.isDown;
00234
00235 paintButton( button );
00236 }
00237
00238 QPixmap MediaWidget::combineImageWithBackground( const QImage &image, const QPixmap &background, const QPoint &offset )
00239 {
00240 QPixmap pix( image.size() );
00241 QPainter p( &pix );
00242 p.drawTiledPixmap( pix.rect(), background, offset );
00243 p.drawImage( 0, 0, image );
00244 return pix;
00245 }
00246
00247 QPixmap MediaWidget::addMaskToPixmap( const QPixmap &pix, const QBitmap &mask )
00248 {
00249 QPixmap result( pix );
00250 result.setMask( mask );
00251 return result;
00252 }
00253
00254
00255