diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-03-06 20:47:17 +0100 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2020-03-06 23:23:00 +0100 |
commit | 5699f1e0cb782003a1bb7ff3823b0b1c41819a07 (patch) | |
tree | 525e029fc862e9e9f67d732b28c75adccf11f1e2 | |
parent | 54c6fcff19fdf9e9074bdf7b7a3f68ec398c51df (diff) |
basegfx: B2DTuple replace operator[] with "get" and Axis2D enum
opertaor[] was used for index access of of x and y variables
where 0 is x and 1 is y - similar like access to an array with 2
elements.
This comes in handy when you write generic code where the
algorithm is the same for x and y, but using index access operator
doesn't look clean and is potentially dangerous. We know we only
have 2 options (o and 1), but an index access allows for more.
A solution to this is to have a normal "get" method, with an enum
as parameter (Axis2D), which can only have values X or Y.
Change-Id: I3f98d0149214808a336f25599350a78436236827
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/90133
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | chart2/source/view/charttypes/PieChart.cxx | 26 | ||||
-rw-r--r-- | include/basegfx/tuple/b2dtuple.hxx | 26 | ||||
-rw-r--r-- | include/basegfx/utils/common.hxx | 33 | ||||
-rw-r--r-- | svx/source/table/tablehandles.cxx | 10 |
4 files changed, 62 insertions, 33 deletions
diff --git a/chart2/source/view/charttypes/PieChart.cxx b/chart2/source/view/charttypes/PieChart.cxx index 9694f9cb75cf..94037cdc2272 100644 --- a/chart2/source/view/charttypes/PieChart.cxx +++ b/chart2/source/view/charttypes/PieChart.cxx @@ -1330,14 +1330,14 @@ bool PieChart::performLabelBestFitInnerPlacement(ShapeParam& rShapeParam, PieLab // compute lengths of the nearest edge and of the orthogonal edges double fNearestEdgeLength = fLabelWidth; double fOrthogonalEdgeLength = fLabelHeight; - int nAxisIndex = 0; - int nOrthogonalAxisIndex = 1; + basegfx::Axis2D eAxis = basegfx::Axis2D::X; + basegfx::Axis2D eOrthogonalAxis = basegfx::Axis2D::Y; if( nNearestEdgeIndex % 2 == 0 ) // nearest edge is vertical { fNearestEdgeLength = fLabelHeight; fOrthogonalEdgeLength = fLabelWidth; - nAxisIndex = 1; - nOrthogonalAxisIndex = 0; + eAxis = basegfx::Axis2D::Y; + eOrthogonalAxis = basegfx::Axis2D::X; } // compute the distance between N and P @@ -1454,11 +1454,11 @@ bool PieChart::performLabelBestFitInnerPlacement(ShapeParam& rShapeParam, PieLab // compute vertices N, M and G respect with pie center C basegfx::B2DVector aNearestVertex(aPositionalVector); - aNearestVertex[nAxisIndex] += -aDirection[nAxisIndex] * fDistanceNP; + aNearestVertex.set(eAxis, aNearestVertex.get(eAxis) - aDirection.get(eAxis) * fDistanceNP); basegfx::B2DVector aVertexM(aNearestVertex); - aVertexM[nAxisIndex] += aDirection[nAxisIndex] * fNearestEdgeLength; + aVertexM.set(eAxis, aVertexM.get(eAxis) + aDirection.get(eAxis) * fNearestEdgeLength); basegfx::B2DVector aVertexG(aNearestVertex); - aVertexG[nOrthogonalAxisIndex] += aDirection[nOrthogonalAxisIndex] * fOrthogonalEdgeLength; + aVertexG.set(eOrthogonalAxis, aVertexG.get(eOrthogonalAxis) + aDirection.get(eOrthogonalAxis) * fOrthogonalEdgeLength); SAL_INFO( "chart2.pie.label.bestfit.inside", " beta = " << basegfx::rad2deg(fBetaRad) ); @@ -1499,8 +1499,8 @@ bool PieChart::performLabelBestFitInnerPlacement(ShapeParam& rShapeParam, PieLab return false; } - if( ( aNearestVertex[nAxisIndex] >= 0 && aVertexM[nAxisIndex] <= 0 ) - || ( aNearestVertex[nAxisIndex] <= 0 && aVertexM[nAxisIndex] >= 0 ) ) + if( ( aNearestVertex.get(eAxis) >= 0 && aVertexM.get(eAxis) <= 0 ) + || ( aNearestVertex.get(eAxis) <= 0 && aVertexM.get(eAxis) >= 0 ) ) { // check the angle between CP and CN fAngleRad = aPositionalVector.angle(aNearestVertex); @@ -1531,13 +1531,13 @@ bool PieChart::performLabelBestFitInnerPlacement(ShapeParam& rShapeParam, PieLab // compute the b.b. center respect with the pie center basegfx::B2DVector aBBCenter(aNearestVertex); - aBBCenter[nAxisIndex] += aDirection[nAxisIndex] * fNearestEdgeLength / 2; - aBBCenter[nOrthogonalAxisIndex] += aDirection[nOrthogonalAxisIndex] * fOrthogonalEdgeLength / 2; + aBBCenter.set(eAxis, aBBCenter.get(eAxis) + aDirection.get(eAxis) * fNearestEdgeLength / 2); + aBBCenter.set(eOrthogonalAxis, aBBCenter.get(eOrthogonalAxis) + aDirection.get(eOrthogonalAxis) * fOrthogonalEdgeLength / 2); // compute the b.b. anchor point basegfx::B2IVector aNewAnchorPoint = aPieCenter; - aNewAnchorPoint[0] += floor(aBBCenter[0]); - aNewAnchorPoint[1] -= floor(aBBCenter[1]); // the Y axis on the screen points downward + aNewAnchorPoint.setX(aNewAnchorPoint.getX() + floor(aBBCenter.getX())); + aNewAnchorPoint.setY(aNewAnchorPoint.getY() - floor(aBBCenter.getY())); // the Y axis on the screen points downward // compute the translation vector for moving the label from the current // screen position to the new one diff --git a/include/basegfx/tuple/b2dtuple.hxx b/include/basegfx/tuple/b2dtuple.hxx index 059820bd78fd..d26310de9975 100644 --- a/include/basegfx/tuple/b2dtuple.hxx +++ b/include/basegfx/tuple/b2dtuple.hxx @@ -23,6 +23,7 @@ #include <sal/types.h> #include <basegfx/numeric/ftools.hxx> #include <basegfx/basegfxdllapi.h> +#include <basegfx/utils/common.hxx> namespace basegfx { @@ -39,10 +40,11 @@ namespace basegfx class SAL_WARN_UNUSED B2DTuple { protected: - double mfX; - double mfY; + double mfX; + double mfY; public: + /** Create a 2D Tuple The tuple is initialized to (0.0, 0.0) @@ -98,27 +100,21 @@ namespace basegfx mfY = fY; } - /// Array-access to 2D Tuple - const double& operator[] (int nPos) const + double get(Axis2D eAxis) { - // Here, normally one if(...) should be used. In the assumption that - // both double members can be accessed as an array a shortcut is used here. - // if(0 == nPos) return mfX; return mfY; - return *((&mfX) + nPos); + return eAxis == Axis2D::X ? getX() : getY(); } - /// Array-access to 2D Tuple - double& operator[] (int nPos) + void set(Axis2D eAxis, double fValue) { - // Here, normally one if(...) should be used. In the assumption that - // both double members can be accessed as an array a shortcut is used here. - // if(0 == nPos) return mfX; return mfY; - return *((&mfX) + nPos); + if (eAxis == Axis2D::X) + setX(fValue); + else + setY(fValue); } // comparators with tolerance - bool equalZero() const { return (this == &getEmptyTuple() || diff --git a/include/basegfx/utils/common.hxx b/include/basegfx/utils/common.hxx new file mode 100644 index 000000000000..522bfbcb2bff --- /dev/null +++ b/include/basegfx/utils/common.hxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +namespace basegfx +{ +// The axis or dimension in a 2D coordiante system +enum class Axis2D +{ + X, + Y +}; + +} // end of namespace basegfx + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svx/source/table/tablehandles.cxx b/svx/source/table/tablehandles.cxx index daaf45c8832c..2760755421c1 100644 --- a/svx/source/table/tablehandles.cxx +++ b/svx/source/table/tablehandles.cxx @@ -116,17 +116,17 @@ void TableEdgeHdl::getPolyPolygon(basegfx::B2DPolyPolygon& rVisible, basegfx::B2 if( pDrag ) { - int n = mbHorizontal ? 1 : 0; - aOffset[n] = aOffset[n] + GetValidDragOffset( *pDrag ); + basegfx::Axis2D eDragAxis = mbHorizontal ? basegfx::Axis2D::Y : basegfx::Axis2D::X; + aOffset.set(eDragAxis, aOffset.get(eDragAxis) + GetValidDragOffset( *pDrag )); } basegfx::B2DPoint aStart(aOffset), aEnd(aOffset); - int nPos = mbHorizontal ? 0 : 1; + basegfx::Axis2D eAxis = mbHorizontal ? basegfx::Axis2D::X : basegfx::Axis2D::Y; for( const TableEdge& aEdge : maEdges ) { - aStart[nPos] = aOffset[nPos] + aEdge.mnStart; - aEnd[nPos] = aOffset[nPos] + aEdge.mnEnd; + aStart.set(eAxis, aOffset.get(eAxis) + aEdge.mnStart); + aEnd.set(eAxis, aOffset.get(eAxis) + aEdge.mnEnd); basegfx::B2DPolygon aPolygon; aPolygon.append( aStart ); |