1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "ChartModelHelper.hxx"
31 #include "macros.hxx"
32 #include "DiagramHelper.hxx"
33 #include "DataSourceHelper.hxx"
34 #include "ControllerLockGuard.hxx"
35 #include "RangeHighlighter.hxx"
36 #include "InternalDataProvider.hxx"
37 
38 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
39 #include <com/sun/star/chart2/XChartDocument.hpp>
40 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
41 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
42 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
43 #include <com/sun/star/embed/Aspects.hpp>
44 #include <com/sun/star/embed/XVisualObject.hpp>
45 #include <com/sun/star/view/XSelectionChangeListener.hpp>
46 
47 // header for define DBG_ASSERT
48 #include <tools/debug.hxx>
49 
50 //.............................................................................
51 namespace chart
52 {
53 //.............................................................................
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::chart2;
56 
57 uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
58         const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
59 {
60     return new RangeHighlighter( xSelectionSupplier );
61 }
62 
63 uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
64     const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
65 {
66     return new InternalDataProvider( xChartDoc, bConnectToModel );
67 }
68 
69 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
70 {
71     uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
72     if( xChartDoc.is())
73         return ChartModelHelper::findDiagram( xChartDoc );
74     return NULL;
75 }
76 
77 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
78 {
79     try
80     {
81         if( xChartDoc.is())
82             return xChartDoc->getFirstDiagram();
83     }
84     catch( uno::Exception & ex )
85     {
86         ASSERT_EXCEPTION( ex );
87     }
88     return NULL;
89 }
90 
91 uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
92 {
93     uno::Reference< XCoordinateSystem > XCooSys;
94     uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
95     if( xCooSysCnt.is() )
96     {
97         uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
98         if( aCooSysSeq.getLength() )
99             XCooSys = aCooSysSeq[0];
100     }
101     return XCooSys;
102 }
103 
104 ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
105     const uno::Reference< XChartDocument > & xChartDoc )
106 {
107     ::std::vector< uno::Reference< XDataSeries > > aResult;
108 
109     uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
110     if( xDiagram.is())
111         aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
112 
113     return aResult;
114 }
115 
116 ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
117     const uno::Reference< frame::XModel > & xModel )
118 {
119     return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
120 }
121 
122 
123 uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
124 								const uno::Reference< frame::XModel >& xModel
125 						      , const uno::Reference< XDataSeries >&   xGivenDataSeries )
126 {
127     return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
128 }
129 
130 awt::Size ChartModelHelper::getDefaultPageSize()
131 {
132     return awt::Size( 16000, 9000 );
133 }
134 
135 awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
136 {
137     awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
138     uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
139     DBG_ASSERT(xVisualObject.is(),"need xVisualObject for page size");
140     if( xVisualObject.is() )
141         aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
142     return aPageSize;
143 }
144 
145 void ChartModelHelper::setPageSize( const awt::Size& rSize, const uno::Reference< frame::XModel >& xModel )
146 {
147     uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
148     DBG_ASSERT(xVisualObject.is(),"need xVisualObject for page size");
149     if( xVisualObject.is() )
150         xVisualObject->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, rSize );
151 }
152 
153 void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
154 {
155     uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
156     if( xDataReceiver.is() )
157     {
158         uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
159         //trigger selection of cell range
160         if( xSelectionChangeListener.is() )
161         {
162             lang::EventObject aEvent( xSelectionChangeListener );
163             xSelectionChangeListener->selectionChanged( aEvent );
164         }
165     }
166 }
167 
168 bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
169 {
170     bool bIncluded = true;  // hidden cells are included by default.
171 
172     uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
173     if (!xDiagram.is())
174         return bIncluded;
175 
176     uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
177     if (!xProp.is())
178         return bIncluded;
179 
180     try
181     {
182         xProp->getPropertyValue(C2U("IncludeHiddenCells")) >>= bIncluded;
183     }
184     catch( const beans::UnknownPropertyException& )
185     {
186     }
187 
188     return bIncluded;
189 }
190 
191 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, const uno::Reference< frame::XModel >& xChartModel )
192 {
193     bool bChanged = false;
194     try
195     {
196         ControllerLockGuard aLockedControllers( xChartModel );
197 
198         uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(xChartModel), uno::UNO_QUERY );
199         if (xDiagramProperties.is())
200         {
201             bool bOldValue = bIncludeHiddenCells;
202             xDiagramProperties->getPropertyValue( C2U("IncludeHiddenCells") ) >>= bOldValue;
203             if( bOldValue == bIncludeHiddenCells )
204                 bChanged = true;
205 
206             //set the property on all instances in all cases to get the different objects in sync!
207 
208             uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
209 
210             try
211             {
212                 uno::Reference< chart2::XChartDocument > xChartDoc( xChartModel, uno::UNO_QUERY );
213                 if( xChartDoc.is() )
214                 {
215                     uno::Reference< beans::XPropertySet > xDataProviderProperties( xChartDoc->getDataProvider(), uno::UNO_QUERY );
216                     if( xDataProviderProperties.is() )
217                         xDataProviderProperties->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
218                 }
219             }
220             catch( const beans::UnknownPropertyException& )
221             {
222                 //the property is optional!
223             }
224 
225             try
226             {
227                 uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( xChartModel ) );
228                 if( xUsedData.is() )
229                 {
230                     uno::Reference< beans::XPropertySet > xProp;
231                     uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
232                     for( sal_Int32 i=0; i<aData.getLength(); ++i )
233                     {
234                         xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
235                         if(xProp.is())
236                             xProp->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
237                         xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
238                         if(xProp.is())
239                             xProp->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
240                     }
241                 }
242             }
243             catch( const beans::UnknownPropertyException& )
244             {
245                 //the property is optional!
246             }
247 
248             xDiagramProperties->setPropertyValue( C2U("IncludeHiddenCells"), aNewValue);
249         }
250     }
251     catch (uno::Exception& e)
252     {
253         ASSERT_EXCEPTION(e);
254     }
255     return bChanged;
256 }
257 
258 //.............................................................................
259 } //namespace chart
260 //.............................................................................
261