mpviewplugins/mpplaybackviewplugin/src/mpplaybackwidget.cpp
changeset 19 4e84c994a771
child 22 ecf06a08d4d9
equal deleted inserted replaced
5:2a40e88564c8 19:4e84c994a771
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Playback widget for Music Player playback view.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <qtime>
       
    19 
       
    20 #include <hbprogressslider.h>
       
    21 #include <hbinstance.h>
       
    22 #include <hblabel.h>
       
    23 #include <hbfontspec.h>
       
    24 #include <hbdocumentloader.h>
       
    25 #include <hbstackedlayout.h>
       
    26 
       
    27 #include "mpplaybackwidget.h"
       
    28 #include "mpplaybackdata.h"
       
    29 #include "mptrace.h"
       
    30 
       
    31 const unsigned int KMicroSecToMiliSec( 1000 );
       
    32 
       
    33 /*!
       
    34     \class MpPlaybackWidget
       
    35     \brief Music Player playback widget.
       
    36 
       
    37     This widget displays playback information on the playback view.
       
    38 */
       
    39 
       
    40 /*!
       
    41     \fn void setPlaybackPosition(int value)
       
    42 
       
    43     This signal is emitted when the user drags the progressbar to a new
       
    44     position, \a value indicates the position.
       
    45  */
       
    46 
       
    47 /*!
       
    48  Constructs the collection view plugin.
       
    49  */
       
    50 MpPlaybackWidget::MpPlaybackWidget(MpPlaybackData *data, QGraphicsItem *parent )
       
    51     : HbWidget(parent),
       
    52       mPlaybackData(data),
       
    53       mDocumentLoader(0),
       
    54       mProgreesBarDragging(false),
       
    55       mDuration(0)
       
    56 {
       
    57     TX_ENTRY
       
    58     mLayout = new HbStackedLayout(this);
       
    59     mLayout->setContentsMargins( 0.0, 0.0, 0.0, 0.0 );
       
    60     mLayout->setMinimumSize( 0.0, 0.0 );
       
    61     setLayout( mLayout );
       
    62     bool widgetsOk = false;
       
    63     bool layoutOk = false;
       
    64     mDocumentLoader = new HbDocumentLoader();
       
    65     HbMainWindow *mainWindow = hbInstance->allMainWindows()[0];
       
    66 
       
    67     if ( mDocumentLoader ) {
       
    68         mDocumentLoader->load( QString(":/playbackviewdocml/playbackwidget.docml"), &widgetsOk);
       
    69         layoutOk = loadLayout( mainWindow->orientation() );
       
    70     }
       
    71     if ( widgetsOk && layoutOk ) {
       
    72         QGraphicsWidget *tmpWidgetPtr;
       
    73         tmpWidgetPtr = mDocumentLoader->findWidget(QString("playbackWidgetContainer"));
       
    74         tmpWidgetPtr->setParentItem(this);
       
    75         mLayout->addItem( qobject_cast<HbWidget*>( tmpWidgetPtr ) );
       
    76         tmpWidgetPtr = mDocumentLoader->findWidget(QString("albumText"));
       
    77         mAlbumName = qobject_cast<HbLabel*>(tmpWidgetPtr);
       
    78         tmpWidgetPtr = mDocumentLoader->findWidget(QString("artistText"));
       
    79         mArtistName = qobject_cast<HbLabel*>(tmpWidgetPtr);
       
    80         tmpWidgetPtr = mDocumentLoader->findWidget(QString("songText"));
       
    81         mSongTitle = qobject_cast<HbLabel*>(tmpWidgetPtr);
       
    82         tmpWidgetPtr = mDocumentLoader->findWidget(QString("albumArt"));
       
    83         mAlbumArt = qobject_cast<HbLabel*>(tmpWidgetPtr);
       
    84         tmpWidgetPtr = mDocumentLoader->findWidget(QString("progressBar"));
       
    85         mProgressBar = qobject_cast<HbProgressSlider*>(tmpWidgetPtr);
       
    86 
       
    87         //TODO: move this to docml when supported.
       
    88         mProgressBar->setMinMaxTextVisible(true);
       
    89     }
       
    90     else {
       
    91         TX_LOG_ARGS("Error: invalid xml file.");
       
    92         Q_ASSERT_X(widgetsOk, "MpPlaybackWidget", "invalid xml file - widget");
       
    93         Q_ASSERT_X(layoutOk, "MpPlaybackWidget", "invalid xml file - layout");
       
    94     }
       
    95 
       
    96     connect( mainWindow, SIGNAL(orientationChanged(Qt::Orientation)),
       
    97              this, SLOT(loadLayout(Qt::Orientation)) );
       
    98 
       
    99     connect( mProgressBar, SIGNAL(sliderPressed()), this, SLOT(handleSliderPressed()) );
       
   100     connect( mProgressBar, SIGNAL(sliderReleased()), this, SLOT(handleSliderReleased()) );
       
   101     connect( mProgressBar, SIGNAL(sliderMoved(int)), this, SLOT(handleSliderMoved(int)) );
       
   102 
       
   103     connect( mPlaybackData, SIGNAL(playbackInfoChanged()), this, SLOT(playbackInfoChanged()) );
       
   104     connect( mPlaybackData, SIGNAL(durationChanged()), this, SLOT(durationChanged()) );
       
   105     connect( mPlaybackData, SIGNAL(positionChanged()), this, SLOT(positionChanged()) );
       
   106     connect( mPlaybackData, SIGNAL(albumArtReady()), this, SLOT(albumArtChanged()) );
       
   107 
       
   108     mCompositePixmap = QPixmap( 360, 360 );
       
   109     mCompositePixmap.fill( Qt::transparent );
       
   110 
       
   111     TX_EXIT
       
   112 }
       
   113 
       
   114 /*!
       
   115  Constructs the collection view plugin.
       
   116  */
       
   117 MpPlaybackWidget::~MpPlaybackWidget()
       
   118 {
       
   119     TX_ENTRY
       
   120     delete mDocumentLoader;
       
   121     TX_EXIT
       
   122 }
       
   123 
       
   124 /*!
       
   125  Slot to handle playback info changed.
       
   126  */
       
   127 void MpPlaybackWidget::playbackInfoChanged( )
       
   128 {
       
   129     TX_ENTRY
       
   130     mSongTitle->setPlainText( mPlaybackData->title() );
       
   131     mArtistName->setPlainText( mPlaybackData->artist() );
       
   132     mAlbumName->setPlainText( mPlaybackData->album() );
       
   133     TX_EXIT
       
   134 }
       
   135 
       
   136 /*!
       
   137  Slot to handle playback duration changed.
       
   138  */
       
   139 void MpPlaybackWidget::durationChanged()
       
   140 {
       
   141     TX_ENTRY
       
   142     mDuration = mPlaybackData->duration();
       
   143     mDuration /= KMicroSecToMiliSec;
       
   144 
       
   145     mProgressBar->setRange(0, mDuration);
       
   146     mProgressBar->setProgressValue(0);
       
   147     mProgressBar->setSliderValue(0);
       
   148     mProgressBar->setEnabled(true);
       
   149     TX_EXIT
       
   150 }
       
   151 
       
   152 /*!
       
   153  Slot to handle playback position changed.
       
   154  */
       
   155 void MpPlaybackWidget::positionChanged()
       
   156 {
       
   157     TX_ENTRY
       
   158     int position = mPlaybackData->position();
       
   159     position /= KMicroSecToMiliSec;
       
   160 
       
   161     if ( !mProgreesBarDragging ) {
       
   162         mProgressBar->setProgressValue(position);
       
   163         mProgressBar->setSliderValue(position);
       
   164         mProgressBar->setMinText( formatDuration(position) );
       
   165         mProgressBar->setMaxText( formatDuration(mDuration  - position) );
       
   166     }
       
   167     TX_EXIT
       
   168 }
       
   169 
       
   170 /*!
       
   171  Slot to handle Album art changed.
       
   172  */
       
   173 void MpPlaybackWidget::albumArtChanged( )
       
   174 {
       
   175     TX_ENTRY
       
   176     QPixmap pixmap;
       
   177     mPlaybackData->albumArt( pixmap );
       
   178     composeAlbumCover( pixmap );
       
   179 
       
   180     QIcon qicon;
       
   181     if ( !mCompositePixmap.isNull() ) {
       
   182         qicon = QIcon( mCompositePixmap );
       
   183     }
       
   184     else {
       
   185         qicon = QIcon( pixmap );
       
   186     }
       
   187     HbIcon icon( qicon );
       
   188     mAlbumArt->setIcon( icon );
       
   189     TX_EXIT
       
   190 }
       
   191 
       
   192 /*!
       
   193  Orientation change. Load layout based on the \a orientation.
       
   194  */
       
   195 bool MpPlaybackWidget::loadLayout( Qt::Orientation orientation )
       
   196 {
       
   197     bool ret(false);
       
   198     if ( orientation == Qt::Vertical ) {
       
   199         mDocumentLoader->load(QString(":/playbackviewdocml/playbackwidget.docml"), "portrait", &ret);
       
   200     }
       
   201     else {
       
   202         mDocumentLoader->load(QString(":/playbackviewdocml/playbackwidget.docml"), "landscape", & ret);
       
   203     }
       
   204     return ret;
       
   205 }
       
   206 
       
   207 /*!
       
   208  Slot to handle slider pressed.
       
   209  */
       
   210 void MpPlaybackWidget::handleSliderPressed()
       
   211 {
       
   212     TX_LOG
       
   213     mProgreesBarDragging = true;
       
   214 }
       
   215 
       
   216 /*!
       
   217  Slot to handle progressbar relesed.
       
   218  */
       
   219 void MpPlaybackWidget::handleSliderReleased()
       
   220 {
       
   221     TX_ENTRY
       
   222     mProgreesBarDragging = false;
       
   223     emit setPlaybackPosition( KMicroSecToMiliSec * mProgressBar->progressValue() );
       
   224     TX_EXIT
       
   225 }
       
   226 
       
   227 
       
   228 /*!
       
   229  Slot to handle progressbar moved.
       
   230  */
       
   231 void MpPlaybackWidget::handleSliderMoved( int value )
       
   232 {
       
   233     TX_ENTRY_ARGS( "value  =" << value )
       
   234     mProgressBar->setProgressValue(value);
       
   235     mProgressBar->setSliderValue(value);
       
   236     mProgressBar->setMinText( formatDuration(value) );
       
   237     mProgressBar->setMaxText( formatDuration(mDuration  - value) );
       
   238     if ( !mProgreesBarDragging ) {
       
   239         // Click on the progress bar, not a drag.
       
   240         emit setPlaybackPosition( KMicroSecToMiliSec * value );
       
   241     }
       
   242     TX_EXIT
       
   243 }
       
   244 
       
   245 /*!
       
   246  Returns the formated duration defined in \a seconds.
       
   247  */
       
   248 QString MpPlaybackWidget::formatDuration( int seconds )
       
   249 {
       
   250     TX_ENTRY
       
   251     QTime z(0,0,0,0);
       
   252     QTime duration;
       
   253     duration = z.addSecs( seconds );
       
   254     int hour = seconds / 3600;
       
   255     if ( hour == 0 ) {
       
   256         TX_EXIT
       
   257         return duration.toString( "mm:ss" );
       
   258     }
       
   259     else {
       
   260         TX_EXIT
       
   261         return duration.toString( "hh:mm:ss" );
       
   262     }
       
   263 }
       
   264 
       
   265 /*!
       
   266  Compose the album art.
       
   267  */
       
   268 void MpPlaybackWidget::composeAlbumCover( QPixmap& albumart )
       
   269 {
       
   270     TX_ENTRY
       
   271     mCompositePixmap.fill( Qt::transparent );
       
   272     QPainter painter(&mCompositePixmap);
       
   273     painter.setCompositionMode(QPainter::CompositionMode_Clear);
       
   274     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
       
   275     painter.fillRect(mCompositePixmap.rect(), Qt::transparent);
       
   276     painter.drawPixmap(QRect(0, 0,360,360), albumart);
       
   277     TX_EXIT
       
   278 }