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