LibreOffice Module chart2 (master) 1
WrappedStockProperties.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
23#include <DataSeries.hxx>
24#include <DiagramHelper.hxx>
26#include <WrappedProperty.hxx>
27#include <com/sun/star/beans/PropertyAttribute.hpp>
29#include <ChartTypeManager.hxx>
30#include <ChartTypeTemplate.hxx>
31#include <utility>
32
33using namespace ::com::sun::star;
34using ::com::sun::star::uno::Reference;
35using ::com::sun::star::beans::Property;
36
37namespace chart::wrapper
38{
39
40namespace {
41
42class WrappedStockProperty : public WrappedProperty
43{
44public:
45 explicit WrappedStockProperty( const OUString& rOuterName
46 , css::uno::Any aDefaultValue
47 , std::shared_ptr<Chart2ModelContact> spChart2ModelContact );
48
49 void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
50
51 css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& xInnerPropertyState ) const override;
52
53 virtual rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const = 0;
54
55protected:
56 std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact;
57 mutable css::uno::Any m_aOuterValue;
58 css::uno::Any m_aDefaultValue;
59};
60
61}
62
63WrappedStockProperty::WrappedStockProperty( const OUString& rOuterName
64 , css::uno::Any aDefaultValue
65 , std::shared_ptr<Chart2ModelContact> spChart2ModelContact )
66 : WrappedProperty(rOuterName,OUString())
67 , m_spChart2ModelContact(std::move(spChart2ModelContact))
68 , m_aDefaultValue(std::move(aDefaultValue))
69{
70}
71
72void WrappedStockProperty::setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
73{
74 bool bNewValue = false;
75 if( ! (rOuterValue >>= bNewValue) )
76 throw lang::IllegalArgumentException( "stock properties require type sal_Bool", nullptr, 0 );
77
78 m_aOuterValue = rOuterValue;
79
80 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
82 if( !xChartDoc || !xDiagram )
83 return;
84 sal_Int32 nDimension = xDiagram->getDimension();
85 if( nDimension != 2 )
86 return;
87
88 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
89 Diagram::tTemplateWithServiceName aTemplateAndService =
90 xDiagram->getTemplate( xChartTypeManager );
91
93 getNewTemplate( bNewValue, aTemplateAndService.sServiceName, xChartTypeManager );
94
95 if(!xTemplate.is())
96 return;
97
98 try
99 {
100 // locked controllers
101 ControllerLockGuardUNO aCtrlLockGuard( m_spChart2ModelContact->getDocumentModel() );
102 xTemplate->changeDiagram( xDiagram );
103 }
104 catch( const uno::Exception & )
105 {
106 DBG_UNHANDLED_EXCEPTION("chart2");
107 }
108}
109
110css::uno::Any WrappedStockProperty::getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const
111{
112 return m_aDefaultValue;
113}
114
115namespace {
116
117class WrappedVolumeProperty : public WrappedStockProperty
118{
119public:
120 explicit WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
121
122 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
123
124 rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const override;
125};
126
127}
128
129WrappedVolumeProperty::WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
130 : WrappedStockProperty( "Volume", uno::Any(false) , spChart2ModelContact )
131{
132}
133
134css::uno::Any WrappedVolumeProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
135{
136 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
138 if( xDiagram.is() && xChartDoc.is() )
139 {
140 std::vector< rtl::Reference< DataSeries > > aSeriesVector =
141 xDiagram->getDataSeries();
142 if( !aSeriesVector.empty() )
143 {
144 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
145 Diagram::tTemplateWithServiceName aTemplateAndService =
146 xDiagram->getTemplate( xChartTypeManager );
147
148 if( aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockVolumeLowHighClose"
149 || aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
150 m_aOuterValue <<= true;
151 else if( !aTemplateAndService.sServiceName.isEmpty() || !m_aOuterValue.hasValue() )
152 m_aOuterValue <<= false;
153 }
154 else if(!m_aOuterValue.hasValue())
155 m_aOuterValue <<= false;
156 }
157 return m_aOuterValue;
158}
159
160rtl::Reference< ::chart::ChartTypeTemplate > WrappedVolumeProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const
161{
163
164 if(!xFactory.is())
165 return xTemplate;
166
167 if( bNewValue ) //add volume
168 {
169 if( rCurrentTemplate == "com.sun.star.chart2.template.StockLowHighClose" )
170 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockVolumeLowHighClose" );
171 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockOpenLowHighClose" )
172 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" );
173 }
174 else //remove volume
175 {
176 if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeLowHighClose" )
177 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockLowHighClose" );
178 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
179 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockOpenLowHighClose" );
180 }
181 return xTemplate;
182}
183
184namespace {
185
186class WrappedUpDownProperty : public WrappedStockProperty
187{
188public:
189 explicit WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
190
191 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
192
193 rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ChartTypeManager >& xFactory ) const override;
194};
195
196}
197
198WrappedUpDownProperty::WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
199 : WrappedStockProperty( "UpDown", uno::Any(false) , spChart2ModelContact )
200{
201}
202
203css::uno::Any WrappedUpDownProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
204{
205 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
207 if( xDiagram.is() && xChartDoc.is() )
208 {
209 std::vector< rtl::Reference< DataSeries > > aSeriesVector =
210 xDiagram->getDataSeries();
211 if( !aSeriesVector.empty() )
212 {
213 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
214 Diagram::tTemplateWithServiceName aTemplateAndService =
215 xDiagram->getTemplate( xChartTypeManager );
216
217 if( aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockOpenLowHighClose"
218 || aTemplateAndService.sServiceName == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
219 m_aOuterValue <<= true;
220 else if( !aTemplateAndService.sServiceName.isEmpty() || !m_aOuterValue.hasValue() )
221 m_aOuterValue <<= false;
222 }
223 else if(!m_aOuterValue.hasValue())
224 m_aOuterValue <<= false;
225 }
226 return m_aOuterValue;
227}
228rtl::Reference< ::chart::ChartTypeTemplate > WrappedUpDownProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ChartTypeManager >& xFactory ) const
229{
231 if( bNewValue ) //add open series
232 {
233 if( rCurrentTemplate == "com.sun.star.chart2.template.StockLowHighClose" )
234 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockOpenLowHighClose" );
235 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeLowHighClose" )
236 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" );
237 }
238 else //remove open series
239 {
240 if( rCurrentTemplate == "com.sun.star.chart2.template.StockOpenLowHighClose" )
241 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockLowHighClose" );
242 else if( rCurrentTemplate == "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" )
243 xTemplate = xFactory->createTemplate( "com.sun.star.chart2.template.StockVolumeLowHighClose" );
244 }
245 return xTemplate;
246}
247
248namespace
249{
250enum
251{
252 //spline properties
253 PROP_CHART_STOCK_VOLUME = FAST_PROPERTY_ID_START_CHART_STOCK_PROP
254 , PROP_CHART_STOCK_UPDOWN
255};
256
257}//anonymous namespace
258
259void WrappedStockProperties::addProperties( std::vector< Property > & rOutProperties )
260{
261 rOutProperties.emplace_back( "Volume",
262 PROP_CHART_STOCK_VOLUME,
264 beans::PropertyAttribute::BOUND
265 | beans::PropertyAttribute::MAYBEDEFAULT
266 | beans::PropertyAttribute::MAYBEVOID );
267 rOutProperties.emplace_back( "UpDown",
268 PROP_CHART_STOCK_UPDOWN,
270 beans::PropertyAttribute::BOUND
271 | beans::PropertyAttribute::MAYBEDEFAULT
272 | beans::PropertyAttribute::MAYBEVOID );
273}
274
275void WrappedStockProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
276 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
277{
278 rList.emplace_back( new WrappedVolumeProperty( spChart2ModelContact ) );
279 rList.emplace_back( new WrappedUpDownProperty( spChart2ModelContact ) );
280}
281
282} //namespace chart::wrapper
283
284/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact
css::uno::Any m_aOuterValue
css::uno::Any m_aDefaultValue
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
void setPropertyValue(tPropertyValueMap &rOutMap, tPropertyValueMapKey key, const Value &value)
Set a property to a certain value in the given map.
@ FAST_PROPERTY_ID_START_CHART_STOCK_PROP
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)