Adjust includes
[LibreOffice.git] / svx / source / svdraw / charthelper.cxx
blob255c0c03832bf1b18e7c759ab51d1f570383ac5a
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 <svx/charthelper.hxx>
21 #include <tools/globname.hxx>
22 #include <comphelper/classids.hxx>
23 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
24 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
25 #include <com/sun/star/embed/XEmbeddedObject.hpp>
26 #include <com/sun/star/lang/XUnoTunnel.hpp>
27 #include <com/sun/star/util/XUpdatable2.hpp>
28 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <com/sun/star/graphic/PrimitiveFactory2D.hpp>
32 #include <drawinglayer/geometry/viewinformation2d.hxx>
33 #include <com/sun/star/chart2/XChartDocument.hpp>
34 #include <com/sun/star/drawing/FillStyle.hpp>
35 #include <com/sun/star/drawing/LineStyle.hpp>
37 using namespace ::com::sun::star;
39 bool ChartHelper::isGL3DDiagram( const css::uno::Reference<css::chart2::XDiagram>& xDiagram )
41 uno::Reference<chart2::XCoordinateSystemContainer> xCooSysContainer(xDiagram, uno::UNO_QUERY);
43 if (!xCooSysContainer.is())
44 return false;
46 uno::Sequence< uno::Reference<chart2::XCoordinateSystem> > aCooSysList = xCooSysContainer->getCoordinateSystems();
47 for (sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS)
49 uno::Reference<chart2::XCoordinateSystem> xCooSys = aCooSysList[nCS];
51 //iterate through all chart types in the current coordinate system
52 uno::Reference<chart2::XChartTypeContainer> xChartTypeContainer(xCooSys, uno::UNO_QUERY);
53 OSL_ASSERT( xChartTypeContainer.is());
54 if( !xChartTypeContainer.is() )
55 continue;
57 uno::Sequence< uno::Reference<chart2::XChartType> > aChartTypeList = xChartTypeContainer->getChartTypes();
58 for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
60 uno::Reference<chart2::XChartType> xChartType = aChartTypeList[nT];
61 OUString aChartType = xChartType->getChartType();
62 if( aChartType == "com.sun.star.chart2.GL3DBarChartType" )
63 return true;
67 return false;
70 void ChartHelper::updateChart( const uno::Reference< ::frame::XModel >& rXModel, bool bHardUpdate )
72 if (!rXModel.is())
73 return;
75 try
77 const uno::Reference< lang::XMultiServiceFactory > xChartFact(rXModel, uno::UNO_QUERY_THROW);
78 const uno::Reference< lang::XUnoTunnel > xChartView(xChartFact->createInstance("com.sun.star.chart2.ChartView"), uno::UNO_QUERY_THROW);
79 const uno::Reference<util::XUpdatable2> xUpdatable(xChartView, uno::UNO_QUERY_THROW);
81 if (xUpdatable.is())
83 if (bHardUpdate)
84 xUpdatable->updateHard();
85 else
86 xUpdatable->updateSoft();
89 catch(uno::Exception&)
91 OSL_ENSURE(false, "Unexpected exception!");
95 drawinglayer::primitive2d::Primitive2DContainer ChartHelper::tryToGetChartContentAsPrimitive2DSequence(
96 const uno::Reference< ::frame::XModel >& rXModel,
97 basegfx::B2DRange& rRange)
99 drawinglayer::primitive2d::Primitive2DContainer aRetval;
101 if (!rXModel.is())
102 return aRetval;
104 updateChart(rXModel, true);
108 const uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier(rXModel, uno::UNO_QUERY_THROW);
109 const uno::Reference< container::XIndexAccess > xShapeAccess(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW);
111 if(xShapeAccess.is() && xShapeAccess->getCount())
113 const sal_Int32 nShapeCount(xShapeAccess->getCount());
114 const uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
115 const uno::Reference< graphic::XPrimitiveFactory2D > xPrimitiveFactory =
116 graphic::PrimitiveFactory2D::create( xContext );
118 const uno::Sequence< beans::PropertyValue > aParams;
119 uno::Reference< drawing::XShape > xShape;
121 for(sal_Int32 a(0); a < nShapeCount; a++)
123 xShapeAccess->getByIndex(a) >>= xShape;
125 if(xShape.is())
127 const drawinglayer::primitive2d::Primitive2DSequence aNew(
128 xPrimitiveFactory->createPrimitivesFromXShape(
129 xShape,
130 aParams));
132 aRetval.append(aNew);
137 catch(uno::Exception&)
139 OSL_ENSURE(false, "Unexpected exception!");
142 if(!aRetval.empty())
144 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
146 rRange = aRetval.getB2DRange(aViewInformation2D);
149 return aRetval;
152 void ChartHelper::AdaptDefaultsForChart(
153 const uno::Reference < embed::XEmbeddedObject > & xEmbObj)
155 if( xEmbObj.is())
157 uno::Reference< chart2::XChartDocument > xChartDoc( xEmbObj->getComponent(), uno::UNO_QUERY );
158 OSL_ENSURE( xChartDoc.is(), "Trying to set chart property to non-chart OLE" );
159 if( !xChartDoc.is())
160 return;
164 // set background to transparent (none)
165 uno::Reference< beans::XPropertySet > xPageProp( xChartDoc->getPageBackground());
166 if( xPageProp.is())
167 xPageProp->setPropertyValue( "FillStyle",
168 uno::makeAny( drawing::FillStyle_NONE ));
169 // set no border
170 if( xPageProp.is())
171 xPageProp->setPropertyValue( "LineStyle",
172 uno::makeAny( drawing::LineStyle_NONE ));
174 catch( const uno::Exception & )
176 OSL_FAIL( "Exception caught in AdaptDefaultsForChart" );
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */