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 27 : uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
46 : const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
47 : {
48 27 : return new RangeHighlighter( xSelectionSupplier );
49 : }
50 :
51 472 : uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
52 : const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
53 : {
54 472 : 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 472 : if(xChartDoc.is())
59 : {
60 76 : ::com::sun::star::uno::Reference< ::com::sun::star::chart::XChartDocument > xDoc(xChartDoc, uno::UNO_QUERY);
61 :
62 76 : if(xDoc.is())
63 : {
64 76 : ::com::sun::star::uno::Reference< ::com::sun::star::chart::XDiagram > aDiagram = xDoc->getDiagram();
65 :
66 76 : if(aDiagram.is())
67 : {
68 76 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xProp(aDiagram, uno::UNO_QUERY);
69 :
70 76 : if(xProp.is())
71 : {
72 76 : ::com::sun::star::chart::ChartDataRowSource aDataRowSource(::com::sun::star::chart::ChartDataRowSource_COLUMNS);
73 :
74 76 : xProp->getPropertyValue( OUString("DataRowSource") ) >>= aDataRowSource;
75 :
76 76 : bDefaultDataInColumns = (::com::sun::star::chart::ChartDataRowSource_COLUMNS == aDataRowSource);
77 76 : }
78 76 : }
79 76 : }
80 : }
81 :
82 472 : return new InternalDataProvider( xChartDoc, bConnectToModel, bDefaultDataInColumns );
83 : }
84 :
85 760841 : uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
86 : {
87 760841 : uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
88 760841 : if( xChartDoc.is())
89 760841 : return ChartModelHelper::findDiagram( xChartDoc );
90 0 : return NULL;
91 : }
92 :
93 764556 : uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
94 : {
95 : try
96 : {
97 764556 : if( xChartDoc.is())
98 764160 : return xChartDoc->getFirstDiagram();
99 : }
100 0 : catch( const uno::Exception & ex )
101 : {
102 : ASSERT_EXCEPTION( ex );
103 : }
104 396 : return NULL;
105 : }
106 :
107 5800 : uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( ChartModel& rModel )
108 : {
109 5800 : uno::Reference< XCoordinateSystem > XCooSys;
110 11600 : uno::Reference< XCoordinateSystemContainer > xCooSysCnt( rModel.getFirstDiagram(), uno::UNO_QUERY );
111 5800 : if( xCooSysCnt.is() )
112 : {
113 5800 : uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
114 5800 : if( aCooSysSeq.getLength() )
115 5800 : XCooSys = aCooSysSeq[0];
116 : }
117 11600 : return XCooSys;
118 : }
119 :
120 56 : uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
121 : {
122 56 : uno::Reference< XCoordinateSystem > XCooSys;
123 112 : uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
124 56 : if( xCooSysCnt.is() )
125 : {
126 56 : uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
127 56 : if( aCooSysSeq.getLength() )
128 56 : XCooSys = aCooSysSeq[0];
129 : }
130 112 : return XCooSys;
131 : }
132 :
133 578 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
134 : ChartModel& rModel )
135 : {
136 578 : ::std::vector< uno::Reference< XDataSeries > > aResult;
137 :
138 1156 : uno::Reference< XDiagram > xDiagram = rModel.getFirstDiagram();
139 578 : if( xDiagram.is())
140 578 : aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
141 :
142 1156 : return aResult;
143 : }
144 :
145 58 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
146 : const uno::Reference< XChartDocument > & xChartDoc )
147 : {
148 58 : ::std::vector< uno::Reference< XDataSeries > > aResult;
149 :
150 116 : uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
151 58 : if( xDiagram.is())
152 58 : aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
153 :
154 116 : return aResult;
155 : }
156 :
157 4 : ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
158 : const uno::Reference< frame::XModel > & xModel )
159 : {
160 4 : 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 2183 : awt::Size ChartModelHelper::getDefaultPageSize()
171 : {
172 2183 : return awt::Size( 16000, 9000 );
173 : }
174 :
175 482 : awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
176 : {
177 482 : awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
178 482 : uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
179 : OSL_ENSURE(xVisualObject.is(),"need xVisualObject for page size");
180 482 : if( xVisualObject.is() )
181 482 : aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
182 482 : return aPageSize;
183 : }
184 :
185 224 : void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
186 : {
187 224 : uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
188 224 : if( xDataReceiver.is() )
189 : {
190 224 : uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
191 : //trigger selection of cell range
192 224 : if( xSelectionChangeListener.is() )
193 : {
194 224 : lang::EventObject aEvent( xSelectionChangeListener );
195 224 : xSelectionChangeListener->selectionChanged( aEvent );
196 224 : }
197 224 : }
198 224 : }
199 :
200 426 : bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
201 : {
202 426 : bool bIncluded = true; // hidden cells are included by default.
203 :
204 426 : uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
205 426 : if (!xDiagram.is())
206 202 : return bIncluded;
207 :
208 448 : uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
209 224 : if (!xProp.is())
210 0 : return bIncluded;
211 :
212 : try
213 : {
214 224 : xProp->getPropertyValue("IncludeHiddenCells") >>= bIncluded;
215 : }
216 0 : catch( const beans::UnknownPropertyException& )
217 : {
218 : }
219 :
220 650 : return bIncluded;
221 : }
222 :
223 578 : bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, ChartModel& rModel )
224 : {
225 578 : bool bChanged = false;
226 : try
227 : {
228 578 : ControllerLockGuard aLockedControllers( rModel );
229 :
230 1156 : uno::Reference< beans::XPropertySet > xDiagramProperties( rModel.getFirstDiagram(), uno::UNO_QUERY );
231 578 : if (xDiagramProperties.is())
232 : {
233 578 : bool bOldValue = bIncludeHiddenCells;
234 578 : xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue;
235 578 : if( bOldValue == bIncludeHiddenCells )
236 0 : bChanged = true;
237 :
238 : //set the property on all instances in all cases to get the different objects in sync!
239 :
240 578 : uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
241 :
242 : try
243 : {
244 578 : uno::Reference< beans::XPropertySet > xDataProviderProperties( rModel.getDataProvider(), uno::UNO_QUERY );
245 578 : if( xDataProviderProperties.is() )
246 188 : xDataProviderProperties->setPropertyValue("IncludeHiddenCells", aNewValue );
247 : }
248 0 : catch( const beans::UnknownPropertyException& )
249 : {
250 : //the property is optional!
251 : }
252 :
253 : try
254 : {
255 578 : uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( rModel ) );
256 578 : if( xUsedData.is() )
257 : {
258 578 : uno::Reference< beans::XPropertySet > xProp;
259 1156 : uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
260 782 : for( sal_Int32 i=0; i<aData.getLength(); ++i )
261 : {
262 422 : xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
263 422 : if(xProp.is())
264 640 : xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
265 204 : xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
266 204 : if(xProp.is())
267 96 : xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
268 578 : }
269 578 : }
270 : }
271 218 : catch( const beans::UnknownPropertyException& )
272 : {
273 : //the property is optional!
274 : }
275 :
276 578 : xDiagramProperties->setPropertyValue( "IncludeHiddenCells", aNewValue);
277 578 : }
278 : }
279 0 : catch (const uno::Exception& e)
280 : {
281 : ASSERT_EXCEPTION(e);
282 : }
283 578 : return bChanged;
284 : }
285 :
286 108 : } //namespace chart
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|