1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 
27 #include "Chart2ModelContact.hxx"
28 #include "ChartModelHelper.hxx"
29 #include "LegendHelper.hxx"
30 #include "CommonConverters.hxx"
31 #include "macros.hxx"
32 #include "servicenames.hxx"
33 #include "ObjectIdentifier.hxx"
34 #include "chartview/ExplicitValueProvider.hxx"
35 #include "chartview/DrawModelWrapper.hxx"
36 #include "AxisHelper.hxx"
37 #include "DiagramHelper.hxx"
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::chart2;
41 
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::rtl::OUString;
45 
46 namespace chart
47 {
48 namespace wrapper
49 {
50 
Chart2ModelContact(const Reference<uno::XComponentContext> & xContext)51 Chart2ModelContact::Chart2ModelContact(
52     const Reference< uno::XComponentContext > & xContext ) :
53         m_xContext( xContext ),
54         m_xChartModel( 0 ),
55         m_xChartView(0)
56 {
57 }
58 
~Chart2ModelContact()59 Chart2ModelContact::~Chart2ModelContact()
60 {
61     this->clear();
62 }
63 
setModel(const::com::sun::star::uno::Reference<::com::sun::star::frame::XModel> & xChartModel)64 void Chart2ModelContact::setModel( const ::com::sun::star::uno::Reference<
65                        ::com::sun::star::frame::XModel >& xChartModel )
66 {
67     this->clear();
68     m_xChartModel = xChartModel;
69     uno::Reference< lang::XMultiServiceFactory > xTableFactory( xChartModel, uno::UNO_QUERY );
70     if( xTableFactory.is() )
71     {
72         uno::Reference< container::XNameContainer > xDashTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.DashTable" ) ), uno::UNO_QUERY );
73         uno::Reference< container::XNameContainer > xGradientTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.GradientTable" ) ), uno::UNO_QUERY );
74         uno::Reference< container::XNameContainer > xHatchTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.HatchTable" ) ), uno::UNO_QUERY );
75         uno::Reference< container::XNameContainer > xBitmapTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.BitmapTable" ) ), uno::UNO_QUERY );
76         uno::Reference< container::XNameContainer > xTransparencyGradientTable( xTableFactory->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" ) ), uno::UNO_QUERY );
77         //C2U( "com.sun.star.drawing.MarkerTable" )
78         m_aTableMap[ C2U( "LineDashName" ) ] = xDashTable;
79         m_aTableMap[ C2U( "FillGradientName" ) ] = xGradientTable;
80         m_aTableMap[ C2U( "FillHatchName" ) ] = xHatchTable;
81         m_aTableMap[ C2U( "FillBitmapName" ) ] = xBitmapTable;
82         m_aTableMap[ C2U( "FillTransparenceGradientName" ) ] = xTransparencyGradientTable;
83     }
84 }
85 
clear()86 void Chart2ModelContact::clear()
87 {
88     m_xChartModel = uno::WeakReference< frame::XModel >(0);
89     m_xChartView.clear();
90 }
91 
getChartModel() const92 Reference< frame::XModel > Chart2ModelContact::getChartModel() const
93 {
94     return Reference< frame::XModel >( m_xChartModel.get(), uno::UNO_QUERY );
95 }
96 
getChart2Document() const97 Reference< chart2::XChartDocument > Chart2ModelContact::getChart2Document() const
98 {
99     return Reference< chart2::XChartDocument >( m_xChartModel.get(), uno::UNO_QUERY );
100 }
101 
getChart2Diagram() const102 Reference< chart2::XDiagram > Chart2ModelContact::getChart2Diagram() const
103 {
104     return ChartModelHelper::findDiagram( this->getChartModel() );
105 }
106 
getChartView() const107 uno::Reference< lang::XUnoTunnel > Chart2ModelContact::getChartView() const
108 {
109     if(!m_xChartView.is())
110     {
111         // get the chart view
112         Reference<frame::XModel> xModel(m_xChartModel);
113         uno::Reference< lang::XMultiServiceFactory > xFact( xModel, uno::UNO_QUERY );
114 	    if( xFact.is() )
115             m_xChartView = Reference< lang::XUnoTunnel >( xFact->createInstance( CHART_VIEW_SERVICE_NAME ), uno::UNO_QUERY );
116     }
117     return m_xChartView;
118 }
119 
getExplicitValueProvider() const120 ExplicitValueProvider* Chart2ModelContact::getExplicitValueProvider() const
121 {
122     getChartView();
123     if(!m_xChartView.is())
124         return 0;
125 
126     //obtain the ExplicitValueProvider from the chart view
127     ExplicitValueProvider* pProvider = reinterpret_cast<ExplicitValueProvider*>(m_xChartView->getSomething(
128         ExplicitValueProvider::getUnoTunnelId() ));
129     return pProvider;
130 }
131 
getDrawPage()132 uno::Reference< drawing::XDrawPage > Chart2ModelContact::getDrawPage()
133 {
134     uno::Reference< drawing::XDrawPage > xResult;
135     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
136     if( pProvider )
137     {
138         xResult.set( pProvider->getDrawModelWrapper()->getMainDrawPage() );
139     }
140     return xResult;
141 }
142 
getExplicitValuesForAxis(const Reference<XAxis> & xAxis,ExplicitScaleData & rOutExplicitScale,ExplicitIncrementData & rOutExplicitIncrement)143 sal_Bool Chart2ModelContact::getExplicitValuesForAxis(
144     const Reference< XAxis > & xAxis,
145     ExplicitScaleData &  rOutExplicitScale,
146     ExplicitIncrementData & rOutExplicitIncrement )
147 {
148     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
149     if( pProvider )
150     {
151         return pProvider->getExplicitValuesForAxis(
152             xAxis, rOutExplicitScale, rOutExplicitIncrement );
153     }
154     return sal_False;
155 }
156 
getExplicitNumberFormatKeyForAxis(const Reference<chart2::XAxis> & xAxis)157 sal_Int32 Chart2ModelContact::getExplicitNumberFormatKeyForAxis(
158             const Reference< chart2::XAxis >& xAxis )
159 {
160     Reference< chart2::XCoordinateSystem > xCooSys(
161         AxisHelper::getCoordinateSystemOfAxis(
162               xAxis, ChartModelHelper::findDiagram( m_xChartModel ) ) );
163 
164     return ExplicitValueProvider::getExplicitNumberFormatKeyForAxis( xAxis, xCooSys
165               , Reference< util::XNumberFormatsSupplier >( m_xChartModel.get(), uno::UNO_QUERY ) );
166 }
167 
getExplicitNumberFormatKeyForSeries(const Reference<chart2::XDataSeries> & xSeries)168 sal_Int32 Chart2ModelContact::getExplicitNumberFormatKeyForSeries(
169             const Reference< chart2::XDataSeries >& xSeries )
170 {
171     return ExplicitValueProvider::getExplicitNumberFormatKeyForDataLabel(
172         uno::Reference< beans::XPropertySet >( xSeries, uno::UNO_QUERY ),
173         xSeries,
174         -1 /*-1 for whole series*/,
175         ChartModelHelper::findDiagram( m_xChartModel )
176         );
177 }
178 
179 //-----------------------------------------------------------------------------
180 
GetPageSize() const181 awt::Size Chart2ModelContact::GetPageSize() const
182 {
183     return ChartModelHelper::getPageSize(m_xChartModel);
184 }
185 
SubstractAxisTitleSizes(const awt::Rectangle & rPositionRect)186 awt::Rectangle Chart2ModelContact::SubstractAxisTitleSizes( const awt::Rectangle& rPositionRect )
187 {
188     awt::Rectangle aRect = ExplicitValueProvider::substractAxisTitleSizes(
189         m_xChartModel, getChartView(), rPositionRect );
190     return aRect;
191 }
192 
GetDiagramRectangleIncludingTitle() const193 awt::Rectangle Chart2ModelContact::GetDiagramRectangleIncludingTitle() const
194 {
195     awt::Rectangle aRect( GetDiagramRectangleIncludingAxes() );
196 
197     //add axis title sizes to the diagram size
198     aRect = ExplicitValueProvider::addAxisTitleSizes(
199         m_xChartModel, getChartView(), aRect );
200 
201     return aRect;
202 }
203 
GetDiagramRectangleIncludingAxes() const204 awt::Rectangle Chart2ModelContact::GetDiagramRectangleIncludingAxes() const
205 {
206     awt::Rectangle aRect(0,0,0,0);
207     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartModel ) );
208 
209     if( DiagramPositioningMode_INCLUDING == DiagramHelper::getDiagramPositioningMode( xDiagram ) )
210         aRect = DiagramHelper::getDiagramRectangleFromModel(m_xChartModel);
211     else
212     {
213         ExplicitValueProvider* pProvider( getExplicitValueProvider() );
214         if( pProvider )
215             aRect = pProvider->getRectangleOfObject( C2U("PlotAreaIncludingAxes") );
216     }
217     return aRect;
218 }
219 
GetDiagramRectangleExcludingAxes() const220 awt::Rectangle Chart2ModelContact::GetDiagramRectangleExcludingAxes() const
221 {
222     awt::Rectangle aRect(0,0,0,0);
223     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( m_xChartModel ) );
224 
225     if( DiagramPositioningMode_EXCLUDING == DiagramHelper::getDiagramPositioningMode( xDiagram ) )
226         aRect = DiagramHelper::getDiagramRectangleFromModel(m_xChartModel);
227     else
228     {
229         ExplicitValueProvider* pProvider( getExplicitValueProvider() );
230         if( pProvider )
231             aRect = pProvider->getDiagramRectangleExcludingAxes();
232     }
233     return aRect;
234 }
235 
GetLegendSize() const236 awt::Size Chart2ModelContact::GetLegendSize() const
237 {
238     awt::Size aSize;
239     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
240     if( pProvider )
241     {
242         uno::Reference< chart2::XLegend > xLegend( LegendHelper::getLegend( m_xChartModel ) );
243         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xLegend, m_xChartModel ) );
244         aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
245     }
246     return aSize;
247 }
248 
GetLegendPosition() const249 awt::Point Chart2ModelContact::GetLegendPosition() const
250 {
251     awt::Point aPoint;
252     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
253     if( pProvider )
254     {
255         uno::Reference< chart2::XLegend > xLegend( LegendHelper::getLegend( m_xChartModel ) );
256         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xLegend, m_xChartModel ) );
257         aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
258     }
259     return aPoint;
260 }
261 
GetTitleSize(const uno::Reference<::com::sun::star::chart2::XTitle> & xTitle) const262 awt::Size Chart2ModelContact::GetTitleSize( const uno::Reference<
263         ::com::sun::star::chart2::XTitle > & xTitle ) const
264 {
265     awt::Size aSize;
266     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
267     if( pProvider && xTitle.is() )
268     {
269         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xTitle, m_xChartModel ) );
270         aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
271     }
272     return aSize;
273 }
274 
GetTitlePosition(const uno::Reference<::com::sun::star::chart2::XTitle> & xTitle) const275 awt::Point Chart2ModelContact::GetTitlePosition( const uno::Reference<
276         ::com::sun::star::chart2::XTitle > & xTitle ) const
277 {
278     awt::Point aPoint;
279     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
280     if( pProvider && xTitle.is() )
281     {
282         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xTitle, m_xChartModel ) );
283         aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
284     }
285     return aPoint;
286 }
287 
GetAxisSize(const uno::Reference<::com::sun::star::chart2::XAxis> & xAxis) const288 awt::Size Chart2ModelContact::GetAxisSize( const uno::Reference<
289         ::com::sun::star::chart2::XAxis > & xAxis ) const
290 {
291     awt::Size aSize;
292     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
293     if( pProvider && xAxis.is() )
294     {
295         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xAxis, m_xChartModel ) );
296         aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
297     }
298     return aSize;
299 }
300 
GetAxisPosition(const uno::Reference<::com::sun::star::chart2::XAxis> & xAxis) const301 awt::Point Chart2ModelContact::GetAxisPosition( const uno::Reference<
302         ::com::sun::star::chart2::XAxis > & xAxis ) const
303 {
304     awt::Point aPoint;
305     ExplicitValueProvider* pProvider( getExplicitValueProvider() );
306     if( pProvider && xAxis.is() )
307     {
308         rtl::OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xAxis, m_xChartModel ) );
309         aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
310     }
311     return aPoint;
312 }
313 
314 } //  namespace wrapper
315 } //  namespace chart
316