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