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( "AnchorPosition", uno::Any(LegendPosition_CUSTOM
));
76 xObjectProp
->setPropertyValue( "Expansion", uno::Any(css::chart::ChartLegendExpansion_CUSTOM
));
77 chart2::RelativePosition aRelativePosition
;
78 chart2::RelativeSize aRelativeSize
;
79 Point aAnchor
= aObjectRect
.TopLeft();
81 aRelativePosition
.Primary
=
82 static_cast< double >( aAnchor
.X()) /
83 static_cast< double >( aPageRect
.getWidth() );
84 aRelativePosition
.Secondary
=
85 static_cast< double >( aAnchor
.Y()) /
86 static_cast< double >( aPageRect
.getHeight());
88 xObjectProp
->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition
) );
90 aRelativeSize
.Primary
=
91 static_cast< double >( aObjectRect
.getWidth()) /
92 static_cast< double >( aPageRect
.getWidth() );
93 if (aRelativeSize
.Primary
> 1.0)
94 aRelativeSize
.Primary
= 1.0;
95 aRelativeSize
.Secondary
=
96 static_cast< double >( aObjectRect
.getHeight()) /
97 static_cast< double >( aPageRect
.getHeight());
98 if (aRelativeSize
.Secondary
> 1.0)
99 aRelativeSize
.Secondary
= 1.0;
101 xObjectProp
->setPropertyValue( "RelativeSize", uno::Any(aRelativeSize
) );
103 else if(eObjectType
==OBJECTTYPE_DIAGRAM
|| eObjectType
==OBJECTTYPE_DIAGRAM_WALL
|| eObjectType
==OBJECTTYPE_DIAGRAM_FLOOR
)
105 //@todo decide whether x is primary or secondary
108 chart2::RelativePosition aRelativePosition
;
109 aRelativePosition
.Anchor
= drawing::Alignment_CENTER
;
111 Point aPos
= aObjectRect
.Center();
112 aRelativePosition
.Primary
= double(aPos
.X())/double(aPageRect
.getWidth());
113 aRelativePosition
.Secondary
= double(aPos
.Y())/double(aPageRect
.getHeight());
114 xObjectProp
->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition
) );
117 RelativeSize aRelativeSize
;
118 //the anchor points for the diagram are in the middle of the diagram
119 //and in the middle of the page
120 aRelativeSize
.Primary
= double(aObjectRect
.getWidth())/double(aPageRect
.getWidth());
121 aRelativeSize
.Secondary
= double(aObjectRect
.getHeight())/double(aPageRect
.getHeight());
122 xObjectProp
->setPropertyValue( "RelativeSize", uno::Any(aRelativeSize
) );
129 bool PositionAndSizeHelper::moveObject( const OUString
& rObjectCID
130 , const uno::Reference
< frame::XModel
>& xChartModel
131 , const awt::Rectangle
& rNewPositionAndSize
132 , const awt::Rectangle
& rPageRectangle
135 ControllerLockGuardUNO
aLockedControllers( xChartModel
);
137 awt::Rectangle
aNewPositionAndSize( rNewPositionAndSize
);
139 uno::Reference
< beans::XPropertySet
> xObjectProp
= ObjectIdentifier::getObjectPropertySet( rObjectCID
, xChartModel
);
140 ObjectType
eObjectType( ObjectIdentifier::getObjectType( rObjectCID
) );
141 if(eObjectType
==OBJECTTYPE_DIAGRAM
|| eObjectType
==OBJECTTYPE_DIAGRAM_WALL
|| eObjectType
==OBJECTTYPE_DIAGRAM_FLOOR
)
143 xObjectProp
.set( ObjectIdentifier::getDiagramForCID( rObjectCID
, xChartModel
), uno::UNO_QUERY
);
144 if(!xObjectProp
.is())
147 return moveObject( eObjectType
, xObjectProp
, aNewPositionAndSize
, rPageRectangle
);
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */