tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / view / axes / DateScaling.cxx
blobf24f46a8e4571aab76c51e2e69afe74ffe1e98f7
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 "DateScaling.hxx"
21 #include <com/sun/star/chart/TimeUnit.hpp>
22 #include <rtl/math.hxx>
23 #include <cppuhelper/supportsservice.hxx>
25 #include <limits>
27 namespace
30 constexpr OUString lcl_aServiceName_DateScaling = u"com.sun.star.chart2.DateScaling"_ustr;
31 constexpr OUString lcl_aServiceName_InverseDateScaling
32 = u"com.sun.star.chart2.InverseDateScaling"_ustr;
34 const double lcl_fNumberOfMonths = 12.0;//todo: this needs to be offered by basic tools Date class if it should be more generic
37 namespace chart
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::chart2;
41 using ::com::sun::star::chart::TimeUnit::DAY;
42 using ::com::sun::star::chart::TimeUnit::MONTH;
43 using ::com::sun::star::chart::TimeUnit::YEAR;
45 DateScaling::DateScaling( const Date& rNullDate, sal_Int32 nTimeUnit, bool bShifted )
46 : m_aNullDate( rNullDate )
47 , m_nTimeUnit( nTimeUnit )
48 , m_bShifted( bShifted )
52 DateScaling::~DateScaling()
56 double SAL_CALL DateScaling::doScaling( double value )
58 double fResult(value);
59 if( std::isnan( value ) || std::isinf( value ) )
60 return std::numeric_limits<double>::quiet_NaN();
61 switch( m_nTimeUnit )
63 case DAY:
64 fResult = value;
65 if(m_bShifted)
66 fResult+=0.5;
67 break;
68 case YEAR:
69 case MONTH:
70 default:
72 Date aDate(m_aNullDate);
73 aDate.AddDays(::rtl::math::approxFloor(value));
74 fResult = aDate.GetYear();
75 fResult *= lcl_fNumberOfMonths;//assuming equal count of months in each year
76 fResult += aDate.GetMonth();
78 double fDayOfMonth = aDate.GetDay();
79 fDayOfMonth -= 1.0;
80 double fDaysInMonth = aDate.GetDaysInMonth();
81 fResult += fDayOfMonth/fDaysInMonth;
82 if(m_bShifted)
84 if( m_nTimeUnit==YEAR )
85 fResult += 0.5*lcl_fNumberOfMonths;
86 else
87 fResult += 0.5;
89 break;
92 return fResult;
95 uno::Reference< XScaling > SAL_CALL DateScaling::getInverseScaling()
97 return new InverseDateScaling( m_aNullDate, m_nTimeUnit, m_bShifted );
100 OUString SAL_CALL DateScaling::getServiceName()
102 return lcl_aServiceName_DateScaling;
105 OUString SAL_CALL DateScaling::getImplementationName()
107 return lcl_aServiceName_DateScaling;
110 sal_Bool SAL_CALL DateScaling::supportsService( const OUString& rServiceName )
112 return cppu::supportsService(this, rServiceName);
115 css::uno::Sequence< OUString > SAL_CALL DateScaling::getSupportedServiceNames()
117 return { lcl_aServiceName_DateScaling };
120 InverseDateScaling::InverseDateScaling( const Date& rNullDate, sal_Int32 nTimeUnit, bool bShifted )
121 : m_aNullDate( rNullDate )
122 , m_nTimeUnit( nTimeUnit )
123 , m_bShifted( bShifted )
127 InverseDateScaling::~InverseDateScaling()
131 double SAL_CALL InverseDateScaling::doScaling( double value )
133 double fResult(value);
134 if( std::isnan( value ) || std::isinf( value ) )
135 return std::numeric_limits<double>::quiet_NaN();
136 else
138 switch( m_nTimeUnit )
140 case DAY:
141 if(m_bShifted)
142 value -= 0.5;
143 fResult = value;
144 break;
145 case YEAR:
146 case MONTH:
147 default:
148 //Date aDate(m_aNullDate);
149 if(m_bShifted)
151 if( m_nTimeUnit==YEAR )
152 value -= 0.5*lcl_fNumberOfMonths;
153 else
154 value -= 0.5;
156 Date aDate( Date::EMPTY );
157 double fYear = ::rtl::math::approxFloor(value/lcl_fNumberOfMonths);
158 double fMonth = ::rtl::math::approxFloor(value-(fYear*lcl_fNumberOfMonths));
159 if( fMonth==0.0 )
161 fYear--;
162 fMonth=12.0;
164 aDate.SetYear( static_cast<sal_uInt16>(fYear) );
165 aDate.SetMonth( static_cast<sal_uInt16>(fMonth) );
166 aDate.SetDay( 1 );
167 double fMonthCount = (fYear*lcl_fNumberOfMonths)+fMonth;
168 double fDay = (value-fMonthCount)*aDate.GetDaysInMonth();
169 fDay += 1.0;
170 aDate.SetDay( static_cast<sal_uInt16>(::rtl::math::round(fDay)) );
171 fResult = aDate - m_aNullDate;
172 break;
175 return fResult;
178 uno::Reference< XScaling > SAL_CALL InverseDateScaling::getInverseScaling()
180 return new DateScaling( m_aNullDate, m_nTimeUnit, m_bShifted );
183 OUString SAL_CALL InverseDateScaling::getServiceName()
185 return lcl_aServiceName_InverseDateScaling;
188 OUString SAL_CALL InverseDateScaling::getImplementationName()
190 return lcl_aServiceName_InverseDateScaling;
193 sal_Bool SAL_CALL InverseDateScaling::supportsService( const OUString& rServiceName )
195 return cppu::supportsService(this, rServiceName);
198 css::uno::Sequence< OUString > SAL_CALL InverseDateScaling::getSupportedServiceNames()
200 return { lcl_aServiceName_InverseDateScaling };
203 } //namespace chart
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */