Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / WrappedStockProperties.cxx
blob9123543d90363dbe2469b5de599dee7507da5ed1
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 "WrappedStockProperties.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <FastPropertyIdRanges.hxx>
23 #include <DiagramHelper.hxx>
24 #include <ControllerLockGuard.hxx>
25 #include <WrappedProperty.hxx>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/chart2/XChartDocument.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <tools/diagnose_ex.h>
31 using namespace ::com::sun::star;
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::beans::Property;
35 namespace chart
37 namespace wrapper
40 class WrappedStockProperty : public WrappedProperty
42 public:
43 explicit WrappedStockProperty( const OUString& rOuterName
44 , const css::uno::Any& rDefaulValue
45 , const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact );
47 void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
49 css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& xInnerPropertyState ) const override;
51 virtual uno::Reference< chart2::XChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const = 0;
53 protected:
54 std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact;
55 mutable css::uno::Any m_aOuterValue;
56 css::uno::Any m_aDefaultValue;
59 WrappedStockProperty::WrappedStockProperty( const OUString& rOuterName
60 , const css::uno::Any& rDefaulValue
61 , const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact )
62 : WrappedProperty(rOuterName,OUString())
63 , m_spChart2ModelContact(spChart2ModelContact)
64 , m_aOuterValue()
65 , m_aDefaultValue(rDefaulValue)
69 void WrappedStockProperty::setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
71 bool bNewValue = false;
72 if( ! (rOuterValue >>= bNewValue) )
73 throw lang::IllegalArgumentException( "stock properties require type sal_Bool", nullptr, 0 );
75 m_aOuterValue = rOuterValue;
77 Reference< chart2::XChartDocument > xChartDoc( m_spChart2ModelContact->getChart2Document() );
78 Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
79 sal_Int32 nDimension = ::chart::DiagramHelper::getDimension( xDiagram );
80 if( xChartDoc.is() && xDiagram.is() && nDimension==2 )
82 Reference< lang::XMultiServiceFactory > xFactory( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
83 DiagramHelper::tTemplateWithServiceName aTemplateAndService =
84 DiagramHelper::getTemplateForDiagram( xDiagram, xFactory );
86 uno::Reference< chart2::XChartTypeTemplate > xTemplate =
87 getNewTemplate( bNewValue, aTemplateAndService.second, xFactory );
89 if(xTemplate.is())
91 try
93 // locked controllers
94 ControllerLockGuardUNO aCtrlLockGuard( m_spChart2ModelContact->getChartModel() );
95 xTemplate->changeDiagram( xDiagram );
97 catch( const uno::Exception & )
99 DBG_UNHANDLED_EXCEPTION("chart2");
105 css::uno::Any WrappedStockProperty::getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const
107 return m_aDefaultValue;
110 class WrappedVolumeProperty : public WrappedStockProperty
112 public:
113 explicit WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
115 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
117 uno::Reference< chart2::XChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const override;
120 WrappedVolumeProperty::WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
121 : WrappedStockProperty( "Volume", uno::Any(false) , spChart2ModelContact )
125 css::uno::Any WrappedVolumeProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
127 Reference< chart2::XChartDocument > xChartDoc( m_spChart2ModelContact->getChart2Document() );
128 Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
129 if( xDiagram.is() && xChartDoc.is() )
131 std::vector< uno::Reference< chart2::XDataSeries > > aSeriesVector(
132 DiagramHelper::getDataSeriesFromDiagram( xDiagram ) );
133 if( !aSeriesVector.empty() )
135 Reference< lang::XMultiServiceFactory > xFact( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
136 DiagramHelper::tTemplateWithServiceName aTemplateAndService =
137 DiagramHelper::getTemplateForDiagram( xDiagram, xFact );
139 if( aTemplateAndService.second == "com.sun.star.chart2.template.StockVolumeLowHighClose"
140 || aTemplateAndService.second == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
141 m_aOuterValue <<= true;
142 else if( !aTemplateAndService.second.isEmpty() || !m_aOuterValue.hasValue() )
143 m_aOuterValue <<= false;
145 else if(!m_aOuterValue.hasValue())
146 m_aOuterValue <<= false;
148 return m_aOuterValue;
151 uno::Reference< chart2::XChartTypeTemplate > WrappedVolumeProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const
153 uno::Reference< chart2::XChartTypeTemplate > xTemplate;
155 if(!xFactory.is())
156 return xTemplate;
158 if( bNewValue ) //add volume
160 if( rCurrentTemplate == "com.sun.star.chart2.template.StockLowHighClose" )
161 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockVolumeLowHighClose" ), uno::UNO_QUERY );
162 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockOpenLowHighClose" )
163 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ), uno::UNO_QUERY );
165 else //remove volume
167 if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeLowHighClose" )
168 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockLowHighClose" ), uno::UNO_QUERY );
169 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
170 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockOpenLowHighClose" ), uno::UNO_QUERY );
172 return xTemplate;
175 class WrappedUpDownProperty : public WrappedStockProperty
177 public:
178 explicit WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
180 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
182 uno::Reference< chart2::XChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const override;
185 WrappedUpDownProperty::WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
186 : WrappedStockProperty( "UpDown", uno::Any(false) , spChart2ModelContact )
190 css::uno::Any WrappedUpDownProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
192 Reference< chart2::XChartDocument > xChartDoc( m_spChart2ModelContact->getChart2Document() );
193 Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
194 if( xDiagram.is() && xChartDoc.is() )
196 std::vector< uno::Reference< chart2::XDataSeries > > aSeriesVector(
197 DiagramHelper::getDataSeriesFromDiagram( xDiagram ) );
198 if( !aSeriesVector.empty() )
200 Reference< lang::XMultiServiceFactory > xFact( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
201 DiagramHelper::tTemplateWithServiceName aTemplateAndService =
202 DiagramHelper::getTemplateForDiagram( xDiagram, xFact );
204 if( aTemplateAndService.second == "com.sun.star.chart2.template.StockOpenLowHighClose"
205 || aTemplateAndService.second == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
206 m_aOuterValue <<= true;
207 else if( !aTemplateAndService.second.isEmpty() || !m_aOuterValue.hasValue() )
208 m_aOuterValue <<= false;
210 else if(!m_aOuterValue.hasValue())
211 m_aOuterValue <<= false;
213 return m_aOuterValue;
215 uno::Reference< chart2::XChartTypeTemplate > WrappedUpDownProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const
217 uno::Reference< chart2::XChartTypeTemplate > xTemplate;
218 if( bNewValue ) //add open series
220 if( rCurrentTemplate == "com.sun.star.chart2.template.StockLowHighClose" )
221 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockOpenLowHighClose" ), uno::UNO_QUERY );
222 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeLowHighClose" )
223 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ), uno::UNO_QUERY );
225 else //remove open series
227 if( rCurrentTemplate == "com.sun.star.chart2.template.StockOpenLowHighClose" )
228 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockLowHighClose" ), uno::UNO_QUERY );
229 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
230 xTemplate.set( xFactory->createInstance( "com.sun.star.chart2.template.StockVolumeLowHighClose" ), uno::UNO_QUERY );
232 return xTemplate;
235 namespace
237 enum
239 //spline properties
240 PROP_CHART_STOCK_VOLUME = FAST_PROPERTY_ID_START_CHART_STOCK_PROP
241 , PROP_CHART_STOCK_UPDOWN
244 }//anonymous namespace
246 void WrappedStockProperties::addProperties( std::vector< Property > & rOutProperties )
248 rOutProperties.emplace_back( "Volume",
249 PROP_CHART_STOCK_VOLUME,
250 cppu::UnoType<sal_Bool>::get(),
251 beans::PropertyAttribute::BOUND
252 | beans::PropertyAttribute::MAYBEDEFAULT
253 | beans::PropertyAttribute::MAYBEVOID );
254 rOutProperties.emplace_back( "UpDown",
255 PROP_CHART_STOCK_UPDOWN,
256 cppu::UnoType<sal_Bool>::get(),
257 beans::PropertyAttribute::BOUND
258 | beans::PropertyAttribute::MAYBEDEFAULT
259 | beans::PropertyAttribute::MAYBEVOID );
262 void WrappedStockProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
263 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
265 rList.emplace_back( new WrappedVolumeProperty( spChart2ModelContact ) );
266 rList.emplace_back( new WrappedUpDownProperty( spChart2ModelContact ) );
269 } //namespace wrapper
270 } //namespace chart
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */