utilities/mpnowplayingbanner/src/mpnowplayingwidget_p.cpp
branchRCL_3
changeset 52 14979e23cb5e
equal deleted inserted replaced
50:26a1709b9fec 52:14979e23cb5e
       
     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: Music Player now playing widget - Private.
       
    15 *
       
    16 */
       
    17 #include <QPainter>
       
    18 #include <QGraphicsSceneMouseEvent>
       
    19 
       
    20 #include <hbstackedlayout.h>
       
    21 #include <hblabel.h>
       
    22 #include <hbicon.h>
       
    23 #include <hbiconitem.h>
       
    24 #include <hbdocumentloader.h>
       
    25 #include <hbinstance.h>
       
    26 #include <hbstyleloader.h>
       
    27 
       
    28 #include "mpenginefactory.h"
       
    29 #include "mpnowplayingwidget_p.h"
       
    30 #include "mpnowplayingwidget.h"
       
    31 #include "mptrace.h"
       
    32 
       
    33 const char *NOW_PLAYING_XML = ":/xml/nowplaying.docml";
       
    34 const char *NOW_PLAYING_CSS = ":/css/banner_color.css";
       
    35 
       
    36 /*!
       
    37     \internal
       
    38     \class MpNowPlayingWidgetPrivate
       
    39     \brief Custom HB widget that displays currently playing song information.
       
    40 
       
    41     This is a composite widget made out of HB widgets, it displays information
       
    42     for the current playing song on the playback engine indicated at
       
    43     construction time.
       
    44 */
       
    45 
       
    46 /*!
       
    47  \internal
       
    48  Constructs the now playing widget private.
       
    49  */
       
    50 MpNowPlayingWidgetPrivate::MpNowPlayingWidgetPrivate( MpNowPlayingWidget *qq )
       
    51     : q_ptr( qq ),
       
    52       mPrimaryText(0),
       
    53       mSecondaryText(0),
       
    54       mState( MpPlaybackData::NotPlaying ),
       
    55       mPlaybackData(0),
       
    56       mMpEngine(0),
       
    57       mIcon(0),
       
    58       mDocumentLoader(0),
       
    59       mCurrentPressedState( BannerNone )
       
    60 {
       
    61     TX_ENTRY_ARGS( " Q pointer=" << ( void * )qq )
       
    62     QGraphicsWidget *widget;
       
    63     bool widgetsOk = false;
       
    64     
       
    65     HbStyleLoader::registerFilePath(NOW_PLAYING_CSS);  
       
    66     
       
    67     mPlayIcon = new HbIcon( QString( "qtg_mono_play" ) );
       
    68     mPauseIcon = new HbIcon( QString( "qtg_mono_pause" ) );
       
    69 
       
    70     mDocumentLoader = new HbDocumentLoader();
       
    71     if (mDocumentLoader) {
       
    72             mDocumentLoader->load( NOW_PLAYING_XML, &widgetsOk);
       
    73     }
       
    74 
       
    75     if (widgetsOk) {
       
    76 
       
    77         // find the main widget container and all of its child items
       
    78         widget = mDocumentLoader->findWidget(QString("nowPlayingContainer"));
       
    79         HbWidget* container = qobject_cast<HbWidget*>(widget);
       
    80         container->setParentItem(q_ptr);
       
    81 
       
    82         widget = mDocumentLoader->findWidget(QString("primaryText"));
       
    83         mPrimaryText = qobject_cast<HbLabel*>(widget);
       
    84         mPrimaryText->setObjectName( "bannerLabel" );
       
    85         mPrimaryText->setProperty( "state", "normal" );
       
    86         
       
    87 
       
    88         widget = mDocumentLoader->findWidget(QString("secondaryText"));
       
    89         mSecondaryText = qobject_cast<HbLabel*>(widget);
       
    90         mSecondaryText->setObjectName( "bannerLabel" );
       
    91         mSecondaryText->setProperty( "state", "normal" );
       
    92         
       
    93         
       
    94         widget = mDocumentLoader->findWidget(QString("playPause"));
       
    95         mIcon = qobject_cast<HbLabel*>(widget);
       
    96         mIcon->setObjectName( "bannerLabel" );
       
    97         mIcon->setProperty( "state", "normal" );
       
    98         
       
    99         HbStackedLayout *mylayout;
       
   100         mylayout = new HbStackedLayout(q_ptr);
       
   101         mylayout->addItem( container );
       
   102 
       
   103         q_ptr->setLayout( mylayout );
       
   104     }
       
   105     else {
       
   106         TX_LOG_ARGS("Error: invalid xml file.");
       
   107         Q_ASSERT_X(widgetsOk, "MpNowPlayingWidgetPrivate", "invalid xml file");
       
   108     }
       
   109 
       
   110     mMpEngine = MpEngineFactory::sharedEngine();
       
   111     mPlaybackData = mMpEngine->playbackData();
       
   112     setEnabled(true);
       
   113     TX_EXIT
       
   114 }
       
   115 
       
   116 
       
   117 /*!
       
   118  \internal
       
   119  Destructs the now playing widget private.
       
   120  */
       
   121 MpNowPlayingWidgetPrivate::~MpNowPlayingWidgetPrivate()
       
   122 {
       
   123     TX_ENTRY
       
   124     delete mPlayIcon;
       
   125     delete mPauseIcon;
       
   126     delete mDocumentLoader;
       
   127     TX_EXIT
       
   128 }
       
   129 
       
   130 /*!
       
   131  If \a enabled is true, the item is enabled; otherwise, it is disabled.
       
   132  Item is enabled by default.
       
   133  */
       
   134 void MpNowPlayingWidgetPrivate::setEnabled( bool enabled )
       
   135 {
       
   136     if ( enabled ) {
       
   137         connect( mPlaybackData, SIGNAL( playbackStateChanged() ),
       
   138                  this, SLOT( setState() ) );
       
   139 
       
   140         connect( mPlaybackData, SIGNAL( playbackInfoChanged() ),
       
   141                  this, SLOT( updateBannerInfo() ) );
       
   142 
       
   143     setState();
       
   144     updateBannerInfo();
       
   145     }
       
   146     else {
       
   147         disconnect( mPlaybackData, SIGNAL( playbackStateChanged() ),
       
   148                  this, SLOT( setState() ) );
       
   149     
       
   150         disconnect( mPlaybackData, SIGNAL( playbackInfoChanged() ),
       
   151                  this, SLOT( updateBannerInfo() ) );
       
   152     }
       
   153 }
       
   154 
       
   155 /*!
       
   156  Return if banner is attached based on current playback state
       
   157  */
       
   158 bool MpNowPlayingWidgetPrivate::isBannerAttached()
       
   159 {
       
   160     return mState == MpPlaybackData::NotPlaying ? false : true;
       
   161 }
       
   162 
       
   163 /*!
       
   164  \internal
       
   165  Offers click \a event to the widget privated side, if event is consumed it returns true.
       
   166  */
       
   167 
       
   168 bool MpNowPlayingWidgetPrivate::handleClickEvent(QGraphicsSceneMouseEvent *event)
       
   169 {
       
   170     bool ret = true;
       
   171     if ( mCurrentPressedState != BannerNone ) {
       
   172         //Widget was previosly pressed, handle the event.
       
   173         QRectF geometry = q_ptr->rect();
       
   174         qreal delta = mIcon->windowFrameGeometry().top() - geometry.top();
       
   175         QRectF iconTouchRect( mIcon->windowFrameGeometry() );
       
   176         iconTouchRect.adjust( -delta, -delta, delta, delta );
       
   177         if ( iconTouchRect.contains( event->pos() ) ) {
       
   178             if ( mCurrentPressedState == BannerIcon ) {
       
   179                     // click on play/pause
       
   180                 mMpEngine->playPause();
       
   181             }
       
   182         }
       
   183         else if ( mCurrentPressedState == BannerLabels && geometry.contains( event->pos() )){
       
   184             // click somewhere else on the widget.
       
   185             ret = false;
       
   186         }
       
   187         /*else {
       
   188         // click outside the widget.
       
   189         }*/
       
   190         mCurrentPressedState = BannerNone;
       
   191     }
       
   192     return ret;
       
   193 }
       
   194 
       
   195 /*!
       
   196  \internal
       
   197  Changes text and icon color when \a pressed
       
   198  */
       
   199 void MpNowPlayingWidgetPrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event, bool pressed)
       
   200 {   
       
   201 
       
   202     QRectF geometry = q_ptr->rect();
       
   203     qreal delta = mIcon->windowFrameGeometry().top() - geometry.top();
       
   204     QRectF iconTouchRect( mIcon->windowFrameGeometry() );
       
   205     iconTouchRect.adjust( -delta, -delta, delta, delta );
       
   206     
       
   207     if( iconTouchRect.contains( event->pos() ) &&  pressed ) {  
       
   208         if( mCurrentPressedState == BannerNone ) {
       
   209             mCurrentPressedState = BannerIcon;
       
   210         }
       
   211         mIcon->setProperty( "state", "pressed" );
       
   212     }
       
   213     else if( geometry.contains( event->pos() ) && pressed ){
       
   214         if( mCurrentPressedState == BannerNone ) {
       
   215             mCurrentPressedState = BannerLabels;
       
   216         }
       
   217         mPrimaryText->setProperty( "state", "pressed" );
       
   218         mSecondaryText->setProperty( "state", "pressed" );
       
   219     }
       
   220     else { 
       
   221         mIcon->setProperty( "state", "normal" );
       
   222         mPrimaryText->setProperty( "state", "normal" );
       
   223         mSecondaryText->setProperty( "state", "normal" );
       
   224     }
       
   225 }
       
   226 
       
   227 /*!
       
   228  \internal
       
   229  Changes text and icon color when moved
       
   230  */
       
   231 void MpNowPlayingWidgetPrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   232 {   
       
   233 
       
   234     QRectF geometry = q_ptr->rect();
       
   235     qreal delta = mIcon->windowFrameGeometry().top() - geometry.top();
       
   236     QRectF iconTouchRect( mIcon->windowFrameGeometry() );
       
   237     iconTouchRect.adjust( -delta, -delta, delta, delta );
       
   238     
       
   239     if( iconTouchRect.contains( event->pos() ) ) {  
       
   240         if( mCurrentPressedState == BannerIcon ) {
       
   241             mIcon->setProperty( "state", "pressed" );
       
   242         }
       
   243         else {
       
   244             mIcon->setProperty( "state", "normal" );
       
   245         }
       
   246         mPrimaryText->setProperty( "state", "normal" );
       
   247         mSecondaryText->setProperty( "state", "normal" );
       
   248     }
       
   249     else if( geometry.contains( event->pos() ) &&  mCurrentPressedState == BannerLabels ){
       
   250         mPrimaryText->setProperty( "state", "pressed" );
       
   251         mSecondaryText->setProperty( "state", "pressed" );
       
   252         mIcon->setProperty( "state", "normal" );
       
   253     }
       
   254     else { 
       
   255         mIcon->setProperty( "state", "normal" );
       
   256         mPrimaryText->setProperty( "state", "normal" );
       
   257         mSecondaryText->setProperty( "state", "normal" );
       
   258     }
       
   259 }
       
   260 
       
   261 /*!
       
   262  \internal
       
   263  Sets the simplified playback \a state.
       
   264  */
       
   265 void MpNowPlayingWidgetPrivate::setState( )
       
   266 {
       
   267     MpPlaybackData::SimplifiedState state = mPlaybackData->playbackState();
       
   268     TX_ENTRY_ARGS( "State = " << state )
       
   269             
       
   270     switch ( state ) {
       
   271         case MpPlaybackData::NotPlaying:
       
   272             if ( mState != MpPlaybackData::NotPlaying )
       
   273                 emit q_ptr->playbackAttachmentChanged( false );
       
   274             break;
       
   275         case MpPlaybackData::Playing:
       
   276             mIcon->setIcon( *mPauseIcon );
       
   277             mIcon->setProperty( "state", mIcon->property("state").toString() );
       
   278             if ( mState == MpPlaybackData::NotPlaying )
       
   279                 emit q_ptr->playbackAttachmentChanged( true );
       
   280             break;
       
   281         case MpPlaybackData::Paused:
       
   282         case MpPlaybackData::Stopped:    
       
   283             mIcon->setIcon( *mPlayIcon );
       
   284             mIcon->setProperty( "state", mIcon->property("state").toString() );
       
   285             if ( mState == MpPlaybackData::NotPlaying )
       
   286                 emit q_ptr->playbackAttachmentChanged( true );
       
   287             break;
       
   288         default:
       
   289             break;
       
   290     }
       
   291     mState = state;
       
   292     TX_EXIT
       
   293 }
       
   294 
       
   295 /*!
       
   296  \internal
       
   297  Sets the \a title 
       
   298  */
       
   299 void MpNowPlayingWidgetPrivate::updateBannerInfo()
       
   300 {
       
   301     mPrimaryText->setPlainText( mPlaybackData->title() );
       
   302     mPrimaryText->setProperty( "state", mPrimaryText->property("state").toString() );
       
   303     
       
   304     QString data = mPlaybackData->artist();
       
   305     if ( !data.isEmpty() ) {
       
   306         mSecondaryText->setPlainText( data );
       
   307     }
       
   308     else {
       
   309         mSecondaryText->setPlainText( hbTrId( "txt_mus_other_unknown" ) );
       
   310     }
       
   311     mSecondaryText->setProperty( "state", mSecondaryText->property("state").toString() );
       
   312 }
       
   313