LibreOffice Module chart2 (master) 1
NumberFormatterWrapper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
27
28namespace chart
29{
30using namespace ::com::sun::star;
31
34 , sal_Int32 nNumberFormatKey )
35 : m_aNumberFormatterWrapper(xSupplier)
36 , m_nNumberFormatKey( nNumberFormatKey )
37{
38}
39
41{
42}
43
44OUString FixedNumberFormatter::getFormattedString( double fValue, Color& rLabelColor, bool& rbColorChanged ) const
45{
47 m_nNumberFormatKey, fValue, rLabelColor, rbColorChanged );
48}
49
51 : m_xNumberFormatsSupplier(xSupplier)
52 , m_pNumberFormatter(nullptr)
53
54{
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");
63}
64
66{
67}
68
69namespace
70{
71 bool getDate(const css::uno::Any& rAny, util::Date& rDate)
72 {
73 if (rAny >>= rDate)
74 return true;
75 util::DateTime aUtilDateTime;
76 if (rAny >>= aUtilDateTime)
77 {
78 rDate.Day = aUtilDateTime.Day;
79 rDate.Month = aUtilDateTime.Month;
80 rDate.Year = aUtilDateTime.Year;
81 return true;
82 }
83 SAL_WARN("chart2.tools", "neither a util::Date nor a util::DateTime");
84 return false;
85 }
86}
87
89{
90 Date aRet(30,12,1899);
91
92 util::Date aUtilDate;
93 if (m_aNullDate.hasValue() && getDate(m_aNullDate, aUtilDate))
94 {
95 aRet = Date(aUtilDate.Day,aUtilDate.Month,aUtilDate.Year);
96 }
97 else if( m_pNumberFormatter )
98 {
100 }
101 return aRet;
102}
103
104OUString NumberFormatterWrapper::getFormattedString( sal_Int32 nNumberFormatKey, double fValue,
105 Color& rLabelColor, bool& rbColorChanged ) const
106{
107 OUString aText;
108 const Color* pTextColor = nullptr;
109 if( !m_pNumberFormatter )
110 {
111 OSL_FAIL("Need a NumberFormatter");
112 return aText;
113 }
114 // i99104 handle null date correctly
115 sal_Int16 nYear = 1899;
116 sal_uInt16 nDay = 30,nMonth = 12;
117 if ( m_aNullDate.hasValue() )
118 {
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);
126 }
127 // tdf#130969: use UNLIMITED_PRECISION in case of GENERAL Number Format
130 m_pNumberFormatter->GetOutputString(fValue, nNumberFormatKey, aText, &pTextColor);
131 if ( m_aNullDate.hasValue() )
132 {
133 m_pNumberFormatter->ChangeNullDate(nDay,nMonth,nYear);
134 }
135
136 if(pTextColor)
137 {
138 rbColorChanged = true;
139 rLabelColor = *pTextColor;
140 }
141 else
142 rbColorChanged = false;
143
144 return aText;
145}
146
147} //namespace chart
148
149/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 GetYear() const
sal_uInt16 GetDay() const
sal_uInt16 GetMonth() const
SvNumberFormatter * GetNumberFormatter() const
sal_uInt16 GetStandardPrec() const
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, const Color **ppColor, bool bUseStarFormat=false)
const Date & GetNullDate() const
static const sal_uInt16 UNLIMITED_PRECISION
void ChangeNullDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
void ChangeStandardPrec(short nPrec)
NumberFormatterWrapper m_aNumberFormatterWrapper
FixedNumberFormatter(const css::uno::Reference< css::util::XNumberFormatsSupplier > &xSupplier, sal_Int32 nNumberFormatKey)
OUString getFormattedString(double fValue, Color &rLabelColor, bool &rbColorChanged) const
css::uno::Reference< css::util::XNumberFormatsSupplier > m_xNumberFormatsSupplier
OUString getFormattedString(sal_Int32 nNumberFormatKey, double fValue, Color &rLabelColor, bool &rbColorChanged) const
NumberFormatterWrapper(const css::uno::Reference< css::util::XNumberFormatsSupplier > &xSupplier)
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)