Update git submodules
[LibreOffice.git] / xmloff / source / core / ProgressBarHelper.cxx
blob3ff70fb0434d43001e07d7ea30a10dc9b8d36564
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 : m_xStatusIndicator(std::move(xTempStatusIndicator))
33 , m_nRange(nDefaultProgressBarRange)
34 , m_nReference(100)
35 , m_nValue(0)
36 , m_fOldPercent(0.0)
37 , m_bStrict(bTempStrict)
38 , m_bRepeat(true)
39 #ifdef DBG_UTIL
40 , m_bFailure(false)
41 #endif
45 ProgressBarHelper::~ProgressBarHelper()
49 void ProgressBarHelper::ChangeReference(sal_Int32 nNewReference)
51 if((nNewReference <= 0) || (nNewReference == m_nReference))
52 return;
54 if (m_nReference)
56 double fPercent(static_cast<double>(nNewReference) / m_nReference);
57 double fValue(m_nValue * fPercent);
58 m_nValue = static_cast<sal_Int32>(fValue);
59 m_nReference = nNewReference;
61 else
63 m_nReference = nNewReference;
64 m_nValue = 0;
68 void ProgressBarHelper::SetValue(sal_Int32 nTempValue)
70 if (!m_xStatusIndicator.is() || (m_nReference <= 0))
71 return;
73 if ((nTempValue >= m_nValue) && (!m_bStrict || (nTempValue <= m_nReference)))
75 // #91317# no progress bar with values > 100%
76 if (nTempValue > m_nReference)
78 if (!m_bRepeat)
79 m_nValue = m_nReference;
80 else
82 m_xStatusIndicator->reset();
83 m_nValue = 0;
86 else
87 m_nValue = nTempValue;
89 double fValue(m_nValue);
90 double fNewValue ((fValue * m_nRange) / m_nReference);
92 double fPercent((fNewValue * 100) / m_nRange);
93 if (fPercent >= (m_fOldPercent + fProgressStep) || fPercent < m_fOldPercent)
95 m_xStatusIndicator->setValue(static_cast<sal_Int32>(fNewValue));
96 m_fOldPercent = fPercent;
99 #ifdef DBG_UTIL
100 else if (!m_bFailure)
102 OSL_FAIL("tried to set a wrong value on the progressbar");
103 m_bFailure = true;
105 #endif
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */