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"
22 #include "ChartModelHelper.hxx"
23 #include "ControllerLockGuard.hxx"
24 #include <com/sun/star/chart2/LegendPosition.hpp>
25 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
26 #include <com/sun/star/chart2/RelativePosition.hpp>
27 #include <com/sun/star/chart2/RelativeSize.hpp>
28 #include "chartview/ExplicitValueProvider.hxx"
30 #include <tools/gen.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::chart2
;
38 bool PositionAndSizeHelper::moveObject( ObjectType eObjectType
39 , const uno::Reference
< beans::XPropertySet
>& xObjectProp
40 , const awt::Rectangle
& rNewPositionAndSize
41 , const awt::Rectangle
& rPageRectangle
46 Rectangle
aObjectRect( Point(rNewPositionAndSize
.X
,rNewPositionAndSize
.Y
), Size(rNewPositionAndSize
.Width
,rNewPositionAndSize
.Height
) );
47 Rectangle
aPageRect( Point(rPageRectangle
.X
,rPageRectangle
.Y
), Size(rPageRectangle
.Width
,rPageRectangle
.Height
) );
49 if( OBJECTTYPE_TITLE
==eObjectType
)
51 //@todo decide whether x is primary or secondary
52 chart2::RelativePosition aRelativePosition
;
53 aRelativePosition
.Anchor
= drawing::Alignment_CENTER
;
54 //the anchor point at the title object is top/middle
55 Point aPos
= aObjectRect
.TopLeft();
56 aRelativePosition
.Primary
= (double(aPos
.X())+double(aObjectRect
.getWidth())/2.0)/double(aPageRect
.getWidth());
57 aRelativePosition
.Secondary
= (double(aPos
.Y())+double(aObjectRect
.getHeight())/2.0)/double(aPageRect
.getHeight());
58 xObjectProp
->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition
) );
60 else if( OBJECTTYPE_DATA_CURVE_EQUATION
==eObjectType
)
62 //@todo decide whether x is primary or secondary
63 chart2::RelativePosition aRelativePosition
;
64 aRelativePosition
.Anchor
= drawing::Alignment_TOP_LEFT
;
65 //the anchor point at the title object is top/middle
66 Point aPos
= aObjectRect
.TopLeft();
67 aRelativePosition
.Primary
= double(aPos
.X())/double(aPageRect
.getWidth());
68 aRelativePosition
.Secondary
= double(aPos
.Y())/double(aPageRect
.getHeight());
69 xObjectProp
->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition
) );
71 else if(OBJECTTYPE_LEGEND
==eObjectType
)
73 xObjectProp
->setPropertyValue( "AnchorPosition", uno::makeAny(LegendPosition(LegendPosition_CUSTOM
)));
74 xObjectProp
->setPropertyValue( "Expansion", uno::makeAny(::com::sun::star::chart::ChartLegendExpansion_CUSTOM
));
75 chart2::RelativePosition aRelativePosition
;
76 chart2::RelativeSize aRelativeSize
;
77 Point aAnchor
= aObjectRect
.TopLeft();
79 aRelativePosition
.Primary
=
80 static_cast< double >( aAnchor
.X()) /
81 static_cast< double >( aPageRect
.getWidth() );
82 aRelativePosition
.Secondary
=
83 static_cast< double >( aAnchor
.Y()) /
84 static_cast< double >( aPageRect
.getHeight());
86 xObjectProp
->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition
) );
88 aRelativeSize
.Primary
=
89 static_cast< double >( aObjectRect
.getWidth()) /
90 static_cast< double >( aPageRect
.getWidth() );
91 if (aRelativeSize
.Primary
> 1.0)
92 aRelativeSize
.Primary
= 1.0;
93 aRelativeSize
.Secondary
=
94 static_cast< double >( aObjectRect
.getHeight()) /
95 static_cast< double >( aPageRect
.getHeight());
96 if (aRelativeSize
.Secondary
> 1.0)
97 aRelativeSize
.Secondary
= 1.0;
99 xObjectProp
->setPropertyValue( "RelativeSize", uno::makeAny(aRelativeSize
) );
101 else if(OBJECTTYPE_DIAGRAM
==eObjectType
|| OBJECTTYPE_DIAGRAM_WALL
==eObjectType
|| OBJECTTYPE_DIAGRAM_FLOOR
==eObjectType
)
103 //@todo decide whether x is primary or secondary
106 chart2::RelativePosition aRelativePosition
;
107 aRelativePosition
.Anchor
= drawing::Alignment_CENTER
;
109 Point aPos
= aObjectRect
.Center();
110 aRelativePosition
.Primary
= double(aPos
.X())/double(aPageRect
.getWidth());
111 aRelativePosition
.Secondary
= double(aPos
.Y())/double(aPageRect
.getHeight());
112 xObjectProp
->setPropertyValue( "RelativePosition", uno::makeAny(aRelativePosition
) );
115 RelativeSize aRelativeSize
;
116 //the anchor points for the diagram are in the middle of the diagram
117 //and in the middle of the page
118 aRelativeSize
.Primary
= double(aObjectRect
.getWidth())/double(aPageRect
.getWidth());
119 aRelativeSize
.Secondary
= double(aObjectRect
.getHeight())/double(aPageRect
.getHeight());
120 xObjectProp
->setPropertyValue( "RelativeSize", uno::makeAny(aRelativeSize
) );
127 bool PositionAndSizeHelper::moveObject( const OUString
& rObjectCID
128 , const uno::Reference
< frame::XModel
>& xChartModel
129 , const awt::Rectangle
& rNewPositionAndSize
130 , const awt::Rectangle
& rPageRectangle
133 ControllerLockGuardUNO
aLockedControllers( xChartModel
);
135 awt::Rectangle
aNewPositionAndSize( rNewPositionAndSize
);
137 uno::Reference
< beans::XPropertySet
> xObjectProp
= ObjectIdentifier::getObjectPropertySet( rObjectCID
, xChartModel
);
138 ObjectType
eObjectType( ObjectIdentifier::getObjectType( rObjectCID
) );
139 if(OBJECTTYPE_DIAGRAM
==eObjectType
|| OBJECTTYPE_DIAGRAM_WALL
==eObjectType
|| OBJECTTYPE_DIAGRAM_FLOOR
==eObjectType
)
141 xObjectProp
= uno::Reference
< beans::XPropertySet
>( ObjectIdentifier::getDiagramForCID( rObjectCID
, xChartModel
), uno::UNO_QUERY
);
142 if(!xObjectProp
.is())
145 return moveObject( eObjectType
, xObjectProp
, aNewPositionAndSize
, rPageRectangle
);
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */