1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/chart2/LegendPosition.hpp>
23 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
24 #include <com/sun/star/chart2/RelativePosition.hpp>
25 #include <com/sun/star/chart2/RelativeSize.hpp>
27 #include <tools/gen.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/awt/Rectangle.hpp>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::chart2
;
36 bool PositionAndSizeHelper::moveObject( ObjectType eObjectType
37 , const uno::Reference
< beans::XPropertySet
>& xObjectProp
38 , const awt::Rectangle
& rNewPositionAndSize
39 , const awt::Rectangle
& rPageRectangle
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
.getWidth() == 0 || aPageRect
.getHeight() == 0)
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
.getWidth())/2.0)/double(aPageRect
.getWidth());
59 aRelativePosition
.Secondary
= (double(aPos
.Y())+double(aObjectRect
.getHeight())/2.0)/double(aPageRect
.getHeight());
60 xObjectProp
->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition
) );
62 else if( eObjectType
==OBJECTTYPE_DATA_CURVE_EQUATION
)
64 //@todo decide whether x is primary or secondary
65 chart2::RelativePosition aRelativePosition
;
66 aRelativePosition
.Anchor
= drawing::Alignment_TOP_LEFT
;
67 //the anchor point at the title object is top/middle
68 Point aPos
= aObjectRect
.TopLeft();
69 aRelativePosition
.Primary
= double(aPos
.X())/double(aPageRect
.getWidth());
70 aRelativePosition
.Secondary
= double(aPos
.Y())/double(aPageRect
.getHeight());
71 xObjectProp
->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition
) );
73 else if(eObjectType
==OBJECTTYPE_LEGEND
)
75 xObjectProp
->setPropertyValue( "Expansion", uno::Any(css::chart::ChartLegendExpansion_CUSTOM
));
76 chart2::RelativePosition aRelativePosition
;
77 chart2::RelativeSize aRelativeSize
;
78 Point aAnchor
= aObjectRect
.TopLeft();
80 aRelativePosition
.Primary
=
81 static_cast< double >( aAnchor
.X()) /
82 static_cast< double >( aPageRect
.getWidth() );
83 aRelativePosition
.Secondary
=
84 static_cast< double >( aAnchor
.Y()) /
85 static_cast< double >( aPageRect
.getHeight());
87 xObjectProp
->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition
) );
89 aRelativeSize
.Primary
=
90 static_cast< double >( aObjectRect
.getWidth()) /
91 static_cast< double >( aPageRect
.getWidth() );
92 if (aRelativeSize
.Primary
> 1.0)
93 aRelativeSize
.Primary
= 1.0;
94 aRelativeSize
.Secondary
=
95 static_cast< double >( aObjectRect
.getHeight()) /
96 static_cast< double >( aPageRect
.getHeight());
97 if (aRelativeSize
.Secondary
> 1.0)
98 aRelativeSize
.Secondary
= 1.0;
100 xObjectProp
->setPropertyValue( "RelativeSize", uno::Any(aRelativeSize
) );
102 else if(eObjectType
==OBJECTTYPE_DIAGRAM
|| eObjectType
==OBJECTTYPE_DIAGRAM_WALL
|| eObjectType
==OBJECTTYPE_DIAGRAM_FLOOR
)
104 //@todo decide whether x is primary or secondary
107 chart2::RelativePosition aRelativePosition
;
108 aRelativePosition
.Anchor
= drawing::Alignment_CENTER
;
110 Point aPos
= aObjectRect
.Center();
111 aRelativePosition
.Primary
= double(aPos
.X())/double(aPageRect
.getWidth());
112 aRelativePosition
.Secondary
= double(aPos
.Y())/double(aPageRect
.getHeight());
113 xObjectProp
->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition
) );
116 RelativeSize aRelativeSize
;
117 //the anchor points for the diagram are in the middle of the diagram
118 //and in the middle of the page
119 aRelativeSize
.Primary
= double(aObjectRect
.getWidth())/double(aPageRect
.getWidth());
120 aRelativeSize
.Secondary
= double(aObjectRect
.getHeight())/double(aPageRect
.getHeight());
121 xObjectProp
->setPropertyValue( "RelativeSize", uno::Any(aRelativeSize
) );
128 bool PositionAndSizeHelper::moveObject( const OUString
& rObjectCID
129 , const uno::Reference
< frame::XModel
>& xChartModel
130 , const awt::Rectangle
& rNewPositionAndSize
131 , const awt::Rectangle
& rPageRectangle
134 ControllerLockGuardUNO
aLockedControllers( xChartModel
);
136 awt::Rectangle
aNewPositionAndSize( rNewPositionAndSize
);
138 uno::Reference
< beans::XPropertySet
> xObjectProp
= ObjectIdentifier::getObjectPropertySet( rObjectCID
, xChartModel
);
139 ObjectType
eObjectType( ObjectIdentifier::getObjectType( rObjectCID
) );
140 if(eObjectType
==OBJECTTYPE_DIAGRAM
|| eObjectType
==OBJECTTYPE_DIAGRAM_WALL
|| eObjectType
==OBJECTTYPE_DIAGRAM_FLOOR
)
142 xObjectProp
.set( ObjectIdentifier::getDiagramForCID( rObjectCID
, xChartModel
), uno::UNO_QUERY
);
143 if(!xObjectProp
.is())
146 return moveObject( eObjectType
, xObjectProp
, aNewPositionAndSize
, rPageRectangle
);
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */