Debian Codegraph @

chart2/source/controller/chartapiwrapper/WrappedStockProperties.cxx

raw
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
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>
31
32 using namespace ::com::sun::star;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::beans::Property;
35
36 namespace chart::wrapper
37 {
38
39 namespace {
40
41 class WrappedStockProperty : public WrappedProperty
42 {
43 public:
44 explicit WrappedStockProperty( const OUString& rOuterName
45 , css::uno::Any aDefaultValue
46 , std::shared_ptr<Chart2ModelContact> spChart2ModelContact );
47
48 void setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
49
50 css::uno::Any getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& xInnerPropertyState ) const override;
51
52 virtual rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const = 0;
53
54 protected:
55 std::shared_ptr< Chart2ModelContact > m_spChart2ModelContact;
56 mutable css::uno::Any m_aOuterValue;
57 css::uno::Any m_aDefaultValue;
58 };
59
60 }
61
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))
68 {
69 }
70
71 void WrappedStockProperty::setPropertyValue( const css::uno::Any& rOuterValue, const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
72 {
73 bool bNewValue = false;
74 if( ! (rOuterValue >>= bNewValue) )
75 throw lang::IllegalArgumentException( u"stock properties require type sal_Bool"_ustr, nullptr, 0 );
76
77 m_aOuterValue = rOuterValue;
78
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;
86
87 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
88 Diagram::tTemplateWithServiceName aTemplateAndService =
89 xDiagram->getTemplate( xChartTypeManager );
90
91 rtl::Reference< ::chart::ChartTypeTemplate > xTemplate =
92 getNewTemplate( bNewValue, aTemplateAndService.sServiceName, xChartTypeManager );
93
94 if(!xTemplate.is())
95 return;
96
97 try
98 {
99 // locked controllers
100 ControllerLockGuardUNO aCtrlLockGuard( m_spChart2ModelContact->getDocumentModel() );
101 xTemplate->changeDiagram( xDiagram );
102 }
103 catch( const uno::Exception & )
104 {
105 DBG_UNHANDLED_EXCEPTION("chart2");
106 }
107 }
108
109 css::uno::Any WrappedStockProperty::getPropertyDefault( const css::uno::Reference< css::beans::XPropertyState >& /*xInnerPropertyState*/ ) const
110 {
111 return m_aDefaultValue;
112 }
113
114 namespace {
115
116 class WrappedVolumeProperty : public WrappedStockProperty
117 {
118 public:
119 explicit WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
120
121 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
122
123 rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const override;
124 };
125
126 }
127
128 WrappedVolumeProperty::WrappedVolumeProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
129 : WrappedStockProperty( u"Volume"_ustr, uno::Any(false) , spChart2ModelContact )
130 {
131 }
132
133 css::uno::Any WrappedVolumeProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
134 {
135 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
136 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
137 if( xDiagram.is() && xChartDoc.is() )
138 {
139 std::vector< rtl::Reference< DataSeries > > aSeriesVector =
140 xDiagram->getDataSeries();
141 if( !aSeriesVector.empty() )
142 {
143 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
144 Diagram::tTemplateWithServiceName aTemplateAndService =
145 xDiagram->getTemplate( xChartTypeManager );
146
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;
152 }
153 else if(!m_aOuterValue.hasValue())
154 m_aOuterValue <<= false;
155 }
156 return m_aOuterValue;
157 }
158
159 rtl::Reference< ::chart::ChartTypeTemplate > WrappedVolumeProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ::chart::ChartTypeManager >& xFactory ) const
160 {
161 rtl::Reference< ::chart::ChartTypeTemplate > xTemplate;
162
163 if(!xFactory.is())
164 return xTemplate;
165
166 if( bNewValue ) //add volume
167 {
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 );
172 }
173 else //remove volume
174 {
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 );
179 }
180 return xTemplate;
181 }
182
183 namespace {
184
185 class WrappedUpDownProperty : public WrappedStockProperty
186 {
187 public:
188 explicit WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact);
189
190 css::uno::Any getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& xInnerPropertySet ) const override;
191
192 rtl::Reference< ::chart::ChartTypeTemplate > getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ChartTypeManager >& xFactory ) const override;
193 };
194
195 }
196
197 WrappedUpDownProperty::WrappedUpDownProperty(const std::shared_ptr<Chart2ModelContact>& spChart2ModelContact)
198 : WrappedStockProperty( u"UpDown"_ustr, uno::Any(false) , spChart2ModelContact )
199 {
200 }
201
202 css::uno::Any WrappedUpDownProperty::getPropertyValue( const css::uno::Reference< css::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
203 {
204 rtl::Reference< ChartModel > xChartDoc( m_spChart2ModelContact->getDocumentModel() );
205 rtl::Reference< ::chart::Diagram > xDiagram( m_spChart2ModelContact->getDiagram() );
206 if( xDiagram.is() && xChartDoc.is() )
207 {
208 std::vector< rtl::Reference< DataSeries > > aSeriesVector =
209 xDiagram->getDataSeries();
210 if( !aSeriesVector.empty() )
211 {
212 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartDoc->getTypeManager();
213 Diagram::tTemplateWithServiceName aTemplateAndService =
214 xDiagram->getTemplate( xChartTypeManager );
215
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;
221 }
222 else if(!m_aOuterValue.hasValue())
223 m_aOuterValue <<= false;
224 }
225 return m_aOuterValue;
226 }
227 rtl::Reference< ::chart::ChartTypeTemplate > WrappedUpDownProperty::getNewTemplate( bool bNewValue, const OUString& rCurrentTemplate, const rtl::Reference< ChartTypeManager >& xFactory ) const
228 {
229 rtl::Reference< ::chart::ChartTypeTemplate > xTemplate;
230 if( bNewValue ) //add open series
231 {
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 );
236 }
237 else //remove open series
238 {
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 );
243 }
244 return xTemplate;
245 }
246
247 namespace
248 {
249 enum
250 {
251 //spline properties
252 PROP_CHART_STOCK_VOLUME = FAST_PROPERTY_ID_START_CHART_STOCK_PROP
253 , PROP_CHART_STOCK_UPDOWN
254 };
255
256 }//anonymous namespace
257
258 void WrappedStockProperties::addProperties( std::vector< Property > & rOutProperties )
259 {
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 );
272 }
273
274 void WrappedStockProperties::addWrappedProperties( std::vector< std::unique_ptr<WrappedProperty> >& rList
275 , const std::shared_ptr< Chart2ModelContact >& spChart2ModelContact )
276 {
277 rList.emplace_back( new WrappedVolumeProperty( spChart2ModelContact ) );
278 rList.emplace_back( new WrappedUpDownProperty( spChart2ModelContact ) );
279 }
280
281 } //namespace chart::wrapper
282
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */