bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / ChartModelHelper.cxx
blob7002a471f2d08d0eb86e000f3f45d75aa8225256
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 "ChartModelHelper.hxx"
21 #include "macros.hxx"
22 #include "DiagramHelper.hxx"
23 #include "DataSourceHelper.hxx"
24 #include "ControllerLockGuard.hxx"
25 #include "RangeHighlighter.hxx"
26 #include "InternalDataProvider.hxx"
28 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
29 #include <com/sun/star/chart2/XChartDocument.hpp>
30 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
31 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
32 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
33 #include <com/sun/star/embed/Aspects.hpp>
34 #include <com/sun/star/embed/XVisualObject.hpp>
35 #include <com/sun/star/view/XSelectionChangeListener.hpp>
37 //.............................................................................
38 namespace chart
40 //.............................................................................
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::chart2;
44 uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
45 const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
47 return new RangeHighlighter( xSelectionSupplier );
50 uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
51 const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
53 return new InternalDataProvider( xChartDoc, bConnectToModel );
56 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
58 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
59 if( xChartDoc.is())
60 return ChartModelHelper::findDiagram( xChartDoc );
61 return NULL;
64 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
66 try
68 if( xChartDoc.is())
69 return xChartDoc->getFirstDiagram();
71 catch( const uno::Exception & ex )
73 ASSERT_EXCEPTION( ex );
75 return NULL;
78 uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
80 uno::Reference< XCoordinateSystem > XCooSys;
81 uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
82 if( xCooSysCnt.is() )
84 uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
85 if( aCooSysSeq.getLength() )
86 XCooSys = aCooSysSeq[0];
88 return XCooSys;
91 ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
92 const uno::Reference< XChartDocument > & xChartDoc )
94 ::std::vector< uno::Reference< XDataSeries > > aResult;
96 uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
97 if( xDiagram.is())
98 aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
100 return aResult;
103 ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
104 const uno::Reference< frame::XModel > & xModel )
106 return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
110 uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
111 const uno::Reference< frame::XModel >& xModel
112 , const uno::Reference< XDataSeries >& xGivenDataSeries )
114 return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
117 awt::Size ChartModelHelper::getDefaultPageSize()
119 return awt::Size( 16000, 9000 );
122 awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
124 awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
125 uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
126 OSL_ENSURE(xVisualObject.is(),"need xVisualObject for page size");
127 if( xVisualObject.is() )
128 aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
129 return aPageSize;
132 void ChartModelHelper::setPageSize( const awt::Size& rSize, const uno::Reference< frame::XModel >& xModel )
134 uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
135 OSL_ENSURE(xVisualObject.is(),"need xVisualObject for page size");
136 if( xVisualObject.is() )
137 xVisualObject->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, rSize );
140 void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
142 uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
143 if( xDataReceiver.is() )
145 uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
146 //trigger selection of cell range
147 if( xSelectionChangeListener.is() )
149 lang::EventObject aEvent( xSelectionChangeListener );
150 xSelectionChangeListener->selectionChanged( aEvent );
155 bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
157 bool bIncluded = true; // hidden cells are included by default.
159 uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
160 if (!xDiagram.is())
161 return bIncluded;
163 uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
164 if (!xProp.is())
165 return bIncluded;
169 xProp->getPropertyValue("IncludeHiddenCells") >>= bIncluded;
171 catch( const beans::UnknownPropertyException& )
175 return bIncluded;
178 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, const uno::Reference< frame::XModel >& xChartModel )
180 bool bChanged = false;
183 ControllerLockGuard aLockedControllers( xChartModel );
185 uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(xChartModel), uno::UNO_QUERY );
186 if (xDiagramProperties.is())
188 bool bOldValue = bIncludeHiddenCells;
189 xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue;
190 if( bOldValue == bIncludeHiddenCells )
191 bChanged = true;
193 //set the property on all instances in all cases to get the different objects in sync!
195 uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
199 uno::Reference< chart2::XChartDocument > xChartDoc( xChartModel, uno::UNO_QUERY );
200 if( xChartDoc.is() )
202 uno::Reference< beans::XPropertySet > xDataProviderProperties( xChartDoc->getDataProvider(), uno::UNO_QUERY );
203 if( xDataProviderProperties.is() )
204 xDataProviderProperties->setPropertyValue("IncludeHiddenCells", aNewValue );
207 catch( const beans::UnknownPropertyException& )
209 //the property is optional!
214 uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( xChartModel ) );
215 if( xUsedData.is() )
217 uno::Reference< beans::XPropertySet > xProp;
218 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
219 for( sal_Int32 i=0; i<aData.getLength(); ++i )
221 xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
222 if(xProp.is())
223 xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
224 xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
225 if(xProp.is())
226 xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
230 catch( const beans::UnknownPropertyException& )
232 //the property is optional!
235 xDiagramProperties->setPropertyValue( "IncludeHiddenCells", aNewValue);
238 catch (const uno::Exception& e)
240 ASSERT_EXCEPTION(e);
242 return bChanged;
245 //.............................................................................
246 } //namespace chart
247 //.............................................................................
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */