xref: /aoo41x/main/oox/source/helper/progressbar.cxx (revision ca5ec200)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/helper/progressbar.hxx"
25 
26 #include <com/sun/star/task/XStatusIndicator.hpp>
27 #include "oox/helper/helper.hxx"
28 
29 namespace oox {
30 
31 // ============================================================================
32 
33 using namespace ::com::sun::star::task;
34 using namespace ::com::sun::star::uno;
35 
36 using ::rtl::OUString;
37 
38 namespace {
39 
40 const sal_Int32 PROGRESS_RANGE      = 1000000;
41 
42 } // namespace
43 
44 // ============================================================================
45 
~IProgressBar()46 IProgressBar::~IProgressBar()
47 {
48 }
49 
50 // ----------------------------------------------------------------------------
51 
~ISegmentProgressBar()52 ISegmentProgressBar::~ISegmentProgressBar()
53 {
54 }
55 
56 // ============================================================================
57 // ============================================================================
58 
ProgressBar(const Reference<XStatusIndicator> & rxIndicator,const OUString & rText)59 ProgressBar::ProgressBar( const Reference< XStatusIndicator >& rxIndicator, const OUString& rText ) :
60     mxIndicator( rxIndicator ),
61     mfPosition( 0 )
62 {
63     if( mxIndicator.is() )
64         mxIndicator->start( rText, PROGRESS_RANGE );
65 }
66 
~ProgressBar()67 ProgressBar::~ProgressBar()
68 {
69     if( mxIndicator.is() )
70         mxIndicator->end();
71 }
72 
getPosition() const73 double ProgressBar::getPosition() const
74 {
75     return mfPosition;
76 }
77 
setPosition(double fPosition)78 void ProgressBar::setPosition( double fPosition )
79 {
80     OSL_ENSURE( (mfPosition <= fPosition) && (fPosition <= 1.0), "ProgressBar::setPosition - invalid position" );
81     mfPosition = getLimitedValue< double >( fPosition, mfPosition, 1.0 );
82     if( mxIndicator.is() )
83         mxIndicator->setValue( static_cast< sal_Int32 >( mfPosition * PROGRESS_RANGE ) );
84 }
85 
86 // ============================================================================
87 
88 namespace prv {
89 
90 class SubSegment : public ISegmentProgressBar
91 {
92 public:
93     explicit            SubSegment( IProgressBar& rParentProgress, double fStartPos, double fLength );
94 
95     virtual double      getPosition() const;
96     virtual void        setPosition( double fPosition );
97 
98     virtual double      getFreeLength() const;
99     virtual ISegmentProgressBarRef createSegment( double fLength );
100 
101 private:
102     IProgressBar&       mrParentProgress;
103     double              mfStartPos;
104     double              mfLength;
105     double              mfPosition;
106     double              mfFreeStart;
107 };
108 
109 // ----------------------------------------------------------------------------
110 
SubSegment(IProgressBar & rParentProgress,double fStartPos,double fLength)111 SubSegment::SubSegment( IProgressBar& rParentProgress, double fStartPos, double fLength ) :
112     mrParentProgress( rParentProgress ),
113     mfStartPos( fStartPos ),
114     mfLength( fLength ),
115     mfPosition( 0.0 ),
116     mfFreeStart( 0.0 )
117 {
118 }
119 
getPosition() const120 double SubSegment::getPosition() const
121 {
122     return mfPosition;
123 }
124 
setPosition(double fPosition)125 void SubSegment::setPosition( double fPosition )
126 {
127     OSL_ENSURE( (mfPosition <= fPosition) && (fPosition <= 1.0), "SubSegment::setPosition - invalid position" );
128     mfPosition = getLimitedValue< double >( fPosition, mfPosition, 1.0 );
129     mrParentProgress.setPosition( mfStartPos + mfPosition * mfLength );
130 }
131 
getFreeLength() const132 double SubSegment::getFreeLength() const
133 {
134     return 1.0 - mfFreeStart;
135 }
136 
createSegment(double fLength)137 ISegmentProgressBarRef SubSegment::createSegment( double fLength )
138 {
139     OSL_ENSURE( (0.0 < fLength) && (fLength <= getFreeLength()), "SubSegment::createSegment - invalid length" );
140     fLength = getLimitedValue< double >( fLength, 0.0, getFreeLength() );
141     ISegmentProgressBarRef xSegment( new prv::SubSegment( *this, mfFreeStart, fLength ) );
142     mfFreeStart += fLength;
143     return xSegment;
144 }
145 
146 } // namespace prv
147 
148 // ============================================================================
149 
SegmentProgressBar(const Reference<XStatusIndicator> & rxIndicator,const OUString & rText)150 SegmentProgressBar::SegmentProgressBar( const Reference< XStatusIndicator >& rxIndicator, const OUString& rText ) :
151     maProgress( rxIndicator, rText ),
152     mfFreeStart( 0.0 )
153 {
154 }
155 
getPosition() const156 double SegmentProgressBar::getPosition() const
157 {
158     return maProgress.getPosition();
159 }
160 
setPosition(double fPosition)161 void SegmentProgressBar::setPosition( double fPosition )
162 {
163     maProgress.setPosition( fPosition );
164 }
165 
getFreeLength() const166 double SegmentProgressBar::getFreeLength() const
167 {
168     return 1.0 - mfFreeStart;
169 }
170 
createSegment(double fLength)171 ISegmentProgressBarRef SegmentProgressBar::createSegment( double fLength )
172 {
173     OSL_ENSURE( (0.0 < fLength) && (fLength <= getFreeLength()), "SegmentProgressBar::createSegment - invalid length" );
174     fLength = getLimitedValue< double >( fLength, 0.0, getFreeLength() );
175     ISegmentProgressBarRef xSegment( new prv::SubSegment( maProgress, mfFreeStart, fLength ) );
176     mfFreeStart += fLength;
177     return xSegment;
178 }
179 
180 // ============================================================================
181 
182 } // namespace oox
183