Bump version to 24.04.3.4
[LibreOffice.git] / chart2 / source / tools / NumberFormatterWrapper.cxx
blobe00df1fae04e85eba9c4ca64570b2511c80126a0
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 <NumberFormatterWrapper.hxx>
21 #include <svl/numformat.hxx>
22 #include <svl/numuno.hxx>
23 #include <tools/color.hxx>
24 #include <com/sun/star/util/Date.hpp>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
28 namespace chart
30 using namespace ::com::sun::star;
32 FixedNumberFormatter::FixedNumberFormatter(
33 const uno::Reference< util::XNumberFormatsSupplier >& xSupplier
34 , sal_Int32 nNumberFormatKey )
35 : m_aNumberFormatterWrapper(xSupplier)
36 , m_nNumberFormatKey( nNumberFormatKey )
40 FixedNumberFormatter::~FixedNumberFormatter()
44 OUString FixedNumberFormatter::getFormattedString( double fValue, Color& rLabelColor, bool& rbColorChanged ) const
46 return m_aNumberFormatterWrapper.getFormattedString(
47 m_nNumberFormatKey, fValue, rLabelColor, rbColorChanged );
50 NumberFormatterWrapper::NumberFormatterWrapper( const uno::Reference< util::XNumberFormatsSupplier >& xSupplier )
51 : m_xNumberFormatsSupplier(xSupplier)
52 , m_pNumberFormatter(nullptr)
55 uno::Reference<beans::XPropertySet> xProp(m_xNumberFormatsSupplier,uno::UNO_QUERY);
56 OUString sNullDate( "NullDate" );
57 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(sNullDate) )
58 m_aNullDate = xProp->getPropertyValue(sNullDate);
59 SvNumberFormatsSupplierObj* pSupplierObj = comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>( xSupplier );
60 if( pSupplierObj )
61 m_pNumberFormatter = pSupplierObj->GetNumberFormatter();
62 SAL_WARN_IF(!m_pNumberFormatter,"chart2.tools","need a numberformatter");
65 NumberFormatterWrapper::~NumberFormatterWrapper()
69 namespace
71 bool getDate(const css::uno::Any& rAny, util::Date& rDate)
73 if (rAny >>= rDate)
74 return true;
75 util::DateTime aUtilDateTime;
76 if (rAny >>= aUtilDateTime)
78 rDate.Day = aUtilDateTime.Day;
79 rDate.Month = aUtilDateTime.Month;
80 rDate.Year = aUtilDateTime.Year;
81 return true;
83 SAL_WARN("chart2.tools", "neither a util::Date nor a util::DateTime");
84 return false;
88 Date NumberFormatterWrapper::getNullDate() const
90 Date aRet(30,12,1899);
92 util::Date aUtilDate;
93 if (m_aNullDate.hasValue() && getDate(m_aNullDate, aUtilDate))
95 aRet = Date(aUtilDate.Day,aUtilDate.Month,aUtilDate.Year);
97 else if( m_pNumberFormatter )
99 aRet = m_pNumberFormatter->GetNullDate();
101 return aRet;
104 OUString NumberFormatterWrapper::getFormattedString( sal_Int32 nNumberFormatKey, double fValue,
105 Color& rLabelColor, bool& rbColorChanged ) const
107 OUString aText;
108 const Color* pTextColor = nullptr;
109 if( !m_pNumberFormatter )
111 OSL_FAIL("Need a NumberFormatter");
112 return aText;
114 // i99104 handle null date correctly
115 sal_Int16 nYear = 1899;
116 sal_uInt16 nDay = 30,nMonth = 12;
117 if ( m_aNullDate.hasValue() )
119 const Date& rDate = m_pNumberFormatter->GetNullDate();
120 nYear = rDate.GetYear();
121 nMonth = rDate.GetMonth();
122 nDay = rDate.GetDay();
123 util::Date aNewNullDate;
124 if (getDate(m_aNullDate, aNewNullDate))
125 m_pNumberFormatter->ChangeNullDate(aNewNullDate.Day,aNewNullDate.Month,aNewNullDate.Year);
127 // tdf#130969: use UNLIMITED_PRECISION in case of GENERAL Number Format
128 if( m_pNumberFormatter->GetStandardPrec() != SvNumberFormatter::UNLIMITED_PRECISION )
129 m_pNumberFormatter->ChangeStandardPrec(SvNumberFormatter::UNLIMITED_PRECISION);
130 m_pNumberFormatter->GetOutputString(fValue, nNumberFormatKey, aText, &pTextColor);
131 if ( m_aNullDate.hasValue() )
133 m_pNumberFormatter->ChangeNullDate(nDay,nMonth,nYear);
136 if(pTextColor)
138 rbColorChanged = true;
139 rLabelColor = *pTextColor;
141 else
142 rbColorChanged = false;
144 return aText;
147 } //namespace chart
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */