LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/filter/ftools - fprogressbar.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 111 116 95.7 %
Date: 2013-07-09 Functions: 23 23 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             : 
      24             : // ============================================================================
      25             : 
      26         118 : ScfProgressBar::ScfProgressSegment::ScfProgressSegment( sal_Size nSize ) :
      27             :     mxProgress( 0 ),
      28             :     mnSize( nSize ),
      29         118 :     mnPos( 0 )
      30             : {
      31         118 : }
      32             : 
      33         118 : ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
      34             : {
      35         118 : }
      36             : 
      37             : // ============================================================================
      38             : 
      39           2 : ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, const String& rText ) :
      40           2 :     maText( rText )
      41             : {
      42           2 :     Init( pDocShell );
      43           2 : }
      44             : 
      45          84 : ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
      46          84 :     maText( ScGlobal::GetRscString( nResId ) )
      47             : {
      48          84 :     Init( pDocShell );
      49          84 : }
      50             : 
      51          18 : ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment )
      52             : {
      53          18 :     Init( rParProgress.mpDocShell );
      54          18 :     mpParentProgress = &rParProgress;
      55          18 :     mpParentSegment = pParSegment;
      56          18 : }
      57             : 
      58         160 : ScfProgressBar::~ScfProgressBar()
      59             : {
      60         160 : }
      61             : 
      62         104 : void ScfProgressBar::Init( SfxObjectShell* pDocShell )
      63             : {
      64         104 :     mpDocShell = pDocShell;
      65         104 :     mpParentProgress = 0;
      66         104 :     mpParentSegment = mpCurrSegment = 0;
      67         104 :     mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0;
      68         104 :     mnSysProgressScale = 1;     // used to workaround the ULONG_MAX/100 limit
      69         104 :     mbInProgress = false;
      70         104 : }
      71             : 
      72         123 : ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment )
      73             : {
      74         123 :     if( nSegment < 0 )
      75           0 :         return 0;
      76         123 :     return &(maSegments.at( nSegment ));
      77             : }
      78             : 
      79         128 : void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment )
      80             : {
      81         128 :     if( mpCurrSegment != pSegment )
      82             :     {
      83         118 :         mpCurrSegment = pSegment;
      84             : 
      85         118 :         if( mpParentProgress && mpParentSegment )
      86             :         {
      87          23 :             mpParentProgress->SetCurrSegment( mpParentSegment );
      88             :         }
      89          95 :         else if( !mxSysProgress.get() && (mnTotalSize > 0) )
      90             :         {
      91             :             // System progress has an internal limit of ULONG_MAX/100.
      92          86 :             mnSysProgressScale = 1;
      93          86 :             sal_uLong nSysTotalSize = static_cast< sal_uLong >( mnTotalSize );
      94         172 :             while( nSysTotalSize >= ULONG_MAX / 100 )
      95             :             {
      96           0 :                 nSysTotalSize /= 2;
      97           0 :                 mnSysProgressScale *= 2;
      98             :             }
      99          86 :             mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize ) );
     100             :         }
     101             : 
     102         118 :         if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) )
     103             :         {
     104         104 :             mnUnitSize = mnTotalSize / 256 + 1;   // at most 256 calls of system progress
     105         104 :             mnNextUnitPos = 0;
     106         104 :             mbInProgress = true;
     107             :         }
     108             :     }
     109         128 : }
     110             : 
     111      437704 : void ScfProgressBar::IncreaseProgressBar( sal_Size nDelta )
     112             : {
     113      437704 :     sal_Size nNewPos = mnTotalPos + nDelta;
     114             : 
     115             :     // call back to parent progress bar
     116      437704 :     if( mpParentProgress && mpParentSegment )
     117             :     {
     118             :         // calculate new position of parent progress bar
     119             :         sal_Size nParentPos = static_cast< sal_Size >(
     120         193 :             static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize );
     121         193 :         mpParentProgress->ProgressAbs( nParentPos );
     122             :     }
     123             :     // modify system progress bar
     124      437511 :     else if( mxSysProgress.get() )
     125             :     {
     126      437511 :         if( nNewPos >= mnNextUnitPos )
     127             :         {
     128        6370 :             mnNextUnitPos = nNewPos + mnUnitSize;
     129        6370 :             mxSysProgress->SetState( static_cast< sal_uLong >( nNewPos / mnSysProgressScale ) );
     130             :         }
     131             :     }
     132             :     else
     133             :     {
     134             :         OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
     135             :     }
     136             : 
     137      437704 :     mnTotalPos = nNewPos;
     138      437704 : }
     139             : 
     140         118 : sal_Int32 ScfProgressBar::AddSegment( sal_Size nSize )
     141             : {
     142             :     OSL_ENSURE( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" );
     143         118 :     if( nSize == 0 )
     144           0 :         return SCF_INV_SEGMENT;
     145             : 
     146         118 :     maSegments.push_back( new ScfProgressSegment( nSize ) );
     147         118 :     mnTotalSize += nSize;
     148         118 :     return static_cast< sal_Int32 >( maSegments.size() - 1 );
     149             : }
     150             : 
     151          18 : ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment )
     152             : {
     153          18 :     ScfProgressSegment* pSegment = GetSegment( nSegment );
     154             :     OSL_ENSURE( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
     155          18 :     if( pSegment && (pSegment->mnPos == 0) )
     156             :     {
     157          18 :         if( !pSegment->mxProgress.get() )
     158          18 :             pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) );
     159          18 :         return *pSegment->mxProgress;
     160             :     }
     161           0 :     return *this;
     162             : }
     163             : 
     164         222 : bool ScfProgressBar::IsFull() const
     165             : {
     166             :     OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" );
     167         222 :     return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize);
     168             : }
     169             : 
     170         105 : void ScfProgressBar::ActivateSegment( sal_Int32 nSegment )
     171             : {
     172             :     OSL_ENSURE( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
     173         105 :     if( mnTotalSize > 0 )
     174         105 :         SetCurrSegment( GetSegment( nSegment ) );
     175         105 : }
     176             : 
     177      438558 : void ScfProgressBar::ProgressAbs( sal_Size nPos )
     178             : {
     179             :     OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" );
     180      438558 :     if( mpCurrSegment )
     181             :     {
     182             :         OSL_ENSURE( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" );
     183             :         OSL_ENSURE( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" );
     184      438558 :         if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) )
     185             :         {
     186      437704 :             IncreaseProgressBar( nPos - mpCurrSegment->mnPos );
     187      437704 :             mpCurrSegment->mnPos = nPos;
     188             :         }
     189             :     }
     190      438558 : }
     191             : 
     192         325 : void ScfProgressBar::Progress( sal_Size nDelta )
     193             : {
     194         325 :     ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 );
     195         325 : }
     196             : 
     197             : // ============================================================================
     198             : 
     199           2 : ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, const String& rText ) :
     200           2 :     maProgress( pDocShell, rText )
     201             : {
     202           2 :     Init( nSize );
     203           2 : }
     204             : 
     205          46 : ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
     206          46 :     maProgress( pDocShell, nResId )
     207             : {
     208          46 :     Init( nSize );
     209          46 : }
     210             : 
     211          48 : void ScfSimpleProgressBar::Init( sal_Size nSize )
     212             : {
     213          48 :     sal_Int32 nSegment = maProgress.AddSegment( nSize );
     214          48 :     if( nSegment >= 0 )
     215          48 :         maProgress.ActivateSegment( nSegment );
     216          48 : }
     217             : 
     218           2 : ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
     219           2 :     mrStrm( rStrm )
     220             : {
     221           2 :     Init( pDocShell, ScGlobal::GetRscString( nResId ) );
     222           2 : }
     223             : 
     224         310 : void ScfStreamProgressBar::Progress()
     225             : {
     226         310 :     mxProgress->ProgressAbs( mrStrm.Tell() );
     227         310 : }
     228             : 
     229           2 : void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const String& rText )
     230             : {
     231           2 :     sal_Size nPos = mrStrm.Tell();
     232           2 :     mrStrm.Seek( STREAM_SEEK_TO_END );
     233           2 :     sal_Size nSize = mrStrm.Tell();
     234           2 :     mrStrm.Seek( nPos );
     235             : 
     236           2 :     mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) );
     237           2 :     Progress();
     238           2 : }
     239             : 
     240             : // ============================================================================
     241             : 
     242             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10