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