LCOV - code coverage report
Current view: top level - sc/source/filter/ftools - fprogressbar.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 111 116 95.7 %
Date: 2015-06-13 12:38:46 Functions: 25 25 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "fprogressbar.hxx"
      21             : #include "global.hxx"
      22             : #include "progress.hxx"
      23             : #include <osl/diagnose.h>
      24             : 
      25         476 : ScfProgressBar::ScfProgressSegment::ScfProgressSegment( sal_Size nSize ) :
      26             :     mnSize( nSize ),
      27         476 :     mnPos( 0 )
      28             : {
      29         476 : }
      30             : 
      31         476 : ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
      32             : {
      33         476 : }
      34             : 
      35           5 : ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, const OUString& rText ) :
      36           5 :     maText( rText )
      37             : {
      38           5 :     Init( pDocShell );
      39           5 : }
      40             : 
      41         198 : ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
      42         198 :     maText( ScGlobal::GetRscString( nResId ) )
      43             : {
      44         198 :     Init( pDocShell );
      45         198 : }
      46             : 
      47         144 : ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment )
      48             : {
      49         144 :     Init( rParProgress.mpDocShell );
      50         144 :     mpParentProgress = &rParProgress;
      51         144 :     mpParentSegment = pParSegment;
      52         144 : }
      53             : 
      54         605 : ScfProgressBar::~ScfProgressBar()
      55             : {
      56         605 : }
      57             : 
      58         347 : void ScfProgressBar::Init( SfxObjectShell* pDocShell )
      59             : {
      60         347 :     mpDocShell = pDocShell;
      61         347 :     mpParentProgress = 0;
      62         347 :     mpParentSegment = mpCurrSegment = 0;
      63         347 :     mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0;
      64         347 :     mnSysProgressScale = 1;     // used to workaround the ULONG_MAX/100 limit
      65         347 :     mbInProgress = false;
      66         347 : }
      67             : 
      68         533 : ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment )
      69             : {
      70         533 :     if( nSegment < 0 )
      71           0 :         return 0;
      72         533 :     return &(maSegments.at( nSegment ));
      73             : }
      74             : 
      75         590 : void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment )
      76             : {
      77         590 :     if( mpCurrSegment != pSegment )
      78             :     {
      79         476 :         mpCurrSegment = pSegment;
      80             : 
      81         476 :         if( mpParentProgress && mpParentSegment )
      82             :         {
      83         201 :             mpParentProgress->SetCurrSegment( mpParentSegment );
      84             :         }
      85         275 :         else if( !mxSysProgress.get() && (mnTotalSize > 0) )
      86             :         {
      87             :             // System progress has an internal limit of ULONG_MAX/100.
      88         203 :             mnSysProgressScale = 1;
      89         203 :             sal_uLong nSysTotalSize = static_cast< sal_uLong >( mnTotalSize );
      90         406 :             while( nSysTotalSize >= ULONG_MAX / 100 )
      91             :             {
      92           0 :                 nSysTotalSize /= 2;
      93           0 :                 mnSysProgressScale *= 2;
      94             :             }
      95         203 :             mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize ) );
      96             :         }
      97             : 
      98         476 :         if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) )
      99             :         {
     100         347 :             mnUnitSize = mnTotalSize / 256 + 1;   // at most 256 calls of system progress
     101         347 :             mnNextUnitPos = 0;
     102         347 :             mbInProgress = true;
     103             :         }
     104             :     }
     105         590 : }
     106             : 
     107      584655 : void ScfProgressBar::IncreaseProgressBar( sal_Size nDelta )
     108             : {
     109      584655 :     sal_Size nNewPos = mnTotalPos + nDelta;
     110             : 
     111             :     // call back to parent progress bar
     112      584655 :     if( mpParentProgress && mpParentSegment )
     113             :     {
     114             :         // calculate new position of parent progress bar
     115             :         sal_Size nParentPos = static_cast< sal_Size >(
     116        1700 :             static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize );
     117        1700 :         mpParentProgress->ProgressAbs( nParentPos );
     118             :     }
     119             :     // modify system progress bar
     120      582955 :     else if( mxSysProgress.get() )
     121             :     {
     122      582955 :         if( nNewPos >= mnNextUnitPos )
     123             :         {
     124       11933 :             mnNextUnitPos = nNewPos + mnUnitSize;
     125       11933 :             mxSysProgress->SetState( static_cast< sal_uLong >( nNewPos / mnSysProgressScale ) );
     126             :         }
     127             :     }
     128             :     else
     129             :     {
     130             :         OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
     131             :     }
     132             : 
     133      584655 :     mnTotalPos = nNewPos;
     134      584655 : }
     135             : 
     136         476 : sal_Int32 ScfProgressBar::AddSegment( sal_Size nSize )
     137             : {
     138             :     OSL_ENSURE( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" );
     139         476 :     if( nSize == 0 )
     140           0 :         return SCF_INV_SEGMENT;
     141             : 
     142         476 :     maSegments.push_back( new ScfProgressSegment( nSize ) );
     143         476 :     mnTotalSize += nSize;
     144         476 :     return static_cast< sal_Int32 >( maSegments.size() - 1 );
     145             : }
     146             : 
     147         144 : ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment )
     148             : {
     149         144 :     ScfProgressSegment* pSegment = GetSegment( nSegment );
     150             :     OSL_ENSURE( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
     151         144 :     if( pSegment && (pSegment->mnPos == 0) )
     152             :     {
     153         144 :         if( !pSegment->mxProgress.get() )
     154         144 :             pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) );
     155         144 :         return *pSegment->mxProgress;
     156             :     }
     157           0 :     return *this;
     158             : }
     159             : 
     160        1838 : bool ScfProgressBar::IsFull() const
     161             : {
     162             :     OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" );
     163        1838 :     return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize);
     164             : }
     165             : 
     166         389 : void ScfProgressBar::ActivateSegment( sal_Int32 nSegment )
     167             : {
     168             :     OSL_ENSURE( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
     169         389 :     if( mnTotalSize > 0 )
     170         389 :         SetCurrSegment( GetSegment( nSegment ) );
     171         389 : }
     172             : 
     173      585512 : void ScfProgressBar::ProgressAbs( sal_Size nPos )
     174             : {
     175             :     OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" );
     176      585512 :     if( mpCurrSegment )
     177             :     {
     178             :         OSL_ENSURE( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" );
     179             :         OSL_ENSURE( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" );
     180      585512 :         if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) )
     181             :         {
     182      584655 :             IncreaseProgressBar( nPos - mpCurrSegment->mnPos );
     183      584655 :             mpCurrSegment->mnPos = nPos;
     184             :         }
     185             :     }
     186      585512 : }
     187             : 
     188        1857 : void ScfProgressBar::Progress( sal_Size nDelta )
     189             : {
     190        1857 :     ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 );
     191        1857 : }
     192             : 
     193           5 : ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, const OUString& rText ) :
     194           5 :     maProgress( pDocShell, rText )
     195             : {
     196           5 :     Init( nSize );
     197           5 : }
     198             : 
     199          84 : ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
     200          84 :     maProgress( pDocShell, nResId )
     201             : {
     202          84 :     Init( nSize );
     203          84 : }
     204             : 
     205          89 : void ScfSimpleProgressBar::Init( sal_Size nSize )
     206             : {
     207          89 :     sal_Int32 nSegment = maProgress.AddSegment( nSize );
     208          89 :     if( nSegment >= 0 )
     209          89 :         maProgress.ActivateSegment( nSegment );
     210          89 : }
     211             : 
     212           5 : ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
     213           5 :     mrStrm( rStrm )
     214             : {
     215           5 :     Init( pDocShell, ScGlobal::GetRscString( nResId ) );
     216           5 : }
     217             : 
     218        2692 : void ScfStreamProgressBar::Progress()
     219             : {
     220        2692 :     mxProgress->ProgressAbs( mrStrm.Tell() );
     221        2692 : }
     222             : 
     223           5 : void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const OUString& rText )
     224             : {
     225           5 :     sal_Size nPos = mrStrm.Tell();
     226           5 :     mrStrm.Seek( STREAM_SEEK_TO_END );
     227           5 :     sal_Size nSize = mrStrm.Tell();
     228           5 :     mrStrm.Seek( nPos );
     229             : 
     230           5 :     mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) );
     231           5 :     Progress();
     232          35 : }
     233             : 
     234             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11