Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / ftools / fprogressbar.cxx
blob2ce5ab4035627df084b88568f96c8dc8fa4e7ec9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <fprogressbar.hxx>
21 #include <globstr.hrc>
22 #include <scresid.hxx>
23 #include <progress.hxx>
24 #include <osl/diagnose.h>
25 #include <tools/stream.hxx>
27 #include <limits>
28 #include <utility>
30 ScfProgressBar::ScfProgressSegment::ScfProgressSegment( std::size_t nSize ) :
31 mnSize( nSize ),
32 mnPos( 0 )
36 ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
40 ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, OUString aText ) :
41 maText(std::move( aText ))
43 Init( pDocShell );
46 ScfProgressBar::ScfProgressBar(SfxObjectShell* pDocShell, TranslateId pResId)
47 : maText(ScResId(pResId))
49 Init( pDocShell );
52 ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment )
54 Init( rParProgress.mpDocShell );
55 mpParentProgress = &rParProgress;
56 mpParentSegment = pParSegment;
59 ScfProgressBar::~ScfProgressBar()
63 void ScfProgressBar::Init( SfxObjectShell* pDocShell )
65 mpDocShell = pDocShell;
66 mpParentProgress = nullptr;
67 mpParentSegment = mpCurrSegment = nullptr;
68 mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0;
69 mnSysProgressScale = 1; // used to workaround the SfxProgress sal_uInt32 limit
70 mbInProgress = false;
73 ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment )
75 if( nSegment < 0 )
76 return nullptr;
77 return maSegments.at( nSegment ).get();
80 void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment )
82 if( mpCurrSegment == pSegment )
83 return;
85 mpCurrSegment = pSegment;
87 if( mpParentProgress && mpParentSegment )
89 mpParentProgress->SetCurrSegment( mpParentSegment );
91 else if( !mxSysProgress && (mnTotalSize > 0) )
93 // SfxProgress has a limit of sal_uInt32.
94 mnSysProgressScale = 1;
95 std::size_t nSysTotalSize = mnTotalSize;
96 while( nSysTotalSize > std::numeric_limits<sal_uInt32>::max() )
98 nSysTotalSize /= 2;
99 mnSysProgressScale *= 2;
101 mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize, true ) );
104 if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) )
106 mnUnitSize = mnTotalSize / 256 + 1; // at most 256 calls of system progress
107 mnNextUnitPos = 0;
108 mbInProgress = true;
112 void ScfProgressBar::IncreaseProgressBar( std::size_t nDelta )
114 std::size_t nNewPos = mnTotalPos + nDelta;
116 // call back to parent progress bar
117 if( mpParentProgress && mpParentSegment )
119 // calculate new position of parent progress bar
120 std::size_t nParentPos = static_cast< std::size_t >(
121 static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize );
122 mpParentProgress->ProgressAbs( nParentPos );
124 // modify system progress bar
125 else if( mxSysProgress )
127 if( nNewPos >= mnNextUnitPos )
129 mnNextUnitPos = nNewPos + mnUnitSize;
130 mxSysProgress->SetState( static_cast< sal_uLong >( nNewPos / mnSysProgressScale ) );
133 else
135 OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
138 mnTotalPos = nNewPos;
141 sal_Int32 ScfProgressBar::AddSegment( std::size_t nSize )
143 OSL_ENSURE( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" );
144 if( nSize == 0 )
145 return SCF_INV_SEGMENT;
147 maSegments.push_back( std::make_unique<ScfProgressSegment>( nSize ) );
148 mnTotalSize += nSize;
149 return static_cast< sal_Int32 >( maSegments.size() - 1 );
152 ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment )
154 ScfProgressSegment* pSegment = GetSegment( nSegment );
155 OSL_ENSURE( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
156 if( pSegment && (pSegment->mnPos == 0) )
158 if( !pSegment->mxProgress )
159 pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) );
160 return *pSegment->mxProgress;
162 return *this;
165 bool ScfProgressBar::IsFull() const
167 OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" );
168 return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize);
171 void ScfProgressBar::ActivateSegment( sal_Int32 nSegment )
173 OSL_ENSURE( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
174 if( mnTotalSize > 0 )
175 SetCurrSegment( GetSegment( nSegment ) );
178 void ScfProgressBar::ProgressAbs( std::size_t nPos )
180 OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" );
181 if( mpCurrSegment )
183 OSL_ENSURE( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" );
184 OSL_ENSURE( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" );
185 if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) )
187 IncreaseProgressBar( nPos - mpCurrSegment->mnPos );
188 mpCurrSegment->mnPos = nPos;
193 void ScfProgressBar::Progress( std::size_t nDelta )
195 ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 );
198 ScfSimpleProgressBar::ScfSimpleProgressBar( std::size_t nSize, SfxObjectShell* pDocShell, const OUString& rText ) :
199 maProgress( pDocShell, rText )
201 Init( nSize );
204 ScfSimpleProgressBar::ScfSimpleProgressBar(std::size_t nSize, SfxObjectShell* pDocShell, TranslateId pResId)
205 : maProgress(pDocShell, pResId)
207 Init( nSize );
210 void ScfSimpleProgressBar::Init( std::size_t nSize )
212 sal_Int32 nSegment = maProgress.AddSegment( nSize );
213 if( nSegment >= 0 )
214 maProgress.ActivateSegment( nSegment );
217 ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell ) :
218 mrStrm( rStrm )
220 Init( pDocShell, ScResId( STR_LOAD_DOC ) );
223 void ScfStreamProgressBar::Progress()
225 mxProgress->ProgressAbs( mrStrm.Tell() );
228 void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const OUString& rText )
230 sal_uInt64 const nSize = mrStrm.TellEnd();
231 mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) );
232 Progress();
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */