1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ChartModelHelper.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"
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>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::chart2
;
45 uno::Reference
< chart2::data::XRangeHighlighter
> ChartModelHelper::createRangeHighlighter(
46 const uno::Reference
< view::XSelectionSupplier
> & xSelectionSupplier
)
48 return new RangeHighlighter( xSelectionSupplier
);
51 uno::Reference
< chart2::data::XDataProvider
> ChartModelHelper::createInternalDataProvider(
52 const uno::Reference
< ::com::sun::star::chart2::XChartDocument
>& xChartDoc
, bool bConnectToModel
)
54 bool bDefaultDataInColumns(true);
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
60 ::com::sun::star::uno::Reference
< ::com::sun::star::chart::XChartDocument
> xDoc(xChartDoc
, uno::UNO_QUERY
);
64 ::com::sun::star::uno::Reference
< ::com::sun::star::chart::XDiagram
> aDiagram
= xDoc
->getDiagram();
68 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> xProp(aDiagram
, uno::UNO_QUERY
);
72 ::com::sun::star::chart::ChartDataRowSource
aDataRowSource(::com::sun::star::chart::ChartDataRowSource_COLUMNS
);
74 xProp
->getPropertyValue( OUString("DataRowSource") ) >>= aDataRowSource
;
76 bDefaultDataInColumns
= (::com::sun::star::chart::ChartDataRowSource_COLUMNS
== aDataRowSource
);
82 return new InternalDataProvider( xChartDoc
, bConnectToModel
, bDefaultDataInColumns
);
85 uno::Reference
< XDiagram
> ChartModelHelper::findDiagram( const uno::Reference
< frame::XModel
>& xModel
)
87 uno::Reference
< XChartDocument
> xChartDoc( xModel
, uno::UNO_QUERY
);
89 return ChartModelHelper::findDiagram( xChartDoc
);
93 uno::Reference
< XDiagram
> ChartModelHelper::findDiagram( const uno::Reference
< chart2::XChartDocument
>& xChartDoc
)
98 return xChartDoc
->getFirstDiagram();
100 catch( const uno::Exception
& ex
)
102 ASSERT_EXCEPTION( ex
);
107 uno::Reference
< XCoordinateSystem
> ChartModelHelper::getFirstCoordinateSystem( ChartModel
& rModel
)
109 uno::Reference
< XCoordinateSystem
> XCooSys
;
110 uno::Reference
< XCoordinateSystemContainer
> xCooSysCnt( rModel
.getFirstDiagram(), uno::UNO_QUERY
);
111 if( xCooSysCnt
.is() )
113 uno::Sequence
< uno::Reference
< XCoordinateSystem
> > aCooSysSeq( xCooSysCnt
->getCoordinateSystems() );
114 if( aCooSysSeq
.getLength() )
115 XCooSys
= aCooSysSeq
[0];
120 uno::Reference
< XCoordinateSystem
> ChartModelHelper::getFirstCoordinateSystem( const uno::Reference
< frame::XModel
>& xModel
)
122 uno::Reference
< XCoordinateSystem
> XCooSys
;
123 uno::Reference
< XCoordinateSystemContainer
> xCooSysCnt( ChartModelHelper::findDiagram( xModel
), uno::UNO_QUERY
);
124 if( xCooSysCnt
.is() )
126 uno::Sequence
< uno::Reference
< XCoordinateSystem
> > aCooSysSeq( xCooSysCnt
->getCoordinateSystems() );
127 if( aCooSysSeq
.getLength() )
128 XCooSys
= aCooSysSeq
[0];
133 ::std::vector
< uno::Reference
< XDataSeries
> > ChartModelHelper::getDataSeries(
136 ::std::vector
< uno::Reference
< XDataSeries
> > aResult
;
138 uno::Reference
< XDiagram
> xDiagram
= rModel
.getFirstDiagram();
140 aResult
= DiagramHelper::getDataSeriesFromDiagram( xDiagram
);
145 ::std::vector
< uno::Reference
< XDataSeries
> > ChartModelHelper::getDataSeries(
146 const uno::Reference
< XChartDocument
> & xChartDoc
)
148 ::std::vector
< uno::Reference
< XDataSeries
> > aResult
;
150 uno::Reference
< XDiagram
> xDiagram
= ChartModelHelper::findDiagram( xChartDoc
);
152 aResult
= DiagramHelper::getDataSeriesFromDiagram( xDiagram
);
157 ::std::vector
< uno::Reference
< XDataSeries
> > ChartModelHelper::getDataSeries(
158 const uno::Reference
< frame::XModel
> & xModel
)
160 return getDataSeries( uno::Reference
< chart2::XChartDocument
>( xModel
, uno::UNO_QUERY
));
163 uno::Reference
< XChartType
> ChartModelHelper::getChartTypeOfSeries(
164 const uno::Reference
< frame::XModel
>& xModel
165 , const uno::Reference
< XDataSeries
>& xGivenDataSeries
)
167 return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel
), xGivenDataSeries
);
170 awt::Size
ChartModelHelper::getDefaultPageSize()
172 return awt::Size( 16000, 9000 );
175 awt::Size
ChartModelHelper::getPageSize( const uno::Reference
< frame::XModel
>& xModel
)
177 awt::Size
aPageSize( ChartModelHelper::getDefaultPageSize() );
178 uno::Reference
< embed::XVisualObject
> xVisualObject(xModel
,uno::UNO_QUERY
);
179 OSL_ENSURE(xVisualObject
.is(),"need xVisualObject for page size");
180 if( xVisualObject
.is() )
181 aPageSize
= xVisualObject
->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT
);
185 void ChartModelHelper::triggerRangeHighlighting( const uno::Reference
< frame::XModel
>& xModel
)
187 uno::Reference
< chart2::data::XDataReceiver
> xDataReceiver( xModel
, uno::UNO_QUERY
);
188 if( xDataReceiver
.is() )
190 uno::Reference
< view::XSelectionChangeListener
> xSelectionChangeListener( xDataReceiver
->getRangeHighlighter(), uno::UNO_QUERY
);
191 //trigger selection of cell range
192 if( xSelectionChangeListener
.is() )
194 lang::EventObject
aEvent( xSelectionChangeListener
);
195 xSelectionChangeListener
->selectionChanged( aEvent
);
200 bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference
< frame::XModel
>& xChartModel
)
202 bool bIncluded
= true; // hidden cells are included by default.
204 uno::Reference
< chart2::XDiagram
> xDiagram( ChartModelHelper::findDiagram(xChartModel
) );
208 uno::Reference
< beans::XPropertySet
> xProp( xDiagram
, uno::UNO_QUERY
);
214 xProp
->getPropertyValue("IncludeHiddenCells") >>= bIncluded
;
216 catch( const beans::UnknownPropertyException
& )
223 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells
, ChartModel
& rModel
)
225 bool bChanged
= false;
228 ControllerLockGuard
aLockedControllers( rModel
);
230 uno::Reference
< beans::XPropertySet
> xDiagramProperties( rModel
.getFirstDiagram(), uno::UNO_QUERY
);
231 if (xDiagramProperties
.is())
233 bool bOldValue
= bIncludeHiddenCells
;
234 xDiagramProperties
->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue
;
235 if( bOldValue
== bIncludeHiddenCells
)
238 //set the property on all instances in all cases to get the different objects in sync!
240 uno::Any aNewValue
= uno::makeAny(bIncludeHiddenCells
);
244 uno::Reference
< beans::XPropertySet
> xDataProviderProperties( rModel
.getDataProvider(), uno::UNO_QUERY
);
245 if( xDataProviderProperties
.is() )
246 xDataProviderProperties
->setPropertyValue("IncludeHiddenCells", aNewValue
);
248 catch( const beans::UnknownPropertyException
& )
250 //the property is optional!
255 uno::Reference
< chart2::data::XDataSource
> xUsedData( DataSourceHelper::getUsedData( rModel
) );
258 uno::Reference
< beans::XPropertySet
> xProp
;
259 uno::Sequence
< uno::Reference
< chart2::data::XLabeledDataSequence
> > aData( xUsedData
->getDataSequences());
260 for( sal_Int32 i
=0; i
<aData
.getLength(); ++i
)
262 xProp
.set( uno::Reference
< beans::XPropertySet
>( aData
[i
]->getValues(), uno::UNO_QUERY
) );
264 xProp
->setPropertyValue("IncludeHiddenCells", aNewValue
);
265 xProp
.set( uno::Reference
< beans::XPropertySet
>( aData
[i
]->getLabel(), uno::UNO_QUERY
) );
267 xProp
->setPropertyValue("IncludeHiddenCells", aNewValue
);
271 catch( const beans::UnknownPropertyException
& )
273 //the property is optional!
276 xDiagramProperties
->setPropertyValue( "IncludeHiddenCells", aNewValue
);
279 catch (const uno::Exception
& e
)
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */