videoplayback/videoplaybackview/controlsrc/videoplaybackprogressbar.cpp
changeset 44 518105d52e45
child 49 824471cb468a
equal deleted inserted replaced
42:17f382c040b1 44:518105d52e45
       
     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#28 %
       
    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 "videoplaybackprogressbar.h"
       
    35 #include "videoplaybackdocumentloader.h"
       
    36 #include "videoplaybackcontrolscontroller.h"
       
    37 
       
    38 const int KSeekingTimeOut = 250;
       
    39 
       
    40 // -------------------------------------------------------------------------------------------------
       
    41 // VideoPlaybackProgressBar::VideoPlaybackProgressBar
       
    42 // -------------------------------------------------------------------------------------------------
       
    43 //
       
    44 VideoPlaybackProgressBar::VideoPlaybackProgressBar(
       
    45         VideoPlaybackControlsController* 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("VideoPlaybackProgressBar::VideoPlaybackProgressBar()"));
       
    57 }
       
    58 
       
    59 // -------------------------------------------------------------------------------------------------
       
    60 // VideoPlaybackProgressBar::~VideoPlaybackProgressBar
       
    61 // -------------------------------------------------------------------------------------------------
       
    62 //
       
    63 VideoPlaybackProgressBar::~VideoPlaybackProgressBar()
       
    64 {
       
    65     MPX_ENTER_EXIT(_L("VideoPlaybackProgressBar::~VideoPlaybackProgressBar()"));
       
    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 // VideoPlaybackProgressBar::initialize
       
    83 // -------------------------------------------------------------------------------------------------
       
    84 //
       
    85 void VideoPlaybackProgressBar::initialize()
       
    86 {
       
    87     MPX_ENTER_EXIT(_L("VideoPlaybackProgressBar::initialize()"));
       
    88 
       
    89     VideoPlaybackDocumentLoader *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 // VideoPlaybackProgressBar::durationChanged
       
   143 // -------------------------------------------------------------------------------------------------
       
   144 //
       
   145 void VideoPlaybackProgressBar::durationChanged( int duration )
       
   146 {
       
   147     MPX_DEBUG(_L("VideoPlaybackControlsController::durationChanged duration = %d"), duration );
       
   148 
       
   149     if ( mLiveStreaming )
       
   150     {
       
   151         mDuration = 0;
       
   152         mProgressSlider->setMaxText( hbTrId( "txt_videos_slidervalue_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 // VideoPlaybackProgressBar::positionChanged
       
   175 // -------------------------------------------------------------------------------------------------
       
   176 //
       
   177 void VideoPlaybackProgressBar::positionChanged( int position )
       
   178 {
       
   179     MPX_DEBUG(_L("VideoPlaybackControlsController::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 // VideoPlaybackProgressBar::updatePostion
       
   192 // -------------------------------------------------------------------------------------------------
       
   193 //
       
   194 void VideoPlaybackProgressBar::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("VideoPlaybackProgressBar::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 // VideoPlaybackProgressBar::valueToReadableFormat
       
   221 // -------------------------------------------------------------------------------------------------
       
   222 //
       
   223 QString VideoPlaybackProgressBar::valueToReadableFormat( int value )
       
   224 {
       
   225     MPX_DEBUG(_L("VideoPlaybackControlsController::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 // VideoPlaybackProgressBar::handleSliderPressed
       
   252 // -------------------------------------------------------------------------------------------------
       
   253 //
       
   254 void VideoPlaybackProgressBar::handleSliderPressed()
       
   255 {
       
   256     MPX_ENTER_EXIT(_L("VideoPlaybackProgressBar::handleSliderPressed()"));
       
   257 
       
   258     mSliderDragging = true;
       
   259 
       
   260     mController->resetDisappearingTimers( ETimerCancel );
       
   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 // VideoPlaybackProgressBar::handleSliderMoved
       
   274 // -------------------------------------------------------------------------------------------------
       
   275 //
       
   276 void VideoPlaybackProgressBar::handleSliderMoved( int value )
       
   277 {
       
   278     MPX_DEBUG(_L("VideoPlaybackProgressBar::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("VideoPlaybackProgressBar::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("VideoPlaybackProgressBar::setPosition() reached end of the clip"));
       
   300 
       
   301             mController->handleCommand( EMPXPbvCmdEndOfClip );
       
   302         }
       
   303         else
       
   304         {
       
   305             value = mProgressSlider->sliderValue();
       
   306 
       
   307             MPX_DEBUG(_L("VideoPlaybackProgressBar::setPosition() position = %d"), value);
       
   308             mController->handleCommand( EMPXPbvCmdSetPosition, value );
       
   309         }
       
   310     }
       
   311 }
       
   312 
       
   313 // -------------------------------------------------------------------------------------------------
       
   314 // VideoPlaybackProgressBar::handleSliderReleased
       
   315 // -------------------------------------------------------------------------------------------------
       
   316 //
       
   317 void VideoPlaybackProgressBar::handleSliderReleased()
       
   318 {
       
   319     MPX_ENTER_EXIT(_L("VideoPlaybackProgressBar::handleSliderReleased()"));
       
   320 
       
   321     mSliderDragging = false;
       
   322 
       
   323     if ( mSeekingTimer && mSeekingTimer->isActive() )
       
   324     {
       
   325         mSeekingTimer->stop();
       
   326     }
       
   327 
       
   328     mController->resetDisappearingTimers( ETimerReset );
       
   329     int value = mProgressSlider->sliderValue();
       
   330 
       
   331     if ( value >= mDuration )
       
   332     {
       
   333         MPX_DEBUG(_L("VideoPlaybackProgressBar::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("VideoPlaybackProgressBar::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 // VideoPlaybackProgressBar::updateWithFileDetails()
       
   359 // -------------------------------------------------------------------------------------------------
       
   360 //
       
   361 void VideoPlaybackProgressBar::updateWithFileDetails(
       
   362         VideoPlaybackViewFileDetails* details )
       
   363 {
       
   364     Q_UNUSED( details );
       
   365     
       
   366     MPX_DEBUG(_L("VideoPlaybackProgressBar::updateControlsWithFileDetails()"));
       
   367 
       
   368     setEnableProgressSlider( true );
       
   369 
       
   370     mProgressSlider->backgroundItem()->setVisible(
       
   371             ( mController->viewMode() == EFullScreenView )? ETrue:EFalse );
       
   372 }
       
   373 
       
   374 // -------------------------------------------------------------------------------------------------
       
   375 // VideoPlaybackProgressBar::updateState()
       
   376 // -------------------------------------------------------------------------------------------------
       
   377 //
       
   378 void VideoPlaybackProgressBar::updateState( TMPXPlaybackState state )
       
   379 {
       
   380     MPX_DEBUG(_L("VideoPlaybackProgressBar::updateState() state = %d"), state );
       
   381 
       
   382     switch ( state )
       
   383     {
       
   384         case EPbStatePlaying:
       
   385         case EPbStatePaused:
       
   386         {
       
   387             setEnableProgressSlider( true );
       
   388 
       
   389             break;
       
   390         }
       
   391         default:
       
   392         {
       
   393             setEnableProgressSlider( false );
       
   394 
       
   395             break;
       
   396         }
       
   397     }
       
   398 }
       
   399 
       
   400 // -------------------------------------------------------------------------------------------------
       
   401 // VideoPlaybackProgressBar::handleSeekingTimeout()
       
   402 // -------------------------------------------------------------------------------------------------
       
   403 //
       
   404 void VideoPlaybackProgressBar::handleSeekingTimeout()
       
   405 {
       
   406     if ( mDraggingPosition >= mDuration )
       
   407     {
       
   408         mDraggingPosition = mDuration;
       
   409     }
       
   410     else if( mDraggingPosition < 0 )
       
   411     {
       
   412         mDraggingPosition = 0;
       
   413     }
       
   414 
       
   415     MPX_DEBUG(_L("VideoPlaybackProgressBar::handleSeekingTimeout() Dragging pos = %d, Set pos = %d")
       
   416             , mDraggingPosition, mSetPosition );
       
   417 
       
   418     if ( mSetPosition != mDraggingPosition )
       
   419     {
       
   420         mSetPosition = mDraggingPosition;
       
   421         mController->handleCommand( EMPXPbvCmdSetPosition, mSetPosition );
       
   422     }
       
   423 }
       
   424 
       
   425 // -------------------------------------------------------------------------------------------------
       
   426 // VideoPlaybackProgressBar::setEnableProgressSlider()
       
   427 // -------------------------------------------------------------------------------------------------
       
   428 //
       
   429 void VideoPlaybackProgressBar::setEnableProgressSlider( bool enable )
       
   430 {
       
   431     MPX_DEBUG(_L("VideoPlaybackProgressBar::setEnableProgressSlider() enable = %d"), enable);
       
   432 
       
   433     if ( ! enable ||
       
   434          ! mController->fileDetails()->mPausableStream ||
       
   435          ! mController->fileDetails()->mSeekable )
       
   436     {
       
   437         if ( mProgressSlider->isEnabled() )
       
   438         {
       
   439             mProgressSlider->setEnabled( false );
       
   440         }
       
   441     }
       
   442     else if ( ! mProgressSlider->isEnabled() )
       
   443     {
       
   444         mProgressSlider->setEnabled( true );
       
   445     }
       
   446 }
       
   447 
       
   448 //End of file