merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / ChartModelHelper.cxx
bloba49b2abeac986ea908a172072864e6d6e7535322
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "ChartModelHelper.hxx"
31 #include "macros.hxx"
32 #include "DiagramHelper.hxx"
33 #include "DataSourceHelper.hxx"
34 #include "ControllerLockGuard.hxx"
35 #include "RangeHighlighter.hxx"
36 #include "InternalDataProvider.hxx"
38 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
39 #include <com/sun/star/chart2/XChartDocument.hpp>
40 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
41 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
42 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
43 #include <com/sun/star/embed/Aspects.hpp>
44 #include <com/sun/star/embed/XVisualObject.hpp>
45 #include <com/sun/star/view/XSelectionChangeListener.hpp>
47 // header for define DBG_ASSERT
48 #include <tools/debug.hxx>
50 //.............................................................................
51 namespace chart
53 //.............................................................................
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::chart2;
57 //static
58 uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
59 const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
61 return new RangeHighlighter( xSelectionSupplier );
64 //static
65 uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
66 const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
68 return new InternalDataProvider( xChartDoc, bConnectToModel );
71 //static
72 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
74 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
75 if( xChartDoc.is())
76 return ChartModelHelper::findDiagram( xChartDoc );
77 return NULL;
80 // static
81 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
83 try
85 if( xChartDoc.is())
86 return xChartDoc->getFirstDiagram();
88 catch( uno::Exception & ex )
90 ASSERT_EXCEPTION( ex );
92 return NULL;
95 //static
96 uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
98 uno::Reference< XCoordinateSystem > XCooSys;
99 uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
100 if( xCooSysCnt.is() )
102 uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
103 if( aCooSysSeq.getLength() )
104 XCooSys = aCooSysSeq[0];
106 return XCooSys;
109 // static
110 ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
111 const uno::Reference< XChartDocument > & xChartDoc )
113 ::std::vector< uno::Reference< XDataSeries > > aResult;
115 uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
116 if( xDiagram.is())
117 aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
119 return aResult;
122 // static
123 ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
124 const uno::Reference< frame::XModel > & xModel )
126 return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
130 uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
131 const uno::Reference< frame::XModel >& xModel
132 , const uno::Reference< XDataSeries >& xGivenDataSeries )
134 return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
137 awt::Size ChartModelHelper::getDefaultPageSize()
139 return awt::Size( 16000, 9000 );
142 awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
144 awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
145 uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
146 DBG_ASSERT(xVisualObject.is(),"need xVisualObject for page size");
147 if( xVisualObject.is() )
148 aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
149 return aPageSize;
152 void ChartModelHelper::setPageSize( const awt::Size& rSize, const uno::Reference< frame::XModel >& xModel )
154 uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
155 DBG_ASSERT(xVisualObject.is(),"need xVisualObject for page size");
156 if( xVisualObject.is() )
157 xVisualObject->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, rSize );
160 void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
162 uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
163 if( xDataReceiver.is() )
165 uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
166 //trigger selection of cell range
167 if( xSelectionChangeListener.is() )
169 lang::EventObject aEvent( xSelectionChangeListener );
170 xSelectionChangeListener->selectionChanged( aEvent );
175 bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
177 bool bIncluded = true; // hidden cells are included by default.
179 uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
180 if (!xDiagram.is())
181 return bIncluded;
183 uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
184 if (!xProp.is())
185 return bIncluded;
189 xProp->getPropertyValue(C2U("IncludeHiddenCells")) >>= bIncluded;
191 catch( const beans::UnknownPropertyException& )
195 return bIncluded;
198 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, const uno::Reference< frame::XModel >& xChartModel )
200 bool bChanged = false;
203 ControllerLockGuard aLockedControllers( xChartModel );
205 uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(xChartModel), uno::UNO_QUERY );
206 if (xDiagramProperties.is())
208 bool bOldValue = bIncludeHiddenCells;
209 xDiagramProperties->getPropertyValue( C2U("IncludeHiddenCells") ) >>= bOldValue;
210 if( bOldValue == bIncludeHiddenCells )
211 bChanged = true;
213 //set the property on all instances in all cases to get the different objects in sync!
215 uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
219 uno::Reference< chart2::XChartDocument > xChartDoc( xChartModel, uno::UNO_QUERY );
220 if( xChartDoc.is() )
222 uno::Reference< beans::XPropertySet > xDataProviderProperties( xChartDoc->getDataProvider(), uno::UNO_QUERY );
223 if( xDataProviderProperties.is() )
224 xDataProviderProperties->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
227 catch( const beans::UnknownPropertyException& )
229 //the property is optional!
234 uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( xChartModel ) );
235 if( xUsedData.is() )
237 uno::Reference< beans::XPropertySet > xProp;
238 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
239 for( sal_Int32 i=0; i<aData.getLength(); ++i )
241 xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
242 if(xProp.is())
243 xProp->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
244 xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
245 if(xProp.is())
246 xProp->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
250 catch( const beans::UnknownPropertyException& )
252 //the property is optional!
255 xDiagramProperties->setPropertyValue( C2U("IncludeHiddenCells"), aNewValue);
258 catch (uno::Exception& e)
260 ASSERT_EXCEPTION(e);
262 return bChanged;
265 //.............................................................................
266 } //namespace chart
267 //.............................................................................