Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
27 : #include "ChartModel.hxx"
28 :
29 : #include <com/sun/star/chart/ChartDataRowSource.hpp>
30 : #include <com/sun/star/chart/XChartDocument.hpp>
31 : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
32 : #include <com/sun/star/chart2/XChartDocument.hpp>
33 : #include <com/sun/star/chart2/XChartTypeContainer.hpp>
34 : #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
35 : #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
36 : #include <com/sun/star/embed/Aspects.hpp>
37 : #include <com/sun/star/embed/XVisualObject.hpp>
38 : #include <com/sun/star/view/XSelectionChangeListener.hpp>
39 :
40 : namespace chart
41 : {
42 : using namespace ::com::sun::star;
43 : using namespace ::com::sun::star::chart2;
44 :
45 17 : uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
46 : const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
47 : {
48 17 : return new RangeHighlighter( xSelectionSupplier );
49 : }
50 :
51 189 : uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
52 : const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
53 : {
54 189 : bool bDefaultDataInColumns(true);
55 :
56 : // #i120559# Try to access the current state of "DataRowSource" for the
57 : // chart data and use it as default for creating a new InternalDataProvider
58 189 : if(xChartDoc.is())
59 : {
60 30 : ::com::sun::star::uno::Reference< ::com::sun::star::chart::XChartDocument > xDoc(xChartDoc, uno::UNO_QUERY);
61 :
62 30 : if(xDoc.is())
63 : {
64 30 : ::com::sun::star::uno::Reference< ::com::sun::star::chart::XDiagram > aDiagram = xDoc->getDiagram();
65 :
66 30 : if(aDiagram.is())
67 : {
68 30 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xProp(aDiagram, uno::UNO_QUERY);
69 :
70 30 : if(xProp.is())
71 : {
72 30 : ::com::sun::star::chart::ChartDataRowSource aDataRowSource(::com::sun::star::chart::ChartDataRowSource_COLUMNS);
73 :
74 30 : xProp->getPropertyValue( OUString("DataRowSource") ) >>= aDataRowSource;
75 :
76 30 : bDefaultDataInColumns = (::com::sun::star::chart::ChartDataRowSource_COLUMNS == aDataRowSource);
77 30 : }
78 30 : }
79 30 : }
80 : }
81 :
82 189 : return new InternalDataProvider( xChartDoc, bConnectToModel, bDefaultDataInColumns );
83 : }
84 :
85 377885 : uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
86 : {
87 377885 : uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
88 377885 : if( xChartDoc.is())
89 377885 : return ChartModelHelper::findDiagram( xChartDoc );
90 0 : return NULL;
91 : }
92 :
93 379743 : uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
94 : {
95 : try
96 : {
97 379743 : if( xChartDoc.is())
98 379584 : return xChartDoc->getFirstDiagram();
99 : }
100 0 : catch( const uno::Exception & ex )
101 : {
102 : ASSERT_EXCEPTION( ex );
103 : }
104 159 : return NULL;
105 : }
106 :
107 7002 : uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( ChartModel& rModel )
108 : {
109 7002 : uno::Reference< XCoordinateSystem > XCooSys;
110 14004 : uno::Reference< XCoordinateSystemContainer > xCooSysCnt( rModel.getFirstDiagram(), uno::UNO_QUERY );
111 7002 : if( xCooSysCnt.is() )
112 : {
113 7002 : uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
114 7002 : if( aCooSysSeq.getLength() )
115 7002 : XCooSys = aCooSysSeq[0];
116 : }
117 14004 : return XCooSys;
118 : }
119 :
120 28 : uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
121 : {
122 28 : uno::Reference< XCoordinateSystem > XCooSys;
123 56 : uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
124 28 : if( xCooSysCnt.is() )
125 : {
126 28 : uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
127 28 : if( aCooSysSeq.getLength() )
128 28 : XCooSys = aCooSysSeq[0];
129 : }
130 56 : return XCooSys;
131 : }
132 :
133 308 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
134 : ChartModel& rModel )
135 : {
136 308 : ::std::vector< uno::Reference< XDataSeries > > aResult;
137 :
138 616 : uno::Reference< XDiagram > xDiagram = rModel.getFirstDiagram();
139 308 : if( xDiagram.is())
140 308 : aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
141 :
142 616 : return aResult;
143 : }
144 :
145 29 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
146 : const uno::Reference< XChartDocument > & xChartDoc )
147 : {
148 29 : ::std::vector< uno::Reference< XDataSeries > > aResult;
149 :
150 58 : uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
151 29 : if( xDiagram.is())
152 29 : aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
153 :
154 58 : return aResult;
155 : }
156 :
157 2 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
158 : const uno::Reference< frame::XModel > & xModel )
159 : {
160 2 : return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
161 : }
162 :
163 0 : uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
164 : const uno::Reference< frame::XModel >& xModel
165 : , const uno::Reference< XDataSeries >& xGivenDataSeries )
166 : {
167 0 : return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
168 : }
169 :
170 2663 : awt::Size ChartModelHelper::getDefaultPageSize()
171 : {
172 2663 : return awt::Size( 16000, 9000 );
173 : }
174 :
175 207 : awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
176 : {
177 207 : awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
178 207 : uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
179 : OSL_ENSURE(xVisualObject.is(),"need xVisualObject for page size");
180 207 : if( xVisualObject.is() )
181 207 : aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
182 207 : return aPageSize;
183 : }
184 :
185 763 : void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
186 : {
187 763 : uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
188 763 : if( xDataReceiver.is() )
189 : {
190 763 : uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
191 : //trigger selection of cell range
192 763 : if( xSelectionChangeListener.is() )
193 : {
194 763 : lang::EventObject aEvent( xSelectionChangeListener );
195 763 : xSelectionChangeListener->selectionChanged( aEvent );
196 763 : }
197 763 : }
198 763 : }
199 :
200 1519 : bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
201 : {
202 1519 : bool bIncluded = true; // hidden cells are included by default.
203 :
204 1519 : uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
205 1519 : if (!xDiagram.is())
206 161 : return bIncluded;
207 :
208 2716 : uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
209 1358 : if (!xProp.is())
210 0 : return bIncluded;
211 :
212 : try
213 : {
214 1358 : xProp->getPropertyValue("IncludeHiddenCells") >>= bIncluded;
215 : }
216 0 : catch( const beans::UnknownPropertyException& )
217 : {
218 : }
219 :
220 2877 : return bIncluded;
221 : }
222 :
223 308 : bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, ChartModel& rModel )
224 : {
225 308 : bool bChanged = false;
226 : try
227 : {
228 308 : ControllerLockGuard aLockedControllers( rModel );
229 :
230 616 : uno::Reference< beans::XPropertySet > xDiagramProperties( rModel.getFirstDiagram(), uno::UNO_QUERY );
231 308 : if (xDiagramProperties.is())
232 : {
233 308 : bool bOldValue = bIncludeHiddenCells;
234 308 : xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue;
235 308 : if( bOldValue == bIncludeHiddenCells )
236 2 : bChanged = true;
237 :
238 : //set the property on all instances in all cases to get the different objects in sync!
239 :
240 308 : uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
241 :
242 : try
243 : {
244 308 : uno::Reference< beans::XPropertySet > xDataProviderProperties( rModel.getDataProvider(), uno::UNO_QUERY );
245 308 : if( xDataProviderProperties.is() )
246 152 : xDataProviderProperties->setPropertyValue("IncludeHiddenCells", aNewValue );
247 : }
248 0 : catch( const beans::UnknownPropertyException& )
249 : {
250 : //the property is optional!
251 : }
252 :
253 : try
254 : {
255 308 : uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( rModel ) );
256 308 : if( xUsedData.is() )
257 : {
258 308 : uno::Reference< beans::XPropertySet > xProp;
259 616 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
260 540 : for( sal_Int32 i=0; i<aData.getLength(); ++i )
261 : {
262 341 : xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
263 341 : if(xProp.is())
264 450 : xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
265 232 : xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
266 232 : if(xProp.is())
267 130 : xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
268 308 : }
269 308 : }
270 : }
271 109 : catch( const beans::UnknownPropertyException& )
272 : {
273 : //the property is optional!
274 : }
275 :
276 308 : xDiagramProperties->setPropertyValue( "IncludeHiddenCells", aNewValue);
277 308 : }
278 : }
279 0 : catch (const uno::Exception& e)
280 : {
281 : ASSERT_EXCEPTION(e);
282 : }
283 308 : return bChanged;
284 : }
285 :
286 : } //namespace chart
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|