tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / chart2 / source / controller / main / PositionAndSizeHelper.cxx
blobdbb5368686ae6bfa06c0ee5168440de7fe977c59
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 <PositionAndSizeHelper.hxx>
21 #include <ControllerLockGuard.hxx>
22 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
23 #include <com/sun/star/chart2/RelativePosition.hpp>
24 #include <com/sun/star/chart2/RelativeSize.hpp>
25 #include <tools/gen.hxx>
26 #include <com/sun/star/awt/Rectangle.hpp>
27 #include <ChartModel.hxx>
28 #include <Diagram.hxx>
30 namespace chart
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::chart2;
35 bool PositionAndSizeHelper::moveObject( ObjectType eObjectType
36 , const uno::Reference< beans::XPropertySet >& xObjectProp
37 , const awt::Rectangle& rNewPositionAndSize
38 , const awt::Rectangle& rOldPositionAndSize
39 , const awt::Rectangle& rPageRectangle
42 if(!xObjectProp.is())
43 return false;
44 tools::Rectangle aObjectRect( Point(rNewPositionAndSize.X,rNewPositionAndSize.Y), Size(rNewPositionAndSize.Width,rNewPositionAndSize.Height) );
45 tools::Rectangle aPageRect( Point(rPageRectangle.X,rPageRectangle.Y), Size(rPageRectangle.Width,rPageRectangle.Height) );
47 // every following branch divides by width and height
48 if (aPageRect.getOpenWidth() == 0 || aPageRect.getOpenHeight() == 0)
49 return false;
51 if( eObjectType==OBJECTTYPE_TITLE )
53 //@todo decide whether x is primary or secondary
54 chart2::RelativePosition aRelativePosition;
55 aRelativePosition.Anchor = drawing::Alignment_CENTER;
56 //the anchor point at the title object is top/middle
57 Point aPos = aObjectRect.TopLeft();
58 aRelativePosition.Primary = (double(aPos.X())+double(aObjectRect.getOpenWidth())/2.0)/double(aPageRect.getOpenWidth());
59 aRelativePosition.Secondary = (double(aPos.Y())+double(aObjectRect.getOpenHeight())/2.0)/double(aPageRect.getOpenHeight());
60 xObjectProp->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
62 else if( eObjectType == OBJECTTYPE_DATA_LABEL )
64 RelativePosition aAbsolutePosition;
65 RelativePosition aCustomLabelPosition;
66 aAbsolutePosition.Primary = double(rOldPositionAndSize.X) / double(aPageRect.getOpenWidth());
67 aAbsolutePosition.Secondary = double(rOldPositionAndSize.Y) / double(aPageRect.getOpenHeight());
69 if( xObjectProp->getPropertyValue(u"CustomLabelPosition"_ustr) >>= aCustomLabelPosition )
71 aAbsolutePosition.Primary -= aCustomLabelPosition.Primary;
72 aAbsolutePosition.Secondary -= aCustomLabelPosition.Secondary;
75 //the anchor point at the data label object is top/left
76 Point aPos = aObjectRect.TopLeft();
77 double fRotation = 0.0;
78 xObjectProp->getPropertyValue(u"TextRotation"_ustr) >>= fRotation;
79 if( fRotation == 90.0 )
80 aPos = aObjectRect.BottomLeft();
81 else if( fRotation == 270.0 )
82 aPos = aObjectRect.TopRight();
84 aCustomLabelPosition.Primary = double(aPos.X()) / double(aPageRect.getOpenWidth()) - aAbsolutePosition.Primary;
85 aCustomLabelPosition.Secondary = double(aPos.Y()) / double(aPageRect.getOpenHeight()) - aAbsolutePosition.Secondary;
86 xObjectProp->setPropertyValue(u"CustomLabelPosition"_ustr, uno::Any(aCustomLabelPosition));
88 else if( eObjectType==OBJECTTYPE_DATA_CURVE_EQUATION )
90 //@todo decide whether x is primary or secondary
91 chart2::RelativePosition aRelativePosition;
92 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
93 //the anchor point at the title object is top/middle
94 Point aPos = aObjectRect.TopLeft();
95 aRelativePosition.Primary = double(aPos.X())/double(aPageRect.getOpenWidth());
96 aRelativePosition.Secondary = double(aPos.Y())/double(aPageRect.getOpenHeight());
97 xObjectProp->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
99 else if(eObjectType==OBJECTTYPE_LEGEND)
101 xObjectProp->setPropertyValue( u"Expansion"_ustr, uno::Any(css::chart::ChartLegendExpansion_CUSTOM));
102 chart2::RelativePosition aRelativePosition;
103 chart2::RelativeSize aRelativeSize;
104 Point aAnchor = aObjectRect.TopLeft();
106 aRelativePosition.Primary =
107 static_cast< double >( aAnchor.X()) /
108 static_cast< double >( aPageRect.getOpenWidth() );
109 aRelativePosition.Secondary =
110 static_cast< double >( aAnchor.Y()) /
111 static_cast< double >( aPageRect.getOpenHeight());
113 xObjectProp->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
115 aRelativeSize.Primary =
116 static_cast< double >( aObjectRect.getOpenWidth()) /
117 static_cast< double >( aPageRect.getOpenWidth() );
118 if (aRelativeSize.Primary > 1.0)
119 aRelativeSize.Primary = 1.0;
120 aRelativeSize.Secondary =
121 static_cast< double >( aObjectRect.getOpenHeight()) /
122 static_cast< double >( aPageRect.getOpenHeight());
123 if (aRelativeSize.Secondary > 1.0)
124 aRelativeSize.Secondary = 1.0;
126 xObjectProp->setPropertyValue( u"RelativeSize"_ustr, uno::Any(aRelativeSize) );
128 else if(eObjectType==OBJECTTYPE_DIAGRAM || eObjectType==OBJECTTYPE_DIAGRAM_WALL || eObjectType==OBJECTTYPE_DIAGRAM_FLOOR)
130 //@todo decide whether x is primary or secondary
132 //set position:
133 chart2::RelativePosition aRelativePosition;
134 aRelativePosition.Anchor = drawing::Alignment_CENTER;
136 Point aPos = aObjectRect.Center();
137 aRelativePosition.Primary = double(aPos.X())/double(aPageRect.getOpenWidth());
138 aRelativePosition.Secondary = double(aPos.Y())/double(aPageRect.getOpenHeight());
139 xObjectProp->setPropertyValue( u"RelativePosition"_ustr, uno::Any(aRelativePosition) );
141 //set size:
142 RelativeSize aRelativeSize;
143 //the anchor points for the diagram are in the middle of the diagram
144 //and in the middle of the page
145 aRelativeSize.Primary = double(aObjectRect.getOpenWidth())/double(aPageRect.getOpenWidth());
146 aRelativeSize.Secondary = double(aObjectRect.getOpenHeight())/double(aPageRect.getOpenHeight());
147 xObjectProp->setPropertyValue( u"RelativeSize"_ustr, uno::Any(aRelativeSize) );
149 else
150 return false;
151 return true;
154 bool PositionAndSizeHelper::moveObject( std::u16string_view rObjectCID
155 , const rtl::Reference<::chart::ChartModel>& xChartModel
156 , const awt::Rectangle& rNewPositionAndSize
157 , const awt::Rectangle& rOldPositionAndSize
158 , const awt::Rectangle& rPageRectangle
161 ControllerLockGuardUNO aLockedControllers( xChartModel );
163 awt::Rectangle aNewPositionAndSize( rNewPositionAndSize );
165 uno::Reference< beans::XPropertySet > xObjectProp = ObjectIdentifier::getObjectPropertySet( rObjectCID, xChartModel );
166 ObjectType eObjectType( ObjectIdentifier::getObjectType( rObjectCID ) );
167 if(eObjectType==OBJECTTYPE_DIAGRAM || eObjectType==OBJECTTYPE_DIAGRAM_WALL || eObjectType==OBJECTTYPE_DIAGRAM_FLOOR)
169 xObjectProp = ObjectIdentifier::getDiagramForCID( rObjectCID, xChartModel );
170 if(!xObjectProp.is())
171 return false;
173 return moveObject( eObjectType, xObjectProp, aNewPositionAndSize, rOldPositionAndSize, rPageRectangle );
176 } //namespace chart
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */