Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / chart2uno.hxx
bloba8ac4422464e413a2890a085097aedb80dc6a398
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 #pragma once
22 #include "cellsuno.hxx"
23 #include "rangelst.hxx"
24 #include "externalrefmgr.hxx"
25 #include "types.hxx"
26 #include "chartlis.hxx"
28 #include <svl/lstner.hxx>
29 #include <com/sun/star/chart2/data/XDataProvider.hpp>
30 #include <com/sun/star/chart2/data/XSheetDataProvider.hpp>
31 #include <com/sun/star/chart2/data/XRangeXMLConversion.hpp>
32 #include <com/sun/star/chart2/data/XDataSource.hpp>
33 #include <com/sun/star/chart2/data/XDataSequence.hpp>
34 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
35 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
36 #include <com/sun/star/chart2/data/DataSequenceRole.hpp>
37 #include <com/sun/star/chart2/XTimeBased.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/util/XCloneable.hpp>
41 #include <com/sun/star/util/XModifyBroadcaster.hpp>
42 #include <cppuhelper/implbase.hxx>
43 #include <rtl/ustring.hxx>
44 #include <svl/itemprop.hxx>
46 #include <memory>
47 #include <optional>
48 #include <unordered_set>
49 #include <vector>
51 namespace com::sun::star::chart2::data { class XLabeledDataSequence; }
53 class ScDocument;
55 // DataProvider
56 class SC_DLLPUBLIC ScChart2DataProvider final : public
57 ::cppu::WeakImplHelper<
58 css::chart2::data::XDataProvider,
59 css::chart2::data::XSheetDataProvider,
60 css::chart2::data::XRangeXMLConversion,
61 css::beans::XPropertySet,
62 css::lang::XServiceInfo>,
63 public SfxListener
65 public:
67 explicit ScChart2DataProvider( ScDocument* pDoc );
68 virtual ~ScChart2DataProvider() override;
69 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
71 // XDataProvider
72 virtual sal_Bool SAL_CALL createDataSourcePossible(
73 const css::uno::Sequence< css::beans::PropertyValue >& aArguments ) override;
75 virtual css::uno::Reference<
76 css::chart2::data::XDataSource > SAL_CALL createDataSource(
77 const css::uno::Sequence< css::beans::PropertyValue >& aArguments ) override;
79 virtual css::uno::Sequence<
80 css::beans::PropertyValue > SAL_CALL detectArguments(
81 const css::uno::Reference< css::chart2::data::XDataSource >& xDataSource ) override;
83 virtual sal_Bool SAL_CALL createDataSequenceByRangeRepresentationPossible(
84 const OUString& aRangeRepresentation ) override;
86 virtual css::uno::Reference<
87 css::chart2::data::XDataSequence > SAL_CALL createDataSequenceByRangeRepresentation(
88 const OUString& aRangeRepresentation ) override;
90 virtual css::uno::Reference<css::chart2::data::XDataSequence> SAL_CALL
91 createDataSequenceByValueArray( const OUString& aRole, const OUString& aRangeRepresentation,
92 const OUString& aRoleQualifier ) override;
94 virtual css::uno::Reference< css::sheet::XRangeSelection > SAL_CALL getRangeSelection() override;
96 // XSheetDataProvider
97 virtual sal_Bool SAL_CALL createDataSequenceByFormulaTokensPossible(
98 const css::uno::Sequence< css::sheet::FormulaToken >& aTokens ) override;
100 virtual css::uno::Reference< css::chart2::data::XDataSequence >
101 SAL_CALL createDataSequenceByFormulaTokens(
102 const css::uno::Sequence< css::sheet::FormulaToken >& aTokens ) override;
104 // XRangeXMLConversion
105 virtual OUString SAL_CALL convertRangeToXML( const OUString& sRangeRepresentation ) override;
107 virtual OUString SAL_CALL convertRangeFromXML( const OUString& sXMLRange ) override;
109 // XPropertySet
110 virtual css::uno::Reference< css::beans::XPropertySetInfo> SAL_CALL
111 getPropertySetInfo() override;
113 virtual void SAL_CALL setPropertyValue(
114 const OUString& rPropertyName,
115 const css::uno::Any& rValue) override;
117 virtual css::uno::Any SAL_CALL getPropertyValue(
118 const OUString& rPropertyName) override;
120 virtual void SAL_CALL addPropertyChangeListener(
121 const OUString& rPropertyName,
122 const css::uno::Reference< css::beans::XPropertyChangeListener>& xListener) override;
124 virtual void SAL_CALL removePropertyChangeListener(
125 const OUString& rPropertyName,
126 const css::uno::Reference< css::beans::XPropertyChangeListener>& rListener) override;
128 virtual void SAL_CALL addVetoableChangeListener(
129 const OUString& rPropertyName,
130 const css::uno::Reference< css::beans::XVetoableChangeListener>& rListener) override;
132 virtual void SAL_CALL removeVetoableChangeListener(
133 const OUString& rPropertyName,
134 const css::uno::Reference< css::beans::XVetoableChangeListener>& rListener) override;
136 // XServiceInfo
137 virtual OUString SAL_CALL getImplementationName() override;
139 virtual sal_Bool SAL_CALL supportsService( const OUString&
140 rServiceName) override;
142 virtual css::uno::Sequence< OUString> SAL_CALL
143 getSupportedServiceNames() override;
145 private:
147 ScDocument* m_pDocument;
148 SfxItemPropertySet m_aPropSet;
149 bool m_bIncludeHiddenCells;
152 // DataSource
153 class ScChart2DataSource final : public
154 ::cppu::WeakImplHelper<
155 css::chart2::data::XDataSource,
156 css::lang::XServiceInfo>,
157 public SfxListener
159 public:
161 explicit ScChart2DataSource( ScDocument* pDoc);
162 virtual ~ScChart2DataSource() override;
163 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
165 // XDataSource
166 virtual css::uno::Sequence< css::uno::Reference<
167 css::chart2::data::XLabeledDataSequence > > SAL_CALL
168 getDataSequences() override;
170 // XServiceInfo
171 virtual OUString SAL_CALL getImplementationName() override;
173 virtual sal_Bool SAL_CALL supportsService( const OUString&
174 rServiceName) override;
176 virtual css::uno::Sequence< OUString> SAL_CALL
177 getSupportedServiceNames() override;
179 // implementation
181 void AddLabeledSequence(const css::uno::Reference < css::chart2::data::XLabeledDataSequence >& xNew);
183 private:
185 ScDocument* m_pDocument;
186 std::vector < css::uno::Reference< css::chart2::data::XLabeledDataSequence > > m_aLabeledSequences;
190 // DataSequence
191 class ScChart2DataSequence final : public
192 ::cppu::WeakImplHelper<
193 css::chart2::data::XDataSequence,
194 css::chart2::data::XTextualDataSequence,
195 css::chart2::data::XNumericalDataSequence,
196 css::chart2::XTimeBased,
197 css::util::XCloneable,
198 css::util::XModifyBroadcaster,
199 css::beans::XPropertySet,
200 css::lang::XServiceInfo>,
201 public SfxListener
203 public:
204 explicit ScChart2DataSequence( ScDocument* pDoc,
205 ::std::vector<ScTokenRef>&& rTokens, bool bIncludeHiddenCells );
207 virtual ~ScChart2DataSequence() override;
208 ScChart2DataSequence(ScDocument* pDoc, const ScChart2DataSequence&);
209 ScChart2DataSequence(const ScChart2DataSequence&) = delete;
210 ScChart2DataSequence& operator=(const ScChart2DataSequence&) = delete;
212 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
214 // XDataSequence
215 virtual css::uno::Sequence< css::uno::Any >
216 SAL_CALL getData() override;
217 virtual OUString SAL_CALL getSourceRangeRepresentation() override;
218 virtual css::uno::Sequence< OUString >
219 SAL_CALL generateLabel(css::chart2::data::LabelOrigin nOrigin) override;
221 /** Get the number format key for the n-th data entry
222 * If nIndex == -1, then you will get the number format key for the first non-empty entry
224 virtual ::sal_Int32 SAL_CALL getNumberFormatKeyByIndex( ::sal_Int32 nIndex ) override;
226 // XNumericalDataSequence
227 virtual css::uno::Sequence< double >
228 SAL_CALL getNumericalData() override;
230 // XTextualDataSequence
231 virtual css::uno::Sequence< OUString >
232 SAL_CALL getTextualData() override;
234 // XTimeBased
235 virtual sal_Bool SAL_CALL switchToNext(sal_Bool bWrap) override;
236 virtual sal_Bool SAL_CALL setToPointInTime(sal_Int32 nPoint) override;
238 virtual void SAL_CALL setRange(sal_Int32 nStart, sal_Int32 nEnd) override;
240 // XPropertySet
241 virtual css::uno::Reference<
242 css::beans::XPropertySetInfo> SAL_CALL
243 getPropertySetInfo() override;
245 virtual void SAL_CALL setPropertyValue(
246 const OUString& rPropertyName,
247 const css::uno::Any& rValue) override;
249 virtual css::uno::Any SAL_CALL getPropertyValue(
250 const OUString& rPropertyName) override;
252 virtual void SAL_CALL addPropertyChangeListener(
253 const OUString& rPropertyName,
254 const css::uno::Reference< css::beans::XPropertyChangeListener>& xListener) override;
256 virtual void SAL_CALL removePropertyChangeListener(
257 const OUString& rPropertyName,
258 const css::uno::Reference< css::beans::XPropertyChangeListener>& rListener) override;
260 virtual void SAL_CALL addVetoableChangeListener(
261 const OUString& rPropertyName,
262 const css::uno::Reference< css::beans::XVetoableChangeListener>& rListener) override;
264 virtual void SAL_CALL removeVetoableChangeListener(
265 const OUString& rPropertyName,
266 const css::uno::Reference< css::beans::XVetoableChangeListener>& rListener) override;
268 // XCloneable
269 virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override;
271 // XModifyBroadcaster
272 virtual void SAL_CALL addModifyListener(
273 const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
274 virtual void SAL_CALL removeModifyListener(
275 const css::uno::Reference< css::util::XModifyListener >& aListener ) override;
277 // XServiceInfo
278 virtual OUString SAL_CALL getImplementationName() override;
280 virtual sal_Bool SAL_CALL supportsService( const OUString&
281 rServiceName) override;
283 virtual css::uno::Sequence< OUString> SAL_CALL
284 getSupportedServiceNames() override;
286 private:
287 void setDataChangedHint(bool b);
289 // Implementation
290 void RefChanged();
291 DECL_LINK( ValueListenerHdl, const SfxHint&, void );
293 private:
294 class ExternalRefListener final : public ScExternalRefManager::LinkListener
296 public:
297 ExternalRefListener(ScChart2DataSequence& rParent, ScDocument* pDoc);
298 virtual ~ExternalRefListener() override;
299 ExternalRefListener(const ExternalRefListener&) = delete;
300 ExternalRefListener& operator=(const ExternalRefListener&) = delete;
302 virtual void notify(sal_uInt16 nFileId, ScExternalRefManager::LinkUpdateType eType) override;
303 void addFileId(sal_uInt16 nFileId);
304 const std::unordered_set<sal_uInt16>& getAllFileIds() const { return maFileIds;}
306 private:
307 ScChart2DataSequence& mrParent;
308 std::unordered_set<sal_uInt16> maFileIds;
309 ScDocument* mpDoc;
313 * Build an internal data array to cache the data ranges, and other
314 * information such as hidden values.
316 void BuildDataCache();
318 void RebuildDataCache();
320 sal_Int32 FillCacheFromExternalRef(const ScTokenRef& pToken);
322 void UpdateTokensFromRanges(const ScRangeList& rRanges);
324 ExternalRefListener* GetExtRefListener();
326 void StopListeningToAllExternalRefs();
328 private:
330 // data array
331 struct Item
333 double mfValue;
334 OUString maString;
335 bool mbIsValue;
336 ScAddress mAddress;
337 Item();
340 class HiddenRangeListener final : public ScChartHiddenRangeListener
342 public:
343 HiddenRangeListener(ScChart2DataSequence& rParent);
344 virtual ~HiddenRangeListener() override;
346 virtual void notify() override;
348 private:
349 ScChart2DataSequence& mrParent;
352 /** This vector contains the cached data which was calculated with BuildDataCache().
353 We use a shared_ptr because chart likes to Clone this class, which is very
354 expensive when we have lots of data. */
355 std::shared_ptr<std::vector<Item>> m_xDataArray;
358 * Cached data for getData. We may also need to cache data for the
359 * numerical and textural data series if they turn out to be bottlenecks
360 * under certain scenarios.
362 css::uno::Sequence< css::uno::Any > m_aMixedDataCache;
364 css::uno::Sequence<sal_Int32> m_aHiddenValues;
366 // properties
367 css::chart2::data::DataSequenceRole m_aRole;
368 bool m_bIncludeHiddenCells;
370 // internals
371 sal_Int64 m_nObjectId;
372 ScDocument* m_pDocument;
373 std::vector<ScTokenRef> m_aTokens;
374 std::optional<std::vector<sal_uInt32>>
375 m_oRangeIndices;
376 std::unique_ptr<ExternalRefListener>
377 m_pExtRefListener;
378 SfxItemPropertySet m_aPropSet;
380 std::unique_ptr<HiddenRangeListener> m_pHiddenListener;
382 std::unique_ptr<ScLinkListener> m_pValueListener;
383 XModifyListenerArr_Impl m_aValueListeners;
385 bool m_bGotDataChangedHint;
386 bool m_bExtDataRebuildQueued;
388 bool mbTimeBased;
389 SCTAB mnTimeBasedStart;
390 SCTAB mnTimeBasedEnd;
391 SCTAB mnCurrentTab;
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */