Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / helper / progressbar.cxx
blob36820d785a2902cc77f920bcb67b8b9573be37a9
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 "oox/helper/progressbar.hxx"
22 #include <osl/diagnose.h>
23 #include <com/sun/star/task/XStatusIndicator.hpp>
24 #include "oox/helper/helper.hxx"
26 namespace oox {
28 using namespace ::com::sun::star::task;
29 using namespace ::com::sun::star::uno;
31 namespace {
33 const sal_Int32 PROGRESS_RANGE = 1000000;
35 } // namespace
37 IProgressBar::~IProgressBar()
41 ISegmentProgressBar::~ISegmentProgressBar()
45 ProgressBar::ProgressBar( const Reference< XStatusIndicator >& rxIndicator, const OUString& rText ) :
46 mxIndicator( rxIndicator ),
47 mfPosition( 0 )
49 if( mxIndicator.is() )
50 mxIndicator->start( rText, PROGRESS_RANGE );
53 ProgressBar::~ProgressBar()
55 if( mxIndicator.is() )
56 mxIndicator->end();
59 double ProgressBar::getPosition() const
61 return mfPosition;
64 void ProgressBar::setPosition( double fPosition )
66 OSL_ENSURE( (mfPosition <= fPosition) && (fPosition <= 1.0), "ProgressBar::setPosition - invalid position" );
67 mfPosition = getLimitedValue< double >( fPosition, mfPosition, 1.0 );
68 if( mxIndicator.is() )
69 mxIndicator->setValue( static_cast< sal_Int32 >( mfPosition * PROGRESS_RANGE ) );
72 namespace prv {
74 class SubSegment : public ISegmentProgressBar
76 public:
77 explicit SubSegment( IProgressBar& rParentProgress, double fStartPos, double fLength );
79 virtual double getPosition() const SAL_OVERRIDE;
80 virtual void setPosition( double fPosition ) SAL_OVERRIDE;
82 virtual double getFreeLength() const SAL_OVERRIDE;
83 virtual ISegmentProgressBarRef createSegment( double fLength ) SAL_OVERRIDE;
85 private:
86 IProgressBar& mrParentProgress;
87 double mfStartPos;
88 double mfLength;
89 double mfPosition;
90 double mfFreeStart;
93 SubSegment::SubSegment( IProgressBar& rParentProgress, double fStartPos, double fLength ) :
94 mrParentProgress( rParentProgress ),
95 mfStartPos( fStartPos ),
96 mfLength( fLength ),
97 mfPosition( 0.0 ),
98 mfFreeStart( 0.0 )
102 double SubSegment::getPosition() const
104 return mfPosition;
107 void SubSegment::setPosition( double fPosition )
109 OSL_ENSURE( (mfPosition <= fPosition) && (fPosition <= 1.0), "SubSegment::setPosition - invalid position" );
110 mfPosition = getLimitedValue< double >( fPosition, mfPosition, 1.0 );
111 mrParentProgress.setPosition( mfStartPos + mfPosition * mfLength );
114 double SubSegment::getFreeLength() const
116 return 1.0 - mfFreeStart;
119 ISegmentProgressBarRef SubSegment::createSegment( double fLength )
121 OSL_ENSURE( (0.0 < fLength) && (fLength <= getFreeLength()), "SubSegment::createSegment - invalid length" );
122 fLength = getLimitedValue< double >( fLength, 0.0, getFreeLength() );
123 ISegmentProgressBarRef xSegment( new prv::SubSegment( *this, mfFreeStart, fLength ) );
124 mfFreeStart += fLength;
125 return xSegment;
128 } // namespace prv
130 SegmentProgressBar::SegmentProgressBar( const Reference< XStatusIndicator >& rxIndicator, const OUString& rText ) :
131 maProgress( rxIndicator, rText ),
132 mfFreeStart( 0.0 )
136 double SegmentProgressBar::getPosition() const
138 return maProgress.getPosition();
141 void SegmentProgressBar::setPosition( double fPosition )
143 maProgress.setPosition( fPosition );
146 double SegmentProgressBar::getFreeLength() const
148 return 1.0 - mfFreeStart;
151 ISegmentProgressBarRef SegmentProgressBar::createSegment( double fLength )
153 OSL_ENSURE( (0.0 < fLength) && (fLength <= getFreeLength()), "SegmentProgressBar::createSegment - invalid length" );
154 fLength = getLimitedValue< double >( fLength, 0.0, getFreeLength() );
155 ISegmentProgressBarRef xSegment( new prv::SubSegment( maProgress, mfFreeStart, fLength ) );
156 mfFreeStart += fLength;
157 return xSegment;
160 } // namespace oox
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */