videoplayback/hbvideoplaybackview/controlsrc/mpxvideoplaybackprogressbar.cpp
changeset 52 e3cecb93e76a
parent 47 45e72b57a2fd
child 59 a76e86df7ccd
equal deleted inserted replaced
47:45e72b57a2fd 52:e3cecb93e76a
     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:  Implementation of QMPXVideoPlaybackProgressBar
       
    15 *
       
    16 */
       
    17 
       
    18 // Version : %version: da1mmcf#24 %
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include <QTime>
       
    24 #include <QTimer>
       
    25 #include <QGraphicsSceneMouseEvent>
       
    26 
       
    27 #include <hblabel.h>
       
    28 #include <hbframeitem.h>
       
    29 #include <hbframedrawer.h>
       
    30 #include <hbprogressslider.h>
       
    31 #include <hbextendedlocale.h>
       
    32 
       
    33 #include "mpxvideo_debug.h"
       
    34 #include "mpxvideoplaybackprogressbar.h"
       
    35 #include "mpxvideoplaybackdocumentloader.h"
       
    36 #include "mpxvideoplaybackcontrolscontroller.h"
       
    37 
       
    38 const int KSeekingTimeOut = 250;
       
    39 
       
    40 // -------------------------------------------------------------------------------------------------
       
    41 // QMPXVideoPlaybackProgressBar::QMPXVideoPlaybackProgressBar
       
    42 // -------------------------------------------------------------------------------------------------
       
    43 //
       
    44 QMPXVideoPlaybackProgressBar::QMPXVideoPlaybackProgressBar(
       
    45         QMPXVideoPlaybackControlsController* controller )
       
    46     : mController( controller )
       
    47     , mDraggingPosition( 0 )
       
    48     , mSetPosition( -1 )
       
    49     , mNeedToResumeAfterSetPosition( false )
       
    50     , mInitialized( false )
       
    51     , mSliderDragging( false )
       
    52     , mLongTimeFormat( false )
       
    53     , mLiveStreaming( false )
       
    54     , mSeekingTimer( NULL )
       
    55 {
       
    56     MPX_ENTER_EXIT(_L("QMPXVideoPlaybackProgressBar::QMPXVideoPlaybackProgressBar()"));
       
    57 }
       
    58 
       
    59 // -------------------------------------------------------------------------------------------------
       
    60 // QMPXVideoPlaybackProgressBar::~QMPXVideoPlaybackProgressBar
       
    61 // -------------------------------------------------------------------------------------------------
       
    62 //
       
    63 QMPXVideoPlaybackProgressBar::~QMPXVideoPlaybackProgressBar()
       
    64 {
       
    65     MPX_ENTER_EXIT(_L("QMPXVideoPlaybackProgressBar::~QMPXVideoPlaybackProgressBar()"));
       
    66 
       
    67     if ( mSeekingTimer )
       
    68     {
       
    69         disconnect( mSeekingTimer, SIGNAL( timeout() ), this, SLOT( handleSeekingTimeout() ) );
       
    70 
       
    71         if ( mSeekingTimer->isActive() )
       
    72         {
       
    73             mSeekingTimer->stop();
       
    74         }
       
    75 
       
    76         delete mSeekingTimer;
       
    77         mSeekingTimer = NULL;
       
    78     }
       
    79 }
       
    80 
       
    81 // -------------------------------------------------------------------------------------------------
       
    82 // QMPXVideoPlaybackProgressBar::initialize
       
    83 // -------------------------------------------------------------------------------------------------
       
    84 //
       
    85 void QMPXVideoPlaybackProgressBar::initialize()
       
    86 {
       
    87     MPX_ENTER_EXIT(_L("QMPXVideoPlaybackProgressBar::initialize()"));
       
    88 
       
    89     QMPXVideoPlaybackDocumentLoader *loader = mController->layoutLoader();
       
    90 
       
    91     //
       
    92     // Don't need to initialize buttons once it gets initialized
       
    93     //
       
    94     if ( loader && ! mInitialized )
       
    95     {
       
    96         mInitialized = true;
       
    97         mLiveStreaming =
       
    98                 ( mController->fileDetails()->mPlaybackMode == EMPXVideoLiveStreaming )? true:false;
       
    99 
       
   100         //
       
   101         // Create a timer for seeking.
       
   102         // We will issue SetPosition every KSeekingTimeOut msec to show the current frame to user
       
   103         //
       
   104         mSeekingTimer = new QTimer();
       
   105         mSeekingTimer->setSingleShot( false );
       
   106         mSeekingTimer->setInterval( KSeekingTimeOut );
       
   107         connect( mSeekingTimer, SIGNAL( timeout() ), this, SLOT( handleSeekingTimeout() ) );
       
   108 
       
   109         //
       
   110         // progress slider
       
   111         //
       
   112         QGraphicsWidget *widget = loader->findWidget( QString( "progressSlider" ) );
       
   113         mProgressSlider = qobject_cast<HbProgressSlider*>( widget );
       
   114 
       
   115         connect( mProgressSlider, SIGNAL( sliderPressed() ), this, SLOT( handleSliderPressed() ) );
       
   116         connect( mProgressSlider, SIGNAL( sliderReleased() ), this, SLOT( handleSliderReleased() ) );
       
   117         connect( mProgressSlider, SIGNAL( sliderMoved( int ) ), this, SLOT( handleSliderMoved( int ) ) );
       
   118 
       
   119         //
       
   120         // If we init the progress bar after pp sends the duration informatin
       
   121         // we need to set the duration manually
       
   122         //
       
   123         durationChanged( (qreal)mController->fileDetails()->mDuration / (qreal)KPbMilliMultiplier );
       
   124 
       
   125         //
       
   126         // Set the position to 0 until we get position information
       
   127         //
       
   128         positionChanged( 0 );
       
   129 
       
   130         //
       
   131         // Set framedrawer for semi transparent background
       
   132         //
       
   133         HbFrameItem *frameItem = new HbFrameItem();
       
   134         frameItem->frameDrawer().setFrameGraphicsName( "qtg_fr_multimedia_trans" );
       
   135         frameItem->frameDrawer().setFrameType( HbFrameDrawer::NinePieces );
       
   136         frameItem->frameDrawer().setFillWholeRect( true );
       
   137         mProgressSlider->setBackgroundItem( frameItem );
       
   138     }
       
   139 }
       
   140 
       
   141 // -------------------------------------------------------------------------------------------------
       
   142 // QMPXVideoPlaybackProgressBar::durationChanged
       
   143 // -------------------------------------------------------------------------------------------------
       
   144 //
       
   145 void QMPXVideoPlaybackProgressBar::durationChanged( int duration )
       
   146 {
       
   147     MPX_DEBUG(_L("QMPXVideoPlaybackControlsController::durationChanged duration = %d"), duration );
       
   148 
       
   149     if ( mLiveStreaming )
       
   150     {
       
   151         mDuration = 0;
       
   152         mProgressSlider->setMaxText( "Live" );
       
   153     }
       
   154     else
       
   155     {
       
   156         mDuration = duration;
       
   157 
       
   158         if ( ( mDuration / 3600 ) > 0 )
       
   159         {
       
   160             mLongTimeFormat = true;
       
   161         }
       
   162         else
       
   163         {
       
   164             mLongTimeFormat = false;
       
   165         }
       
   166 
       
   167         mProgressSlider->setMaxText( valueToReadableFormat( mDuration ) );
       
   168     }
       
   169 
       
   170     mProgressSlider->setRange( 0, mDuration );
       
   171 }
       
   172 
       
   173 // -------------------------------------------------------------------------------------------------
       
   174 // QMPXVideoPlaybackProgressBar::positionChanged
       
   175 // -------------------------------------------------------------------------------------------------
       
   176 //
       
   177 void QMPXVideoPlaybackProgressBar::positionChanged( int position )
       
   178 {
       
   179     MPX_DEBUG(_L("QMPXVideoPlaybackControlsController::positionChanged position = %d"), position );
       
   180 
       
   181     //
       
   182     // While dragging event, don't update old position information from mpx framework
       
   183     //
       
   184     if ( ! mSliderDragging )
       
   185     {
       
   186         updatePostion( position );
       
   187     }
       
   188 }
       
   189 
       
   190 // -------------------------------------------------------------------------------------------------
       
   191 // QMPXVideoPlaybackProgressBar::updatePostion
       
   192 // -------------------------------------------------------------------------------------------------
       
   193 //
       
   194 void QMPXVideoPlaybackProgressBar::updatePostion( int position )
       
   195 {
       
   196     if ( position < 0 )
       
   197     {
       
   198         position = 0;
       
   199     }
       
   200     else if ( position > mDuration && ! mLiveStreaming )
       
   201     {
       
   202         position = mDuration;
       
   203     }
       
   204 
       
   205     MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::updatePostion position = %d"), position );
       
   206 
       
   207     mProgressSlider->setMinText( valueToReadableFormat( position ) );
       
   208 
       
   209     //
       
   210     // Don't need to set the slider for live streaming
       
   211     //
       
   212     if ( ! mLiveStreaming )
       
   213     {
       
   214         mProgressSlider->setSliderValue( position );
       
   215         mProgressSlider->setProgressValue( position );
       
   216     }
       
   217 }
       
   218 
       
   219 // -------------------------------------------------------------------------------------------------
       
   220 // QMPXVideoPlaybackProgressBar::valueToReadableFormat
       
   221 // -------------------------------------------------------------------------------------------------
       
   222 //
       
   223 QString QMPXVideoPlaybackProgressBar::valueToReadableFormat( int value )
       
   224 {
       
   225     MPX_DEBUG(_L("QMPXVideoPlaybackControlsController::valueToReadableFormat value = %d"), value);
       
   226 
       
   227     int hour = value / 3600;
       
   228     value = value % 3600;
       
   229     int minutes = value / 60;
       
   230     value = value % 60;
       
   231     int second = value;
       
   232 
       
   233     QTime time( hour ,minutes ,second );
       
   234     QString str;
       
   235 
       
   236     HbExtendedLocale locale = HbExtendedLocale::system();
       
   237 
       
   238     if ( mLongTimeFormat )
       
   239     {
       
   240         str = locale.format( time, r_qtn_time_durat_long );
       
   241     }
       
   242     else
       
   243     {
       
   244         str = locale.format( time, r_qtn_time_durat_min_sec );
       
   245     }
       
   246 
       
   247     return str;
       
   248 }
       
   249 
       
   250 // -------------------------------------------------------------------------------------------------
       
   251 // QMPXVideoPlaybackProgressBar::handleSliderPressed
       
   252 // -------------------------------------------------------------------------------------------------
       
   253 //
       
   254 void QMPXVideoPlaybackProgressBar::handleSliderPressed()
       
   255 {
       
   256     MPX_ENTER_EXIT(_L("QMPXVideoPlaybackProgressBar::handleSliderPressed()"));
       
   257 
       
   258     mSliderDragging = true;
       
   259 
       
   260     mController->resetDisappearingTimers( EMPXTimerCancel );
       
   261 
       
   262     //
       
   263     // Pause the playback if it's playing
       
   264     //
       
   265     if( mController->state() == EPbStatePlaying )
       
   266     {
       
   267         mNeedToResumeAfterSetPosition = true;
       
   268         mController->handleCommand( EMPXPbvCmdCustomPause );
       
   269     }
       
   270 }
       
   271 
       
   272 // -------------------------------------------------------------------------------------------------
       
   273 // QMPXVideoPlaybackProgressBar::handleSliderMoved
       
   274 // -------------------------------------------------------------------------------------------------
       
   275 //
       
   276 void QMPXVideoPlaybackProgressBar::handleSliderMoved( int value )
       
   277 {
       
   278     MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::handleSliderMoved() position = %d"), value);
       
   279 
       
   280     updatePostion( value );
       
   281 
       
   282     //
       
   283     // If the slider is dragging, start the timer and seek every KSeekingTimeOut msec
       
   284     //
       
   285     if ( mSliderDragging )
       
   286     {
       
   287         mDraggingPosition = value;
       
   288         MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::handleSliderMoved() mDraggingPosition = %d"), mDraggingPosition);
       
   289 
       
   290         if ( mSeekingTimer && ! mSeekingTimer->isActive() )
       
   291         {
       
   292             mSeekingTimer->start();
       
   293         }
       
   294     }
       
   295     else
       
   296     {
       
   297         if ( value >= mDuration )
       
   298         {
       
   299             MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::setPosition() reached end of the clip"));
       
   300 
       
   301             mController->handleCommand( EMPXPbvCmdEndOfClip );
       
   302         }
       
   303         else
       
   304         {
       
   305             value = mProgressSlider->sliderValue();
       
   306 
       
   307             MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::setPosition() position = %d"), value);
       
   308             mController->handleCommand( EMPXPbvCmdSetPosition, value );
       
   309         }
       
   310     }
       
   311 }
       
   312 
       
   313 // -------------------------------------------------------------------------------------------------
       
   314 // QMPXVideoPlaybackProgressBar::handleSliderReleased
       
   315 // -------------------------------------------------------------------------------------------------
       
   316 //
       
   317 void QMPXVideoPlaybackProgressBar::handleSliderReleased()
       
   318 {
       
   319     MPX_ENTER_EXIT(_L("QMPXVideoPlaybackProgressBar::handleSliderReleased()"));
       
   320 
       
   321     mSliderDragging = false;
       
   322 
       
   323     if ( mSeekingTimer && mSeekingTimer->isActive() )
       
   324     {
       
   325         mSeekingTimer->stop();
       
   326     }
       
   327 
       
   328     mController->resetDisappearingTimers( EMPXTimerReset );
       
   329     int value = mProgressSlider->sliderValue();
       
   330 
       
   331     if ( value >= mDuration )
       
   332     {
       
   333         MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::setPosition() reached end of the clip"));
       
   334         mController->handleCommand( EMPXPbvCmdEndOfClip );
       
   335     }
       
   336     else
       
   337     {
       
   338         if ( value < 0 )
       
   339         {
       
   340             value = 0;
       
   341         }
       
   342 
       
   343         MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::setPosition() position = %d"), value);
       
   344         mController->handleCommand( EMPXPbvCmdSetPosition, value );
       
   345 
       
   346         //
       
   347         // Resume if it was playing
       
   348         //
       
   349         if( mNeedToResumeAfterSetPosition )
       
   350         {
       
   351             mNeedToResumeAfterSetPosition = false;
       
   352             mController->handleCommand( EMPXPbvCmdCustomPlay );
       
   353         }
       
   354     }
       
   355 }
       
   356 
       
   357 // -------------------------------------------------------------------------------------------------
       
   358 // QMPXVideoPlaybackProgressBar::updateWithFileDetails()
       
   359 // -------------------------------------------------------------------------------------------------
       
   360 //
       
   361 void QMPXVideoPlaybackProgressBar::updateWithFileDetails(
       
   362         QMPXVideoPlaybackViewFileDetails* details )
       
   363 {
       
   364     MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::updateControlsWithFileDetails()"));
       
   365 
       
   366     if ( ! details->mPausableStream || ! details->mSeekable )
       
   367     {
       
   368         mProgressSlider->setEnabled( false );
       
   369     }
       
   370     else if ( ! mProgressSlider->isEnabled() )
       
   371     {
       
   372         mProgressSlider->setEnabled( true );
       
   373     }
       
   374 
       
   375     mProgressSlider->backgroundItem()->setVisible(
       
   376             ( mController->viewMode() == EFullScreenView )? ETrue:EFalse );
       
   377 }
       
   378 
       
   379 // -------------------------------------------------------------------------------------------------
       
   380 // QMPXVideoPlaybackProgressBar::updateState()
       
   381 // -------------------------------------------------------------------------------------------------
       
   382 //
       
   383 void QMPXVideoPlaybackProgressBar::updateState( TMPXPlaybackState state )
       
   384 {
       
   385     MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::updateState() state = %d"), state );
       
   386 
       
   387     switch ( state )
       
   388     {
       
   389         case EPbStatePlaying:
       
   390         case EPbStatePaused:
       
   391         {
       
   392             if ( ! mProgressSlider->isEnabled() )
       
   393             {
       
   394                 mProgressSlider->setEnabled( true );
       
   395             }
       
   396 
       
   397             break;
       
   398         }
       
   399         default:
       
   400         {
       
   401             if ( mProgressSlider->isEnabled() )
       
   402             {
       
   403                 mProgressSlider->setEnabled( false );
       
   404             }
       
   405             break;
       
   406         }
       
   407     }
       
   408 }
       
   409 
       
   410 // -------------------------------------------------------------------------------------------------
       
   411 // QMPXVideoPlaybackProgressBar::handleSeekingTimeout()
       
   412 // -------------------------------------------------------------------------------------------------
       
   413 //
       
   414 void QMPXVideoPlaybackProgressBar::handleSeekingTimeout()
       
   415 {
       
   416     if ( mDraggingPosition >= mDuration )
       
   417     {
       
   418         mDraggingPosition = mDuration;
       
   419     }
       
   420     else if( mDraggingPosition < 0 )
       
   421     {
       
   422         mDraggingPosition = 0;
       
   423     }
       
   424 
       
   425     MPX_DEBUG(_L("QMPXVideoPlaybackProgressBar::handleSeekingTimeout() Dragging pos = %d, Set pos = %d")
       
   426             , mDraggingPosition, mSetPosition );
       
   427 
       
   428     if ( mSetPosition != mDraggingPosition )
       
   429     {
       
   430         mSetPosition = mDraggingPosition;
       
   431         mController->handleCommand( EMPXPbvCmdSetPosition, mSetPosition );
       
   432     }
       
   433 }
       
   434 
       
   435 //End of file