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>
21 #include <DiagramHelper.hxx>
22 #include <Diagram.hxx>
23 #include <DataSource.hxx>
24 #include <DataSourceHelper.hxx>
25 #include <ControllerLockGuard.hxx>
26 #include <RangeHighlighter.hxx>
27 #include <InternalDataProvider.hxx>
28 #include <ChartModel.hxx>
29 #include <BaseCoordinateSystem.hxx>
30 #include <ChartType.hxx>
31 #include <DataSeries.hxx>
33 #include <com/sun/star/chart/ChartDataRowSource.hpp>
34 #include <com/sun/star/chart/XChartDocument.hpp>
35 #include <com/sun/star/embed/Aspects.hpp>
36 #include <com/sun/star/view/XSelectionChangeListener.hpp>
37 #include <comphelper/diagnose_ex.hxx>
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::chart2
;
44 rtl::Reference
< InternalDataProvider
> ChartModelHelper::createInternalDataProvider(
45 const rtl::Reference
<::chart::ChartModel
>& xChartDoc
, bool bConnectToModel
)
47 bool bDefaultDataInColumns(true);
49 // #i120559# Try to access the current state of "DataRowSource" for the
50 // chart data and use it as default for creating a new InternalDataProvider
53 // old XChartDocument interface
54 css::uno::Reference
< css::chart::XChartDocument
> xDoc(static_cast<cppu::OWeakObject
*>(xChartDoc
.get()), uno::UNO_QUERY
);
58 css::uno::Reference
< css::chart::XDiagram
> aDiagram
= xDoc
->getDiagram();
62 css::uno::Reference
< css::beans::XPropertySet
> xProp(aDiagram
, uno::UNO_QUERY
);
66 css::chart::ChartDataRowSource
aDataRowSource(css::chart::ChartDataRowSource_COLUMNS
);
68 xProp
->getPropertyValue( "DataRowSource" ) >>= aDataRowSource
;
70 bDefaultDataInColumns
= (aDataRowSource
== css::chart::ChartDataRowSource_COLUMNS
);
76 return new InternalDataProvider( xChartDoc
, bConnectToModel
, bDefaultDataInColumns
);
79 rtl::Reference
< BaseCoordinateSystem
> ChartModelHelper::getFirstCoordinateSystem( const rtl::Reference
<::chart::ChartModel
>& xModel
)
81 rtl::Reference
< Diagram
> xDiagram
= xModel
->getFirstChartDiagram();
84 auto aCooSysSeq( xDiagram
->getBaseCoordinateSystems() );
85 if( !aCooSysSeq
.empty() )
91 std::vector
< rtl::Reference
< DataSeries
> > ChartModelHelper::getDataSeries(
92 const rtl::Reference
<::chart::ChartModel
> & xChartDoc
)
94 std::vector
< rtl::Reference
< DataSeries
> > aResult
;
96 rtl::Reference
< Diagram
> xDiagram
= xChartDoc
->getFirstChartDiagram();
98 aResult
= xDiagram
->getDataSeries();
103 rtl::Reference
< ChartType
> ChartModelHelper::getChartTypeOfSeries(
104 const rtl::Reference
<::chart::ChartModel
>& xModel
105 , const rtl::Reference
< DataSeries
>& xGivenDataSeries
)
107 rtl::Reference
<Diagram
> xDiagram
= xModel
->getFirstChartDiagram();
108 return xDiagram
? xDiagram
->getChartTypeOfSeries( xGivenDataSeries
) : nullptr;
111 awt::Size
ChartModelHelper::getDefaultPageSize()
113 return awt::Size( 16000, 9000 );
116 awt::Size
ChartModelHelper::getPageSize( const rtl::Reference
<::chart::ChartModel
>& xModel
)
118 awt::Size
aPageSize( ChartModelHelper::getDefaultPageSize() );
119 OSL_ENSURE(xModel
.is(),"need xVisualObject for page size");
121 aPageSize
= xModel
->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT
);
125 void ChartModelHelper::triggerRangeHighlighting( const rtl::Reference
<::chart::ChartModel
>& xModel
)
129 uno::Reference
< view::XSelectionChangeListener
> xSelectionChangeListener( xModel
->getRangeHighlighter(), uno::UNO_QUERY
);
130 //trigger selection of cell range
131 if( xSelectionChangeListener
.is() )
133 lang::EventObject
aEvent( xSelectionChangeListener
);
134 xSelectionChangeListener
->selectionChanged( aEvent
);
139 bool ChartModelHelper::isIncludeHiddenCells( const rtl::Reference
<::chart::ChartModel
>& xChartModel
)
141 bool bIncluded
= true; // hidden cells are included by default.
143 rtl::Reference
< Diagram
> xDiagram( xChartModel
->getFirstChartDiagram() );
149 xDiagram
->getPropertyValue("IncludeHiddenCells") >>= bIncluded
;
151 catch( const beans::UnknownPropertyException
& )
158 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells
, ChartModel
& rModel
)
160 bool bChanged
= false;
163 ControllerLockGuard
aLockedControllers( rModel
);
165 uno::Reference
< beans::XPropertySet
> xDiagramProperties( rModel
.getFirstDiagram(), uno::UNO_QUERY
);
166 if (xDiagramProperties
.is())
168 bool bOldValue
= bIncludeHiddenCells
;
169 xDiagramProperties
->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue
;
170 if( bOldValue
== bIncludeHiddenCells
)
173 //set the property on all instances in all cases to get the different objects in sync!
175 uno::Any
aNewValue(bIncludeHiddenCells
);
179 uno::Reference
< beans::XPropertySet
> xDataProviderProperties( rModel
.getDataProvider(), uno::UNO_QUERY
);
180 if( xDataProviderProperties
.is() )
181 xDataProviderProperties
->setPropertyValue("IncludeHiddenCells", aNewValue
);
183 catch( const beans::UnknownPropertyException
& )
185 //the property is optional!
190 rtl::Reference
< DataSource
> xUsedData
= DataSourceHelper::getUsedData( rModel
);
193 uno::Reference
< beans::XPropertySet
> xProp
;
194 const uno::Sequence
< uno::Reference
< chart2::data::XLabeledDataSequence
> > aData( xUsedData
->getDataSequences());
195 for( uno::Reference
< chart2::data::XLabeledDataSequence
> const & labeledData
: aData
)
197 xProp
.set( uno::Reference
< beans::XPropertySet
>( labeledData
->getValues(), uno::UNO_QUERY
) );
199 xProp
->setPropertyValue("IncludeHiddenCells", aNewValue
);
200 xProp
.set( uno::Reference
< beans::XPropertySet
>( labeledData
->getLabel(), uno::UNO_QUERY
) );
202 xProp
->setPropertyValue("IncludeHiddenCells", aNewValue
);
206 catch( const beans::UnknownPropertyException
& )
208 //the property is optional!
211 xDiagramProperties
->setPropertyValue( "IncludeHiddenCells", aNewValue
);
214 catch (const uno::Exception
&)
216 TOOLS_WARN_EXCEPTION("chart2", "" );
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */