tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / WrappedStockProperties.cxx
blobafd0d0d7e85a23cb08ed589183a109c038c903d4
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 <DataSeries.hxx>
24 #include <ControllerLockGuard.hxx>
25 #include <WrappedProperty.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <comphelper/diagnose_ex.hxx>
28 #include <ChartTypeManager.hxx>
29 #include <ChartTypeTemplate.hxx>
30 #include <utility>
32 using namespace ::com::sun::star;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::beans::Property;
36 namespace chart::wrapper
39 namespace {
41 class WrappedStockProperty : public WrappedProperty
43 public:
44 explicit WrappedStockProperty( const OUString& rOuterName
45 , css::uno::Any aDefaultValue
46 , std::shared_ptr<Chart2ModelContact> spChart2ModelContact );
48 void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
50 css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& xInnerPropertyState ) const override;
52 virtual rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const = 0;
54 protected:
55 std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact;
56 mutable css::uno::Any m_aOuterValue;
57 css::uno::Any m_aDefaultValue;
62 WrappedStockProperty::WrappedStockProperty( const OUString& rOuterName
63 , css::uno::Any aDefaultValue
64 , std::shared_ptr<Chart2ModelContact> spChart2ModelContact )
65 : WrappedProperty(rOuterName,OUString())
66 , m_spChart2ModelContact(std::move(spChart2ModelContact))
67 , m_aDefaultValue(std::move(aDefaultValue))
71 void WrappedStockProperty::setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
73 bool bNewValue = false;
74 if( ! (rOuterValue >>= bNewValue) )
75 throw lang::IllegalArgumentException( u"stock properties require type sal_Bool"_ustr, nullptr, 0 );
77 m_aOuterValue = rOuterValue;
79 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
80 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
81 if( !xChartDoc || !xDiagram )
82 return;
83 sal_Int32 nDimension = xDiagram->getDimension();
84 if( nDimension != 2 )
85 return;
87 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
88 Diagram::tTemplateWithServiceName aTemplateAndService =
89 xDiagram->getTemplate( xChartTypeManager );
91 rtl::Reference< ::chart::ChartTypeTemplate > xTemplate =
92 getNewTemplate( bNewValue, aTemplateAndService.sServiceName, xChartTypeManager );
94 if(!xTemplate.is())
95 return;
97 try
99 // locked controllers
100 ControllerLockGuardUNO aCtrlLockGuard( m_spChart2ModelContact->getDocumentModel() );
101 xTemplate->changeDiagram( xDiagram );
103 catch( const uno::Exception & )
105 DBG_UNHANDLED_EXCEPTION("chart2");
109 css::uno::Any WrappedStockProperty::getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const
111 return m_aDefaultValue;
114 namespace {
116 class WrappedVolumeProperty : public WrappedStockProperty
118 public:
119 explicit WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
121 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
123 rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const override;
128 WrappedVolumeProperty::WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
129 : WrappedStockProperty( u"Volume"_ustr, uno::Any(false) , spChart2ModelContact )
133 css::uno::Any WrappedVolumeProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
135 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
136 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
137 if( xDiagram.is() && xChartDoc.is() )
139 std::vector< rtl::Reference< DataSeries > > aSeriesVector =
140 xDiagram->getDataSeries();
141 if( !aSeriesVector.empty() )
143 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
144 Diagram::tTemplateWithServiceName aTemplateAndService =
145 xDiagram->getTemplate( xChartTypeManager );
147 if( aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockVolumeLowHighClose"
148 || aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
149 m_aOuterValue <<= true;
150 else if( !aTemplateAndService.sServiceName.isEmpty() || !m_aOuterValue.hasValue() )
151 m_aOuterValue <<= false;
153 else if(!m_aOuterValue.hasValue())
154 m_aOuterValue <<= false;
156 return m_aOuterValue;
159 rtl::Reference< ::chart::ChartTypeTemplate > WrappedVolumeProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const
161 rtl::Reference< ::chart::ChartTypeTemplate > xTemplate;
163 if(!xFactory.is())
164 return xTemplate;
166 if( bNewValue ) //add volume
168 if( rCurrentTemplate == "com.sun.star.chart2.template.StockLowHighClose" )
169 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockVolumeLowHighClose"_ustr );
170 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockOpenLowHighClose" )
171 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockVolumeOpenLowHighClose"_ustr );
173 else //remove volume
175 if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeLowHighClose" )
176 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockLowHighClose"_ustr );
177 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
178 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockOpenLowHighClose"_ustr );
180 return xTemplate;
183 namespace {
185 class WrappedUpDownProperty : public WrappedStockProperty
187 public:
188 explicit WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
190 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
192 rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ChartTypeManager >& xFactory ) const override;
197 WrappedUpDownProperty::WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
198 : WrappedStockProperty( u"UpDown"_ustr, uno::Any(false) , spChart2ModelContact )
202 css::uno::Any WrappedUpDownProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
204 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
205 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
206 if( xDiagram.is() && xChartDoc.is() )
208 std::vector< rtl::Reference< DataSeries > > aSeriesVector =
209 xDiagram->getDataSeries();
210 if( !aSeriesVector.empty() )
212 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
213 Diagram::tTemplateWithServiceName aTemplateAndService =
214 xDiagram->getTemplate( xChartTypeManager );
216 if( aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockOpenLowHighClose"
217 || aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
218 m_aOuterValue <<= true;
219 else if( !aTemplateAndService.sServiceName.isEmpty() || !m_aOuterValue.hasValue() )
220 m_aOuterValue <<= false;
222 else if(!m_aOuterValue.hasValue())
223 m_aOuterValue <<= false;
225 return m_aOuterValue;
227 rtl::Reference< ::chart::ChartTypeTemplate > WrappedUpDownProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ChartTypeManager >& xFactory ) const
229 rtl::Reference< ::chart::ChartTypeTemplate > xTemplate;
230 if( bNewValue ) //add open series
232 if( rCurrentTemplate == "com.sun.star.chart2.template.StockLowHighClose" )
233 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockOpenLowHighClose"_ustr );
234 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeLowHighClose" )
235 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockVolumeOpenLowHighClose"_ustr );
237 else //remove open series
239 if( rCurrentTemplate == "com.sun.star.chart2.template.StockOpenLowHighClose" )
240 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockLowHighClose"_ustr );
241 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
242 xTemplate = xFactory->createTemplate( u"com.sun.star.chart2.template.StockVolumeLowHighClose"_ustr );
244 return xTemplate;
247 namespace
249 enum
251 //spline properties
252 PROP_CHART_STOCK_VOLUME = FAST_PROPERTY_ID_START_CHART_STOCK_PROP
253 , PROP_CHART_STOCK_UPDOWN
256 }//anonymous namespace
258 void WrappedStockProperties::addProperties( std::vector< Property > & rOutProperties )
260 rOutProperties.emplace_back( "Volume",
261 PROP_CHART_STOCK_VOLUME,
262 cppu::UnoType<sal_Bool>::get(),
263 beans::PropertyAttribute::BOUND
264 | beans::PropertyAttribute::MAYBEDEFAULT
265 | beans::PropertyAttribute::MAYBEVOID );
266 rOutProperties.emplace_back( "UpDown",
267 PROP_CHART_STOCK_UPDOWN,
268 cppu::UnoType<sal_Bool>::get(),
269 beans::PropertyAttribute::BOUND
270 | beans::PropertyAttribute::MAYBEDEFAULT
271 | beans::PropertyAttribute::MAYBEVOID );
274 void WrappedStockProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
275 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
277 rList.emplace_back( new WrappedVolumeProperty( spChart2ModelContact ) );
278 rList.emplace_back( new WrappedUpDownProperty( spChart2ModelContact ) );
281 } //namespace chart::wrapper
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */