cid#1640468 Dereference after null check
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / Chart2ModelContact.cxx
blob771ca721b781e7456abe23e8477aa1ab9359917b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "Chart2ModelContact.hxx"
21 #include <ChartModelHelper.hxx>
22 #include <Legend.hxx>
23 #include <LegendHelper.hxx>
24 #include <CommonConverters.hxx>
25 #include <servicenames.hxx>
26 #include <ObjectIdentifier.hxx>
27 #include <chartview/ExplicitValueProvider.hxx>
28 #include <chartview/DrawModelWrapper.hxx>
29 #include <Axis.hxx>
30 #include <AxisHelper.hxx>
31 #include <ChartView.hxx>
32 #include <DiagramHelper.hxx>
33 #include <BaseCoordinateSystem.hxx>
35 #include <ChartModel.hxx>
37 #include <com/sun/star/chart2/XDataSeries.hpp>
38 #include <comphelper/diagnose_ex.hxx>
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::chart2;
43 using ::com::sun::star::uno::Reference;
45 namespace chart::wrapper
48 Chart2ModelContact::Chart2ModelContact(
49 const Reference< uno::XComponentContext > & xContext ) :
50 m_xContext( xContext ),
51 m_xChartModel( nullptr )
55 Chart2ModelContact::~Chart2ModelContact()
57 clear();
60 void Chart2ModelContact::setDocumentModel( ChartModel* pChartModel )
62 clear();
63 m_xChartModel = pChartModel;
64 if( !pChartModel )
65 return;
67 m_aTableMap[u"LineDashName"_ustr].set(pChartModel->createInstance(u"com.sun.star.drawing.DashTable"_ustr), uno::UNO_QUERY);
68 m_aTableMap[u"FillGradientName"_ustr].set(pChartModel->createInstance(u"com.sun.star.drawing.GradientTable"_ustr), uno::UNO_QUERY);
69 m_aTableMap[u"FillHatchName"_ustr].set(pChartModel->createInstance(u"com.sun.star.drawing.HatchTable"_ustr), uno::UNO_QUERY);
70 m_aTableMap[u"FillBitmapName"_ustr].set(pChartModel->createInstance(u"com.sun.star.drawing.BitmapTable"_ustr), uno::UNO_QUERY);
71 m_aTableMap[u"FillTransparenceGradientName"_ustr].set(pChartModel->createInstance(u"com.sun.star.drawing.TransparencyGradientTable"_ustr), uno::UNO_QUERY);
74 void Chart2ModelContact::clear()
76 m_xChartModel.clear();
77 m_xChartView.clear();
80 rtl::Reference< ChartModel > Chart2ModelContact::getDocumentModel() const
82 return m_xChartModel;
85 rtl::Reference< ::chart::Diagram > Chart2ModelContact::getDiagram() const
87 try
89 rtl::Reference<ChartModel> xChartModel = getDocumentModel();
90 if( xChartModel)
91 return xChartModel->getFirstChartDiagram();
93 catch( const uno::Exception & )
95 DBG_UNHANDLED_EXCEPTION("chart2");
97 return nullptr;
100 rtl::Reference< ::chart::ChartView > const & Chart2ModelContact::getChartView() const
102 if(!m_xChartView.is())
104 // get the chart view
105 rtl::Reference<ChartModel> xChartModel( m_xChartModel );
106 if( xChartModel )
108 auto xInstance = xChartModel->createInstance( CHART_VIEW_SERVICE_NAME );
109 auto pChartView = dynamic_cast<ChartView*>(xInstance.get());
110 assert(!xInstance || pChartView);
111 m_xChartView = pChartView;
114 return m_xChartView;
117 ExplicitValueProvider* Chart2ModelContact::getExplicitValueProvider() const
119 getChartView();
121 //obtain the ExplicitValueProvider from the chart view
122 return m_xChartView.get();
125 rtl::Reference<SvxDrawPage> Chart2ModelContact::getDrawPage() const
127 rtl::Reference<SvxDrawPage> xResult;
128 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
129 if( pProvider )
131 xResult = pProvider->getDrawModelWrapper()->getMainDrawPage();
133 return xResult;
136 void Chart2ModelContact::getExplicitValuesForAxis(
137 const rtl::Reference< Axis > & xAxis,
138 ExplicitScaleData & rOutExplicitScale,
139 ExplicitIncrementData & rOutExplicitIncrement )
141 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
142 if( pProvider )
144 pProvider->getExplicitValuesForAxis(
145 xAxis, rOutExplicitScale, rOutExplicitIncrement );
149 sal_Int32 Chart2ModelContact::getExplicitNumberFormatKeyForAxis(
150 const rtl::Reference< ::chart::Axis >& xAxis )
152 rtl::Reference< BaseCoordinateSystem > xCooSys(
153 AxisHelper::getCoordinateSystemOfAxis(
154 xAxis, m_xChartModel.get()->getFirstChartDiagram() ) );
156 return ExplicitValueProvider::getExplicitNumberFormatKeyForAxis( xAxis, xCooSys
157 , m_xChartModel.get() );
160 sal_Int32 Chart2ModelContact::getExplicitNumberFormatKeyForSeries(
161 const Reference< chart2::XDataSeries >& xSeries )
163 return ExplicitValueProvider::getExplicitNumberFormatKeyForDataLabel(
164 uno::Reference< beans::XPropertySet >( xSeries, uno::UNO_QUERY ));
167 awt::Size Chart2ModelContact::GetPageSize() const
169 return ChartModelHelper::getPageSize(m_xChartModel.get());
172 awt::Rectangle Chart2ModelContact::SubstractAxisTitleSizes( const awt::Rectangle& rPositionRect )
174 awt::Rectangle aRect = ExplicitValueProvider::AddSubtractAxisTitleSizes(
175 *m_xChartModel.get(), getChartView().get(), rPositionRect, true );
176 return aRect;
179 awt::Rectangle Chart2ModelContact::GetDiagramRectangleIncludingTitle() const
181 awt::Rectangle aRect( GetDiagramRectangleIncludingAxes() );
183 //add axis title sizes to the diagram size
184 aRect = ExplicitValueProvider::AddSubtractAxisTitleSizes(
185 *m_xChartModel.get(), getChartView().get(), aRect, false );
187 return aRect;
190 awt::Rectangle Chart2ModelContact::GetDiagramRectangleIncludingAxes() const
192 awt::Rectangle aRect(0,0,0,0);
193 rtl::Reference< Diagram > xDiagram = m_xChartModel.get()->getFirstChartDiagram();
195 if( xDiagram && xDiagram->getDiagramPositioningMode() == DiagramPositioningMode::Including )
196 aRect = DiagramHelper::getDiagramRectangleFromModel(m_xChartModel.get());
197 else
199 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
200 if( pProvider )
201 aRect = pProvider->getRectangleOfObject(u"PlotAreaIncludingAxes"_ustr);
203 return aRect;
206 awt::Rectangle Chart2ModelContact::GetDiagramRectangleExcludingAxes() const
208 awt::Rectangle aRect(0,0,0,0);
209 rtl::Reference< Diagram > xDiagram = m_xChartModel.get()->getFirstChartDiagram();
211 if( xDiagram && xDiagram->getDiagramPositioningMode() == DiagramPositioningMode::Excluding )
212 aRect = DiagramHelper::getDiagramRectangleFromModel(m_xChartModel.get());
213 else
215 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
216 if( pProvider )
217 aRect = pProvider->getDiagramRectangleExcludingAxes();
219 return aRect;
222 awt::Size Chart2ModelContact::GetLegendSize() const
224 awt::Size aSize;
225 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
226 if( pProvider )
228 rtl::Reference< Legend > xLegend = LegendHelper::getLegend( *m_xChartModel.get() );
229 OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xLegend, m_xChartModel ) );
230 aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
232 return aSize;
235 awt::Point Chart2ModelContact::GetLegendPosition() const
237 awt::Point aPoint;
238 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
239 if( pProvider )
241 rtl::Reference< Legend > xLegend = LegendHelper::getLegend( *m_xChartModel.get() );
242 OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xLegend, m_xChartModel ) );
243 aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
245 return aPoint;
248 awt::Size Chart2ModelContact::GetTitleSize( const uno::Reference< css::chart2::XTitle > & xTitle ) const
250 awt::Size aSize;
251 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
252 if( pProvider && xTitle.is() )
254 OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xTitle, m_xChartModel ) );
255 aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
257 return aSize;
260 awt::Point Chart2ModelContact::GetTitlePosition( const uno::Reference< css::chart2::XTitle > & xTitle ) const
262 awt::Point aPoint;
263 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
264 if( pProvider && xTitle.is() )
266 OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xTitle, m_xChartModel.get() ) );
267 aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
269 return aPoint;
272 awt::Size Chart2ModelContact::GetAxisSize( const uno::Reference< css::chart2::XAxis > & xAxis ) const
274 awt::Size aSize;
275 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
276 if( pProvider && xAxis.is() )
278 OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xAxis, m_xChartModel.get() ) );
279 aSize = ToSize( pProvider->getRectangleOfObject( aCID ) );
281 return aSize;
284 awt::Point Chart2ModelContact::GetAxisPosition( const uno::Reference< css::chart2::XAxis > & xAxis ) const
286 awt::Point aPoint;
287 ExplicitValueProvider* pProvider( getExplicitValueProvider() );
288 if( pProvider && xAxis.is() )
290 OUString aCID( ObjectIdentifier::createClassifiedIdentifierForObject( xAxis, m_xChartModel.get() ) );
291 aPoint = ToPoint( pProvider->getRectangleOfObject( aCID ) );
293 return aPoint;
296 } // namespace chart::wrapper
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */