xref: /aoo42x/main/svx/source/svdraw/charthelper.cxx (revision 6f0b96b4)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svx.hxx"
24 
25 #include <svx/charthelper.hxx>
26 #include <svtools/embedhlp.hxx>
27 #include <tools/globname.hxx>
28 #include <sot/clsids.hxx>
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include <com/sun/star/util/XUpdatable.hpp>
31 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <comphelper/processfactory.hxx>
34 #include <com/sun/star/graphic/XPrimitiveFactory2D.hpp>
35 #include <drawinglayer/geometry/viewinformation2d.hxx>
36 #include <com/sun/star/chart2/XChartDocument.hpp>
37 #include <com/sun/star/drawing/FillStyle.hpp>
38 #include <com/sun/star/drawing/LineStyle.hpp>
39 
40 //////////////////////////////////////////////////////////////////////////////
41 
42 using namespace ::com::sun::star;
43 
44 //////////////////////////////////////////////////////////////////////////////
45 
46 bool ChartHelper::IsChart(const svt::EmbeddedObjectRef& xObjRef)
47 {
48     if(!xObjRef.is())
49     {
50         return false;
51     }
52 
53     const SvGlobalName aObjClsId(xObjRef->getClassID());
54 
55     if(SvGlobalName(SO3_SCH_CLASSID_30) == aObjClsId
56         || SvGlobalName(SO3_SCH_CLASSID_40) == aObjClsId
57         || SvGlobalName(SO3_SCH_CLASSID_50) == aObjClsId
58         || SvGlobalName(SO3_SCH_CLASSID_60) == aObjClsId)
59     {
60         return true;
61     }
62 
63     return false;
64 }
65 
66 drawinglayer::primitive2d::Primitive2DSequence ChartHelper::tryToGetChartContentAsPrimitive2DSequence(
67     const uno::Reference< ::frame::XModel >& rXModel,
68     basegfx::B2DRange& rRange)
69 {
70     drawinglayer::primitive2d::Primitive2DSequence aRetval;
71 
72     if(rXModel.is())
73     {
74         try
75         {
76             const uno::Reference< lang::XMultiServiceFactory > xChartFact(rXModel, uno::UNO_QUERY_THROW);
77             const uno::Reference< lang::XUnoTunnel > xChartView(xChartFact->createInstance(::rtl::OUString::createFromAscii("com.sun.star.chart2.ChartView")), uno::UNO_QUERY_THROW);
78             const uno::Reference< util::XUpdatable > xUpdatable(xChartView, uno::UNO_QUERY_THROW);
79 
80             if(xUpdatable.is())
81             {
82                 xUpdatable->update();
83 
84                 const uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier(rXModel, uno::UNO_QUERY_THROW);
85                 const uno::Reference< container::XIndexAccess > xShapeAccess(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW);
86 
87                 if(xShapeAccess.is() && xShapeAccess->getCount())
88                 {
89                     const sal_Int32 nShapeCount(xShapeAccess->getCount());
90                     const uno::Reference< lang::XMultiServiceFactory > xMgr(::comphelper::getProcessServiceFactory());
91                     const uno::Reference< graphic::XPrimitiveFactory2D > xPrimitiveFactory(
92                         xMgr->createInstance(
93                             String(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.graphic.PrimitiveFactory2D" ))),
94                             uno::UNO_QUERY);
95 
96                     if(xPrimitiveFactory.is())
97                     {
98                         const uno::Sequence< beans::PropertyValue > aParams;
99                         uno::Reference< drawing::XShape > xShape;
100 
101                         for(sal_Int32 a(0); a < nShapeCount; a++)
102                         {
103                             xShapeAccess->getByIndex(a) >>= xShape;
104 
105                             if(xShape.is())
106                             {
107                                 const drawinglayer::primitive2d::Primitive2DSequence aNew(
108                                     xPrimitiveFactory->createPrimitivesFromXShape(
109                                         xShape,
110                                         aParams));
111 
112                                 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(
113                                     aRetval,
114                                     aNew);
115                             }
116                         }
117                     }
118                 }
119             }
120         }
121         catch(uno::Exception&)
122         {
123             OSL_ENSURE(false, "Unexpected exception!");
124         }
125 
126         if(aRetval.hasElements())
127         {
128             const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
129 
130             rRange = drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(aRetval, aViewInformation2D);
131         }
132     }
133 
134     return aRetval;
135 }
136 
137 void ChartHelper::AdaptDefaultsForChart(
138     const uno::Reference < embed::XEmbeddedObject > & xEmbObj,
139     bool bNoFillStyle,
140     bool bNoLineStyle)
141 {
142     if( xEmbObj.is())
143     {
144         uno::Reference< chart2::XChartDocument > xChartDoc( xEmbObj->getComponent(), uno::UNO_QUERY );
145         OSL_ENSURE( xChartDoc.is(), "Trying to set chart property to non-chart OLE" );
146         if( !xChartDoc.is())
147             return;
148 
149         try
150         {
151             // set background to transparent (none)
152             uno::Reference< beans::XPropertySet > xPageProp( xChartDoc->getPageBackground());
153             if( xPageProp.is())
154                 xPageProp->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FillStyle")),
155                                              uno::makeAny( drawing::FillStyle_NONE ));
156             // set no border
157             if( xPageProp.is())
158                 xPageProp->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("LineStyle")),
159                                              uno::makeAny( drawing::LineStyle_NONE ));
160         }
161         catch( const uno::Exception & )
162         {
163             OSL_ENSURE( false, "Exception caught in AdaptDefaultsForChart" );
164         }
165     }
166 }
167 
168 //////////////////////////////////////////////////////////////////////////////
169 // eof
170