vcl: allow for overriding the default PDF rendering resolution
[LibreOffice.git] / chart2 / source / tools / ChartModelHelper.cxx
blob2ff699fb0f4a518dfe5d2047584d5a080fe4aff4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <DataSourceHelper.hxx>
23 #include <ControllerLockGuard.hxx>
24 #include <RangeHighlighter.hxx>
25 #include <InternalDataProvider.hxx>
26 #include <ChartModel.hxx>
28 #include <com/sun/star/chart/ChartDataRowSource.hpp>
29 #include <com/sun/star/chart/XChartDocument.hpp>
30 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
31 #include <com/sun/star/chart2/XChartDocument.hpp>
32 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
33 #include <com/sun/star/embed/Aspects.hpp>
34 #include <com/sun/star/embed/XVisualObject.hpp>
35 #include <com/sun/star/view/XSelectionChangeListener.hpp>
36 #include <tools/diagnose_ex.h>
37 #include <sal/log.hxx>
39 namespace chart
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::chart2;
44 uno::Reference< chart2::data::XRangeHighlighter > ChartModelHelper::createRangeHighlighter(
45 const uno::Reference< view::XSelectionSupplier > & xSelectionSupplier )
47 return new RangeHighlighter( xSelectionSupplier );
50 uno::Reference< chart2::data::XDataProvider > ChartModelHelper::createInternalDataProvider(
51 const uno::Reference< css::chart2::XChartDocument >& xChartDoc, bool bConnectToModel )
53 bool bDefaultDataInColumns(true);
55 // #i120559# Try to access the current state of "DataRowSource" for the
56 // chart data and use it as default for creating a new InternalDataProvider
57 if(xChartDoc.is())
59 css::uno::Reference< css::chart::XChartDocument > xDoc(xChartDoc, uno::UNO_QUERY);
61 if(xDoc.is())
63 css::uno::Reference< css::chart::XDiagram > aDiagram = xDoc->getDiagram();
65 if(aDiagram.is())
67 css::uno::Reference< css::beans::XPropertySet > xProp(aDiagram, uno::UNO_QUERY);
69 if(xProp.is())
71 css::chart::ChartDataRowSource aDataRowSource(css::chart::ChartDataRowSource_COLUMNS);
73 xProp->getPropertyValue( "DataRowSource" ) >>= aDataRowSource;
75 bDefaultDataInColumns = (aDataRowSource == css::chart::ChartDataRowSource_COLUMNS);
81 return new InternalDataProvider( xChartDoc, bConnectToModel, bDefaultDataInColumns );
84 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< frame::XModel >& xModel )
86 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
87 if( xChartDoc.is())
88 return ChartModelHelper::findDiagram( xChartDoc );
89 return nullptr;
92 uno::Reference< XDiagram > ChartModelHelper::findDiagram( const uno::Reference< chart2::XChartDocument >& xChartDoc )
94 try
96 if( xChartDoc.is())
97 return xChartDoc->getFirstDiagram();
99 catch( const uno::Exception & )
101 DBG_UNHANDLED_EXCEPTION("chart2");
103 return nullptr;
106 uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( ChartModel& rModel )
108 uno::Reference< XCoordinateSystem > XCooSys;
109 uno::Reference< XCoordinateSystemContainer > xCooSysCnt( rModel.getFirstDiagram(), uno::UNO_QUERY );
110 if( xCooSysCnt.is() )
112 uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
113 if( aCooSysSeq.hasElements() )
114 XCooSys = aCooSysSeq[0];
116 return XCooSys;
119 uno::Reference< XCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const uno::Reference< frame::XModel >& xModel )
121 uno::Reference< XCoordinateSystem > XCooSys;
122 uno::Reference< XCoordinateSystemContainer > xCooSysCnt( ChartModelHelper::findDiagram( xModel ), uno::UNO_QUERY );
123 if( xCooSysCnt.is() )
125 uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems() );
126 if( aCooSysSeq.hasElements() )
127 XCooSys = aCooSysSeq[0];
129 return XCooSys;
132 std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
133 ChartModel& rModel )
135 std::vector< uno::Reference< XDataSeries > > aResult;
137 uno::Reference< XDiagram > xDiagram = rModel.getFirstDiagram();
138 if( xDiagram.is())
139 aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
141 return aResult;
144 std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
145 const uno::Reference< XChartDocument > & xChartDoc )
147 std::vector< uno::Reference< XDataSeries > > aResult;
149 uno::Reference< XDiagram > xDiagram = ChartModelHelper::findDiagram( xChartDoc );
150 if( xDiagram.is())
151 aResult = DiagramHelper::getDataSeriesFromDiagram( xDiagram );
153 return aResult;
156 std::vector< uno::Reference< XDataSeries > > ChartModelHelper::getDataSeries(
157 const uno::Reference< frame::XModel > & xModel )
159 return getDataSeries( uno::Reference< chart2::XChartDocument >( xModel, uno::UNO_QUERY ));
162 uno::Reference< XChartType > ChartModelHelper::getChartTypeOfSeries(
163 const uno::Reference< frame::XModel >& xModel
164 , const uno::Reference< XDataSeries >& xGivenDataSeries )
166 return DiagramHelper::getChartTypeOfSeries( ChartModelHelper::findDiagram( xModel ), xGivenDataSeries );
169 awt::Size ChartModelHelper::getDefaultPageSize()
171 return awt::Size( 16000, 9000 );
174 awt::Size ChartModelHelper::getPageSize( const uno::Reference< frame::XModel >& xModel )
176 awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
177 uno::Reference< embed::XVisualObject > xVisualObject(xModel,uno::UNO_QUERY);
178 OSL_ENSURE(xVisualObject.is(),"need xVisualObject for page size");
179 if( xVisualObject.is() )
180 aPageSize = xVisualObject->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
181 return aPageSize;
184 void ChartModelHelper::triggerRangeHighlighting( const uno::Reference< frame::XModel >& xModel )
186 uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xModel, uno::UNO_QUERY );
187 if( xDataReceiver.is() )
189 uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xDataReceiver->getRangeHighlighter(), uno::UNO_QUERY );
190 //trigger selection of cell range
191 if( xSelectionChangeListener.is() )
193 lang::EventObject aEvent( xSelectionChangeListener );
194 xSelectionChangeListener->selectionChanged( aEvent );
199 bool ChartModelHelper::isIncludeHiddenCells( const uno::Reference< frame::XModel >& xChartModel )
201 bool bIncluded = true; // hidden cells are included by default.
203 uno::Reference< chart2::XDiagram > xDiagram( ChartModelHelper::findDiagram(xChartModel) );
204 if (!xDiagram.is())
205 return bIncluded;
207 uno::Reference< beans::XPropertySet > xProp( xDiagram, uno::UNO_QUERY );
208 if (!xProp.is())
209 return bIncluded;
213 xProp->getPropertyValue("IncludeHiddenCells") >>= bIncluded;
215 catch( const beans::UnknownPropertyException& )
219 return bIncluded;
222 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, ChartModel& rModel )
224 bool bChanged = false;
227 ControllerLockGuard aLockedControllers( rModel );
229 uno::Reference< beans::XPropertySet > xDiagramProperties( rModel.getFirstDiagram(), uno::UNO_QUERY );
230 if (xDiagramProperties.is())
232 bool bOldValue = bIncludeHiddenCells;
233 xDiagramProperties->getPropertyValue( "IncludeHiddenCells" ) >>= bOldValue;
234 if( bOldValue == bIncludeHiddenCells )
235 bChanged = true;
237 //set the property on all instances in all cases to get the different objects in sync!
239 uno::Any aNewValue(bIncludeHiddenCells);
243 uno::Reference< beans::XPropertySet > xDataProviderProperties( rModel.getDataProvider(), uno::UNO_QUERY );
244 if( xDataProviderProperties.is() )
245 xDataProviderProperties->setPropertyValue("IncludeHiddenCells", aNewValue );
247 catch( const beans::UnknownPropertyException& )
249 //the property is optional!
254 uno::Reference< chart2::data::XDataSource > xUsedData( DataSourceHelper::getUsedData( rModel ) );
255 if( xUsedData.is() )
257 uno::Reference< beans::XPropertySet > xProp;
258 uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
259 for( sal_Int32 i=0; i<aData.getLength(); ++i )
261 xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getValues(), uno::UNO_QUERY ) );
262 if(xProp.is())
263 xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
264 xProp.set( uno::Reference< beans::XPropertySet >( aData[i]->getLabel(), uno::UNO_QUERY ) );
265 if(xProp.is())
266 xProp->setPropertyValue("IncludeHiddenCells", aNewValue );
270 catch( const beans::UnknownPropertyException& )
272 //the property is optional!
275 xDiagramProperties->setPropertyValue( "IncludeHiddenCells", aNewValue);
278 catch (const uno::Exception&)
280 TOOLS_WARN_EXCEPTION("chart2", "" );
282 return bChanged;
285 } //namespace chart
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */