/src/libreoffice/chart2/source/tools/NumberFormatterWrapper.cxx
Line | Count | Source |
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 | | |
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> |
27 | | |
28 | | namespace chart |
29 | | { |
30 | | using namespace ::com::sun::star; |
31 | | |
32 | | FixedNumberFormatter::FixedNumberFormatter( |
33 | | const uno::Reference< util::XNumberFormatsSupplier >& xSupplier |
34 | | , sal_Int32 nNumberFormatKey ) |
35 | 0 | : m_aNumberFormatterWrapper(xSupplier) |
36 | 0 | , m_nNumberFormatKey( nNumberFormatKey ) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | FixedNumberFormatter::~FixedNumberFormatter() |
41 | 0 | { |
42 | 0 | } |
43 | | |
44 | | OUString FixedNumberFormatter::getFormattedString( double fValue, Color& rLabelColor, bool& rbColorChanged ) const |
45 | 0 | { |
46 | 0 | return m_aNumberFormatterWrapper.getFormattedString( |
47 | 0 | m_nNumberFormatKey, fValue, rLabelColor, rbColorChanged ); |
48 | 0 | } |
49 | | |
50 | | NumberFormatterWrapper::NumberFormatterWrapper( const uno::Reference< util::XNumberFormatsSupplier >& xSupplier ) |
51 | 0 | : m_xNumberFormatsSupplier(xSupplier) |
52 | 0 | , m_pNumberFormatter(nullptr) |
53 | | |
54 | 0 | { |
55 | 0 | uno::Reference<beans::XPropertySet> xProp(m_xNumberFormatsSupplier,uno::UNO_QUERY); |
56 | 0 | OUString sNullDate( u"NullDate"_ustr ); |
57 | 0 | if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(sNullDate) ) |
58 | 0 | m_aNullDate = xProp->getPropertyValue(sNullDate); |
59 | 0 | SvNumberFormatsSupplierObj* pSupplierObj = comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>( xSupplier ); |
60 | 0 | if( pSupplierObj ) |
61 | 0 | m_pNumberFormatter = pSupplierObj->GetNumberFormatter(); |
62 | 0 | SAL_WARN_IF(!m_pNumberFormatter,"chart2.tools","need a numberformatter"); |
63 | 0 | } |
64 | | |
65 | | NumberFormatterWrapper::~NumberFormatterWrapper() |
66 | 0 | { |
67 | 0 | } |
68 | | |
69 | | namespace |
70 | | { |
71 | | bool getDate(const css::uno::Any& rAny, util::Date& rDate) |
72 | 0 | { |
73 | 0 | if (rAny >>= rDate) |
74 | 0 | return true; |
75 | 0 | util::DateTime aUtilDateTime; |
76 | 0 | if (rAny >>= aUtilDateTime) |
77 | 0 | { |
78 | 0 | rDate.Day = aUtilDateTime.Day; |
79 | 0 | rDate.Month = aUtilDateTime.Month; |
80 | 0 | rDate.Year = aUtilDateTime.Year; |
81 | 0 | return true; |
82 | 0 | } |
83 | 0 | SAL_WARN("chart2.tools", "neither a util::Date nor a util::DateTime"); |
84 | 0 | return false; |
85 | 0 | } |
86 | | } |
87 | | |
88 | | Date NumberFormatterWrapper::getNullDate() const |
89 | 0 | { |
90 | 0 | Date aRet(30,12,1899); |
91 | |
|
92 | 0 | util::Date aUtilDate; |
93 | 0 | if (m_aNullDate.hasValue() && getDate(m_aNullDate, aUtilDate)) |
94 | 0 | { |
95 | 0 | aRet = Date(aUtilDate.Day,aUtilDate.Month,aUtilDate.Year); |
96 | 0 | } |
97 | 0 | else if( m_pNumberFormatter ) |
98 | 0 | { |
99 | 0 | aRet = m_pNumberFormatter->GetNullDate(); |
100 | 0 | } |
101 | 0 | return aRet; |
102 | 0 | } |
103 | | |
104 | | OUString NumberFormatterWrapper::getFormattedString( sal_Int32 nNumberFormatKey, double fValue, |
105 | | Color& rLabelColor, bool& rbColorChanged ) const |
106 | 0 | { |
107 | 0 | OUString aText; |
108 | 0 | const Color* pTextColor = nullptr; |
109 | 0 | if( !m_pNumberFormatter ) |
110 | 0 | { |
111 | 0 | OSL_FAIL("Need a NumberFormatter"); |
112 | 0 | return aText; |
113 | 0 | } |
114 | | // i99104 handle null date correctly |
115 | 0 | sal_Int16 nYear = 1899; |
116 | 0 | sal_uInt16 nDay = 30,nMonth = 12; |
117 | 0 | if ( m_aNullDate.hasValue() ) |
118 | 0 | { |
119 | 0 | const Date& rDate = m_pNumberFormatter->GetNullDate(); |
120 | 0 | nYear = rDate.GetYear(); |
121 | 0 | nMonth = rDate.GetMonth(); |
122 | 0 | nDay = rDate.GetDay(); |
123 | 0 | util::Date aNewNullDate; |
124 | 0 | if (getDate(m_aNullDate, aNewNullDate)) |
125 | 0 | m_pNumberFormatter->ChangeNullDate(aNewNullDate.Day,aNewNullDate.Month,aNewNullDate.Year); |
126 | 0 | } |
127 | | // tdf#130969: use UNLIMITED_PRECISION in case of GENERAL Number Format |
128 | 0 | if( m_pNumberFormatter->GetStandardPrec() != SvNumberFormatter::UNLIMITED_PRECISION ) |
129 | 0 | m_pNumberFormatter->ChangeStandardPrec(SvNumberFormatter::UNLIMITED_PRECISION); |
130 | 0 | m_pNumberFormatter->GetOutputString(fValue, nNumberFormatKey, aText, &pTextColor); |
131 | 0 | if ( m_aNullDate.hasValue() ) |
132 | 0 | { |
133 | 0 | m_pNumberFormatter->ChangeNullDate(nDay,nMonth,nYear); |
134 | 0 | } |
135 | |
|
136 | 0 | if(pTextColor) |
137 | 0 | { |
138 | 0 | rbColorChanged = true; |
139 | 0 | rLabelColor = *pTextColor; |
140 | 0 | } |
141 | 0 | else |
142 | 0 | rbColorChanged = false; |
143 | |
|
144 | 0 | return aText; |
145 | 0 | } |
146 | | |
147 | | } //namespace chart |
148 | | |
149 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |