Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / xmloff / source / core / ProgressBarHelper.cxx
blob61248c06e8dfa3779298f1a5757e2b38673e88bf
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 <utility>
21 #include <xmloff/ProgressBarHelper.hxx>
23 #include <osl/diagnose.h>
25 using namespace ::com::sun::star;
27 const sal_Int32 nDefaultProgressBarRange = 1000000;
28 const float fProgressStep = 0.5;
30 ProgressBarHelper::ProgressBarHelper(css::uno::Reference < css::task::XStatusIndicator> xTempStatusIndicator,
31 const bool bTempStrict)
32 : xStatusIndicator(std::move(xTempStatusIndicator))
33 , nRange(nDefaultProgressBarRange)
34 , nReference(100)
35 , nValue(0)
36 , fOldPercent(0.0)
37 , bStrict(bTempStrict)
38 , bRepeat(true)
39 #ifdef DBG_UTIL
40 , bFailure(false)
41 #endif
45 ProgressBarHelper::~ProgressBarHelper()
49 void ProgressBarHelper::ChangeReference(sal_Int32 nNewReference)
51 if((nNewReference <= 0) || (nNewReference == nReference))
52 return;
54 if (nReference)
56 double fPercent(static_cast<double>(nNewReference) / nReference);
57 double fValue(nValue * fPercent);
58 nValue = static_cast<sal_Int32>(fValue);
59 nReference = nNewReference;
61 else
63 nReference = nNewReference;
64 nValue = 0;
68 void ProgressBarHelper::SetValue(sal_Int32 nTempValue)
70 if (!xStatusIndicator.is() || (nReference <= 0))
71 return;
73 if ((nTempValue >= nValue) && (!bStrict || (nTempValue <= nReference)))
75 // #91317# no progress bar with values > 100%
76 if (nTempValue > nReference)
78 if (!bRepeat)
79 nValue = nReference;
80 else
82 xStatusIndicator->reset();
83 nValue = 0;
86 else
87 nValue = nTempValue;
89 double fValue(nValue);
90 double fNewValue ((fValue * nRange) / nReference);
92 double fPercent((fNewValue * 100) / nRange);
93 if (fPercent >= (fOldPercent + fProgressStep) || fPercent < fOldPercent)
95 xStatusIndicator->setValue(static_cast<sal_Int32>(fNewValue));
96 fOldPercent = fPercent;
99 #ifdef DBG_UTIL
100 else if (!bFailure)
102 OSL_FAIL("tried to set a wrong value on the progressbar");
103 bFailure = true;
105 #endif
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */