Get the style color and number just once
[LibreOffice.git] / chart2 / source / tools / UncachedDataSequence.cxx
blob917135bef8aad85757e4e72563fab0e958b4c5a0
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 <UncachedDataSequence.hxx>
21 #include <CommonFunctors.hxx>
22 #include <ModifyListenerHelper.hxx>
23 #include <InternalDataProvider.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <algorithm>
27 #include <strings.hrc>
28 #include <ResId.hxx>
29 #include <utility>
31 using namespace ::com::sun::star;
33 using ::com::sun::star::uno::Sequence;
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::Any;
37 // necessary for MS compiler
38 using ::chart::impl::UncachedDataSequence_Base;
40 namespace
42 constexpr OUString lcl_aServiceName = u"com.sun.star.comp.chart.UncachedDataSequence"_ustr;
44 enum
46 PROP_NUMBERFORMAT_KEY,
47 PROP_PROPOSED_ROLE,
48 PROP_XML_RANGE
50 } // anonymous namespace
52 namespace chart
55 UncachedDataSequence::UncachedDataSequence(
56 rtl::Reference< InternalDataProvider > xIntDataProv,
57 OUString aRangeRepresentation )
58 : m_nNumberFormatKey(0),
59 m_xDataProvider(std::move( xIntDataProv )),
60 m_aSourceRepresentation(std::move( aRangeRepresentation )),
61 m_xModifyEventForwarder( new ModifyEventForwarder() )
63 registerProperties();
66 UncachedDataSequence::UncachedDataSequence(
67 rtl::Reference< InternalDataProvider > xIntDataProv,
68 OUString aRangeRepresentation,
69 const OUString & rRole )
70 : m_nNumberFormatKey(0),
71 m_xDataProvider(std::move( xIntDataProv )),
72 m_aSourceRepresentation(std::move( aRangeRepresentation )),
73 m_xModifyEventForwarder( new ModifyEventForwarder() )
75 registerProperties();
76 std::unique_lock<std::mutex> aGuard;
77 setFastPropertyValue_NoBroadcast( aGuard, PROP_PROPOSED_ROLE, uno::Any( rRole ));
80 UncachedDataSequence::UncachedDataSequence( const UncachedDataSequence & rSource )
81 : m_nNumberFormatKey( rSource.m_nNumberFormatKey ),
82 m_sRole( rSource.m_sRole ),
83 m_xDataProvider( rSource.m_xDataProvider ),
84 m_aSourceRepresentation( rSource.m_aSourceRepresentation ),
85 m_xModifyEventForwarder( new ModifyEventForwarder() )
87 registerProperties();
90 UncachedDataSequence::~UncachedDataSequence()
93 void UncachedDataSequence::registerProperties()
95 registerProperty( u"NumberFormatKey"_ustr,
96 PROP_NUMBERFORMAT_KEY,
97 0, // PropertyAttributes
98 & m_nNumberFormatKey,
99 cppu::UnoType<decltype(m_nNumberFormatKey)>::get() );
101 registerProperty( u"Role"_ustr,
102 PROP_PROPOSED_ROLE,
103 0, // PropertyAttributes
104 & m_sRole,
105 cppu::UnoType<decltype(m_sRole)>::get() );
107 registerProperty( u"CachedXMLRange"_ustr,
108 PROP_XML_RANGE,
109 0, // PropertyAttributes
110 & m_aXMLRange,
111 cppu::UnoType<decltype(m_aXMLRange)>::get() );
114 IMPLEMENT_FORWARD_XINTERFACE2( UncachedDataSequence, UncachedDataSequence_Base, comphelper::OPropertyContainer2 )
115 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UncachedDataSequence, UncachedDataSequence_Base, comphelper::OPropertyContainer2 )
117 // ____ XPropertySet ____
118 Reference< beans::XPropertySetInfo > SAL_CALL UncachedDataSequence::getPropertySetInfo()
120 return createPropertySetInfo( getInfoHelper() );
123 // ____ ::comphelper::OPropertySetHelper ____
124 ::cppu::IPropertyArrayHelper& UncachedDataSequence::getInfoHelper()
126 return *getArrayHelper();
129 // ____ ::comphelper::OPropertyArrayHelper ____
130 ::cppu::IPropertyArrayHelper* UncachedDataSequence::createArrayHelper() const
132 Sequence< beans::Property > aProps;
133 // describes all properties which have been registered in the ctor
134 describeProperties( aProps );
136 return new ::cppu::OPropertyArrayHelper( aProps );
139 OUString SAL_CALL UncachedDataSequence::getImplementationName()
141 return lcl_aServiceName;
144 sal_Bool SAL_CALL UncachedDataSequence::supportsService( const OUString& rServiceName )
146 return cppu::supportsService(this, rServiceName);
149 css::uno::Sequence< OUString > SAL_CALL UncachedDataSequence::getSupportedServiceNames()
151 return {
152 lcl_aServiceName,
153 u"com.sun.star.chart2.data.DataSequence"_ustr,
154 u"com.sun.star.chart2.data.NumericalDataSequence"_ustr,
155 u"com.sun.star.chart2.data.TextualDataSequence"_ustr
159 // ________ XNumericalDataSequence ________
160 Sequence< double > SAL_CALL UncachedDataSequence::getNumericalData()
162 std::unique_lock<std::mutex> aGuard;
163 if( m_xDataProvider.is())
165 const Sequence< uno::Any > aValues( m_xDataProvider->getDataByRangeRepresentation( m_aSourceRepresentation ));
166 return CommonFunctors::convertToSequence(aValues, CommonFunctors::ToDouble());
168 return {};
171 // ________ XTextualDataSequence ________
172 Sequence< OUString > SAL_CALL UncachedDataSequence::getTextualData()
174 std::unique_lock<std::mutex> aGuard;
175 if( m_xDataProvider.is())
177 const Sequence< uno::Any > aValues( m_xDataProvider->getDataByRangeRepresentation( m_aSourceRepresentation ));
178 return CommonFunctors::convertToSequence(aValues, CommonFunctors::ToString());
180 return {};
183 // ________ XDataSequence ________
184 Sequence< Any > SAL_CALL UncachedDataSequence::getData()
186 std::unique_lock<std::mutex> aGuard;
187 if( m_xDataProvider.is())
188 return m_xDataProvider->getDataByRangeRepresentation( m_aSourceRepresentation );
189 return Sequence< Any >();
192 OUString SAL_CALL UncachedDataSequence::getSourceRangeRepresentation()
194 return getName();
197 Sequence< OUString > SAL_CALL UncachedDataSequence::generateLabel( chart2::data::LabelOrigin )
199 // auto-generated label
200 sal_Int32 nSeries = m_aSourceRepresentation.toInt32() + 1;
201 OUString aResString(::chart::SchResId(STR_DATA_UNNAMED_SERIES_WITH_INDEX));
202 static constexpr OUString aReplacementStr(u"%NUMBER"_ustr);
203 sal_Int32 nIndex = aResString.indexOf(aReplacementStr);
204 OUString aName;
205 if( nIndex != -1 )
206 aName = aResString.replaceAt(nIndex, aReplacementStr.getLength(), OUString::number(nSeries));
207 return Sequence< OUString >( &aName, 1 );
210 ::sal_Int32 SAL_CALL UncachedDataSequence::getNumberFormatKeyByIndex( ::sal_Int32 )
212 return m_nNumberFormatKey;
215 // ____ XIndexReplace ____
216 void SAL_CALL UncachedDataSequence::replaceByIndex( ::sal_Int32 Index, const uno::Any& Element )
218 std::unique_lock<std::mutex> aGuard;
219 Sequence< Any > aData( getData());
220 if( Index < aData.getLength() &&
221 m_xDataProvider.is() )
223 aData.getArray()[Index] = Element;
224 m_xDataProvider->setDataByRangeRepresentation( m_aSourceRepresentation, aData );
225 fireModifyEvent();
229 // ____ XIndexAccess (base of XIndexReplace) ____
230 ::sal_Int32 SAL_CALL UncachedDataSequence::getCount()
232 OSL_FAIL( "Implement!" );
233 return 0;
236 uno::Any SAL_CALL UncachedDataSequence::getByIndex( ::sal_Int32 )
238 OSL_FAIL( "Implement!" );
239 return uno::Any();
242 // ____ XElementAccess (base of XIndexAccess) ____
243 uno::Type SAL_CALL UncachedDataSequence::getElementType()
245 return cppu::UnoType<uno::Any>::get();
248 sal_Bool SAL_CALL UncachedDataSequence::hasElements()
250 if( ! m_xDataProvider.is())
251 return false;
252 return m_xDataProvider->hasDataByRangeRepresentation( m_aSourceRepresentation );
255 // ____ XNamed ____
256 OUString SAL_CALL UncachedDataSequence::getName()
258 return m_aSourceRepresentation;
261 void SAL_CALL UncachedDataSequence::setName( const OUString& aName )
263 m_aSourceRepresentation = aName;
264 fireModifyEvent();
267 Reference< util::XCloneable > SAL_CALL UncachedDataSequence::createClone()
269 return new UncachedDataSequence( *this );
272 // ____ XModifiable ____
273 sal_Bool SAL_CALL UncachedDataSequence::isModified()
275 return false;
278 void SAL_CALL UncachedDataSequence::setModified( sal_Bool bModified )
280 if( bModified )
281 fireModifyEvent();
284 // ____ XModifyBroadcaster (base of XModifiable) ____
285 void SAL_CALL UncachedDataSequence::addModifyListener( const Reference< util::XModifyListener >& aListener )
287 m_xModifyEventForwarder->addModifyListener( aListener );
290 void SAL_CALL UncachedDataSequence::removeModifyListener( const Reference< util::XModifyListener >& aListener )
292 m_xModifyEventForwarder->removeModifyListener( aListener );
295 void UncachedDataSequence::fireModifyEvent()
297 // @todo: currently never called, as data changes are not yet reported by
298 // the data provider
299 m_xModifyEventForwarder->modified( lang::EventObject( static_cast< uno::XWeak* >( this )));
302 } // namespace chart
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */