1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "Chart2ModelContact.hxx"
32 #include "ChartModelHelper.hxx"
33 #include "LegendHelper.hxx"
34 #include "CommonConverters.hxx"
35 #include "macros.hxx"
36 #include "servicenames.hxx"
37 #include "ObjectIdentifier.hxx"
38 #include "chartview/ExplicitValueProvider.hxx"
39 #include "chartview/DrawModelWrapper.hxx"
40 #include "AxisHelper.hxx"
41 #include "DiagramHelper.hxx"
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::chart2;
45 
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::rtl::OUString;
49 
50 namespace chart
51 {
52 namespace wrapper
53 {
54 
55 Chart2ModelContact::Chart2ModelContact(
56     const Reference< uno::XComponentContext > & xContext ) :
57         m_xContext( xContext ),
58         m_xChartModel( 0 ),
59         m_xChartView(0)
60 {
61 }
62 
63 Chart2ModelContact::~Chart2ModelContact()
64 {
65     this->clear();
66 }
67 
68 void Chart2ModelContact::setModel( const ::com::sun::star::uno::Reference<
69                        ::com::sun::star::frame::XModel >& xChartModel )
70 {
71     this->clear();
72     m_xChartModel = xChartModel;
73     uno::Reference< lang::XMultiServiceFactory > xTableFactory( xChartModel, uno::UNO_QUERY );
74     if( xTableFactory.is() )
75     {
76         uno::Reference< container::XNameContainer > xDashTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.DashTable" ) ), uno::UNO_QUERY );
77         uno::Reference< container::XNameContainer > xGradientTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.GradientTable" ) ), uno::UNO_QUERY );
78         uno::Reference< container::XNameContainer > xHatchTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.HatchTable" ) ), uno::UNO_QUERY );
79         uno::Reference< container::XNameContainer > xBitmapTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.BitmapTable" ) ), uno::UNO_QUERY );
80         uno::Reference< container::XNameContainer > xTransparencyGradientTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" ) ), uno::UNO_QUERY );
81         //C2U( "com.sun.star.drawing.MarkerTable" )
82         m_aTableMap[ C2U( "LineDashName" ) ] = xDashTable;
83         m_aTableMap[ C2U( "FillGradientName" ) ] = xGradientTable;
84         m_aTableMap[ C2U( "FillHatchName" ) ] = xHatchTable;
85         m_aTableMap[ C2U( "FillBitmapName" ) ] = xBitmapTable;
86         m_aTableMap[ C2U( "FillTransparenceGradientName" ) ] = xTransparencyGradientTable;
87     }
88 }
89 
90 void Chart2ModelContact::clear()
91 {
92     m_xChartModel = uno::WeakReference< frame::XModel >(0);
93     m_xChartView.clear();
94 }
95 
96 Reference< frame::XModel > Chart2ModelContact::getChartModel() const
97 {
98     return Reference< frame::XModel >( m_xChartModel.get(), uno::UNO_QUERY );
99 }
100 
101 Reference< chart2::XChartDocument > Chart2ModelContact::getChart2Document() const
102 {
103     return Reference< chart2::XChartDocument >( m_xChartModel.get(), uno::UNO_QUERY );
104 }
105 
106 Reference< chart2::XDiagram > Chart2ModelContact::getChart2Diagram() const
107 {
108     return ChartModelHelper::findDiagram( this->getChartModel() );
109 }
110 
111 uno::Reference< lang::XUnoTunnel > Chart2ModelContact::getChartView() const
112 {
113     if(!m_xChartView.is())
114     {
115         // get the chart view
116         Reference<frame::XModel> xModel(m_xChartModel);
117         uno::Reference< lang::XMultiServiceFactory > xFact( xModel, uno::UNO_QUERY );
118 	    if( xFact.is() )
119             m_xChartView = Reference< lang::XUnoTunnel >( xFact->createInstance( CHART_VIEW_SERVICE_NAME ), uno::UNO_QUERY );
120     }
121     return m_xChartView;
122 }
123 
124 ExplicitValueProvider* Chart2ModelContact::getExplicitValueProvider() const
125 {
126     getChartView();
127     if(!m_xChartView.is())
128         return 0;
129 
130     //obtain the ExplicitValueProvider from the chart view
131     ExplicitValueProvider* pProvider = reinterpret_cast<ExplicitValueProvider*>(m_xChartView->getSomething(
132         ExplicitValueProvider::getUnoTunnelId() ));
133     return pProvider;
134 }
135 
136 uno::Reference< drawing::XDrawPage > Chart2ModelContact::getDrawPage()
137 {
138     uno::Reference< drawing::XDrawPage > xResult;
139     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
140     if( pProvider )
141     {
142         xResult.set( pProvider->getDrawModelWrapper()->getMainDrawPage() );
143     }
144     return xResult;
145 }
146 
147 sal_Bool Chart2ModelContact::getExplicitValuesForAxis(
148     const Reference< XAxis > & xAxis,
149     ExplicitScaleData &  rOutExplicitScale,
150     ExplicitIncrementData & rOutExplicitIncrement )
151 {
152     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
153     if( pProvider )
154     {
155         return pProvider->getExplicitValuesForAxis(
156             xAxis, rOutExplicitScale, rOutExplicitIncrement );
157     }
158     return sal_False;
159 }
160 
161 sal_Int32 Chart2ModelContact::getExplicitNumberFormatKeyForAxis(
162             const Reference< chart2::XAxis >& xAxis )
163 {
164     Reference< chart2::XCoordinateSystem > xCooSys(
165         AxisHelper::getCoordinateSystemOfAxis(
166               xAxis, ChartModelHelper::findDiagram( m_xChartModel ) ) );
167 
168     return ExplicitValueProvider::getExplicitNumberFormatKeyForAxis( xAxis, xCooSys
169               , Reference< util::XNumberFormatsSupplier >( m_xChartModel.get(), uno::UNO_QUERY ) );
170 }
171 
172 sal_Int32 Chart2ModelContact::getExplicitNumberFormatKeyForSeries(
173             const Reference< chart2::XDataSeries >& xSeries )
174 {
175     return ExplicitValueProvider::getExplicitNumberFormatKeyForDataLabel(
176         uno::Reference< beans::XPropertySet >( xSeries, uno::UNO_QUERY ),
177         xSeries,
178         -1 /*-1 for whole series*/,
179         ChartModelHelper::findDiagram( m_xChartModel )
180         );
181 }
182 
183 //-----------------------------------------------------------------------------
184 
185 awt::Size Chart2ModelContact::GetPageSize() const
186 {
187     return ChartModelHelper::getPageSize(m_xChartModel);
188 }
189 
190 awt::Rectangle Chart2ModelContact::SubstractAxisTitleSizes( const awt::Rectangle& rPositionRect )
191 {
192     awt::Rectangle aRect = ExplicitValueProvider::substractAxisTitleSizes(
193         m_xChartModel, getChartView(), rPositionRect );
194     return aRect;
195 }
196 
197 awt::Rectangle Chart2ModelContact::GetDiagramRectangleIncludingTitle() const
198 {
199     awt::Rectangle aRect( GetDiagramRectangleIncludingAxes() );
200 
201     //add axis title sizes to the diagram size
202     aRect = ExplicitValueProvider::addAxisTitleSizes(
203         m_xChartModel, getChartView(), aRect );
204 
205     return aRect;
206 }
207 
208 awt::Rectangle Chart2ModelContact::GetDiagramRectangleIncludingAxes() const
209 {
210     awt::Rectangle aRect(0,0,0,0);
211     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartModel ) );
212 
213     if( DiagramPositioningMode_INCLUDING == DiagramHelper::getDiagramPositioningMode( xDiagram ) )
214         aRect = DiagramHelper::getDiagramRectangleFromModel(m_xChartModel);
215     else
216     {
217         ExplicitValueProvider* pProvider( getExplicitValueProvider() );
218         if( pProvider )
219             aRect = pProvider->getRectangleOfObject( C2U("PlotAreaIncludingAxes") );
220     }
221     return aRect;
222 }
223 
224 awt::Rectangle Chart2ModelContact::GetDiagramRectangleExcludingAxes() const
225 {
226     awt::Rectangle aRect(0,0,0,0);
227     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartModel ) );
228 
229     if( DiagramPositioningMode_EXCLUDING == DiagramHelper::getDiagramPositioningMode( xDiagram ) )
230         aRect = DiagramHelper::getDiagramRectangleFromModel(m_xChartModel);
231     else
232     {
233         ExplicitValueProvider* pProvider( getExplicitValueProvider() );
234         if( pProvider )
235             aRect = pProvider->getDiagramRectangleExcludingAxes();
236     }
237     return aRect;
238 }
239 
240 awt::Size Chart2ModelContact::GetLegendSize() const
241 {
242     awt::Size aSize;
243     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
244     if( pProvider )
245     {
246         uno::Reference< chart2::XLegend > xLegend( LegendHelper::getLegend( m_xChartModel ) );
247         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xLegend, m_xChartModel ) );
248         aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
249     }
250     return aSize;
251 }
252 
253 awt::Point Chart2ModelContact::GetLegendPosition() const
254 {
255     awt::Point aPoint;
256     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
257     if( pProvider )
258     {
259         uno::Reference< chart2::XLegend > xLegend( LegendHelper::getLegend( m_xChartModel ) );
260         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xLegend, m_xChartModel ) );
261         aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
262     }
263     return aPoint;
264 }
265 
266 awt::Size Chart2ModelContact::GetTitleSize( const uno::Reference<
267         ::com::sun::star::chart2::XTitle > & xTitle ) const
268 {
269     awt::Size aSize;
270     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
271     if( pProvider && xTitle.is() )
272     {
273         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xTitle, m_xChartModel ) );
274         aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
275     }
276     return aSize;
277 }
278 
279 awt::Point Chart2ModelContact::GetTitlePosition( const uno::Reference<
280         ::com::sun::star::chart2::XTitle > & xTitle ) const
281 {
282     awt::Point aPoint;
283     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
284     if( pProvider && xTitle.is() )
285     {
286         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xTitle, m_xChartModel ) );
287         aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
288     }
289     return aPoint;
290 }
291 
292 awt::Size Chart2ModelContact::GetAxisSize( const uno::Reference<
293         ::com::sun::star::chart2::XAxis > & xAxis ) const
294 {
295     awt::Size aSize;
296     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
297     if( pProvider && xAxis.is() )
298     {
299         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xAxis, m_xChartModel ) );
300         aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
301     }
302     return aSize;
303 }
304 
305 awt::Point Chart2ModelContact::GetAxisPosition( const uno::Reference<
306         ::com::sun::star::chart2::XAxis > & xAxis ) const
307 {
308     awt::Point aPoint;
309     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
310     if( pProvider && xAxis.is() )
311     {
312         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xAxis, m_xChartModel ) );
313         aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
314     }
315     return aPoint;
316 }
317 
318 } //  namespace wrapper
319 } //  namespace chart
320