xref: /AOO41X/main/chart2/source/tools/ChartModelHelper.cxx (revision cde9e8dc2218e857da4894ecba5c903312256674)
1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "ChartModelHelper.hxx"
27cdf0e10cSrcweir #include "macros.hxx"
28cdf0e10cSrcweir #include "DiagramHelper.hxx"
29cdf0e10cSrcweir #include "DataSourceHelper.hxx"
30cdf0e10cSrcweir #include "ControllerLockGuard.hxx"
31cdf0e10cSrcweir #include "RangeHighlighter.hxx"
32cdf0e10cSrcweir #include "InternalDataProvider.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataReceiver.hpp>
35cdf0e10cSrcweir #include <com/sun/star/chart2/XChartDocument.hpp>
36cdf0e10cSrcweir #include <com/sun/star/chart2/XChartTypeContainer.hpp>
37cdf0e10cSrcweir #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
38cdf0e10cSrcweir #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
39cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
40cdf0e10cSrcweir #include <com/sun/star/embed/XVisualObject.hpp>
41cdf0e10cSrcweir #include <com/sun/star/view/XSelectionChangeListener.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // header for define DBG_ASSERT
44cdf0e10cSrcweir #include <tools/debug.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //.............................................................................
47cdf0e10cSrcweir namespace chart
48cdf0e10cSrcweir {
49cdf0e10cSrcweir //.............................................................................
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir using namespace ::com::sun::star::chart2;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
54cdf0e10cSrcweir         const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     return new RangeHighlighter( xSelectionSupplier );
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
60cdf0e10cSrcweir     const uno::Reference< ::com::sun::star::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     return new InternalDataProvider( xChartDoc, bConnectToModel );
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
68cdf0e10cSrcweir     if( xChartDoc.is())
69cdf0e10cSrcweir         return ChartModelHelper::findDiagram( xChartDoc );
70cdf0e10cSrcweir     return NULL;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     try
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         if( xChartDoc.is())
78cdf0e10cSrcweir             return xChartDoc->getFirstDiagram();
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir     catch( uno::Exception & ex )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         ASSERT_EXCEPTION( ex );
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir     return NULL;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     uno::Reference< XCoordinateSystem > XCooSys;
90cdf0e10cSrcweir     uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
91cdf0e10cSrcweir     if( xCooSysCnt.is() )
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
94cdf0e10cSrcweir         if( aCooSysSeq.getLength() )
95cdf0e10cSrcweir             XCooSys = aCooSysSeq[0];
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir     return XCooSys;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
101cdf0e10cSrcweir     const uno::Reference< XChartDocument > & xChartDoc )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     ::std::vector< uno::Reference< XDataSeries > > aResult;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
106cdf0e10cSrcweir     if( xDiagram.is())
107cdf0e10cSrcweir         aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     return aResult;
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir ::std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
113cdf0e10cSrcweir     const uno::Reference< frame::XModel > & xModel )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
119cdf0e10cSrcweir uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
120cdf0e10cSrcweir 								const uno::Reference< frame::XModel >& xModel
121cdf0e10cSrcweir 						      , const uno::Reference< XDataSeries >&   xGivenDataSeries )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir awt::Size ChartModelHelper::getDefaultPageSize()
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     return awt::Size( 16000, 9000 );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
134cdf0e10cSrcweir     uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
135cdf0e10cSrcweir     DBG_ASSERT(xVisualObject.is(),"need xVisualObject for page size");
136cdf0e10cSrcweir     if( xVisualObject.is() )
137cdf0e10cSrcweir         aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
138cdf0e10cSrcweir     return aPageSize;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir void ChartModelHelper::setPageSize( const awt::Size& rSize, const uno::Reference< frame::XModel >& xModel )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
144cdf0e10cSrcweir     DBG_ASSERT(xVisualObject.is(),"need xVisualObject for page size");
145cdf0e10cSrcweir     if( xVisualObject.is() )
146cdf0e10cSrcweir         xVisualObject->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, rSize );
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
152cdf0e10cSrcweir     if( xDataReceiver.is() )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
155cdf0e10cSrcweir         //trigger selection of cell range
156cdf0e10cSrcweir         if( xSelectionChangeListener.is() )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             lang::EventObject aEvent( xSelectionChangeListener );
159cdf0e10cSrcweir             xSelectionChangeListener->selectionChanged( aEvent );
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     bool bIncluded = true;  // hidden cells are included by default.
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
169cdf0e10cSrcweir     if (!xDiagram.is())
170cdf0e10cSrcweir         return bIncluded;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
173cdf0e10cSrcweir     if (!xProp.is())
174cdf0e10cSrcweir         return bIncluded;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     try
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         xProp->getPropertyValue(C2U("IncludeHiddenCells")) >>= bIncluded;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir     catch( const beans::UnknownPropertyException& )
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     return bIncluded;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, const uno::Reference< frame::XModel >& xChartModel )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir     bool bChanged = false;
190cdf0e10cSrcweir     try
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         ControllerLockGuard aLockedControllers( xChartModel );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         uno::Reference< beans::XPropertySet > xDiagramProperties( ChartModelHelper::findDiagram(xChartModel), uno::UNO_QUERY );
195cdf0e10cSrcweir         if (xDiagramProperties.is())
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             bool bOldValue = bIncludeHiddenCells;
198cdf0e10cSrcweir             xDiagramProperties->getPropertyValue( C2U("IncludeHiddenCells") ) >>= bOldValue;
199cdf0e10cSrcweir             if( bOldValue == bIncludeHiddenCells )
200cdf0e10cSrcweir                 bChanged = true;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir             //set the property on all instances in all cases to get the different objects in sync!
203cdf0e10cSrcweir 
204cdf0e10cSrcweir             uno::Any aNewValue = uno::makeAny(bIncludeHiddenCells);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir             try
207cdf0e10cSrcweir             {
208cdf0e10cSrcweir                 uno::Reference< chart2::XChartDocument > xChartDoc( xChartModel, uno::UNO_QUERY );
209cdf0e10cSrcweir                 if( xChartDoc.is() )
210cdf0e10cSrcweir                 {
211cdf0e10cSrcweir                     uno::Reference< beans::XPropertySet > xDataProviderProperties( xChartDoc->getDataProvider(), uno::UNO_QUERY );
212cdf0e10cSrcweir                     if( xDataProviderProperties.is() )
213cdf0e10cSrcweir                         xDataProviderProperties->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
214cdf0e10cSrcweir                 }
215cdf0e10cSrcweir             }
216cdf0e10cSrcweir             catch( const beans::UnknownPropertyException& )
217cdf0e10cSrcweir             {
218cdf0e10cSrcweir                 //the property is optional!
219cdf0e10cSrcweir             }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             try
222cdf0e10cSrcweir             {
223cdf0e10cSrcweir                 uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( xChartModel ) );
224cdf0e10cSrcweir                 if( xUsedData.is() )
225cdf0e10cSrcweir                 {
226cdf0e10cSrcweir                     uno::Reference< beans::XPropertySet > xProp;
227cdf0e10cSrcweir                     uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
228cdf0e10cSrcweir                     for( sal_Int32 i=0; i<aData.getLength(); ++i )
229cdf0e10cSrcweir                     {
230cdf0e10cSrcweir                         xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
231cdf0e10cSrcweir                         if(xProp.is())
232cdf0e10cSrcweir                             xProp->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
233cdf0e10cSrcweir                         xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
234cdf0e10cSrcweir                         if(xProp.is())
235cdf0e10cSrcweir                             xProp->setPropertyValue(C2U("IncludeHiddenCells"), aNewValue );
236cdf0e10cSrcweir                     }
237cdf0e10cSrcweir                 }
238cdf0e10cSrcweir             }
239cdf0e10cSrcweir             catch( const beans::UnknownPropertyException& )
240cdf0e10cSrcweir             {
241cdf0e10cSrcweir                 //the property is optional!
242cdf0e10cSrcweir             }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir             xDiagramProperties->setPropertyValue( C2U("IncludeHiddenCells"), aNewValue);
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir     catch (uno::Exception& e)
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         ASSERT_EXCEPTION(e);
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir     return bChanged;
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir //.............................................................................
255cdf0e10cSrcweir } //namespace chart
256cdf0e10cSrcweir //.............................................................................
257