Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / main / PositionAndSizeHelper.cxx
blob7cc51f8e478a1edae755f721faefe8c59064636e
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 <ChartModelHelper.hxx>
22 #include <ControllerLockGuard.hxx>
23 #include <com/sun/star/chart2/LegendPosition.hpp>
24 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
25 #include <com/sun/star/chart2/RelativePosition.hpp>
26 #include <com/sun/star/chart2/RelativeSize.hpp>
27 #include <chartview/ExplicitValueProvider.hxx>
29 #include <tools/gen.hxx>
30 #include <com/sun/star/beans/XPropertySet.hpp>
32 namespace chart
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::chart2;
37 bool PositionAndSizeHelper::moveObject( ObjectType eObjectType
38 , const uno::Reference< beans::XPropertySet >& xObjectProp
39 , const awt::Rectangle& rNewPositionAndSize
40 , const awt::Rectangle& rPageRectangle
43 if(!xObjectProp.is())
44 return false;
45 tools::Rectangle aObjectRect( Point(rNewPositionAndSize.X,rNewPositionAndSize.Y), Size(rNewPositionAndSize.Width,rNewPositionAndSize.Height) );
46 tools::Rectangle aPageRect( Point(rPageRectangle.X,rPageRectangle.Y), Size(rPageRectangle.Width,rPageRectangle.Height) );
48 if( eObjectType==OBJECTTYPE_TITLE )
50 //@todo decide whether x is primary or secondary
51 chart2::RelativePosition aRelativePosition;
52 aRelativePosition.Anchor = drawing::Alignment_CENTER;
53 //the anchor point at the title object is top/middle
54 Point aPos = aObjectRect.TopLeft();
55 aRelativePosition.Primary = (double(aPos.X())+double(aObjectRect.getWidth())/2.0)/double(aPageRect.getWidth());
56 aRelativePosition.Secondary = (double(aPos.Y())+double(aObjectRect.getHeight())/2.0)/double(aPageRect.getHeight());
57 xObjectProp->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
59 else if( eObjectType==OBJECTTYPE_DATA_CURVE_EQUATION )
61 //@todo decide whether x is primary or secondary
62 chart2::RelativePosition aRelativePosition;
63 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
64 //the anchor point at the title object is top/middle
65 Point aPos = aObjectRect.TopLeft();
66 aRelativePosition.Primary = double(aPos.X())/double(aPageRect.getWidth());
67 aRelativePosition.Secondary = double(aPos.Y())/double(aPageRect.getHeight());
68 xObjectProp->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
70 else if(eObjectType==OBJECTTYPE_LEGEND)
72 xObjectProp->setPropertyValue( "AnchorPosition", uno::Any(LegendPosition(LegendPosition_CUSTOM)));
73 xObjectProp->setPropertyValue( "Expansion", uno::Any(css::chart::ChartLegendExpansion_CUSTOM));
74 chart2::RelativePosition aRelativePosition;
75 chart2::RelativeSize aRelativeSize;
76 Point aAnchor = aObjectRect.TopLeft();
78 aRelativePosition.Primary =
79 static_cast< double >( aAnchor.X()) /
80 static_cast< double >( aPageRect.getWidth() );
81 aRelativePosition.Secondary =
82 static_cast< double >( aAnchor.Y()) /
83 static_cast< double >( aPageRect.getHeight());
85 xObjectProp->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
87 aRelativeSize.Primary =
88 static_cast< double >( aObjectRect.getWidth()) /
89 static_cast< double >( aPageRect.getWidth() );
90 if (aRelativeSize.Primary > 1.0)
91 aRelativeSize.Primary = 1.0;
92 aRelativeSize.Secondary =
93 static_cast< double >( aObjectRect.getHeight()) /
94 static_cast< double >( aPageRect.getHeight());
95 if (aRelativeSize.Secondary > 1.0)
96 aRelativeSize.Secondary = 1.0;
98 xObjectProp->setPropertyValue( "RelativeSize", uno::Any(aRelativeSize) );
100 else if(eObjectType==OBJECTTYPE_DIAGRAM || eObjectType==OBJECTTYPE_DIAGRAM_WALL || eObjectType==OBJECTTYPE_DIAGRAM_FLOOR)
102 //@todo decide whether x is primary or secondary
104 //set position:
105 chart2::RelativePosition aRelativePosition;
106 aRelativePosition.Anchor = drawing::Alignment_CENTER;
108 Point aPos = aObjectRect.Center();
109 aRelativePosition.Primary = double(aPos.X())/double(aPageRect.getWidth());
110 aRelativePosition.Secondary = double(aPos.Y())/double(aPageRect.getHeight());
111 xObjectProp->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
113 //set size:
114 RelativeSize aRelativeSize;
115 //the anchor points for the diagram are in the middle of the diagram
116 //and in the middle of the page
117 aRelativeSize.Primary = double(aObjectRect.getWidth())/double(aPageRect.getWidth());
118 aRelativeSize.Secondary = double(aObjectRect.getHeight())/double(aPageRect.getHeight());
119 xObjectProp->setPropertyValue( "RelativeSize", uno::Any(aRelativeSize) );
121 else
122 return false;
123 return true;
126 bool PositionAndSizeHelper::moveObject( const OUString& rObjectCID
127 , const uno::Reference< frame::XModel >& xChartModel
128 , const awt::Rectangle& rNewPositionAndSize
129 , const awt::Rectangle& rPageRectangle
132 ControllerLockGuardUNO aLockedControllers( xChartModel );
134 awt::Rectangle aNewPositionAndSize( rNewPositionAndSize );
136 uno::Reference< beans::XPropertySet > xObjectProp = ObjectIdentifier::getObjectPropertySet( rObjectCID, xChartModel );
137 ObjectType eObjectType( ObjectIdentifier::getObjectType( rObjectCID ) );
138 if(eObjectType==OBJECTTYPE_DIAGRAM || eObjectType==OBJECTTYPE_DIAGRAM_WALL || eObjectType==OBJECTTYPE_DIAGRAM_FLOOR)
140 xObjectProp.set( ObjectIdentifier::getDiagramForCID( rObjectCID, xChartModel ), uno::UNO_QUERY );
141 if(!xObjectProp.is())
142 return false;
144 return moveObject( eObjectType, xObjectProp, aNewPositionAndSize, rPageRectangle );
147 } //namespace chart
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */