tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / view / axes / Tickmarks.cxx
blobc30f2f25d5e547cdaa7557b0e976df1a1cf27455
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 "Tickmarks.hxx"
21 #include "Tickmarks_Equidistant.hxx"
22 #include "Tickmarks_Dates.hxx"
23 #include <ViewDefines.hxx>
24 #include "VAxisProperties.hxx"
25 #include <osl/diagnose.h>
26 #include <com/sun/star/chart2/AxisType.hpp>
27 #include <utility>
29 using namespace ::com::sun::star;
30 using ::basegfx::B2DVector;
32 namespace chart {
34 TickInfo::TickInfo( uno::Reference<chart2::XScaling> xInverse )
35 : fScaledTickValue( 0.0 )
36 , xInverseScaling(std::move( xInverse ))
37 , aTickScreenPosition(0.0,0.0)
38 , nFactorForLimitedTextWidth(1)
39 , bPaintIt( true )
43 double TickInfo::getUnscaledTickValue() const
45 if( xInverseScaling.is() )
46 return xInverseScaling->doScaling( fScaledTickValue );
47 else
48 return fScaledTickValue;
51 sal_Int32 TickInfo::getScreenDistanceBetweenTicks( const TickInfo& rOherTickInfo ) const
53 //return the positive distance between the two first tickmarks in screen values
55 B2DVector aDistance = rOherTickInfo.aTickScreenPosition - aTickScreenPosition;
56 sal_Int32 nRet = static_cast<sal_Int32>(aDistance.getLength());
57 if(nRet<0)
58 nRet *= -1;
59 return nRet;
62 PureTickIter::PureTickIter( TickInfoArrayType& rTickInfoVector )
63 : m_rTickVector(rTickInfoVector)
64 , m_aTickIter(m_rTickVector.begin())
67 PureTickIter::~PureTickIter()
70 TickInfo* PureTickIter::firstInfo()
72 m_aTickIter = m_rTickVector.begin();
73 if(m_aTickIter!=m_rTickVector.end())
74 return &*m_aTickIter;
75 return nullptr;
77 TickInfo* PureTickIter::nextInfo()
79 if(m_aTickIter!=m_rTickVector.end())
81 ++m_aTickIter;
82 if(m_aTickIter!=m_rTickVector.end())
83 return &*m_aTickIter;
85 return nullptr;
88 TickFactory::TickFactory(
89 ExplicitScaleData aScale, ExplicitIncrementData aIncrement )
90 : m_rScale(std::move( aScale ))
91 , m_rIncrement(std::move( aIncrement ))
93 //@todo: make sure that the scale is valid for the scaling
95 if( m_rScale.Scaling.is() )
97 m_xInverseScaling = m_rScale.Scaling->getInverseScaling();
98 OSL_ENSURE( m_xInverseScaling.is(), "each Scaling needs to return an inverse Scaling" );
101 m_fScaledVisibleMin = m_rScale.Minimum;
102 if( m_xInverseScaling.is() )
103 m_fScaledVisibleMin = m_rScale.Scaling->doScaling(m_fScaledVisibleMin);
105 m_fScaledVisibleMax = m_rScale.Maximum;
106 if( m_xInverseScaling.is() )
107 m_fScaledVisibleMax = m_rScale.Scaling->doScaling(m_fScaledVisibleMax);
110 TickFactory::~TickFactory()
114 bool TickFactory::isDateAxis() const
116 return m_rScale.AxisType == chart2::AxisType::DATE;
119 void TickFactory::getAllTicks( TickInfoArraysType& rAllTickInfos ) const
121 if( isDateAxis() )
122 DateTickFactory( m_rScale, m_rIncrement ).getAllTicks( rAllTickInfos );
123 else
124 EquidistantTickFactory( m_rScale, m_rIncrement ).getAllTicks( rAllTickInfos );
127 void TickFactory::getAllTicksShifted( TickInfoArraysType& rAllTickInfos ) const
129 if( isDateAxis() )
130 DateTickFactory( m_rScale, m_rIncrement ).getAllTicksShifted( rAllTickInfos );
131 else
132 EquidistantTickFactory( m_rScale, m_rIncrement ).getAllTicksShifted( rAllTickInfos );
135 // ___TickFactory_2D___
136 TickFactory2D::TickFactory2D(
137 const ExplicitScaleData& rScale, const ExplicitIncrementData& rIncrement
138 //, double fStretch_SceneToScreen, double fOffset_SceneToScreen )
139 , const B2DVector& rStartScreenPos, const B2DVector& rEndScreenPos
140 , const B2DVector& rAxisLineToLabelLineShift )
141 : TickFactory( rScale, rIncrement )
142 , m_aAxisStartScreenPosition2D(rStartScreenPos)
143 , m_aAxisEndScreenPosition2D(rEndScreenPos)
144 , m_aAxisLineToLabelLineShift(rAxisLineToLabelLineShift)
145 , m_fStretch_LogicToScreen(1.0)
146 , m_fOffset_LogicToScreen(0.0)
148 double fWidthY = m_fScaledVisibleMax - m_fScaledVisibleMin;
149 if (m_rScale.Orientation == chart2::AxisOrientation_MATHEMATICAL)
151 m_fStretch_LogicToScreen = 1.0/fWidthY;
152 m_fOffset_LogicToScreen = -m_fScaledVisibleMin;
154 else
156 B2DVector aSwap(m_aAxisStartScreenPosition2D);
157 m_aAxisStartScreenPosition2D = m_aAxisEndScreenPosition2D;
158 m_aAxisEndScreenPosition2D = aSwap;
160 m_fStretch_LogicToScreen = -1.0/fWidthY;
161 m_fOffset_LogicToScreen = -m_fScaledVisibleMax;
165 TickFactory2D::~TickFactory2D()
169 bool TickFactory2D::isHorizontalAxis() const
171 // check trivial cases:
172 if ( m_aAxisStartScreenPosition2D.getY() == m_aAxisEndScreenPosition2D.getY() )
173 return true;
174 if ( m_aAxisStartScreenPosition2D.getX() == m_aAxisEndScreenPosition2D.getX() )
175 return false;
177 // for skew axes compare angle with horizontal vector
178 double fInclination = std::abs(B2DVector(m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D).angle(B2DVector(1.0, 0.0)));
179 return fInclination < M_PI_4 || fInclination > (M_PI-M_PI_4);
181 bool TickFactory2D::isVerticalAxis() const
183 // check trivial cases:
184 if ( m_aAxisStartScreenPosition2D.getX() == m_aAxisEndScreenPosition2D.getX() )
185 return true;
186 if ( m_aAxisStartScreenPosition2D.getY() == m_aAxisEndScreenPosition2D.getY() )
187 return false;
189 // for skew axes compare angle with vertical vector
190 double fInclination = std::abs(B2DVector(m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D).angle(B2DVector(0.0, -1.0)));
191 return fInclination < M_PI_4 || fInclination > (M_PI-M_PI_4);
193 //static
194 sal_Int32 TickFactory2D::getTickScreenDistance( TickIter& rIter )
196 //return the positive distance between the two first tickmarks in screen values
197 //if there are less than two tickmarks -1 is returned
199 const TickInfo* pFirstTickInfo = rIter.firstInfo();
200 const TickInfo* pSecondTickInfo = rIter.nextInfo();
201 if(!pSecondTickInfo || !pFirstTickInfo)
202 return -1;
204 return pFirstTickInfo->getScreenDistanceBetweenTicks( *pSecondTickInfo );
207 B2DVector TickFactory2D::getTickScreenPosition2D( double fScaledLogicTickValue ) const
209 B2DVector aRet(m_aAxisStartScreenPosition2D);
210 aRet += (m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D)
211 *((fScaledLogicTickValue+m_fOffset_LogicToScreen)*m_fStretch_LogicToScreen);
212 return aRet;
215 void TickFactory2D::addPointSequenceForTickLine( drawing::PointSequenceSequence& rPoints
216 , sal_Int32 nSequenceIndex
217 , double fScaledLogicTickValue, double fInnerDirectionSign
218 , const TickmarkProperties& rTickmarkProperties
219 , bool bPlaceAtLabels ) const
221 if( fInnerDirectionSign==0.0 )
222 fInnerDirectionSign = 1.0;
224 B2DVector aTickScreenPosition = getTickScreenPosition2D(fScaledLogicTickValue);
225 if( bPlaceAtLabels )
226 aTickScreenPosition += m_aAxisLineToLabelLineShift;
228 B2DVector aMainDirection = m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D;
229 aMainDirection.normalize();
230 B2DVector aOrthoDirection(-aMainDirection.getY(),aMainDirection.getX());
231 aOrthoDirection *= fInnerDirectionSign;
232 aOrthoDirection.normalize();
234 B2DVector aStart = aTickScreenPosition + aOrthoDirection*rTickmarkProperties.RelativePos;
235 B2DVector aEnd = aStart - aOrthoDirection*rTickmarkProperties.Length;
237 rPoints.getArray()[nSequenceIndex]
238 = { { static_cast<sal_Int32>(aStart.getX()), static_cast<sal_Int32>(aStart.getY()) },
239 { static_cast<sal_Int32>(aEnd.getX()), static_cast<sal_Int32>(aEnd.getY()) } };
242 B2DVector TickFactory2D::getDistanceAxisTickToText( const AxisProperties& rAxisProperties, bool bIncludeFarAwayDistanceIfSo, bool bIncludeSpaceBetweenTickAndText ) const
244 bool bFarAwayLabels = false;
245 if( rAxisProperties.m_eLabelPos == css::chart::ChartAxisLabelPosition_OUTSIDE_START
246 || rAxisProperties.m_eLabelPos == css::chart::ChartAxisLabelPosition_OUTSIDE_END )
247 bFarAwayLabels = true;
249 double fInnerDirectionSign = rAxisProperties.maLabelAlignment.mfInnerTickDirection;
250 if( fInnerDirectionSign==0.0 )
251 fInnerDirectionSign = 1.0;
253 B2DVector aMainDirection = m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D;
254 aMainDirection.normalize();
255 B2DVector aOrthoDirection(-aMainDirection.getY(),aMainDirection.getX());
256 aOrthoDirection *= fInnerDirectionSign;
257 aOrthoDirection.normalize();
259 B2DVector aStart(0,0), aEnd(0,0);
260 if( bFarAwayLabels )
262 TickmarkProperties aProps( AxisProperties::getBiggestTickmarkProperties() );
263 aStart = aOrthoDirection*aProps.RelativePos;
264 aEnd = aStart - aOrthoDirection*aProps.Length;
266 else
268 for( sal_Int32 nN=rAxisProperties.m_aTickmarkPropertiesList.size();nN--;)
270 const TickmarkProperties& rProps = rAxisProperties.m_aTickmarkPropertiesList[nN];
271 B2DVector aNewStart = aOrthoDirection*rProps.RelativePos;
272 B2DVector aNewEnd = aNewStart - aOrthoDirection*rProps.Length;
273 if(aNewStart.getLength()>aStart.getLength())
274 aStart=aNewStart;
275 if(aNewEnd.getLength()>aEnd.getLength())
276 aEnd=aNewEnd;
280 B2DVector aLabelDirection(aStart);
281 if (rAxisProperties.maLabelAlignment.mfInnerTickDirection != rAxisProperties.maLabelAlignment.mfLabelDirection)
282 aLabelDirection = aEnd;
284 B2DVector aOrthoLabelDirection(aOrthoDirection);
285 if (rAxisProperties.maLabelAlignment.mfInnerTickDirection != rAxisProperties.maLabelAlignment.mfLabelDirection)
286 aOrthoLabelDirection*=-1.0;
287 aOrthoLabelDirection.normalize();
288 if( bIncludeSpaceBetweenTickAndText )
289 aLabelDirection += aOrthoLabelDirection*AXIS2D_TICKLABELSPACING;
290 if( bFarAwayLabels && bIncludeFarAwayDistanceIfSo )
291 aLabelDirection += m_aAxisLineToLabelLineShift;
292 return aLabelDirection;
295 void TickFactory2D::createPointSequenceForAxisMainLine( drawing::PointSequenceSequence& rPoints ) const
297 rPoints.getArray()[0] = { { static_cast<sal_Int32>(m_aAxisStartScreenPosition2D.getX()),
298 static_cast<sal_Int32>(m_aAxisStartScreenPosition2D.getY()) },
299 { static_cast<sal_Int32>(m_aAxisEndScreenPosition2D.getX()),
300 static_cast<sal_Int32>(m_aAxisEndScreenPosition2D.getY()) } };
303 void TickFactory2D::updateScreenValues( TickInfoArraysType& rAllTickInfos ) const
305 //get the transformed screen values for all tickmarks in rAllTickInfos
306 for (auto & tickInfos : rAllTickInfos)
308 for (auto & tickInfo : tickInfos)
310 tickInfo.aTickScreenPosition =
311 getTickScreenPosition2D(tickInfo.fScaledTickValue);
316 } //namespace chart
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */