tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / chart2 / source / tools / ChartModelHelper.cxx
blob70a5accd2effb31e1d7c5fa07eca9b0e477e4cae
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 <Diagram.hxx>
22 #include <DataSource.hxx>
23 #include <DataSourceHelper.hxx>
24 #include <ControllerLockGuard.hxx>
25 #include <InternalDataProvider.hxx>
26 #include <ChartModel.hxx>
27 #include <BaseCoordinateSystem.hxx>
28 #include <ChartType.hxx>
29 #include <DataSeries.hxx>
31 #include <com/sun/star/chart/ChartDataRowSource.hpp>
32 #include <com/sun/star/chart/XChartDocument.hpp>
33 #include <com/sun/star/embed/Aspects.hpp>
34 #include <com/sun/star/view/XSelectionChangeListener.hpp>
35 #include <comphelper/diagnose_ex.hxx>
37 namespace chart
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::chart2;
42 rtl::Reference< InternalDataProvider > ChartModelHelper::createInternalDataProvider(
43 const rtl::Reference<::chart::ChartModel>& xChartDoc, bool bConnectToModel )
45 bool bDefaultDataInColumns(true);
47 // #i120559# Try to access the current state of "DataRowSource" for the
48 // chart data and use it as default for creating a new InternalDataProvider
49 if(xChartDoc.is())
51 // old XChartDocument interface
52 css::uno::Reference< css::chart::XChartDocument > xDoc(static_cast<cppu::OWeakObject*>(xChartDoc.get()), uno::UNO_QUERY);
54 if(xDoc.is())
56 css::uno::Reference< css::chart::XDiagram > aDiagram = xDoc->getDiagram();
58 if(aDiagram.is())
60 css::uno::Reference< css::beans::XPropertySet > xProp(aDiagram, uno::UNO_QUERY);
62 if(xProp.is())
64 css::chart::ChartDataRowSource aDataRowSource(css::chart::ChartDataRowSource_COLUMNS);
66 xProp->getPropertyValue( u"DataRowSource"_ustr ) >>= aDataRowSource;
68 bDefaultDataInColumns = (aDataRowSource == css::chart::ChartDataRowSource_COLUMNS);
74 return new InternalDataProvider( xChartDoc, bConnectToModel, bDefaultDataInColumns );
77 rtl::Reference< BaseCoordinateSystem > ChartModelHelper::getFirstCoordinateSystem( const rtl::Reference<::chart::ChartModel>& xModel )
79 rtl::Reference< Diagram > xDiagram = xModel->getFirstChartDiagram();
80 if( xDiagram.is() )
82 auto aCooSysSeq( xDiagram->getBaseCoordinateSystems() );
83 if( !aCooSysSeq.empty() )
84 return aCooSysSeq[0];
86 return nullptr;
89 std::vector< rtl::Reference< DataSeries > > ChartModelHelper::getDataSeries(
90 const rtl::Reference<::chart::ChartModel> & xChartDoc )
92 std::vector< rtl::Reference< DataSeries > > aResult;
94 rtl::Reference< Diagram > xDiagram = xChartDoc->getFirstChartDiagram();
95 if( xDiagram.is())
96 aResult = xDiagram->getDataSeries();
98 return aResult;
101 rtl::Reference< ChartType > ChartModelHelper::getChartTypeOfSeries(
102 const rtl::Reference<::chart::ChartModel>& xModel
103 , const rtl::Reference< DataSeries >& xGivenDataSeries )
105 rtl::Reference<Diagram> xDiagram = xModel->getFirstChartDiagram();
106 return xDiagram ? xDiagram->getChartTypeOfSeries( xGivenDataSeries ) : nullptr;
109 awt::Size ChartModelHelper::getDefaultPageSize()
111 return awt::Size( 16000, 9000 );
114 awt::Size ChartModelHelper::getPageSize( const rtl::Reference<::chart::ChartModel>& xModel )
116 awt::Size aPageSize( ChartModelHelper::getDefaultPageSize() );
117 OSL_ENSURE(xModel.is(),"need xVisualObject for page size");
118 if( xModel.is() )
119 aPageSize = xModel->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
120 return aPageSize;
123 void ChartModelHelper::triggerRangeHighlighting( const rtl::Reference<::chart::ChartModel>& xModel )
125 if( xModel.is() )
127 uno::Reference< view::XSelectionChangeListener > xSelectionChangeListener( xModel->getRangeHighlighter(), uno::UNO_QUERY );
128 //trigger selection of cell range
129 if( xSelectionChangeListener.is() )
131 lang::EventObject aEvent( xSelectionChangeListener );
132 xSelectionChangeListener->selectionChanged( aEvent );
137 bool ChartModelHelper::isIncludeHiddenCells( const rtl::Reference<::chart::ChartModel>& xChartModel )
139 bool bIncluded = true; // hidden cells are included by default.
141 rtl::Reference< Diagram > xDiagram( xChartModel->getFirstChartDiagram() );
142 if (!xDiagram.is())
143 return bIncluded;
147 xDiagram->getPropertyValue(u"IncludeHiddenCells"_ustr) >>= bIncluded;
149 catch( const beans::UnknownPropertyException& )
153 return bIncluded;
156 bool ChartModelHelper::setIncludeHiddenCells( bool bIncludeHiddenCells, ChartModel& rModel )
158 bool bChanged = false;
161 ControllerLockGuard aLockedControllers( rModel );
163 uno::Reference< beans::XPropertySet > xDiagramProperties( rModel.getFirstDiagram(), uno::UNO_QUERY );
164 if (xDiagramProperties.is())
166 bool bOldValue = bIncludeHiddenCells;
167 xDiagramProperties->getPropertyValue( u"IncludeHiddenCells"_ustr ) >>= bOldValue;
168 if( bOldValue == bIncludeHiddenCells )
169 bChanged = true;
171 //set the property on all instances in all cases to get the different objects in sync!
173 uno::Any aNewValue(bIncludeHiddenCells);
177 uno::Reference< beans::XPropertySet > xDataProviderProperties( rModel.getDataProvider(), uno::UNO_QUERY );
178 if( xDataProviderProperties.is() )
179 xDataProviderProperties->setPropertyValue(u"IncludeHiddenCells"_ustr, aNewValue );
181 catch( const beans::UnknownPropertyException& )
183 //the property is optional!
188 rtl::Reference< DataSource > xUsedData = DataSourceHelper::getUsedData( rModel );
189 if( xUsedData.is() )
191 uno::Reference< beans::XPropertySet > xProp;
192 const uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aData( xUsedData->getDataSequences());
193 for( uno::Reference< chart2::data::XLabeledDataSequence > const & labeledData : aData )
195 xProp.set( uno::Reference< beans::XPropertySet >( labeledData->getValues(), uno::UNO_QUERY ) );
196 if(xProp.is())
197 xProp->setPropertyValue(u"IncludeHiddenCells"_ustr, aNewValue );
198 xProp.set( uno::Reference< beans::XPropertySet >( labeledData->getLabel(), uno::UNO_QUERY ) );
199 if(xProp.is())
200 xProp->setPropertyValue(u"IncludeHiddenCells"_ustr, aNewValue );
204 catch( const beans::UnknownPropertyException& )
206 //the property is optional!
209 xDiagramProperties->setPropertyValue( u"IncludeHiddenCells"_ustr, aNewValue);
212 catch (const uno::Exception&)
214 TOOLS_WARN_EXCEPTION("chart2", "" );
216 return bChanged;
219 } //namespace chart
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */