1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "DateScaling.hxx"
21 #include <com/sun/star/chart/TimeUnit.hpp>
22 #include <rtl/math.hxx>
23 #include "com/sun/star/uno/RuntimeException.hpp"
24 #include <cppuhelper/supportsservice.hxx>
29 static const char lcl_aServiceName_DateScaling
[] = "com.sun.star.chart2.DateScaling";
30 static const char lcl_aServiceName_InverseDateScaling
[] = "com.sun.star.chart2.InverseDateScaling";
32 static const double lcl_fNumberOfMonths
= 12.0;//todo: this needs to be offered by basic tools Date class if it should be more generic
37 using namespace ::com::sun::star
;
38 using namespace ::com::sun::star::chart2
;
39 using ::com::sun::star::chart::TimeUnit::DAY
;
40 using ::com::sun::star::chart::TimeUnit::MONTH
;
41 using ::com::sun::star::chart::TimeUnit::YEAR
;
43 DateScaling::DateScaling( const Date
& rNullDate
, sal_Int32 nTimeUnit
, bool bShifted
)
44 : m_aNullDate( rNullDate
)
45 , m_nTimeUnit( nTimeUnit
)
46 , m_bShifted( bShifted
)
50 DateScaling::~DateScaling()
54 double SAL_CALL
DateScaling::doScaling( double value
)
55 throw (uno::RuntimeException
, std::exception
)
57 double fResult(value
);
58 if( ::rtl::math::isNan( value
) || ::rtl::math::isInf( value
) )
59 ::rtl::math::setNan( & fResult
);
62 Date
aDate(m_aNullDate
);
63 aDate
+= static_cast<long>(::rtl::math::approxFloor(value
));
74 fResult
= aDate
.GetYear();
75 fResult
*= lcl_fNumberOfMonths
;//asssuming equal count of months in each year
76 fResult
+= aDate
.GetMonth();
78 double fDayOfMonth
= aDate
.GetDay();
80 double fDaysInMonth
= aDate
.GetDaysInMonth();
81 fResult
+= fDayOfMonth
/fDaysInMonth
;
84 if( YEAR
==m_nTimeUnit
)
85 fResult
+= 0.5*lcl_fNumberOfMonths
;
95 uno::Reference
< XScaling
> SAL_CALL
DateScaling::getInverseScaling()
96 throw (uno::RuntimeException
, std::exception
)
98 return new InverseDateScaling( m_aNullDate
, m_nTimeUnit
, m_bShifted
);
101 OUString SAL_CALL
DateScaling::getServiceName()
102 throw (uno::RuntimeException
, std::exception
)
104 return OUString(lcl_aServiceName_DateScaling
);
107 uno::Sequence
< OUString
> DateScaling::getSupportedServiceNames_Static()
109 uno::Sequence
< OUString
> aSeq(1);
110 aSeq
.getArray()[0] = lcl_aServiceName_DateScaling
;
114 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
115 OUString SAL_CALL
DateScaling::getImplementationName()
116 throw( css::uno::RuntimeException
, std::exception
)
118 return getImplementationName_Static();
121 OUString
DateScaling::getImplementationName_Static()
123 return OUString(lcl_aServiceName_DateScaling
);
126 sal_Bool SAL_CALL
DateScaling::supportsService( const OUString
& rServiceName
)
127 throw( css::uno::RuntimeException
, std::exception
)
129 return cppu::supportsService(this, rServiceName
);
132 css::uno::Sequence
< OUString
> SAL_CALL
DateScaling::getSupportedServiceNames()
133 throw( css::uno::RuntimeException
, std::exception
)
135 return getSupportedServiceNames_Static();
138 InverseDateScaling::InverseDateScaling( const Date
& rNullDate
, sal_Int32 nTimeUnit
, bool bShifted
)
139 : m_aNullDate( rNullDate
)
140 , m_nTimeUnit( nTimeUnit
)
141 , m_bShifted( bShifted
)
145 InverseDateScaling::~InverseDateScaling()
149 double SAL_CALL
InverseDateScaling::doScaling( double value
)
150 throw (uno::RuntimeException
, std::exception
)
152 double fResult(value
);
153 if( ::rtl::math::isNan( value
) || ::rtl::math::isInf( value
) )
154 ::rtl::math::setNan( & fResult
);
157 switch( m_nTimeUnit
)
167 //Date aDate(m_aNullDate);
170 if( YEAR
==m_nTimeUnit
)
171 value
-= 0.5*lcl_fNumberOfMonths
;
175 Date
aDate( Date::EMPTY
);
176 double fYear
= ::rtl::math::approxFloor(value
/lcl_fNumberOfMonths
);
177 double fMonth
= ::rtl::math::approxFloor(value
-(fYear
*lcl_fNumberOfMonths
));
183 aDate
.SetYear( static_cast<sal_uInt16
>(fYear
) );
184 aDate
.SetMonth( static_cast<sal_uInt16
>(fMonth
) );
186 double fMonthCount
= (fYear
*lcl_fNumberOfMonths
)+fMonth
;
187 double fDay
= (value
-fMonthCount
)*aDate
.GetDaysInMonth();
189 aDate
.SetDay( static_cast<sal_uInt16
>(::rtl::math::round(fDay
)) );
190 fResult
= aDate
- m_aNullDate
;
197 uno::Reference
< XScaling
> SAL_CALL
InverseDateScaling::getInverseScaling()
198 throw (uno::RuntimeException
, std::exception
)
200 return new DateScaling( m_aNullDate
, m_nTimeUnit
, m_bShifted
);
203 OUString SAL_CALL
InverseDateScaling::getServiceName()
204 throw (uno::RuntimeException
, std::exception
)
206 return OUString(lcl_aServiceName_InverseDateScaling
);
209 uno::Sequence
< OUString
> InverseDateScaling::getSupportedServiceNames_Static()
211 uno::Sequence
< OUString
> aSeq( 1 );
212 aSeq
.getArray()[0] = lcl_aServiceName_InverseDateScaling
;
216 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
217 OUString SAL_CALL
InverseDateScaling::getImplementationName()
218 throw( css::uno::RuntimeException
, std::exception
)
220 return getImplementationName_Static();
223 OUString
InverseDateScaling::getImplementationName_Static()
225 return OUString(lcl_aServiceName_InverseDateScaling
);
228 sal_Bool SAL_CALL
InverseDateScaling::supportsService( const OUString
& rServiceName
)
229 throw( css::uno::RuntimeException
, std::exception
)
231 return cppu::supportsService(this, rServiceName
);
234 css::uno::Sequence
< OUString
> SAL_CALL
InverseDateScaling::getSupportedServiceNames()
235 throw( css::uno::RuntimeException
, std::exception
)
237 return getSupportedServiceNames_Static();
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */