1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
25 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
26 #include <rtl/ustring.hxx>
27 #include <sal/types.h>
28 #include <tools/long.hxx>
31 #include "calcmacros.hxx"
33 #include <unordered_map>
34 #include <unordered_set>
37 namespace com::sun::star::sheet
{
38 class XDimensionsSupplier
;
39 struct DataPilotFieldReference
;
40 struct DataPilotFieldSortInfo
;
41 struct DataPilotFieldAutoShowInfo
;
42 struct DataPilotFieldLayoutInfo
;
45 class ScDPDimensionSaveData
;
47 enum class ScGeneralFunction
;
49 // classes to save Data Pilot settings
55 std::optional
<OUString
> mpLayoutName
; // custom name to be displayed in the table.
56 sal_uInt16 nVisibleMode
;
57 sal_uInt16 nShowDetailsMode
;
60 ScDPSaveMember(OUString aName
);
61 ScDPSaveMember(const ScDPSaveMember
& r
);
64 bool operator== ( const ScDPSaveMember
& r
) const;
66 const OUString
& GetName() const
69 SC_DLLPUBLIC
bool HasIsVisible() const;
70 SC_DLLPUBLIC
void SetIsVisible(bool bSet
);
71 bool GetIsVisible() const
72 { return bool(nVisibleMode
); }
74 SC_DLLPUBLIC
bool HasShowDetails() const;
75 SC_DLLPUBLIC
void SetShowDetails(bool bSet
);
76 bool GetShowDetails() const
77 { return bool(nShowDetailsMode
); }
79 void SetName( const OUString
& rNew
); // used if the source member was renamed (groups)
81 SC_DLLPUBLIC
void SetLayoutName( const OUString
& rName
);
82 SC_DLLPUBLIC
const std::optional
<OUString
> & GetLayoutName() const;
83 void RemoveLayoutName();
85 void WriteToSource( const css::uno::Reference
<css::uno::XInterface
>& xMember
,
86 sal_Int32 nPosition
);
89 void Dump(int nIndent
= 0) const;
93 class SC_DLLPUBLIC ScDPSaveDimension
97 std::optional
<OUString
> mpLayoutName
;
98 std::optional
<OUString
> mpSubtotalName
;
101 css::sheet::DataPilotFieldOrientation nOrientation
;
102 ScGeneralFunction nFunction
; // for data dimensions
103 tools::Long nUsedHierarchy
;
104 sal_uInt16 nShowEmptyMode
; //! at level
105 bool bRepeatItemLabels
; //! at level
106 bool bSubTotalDefault
; //! at level
107 std::vector
<ScGeneralFunction
> maSubTotalFuncs
;
108 std::unique_ptr
<css::sheet::DataPilotFieldReference
> pReferenceValue
;
109 std::unique_ptr
<css::sheet::DataPilotFieldSortInfo
> pSortInfo
; // (level)
110 std::unique_ptr
<css::sheet::DataPilotFieldAutoShowInfo
> pAutoShowInfo
; // (level)
111 std::unique_ptr
<css::sheet::DataPilotFieldLayoutInfo
> pLayoutInfo
; // (level)
114 typedef std::unordered_set
<OUString
> MemberSetType
;
115 typedef std::vector
<ScDPSaveMember
*> MemberList
;
118 std::unordered_map
<OUString
, std::unique_ptr
<ScDPSaveMember
>> maMemberHash
;
119 MemberList maMemberList
;
122 ScDPSaveDimension(OUString aName
, bool bDataLayout
);
123 ScDPSaveDimension(const ScDPSaveDimension
& r
);
124 ~ScDPSaveDimension();
126 bool operator== ( const ScDPSaveDimension
& r
) const;
128 const MemberList
& GetMembers() const
129 { return maMemberList
; }
131 void AddMember(std::unique_ptr
<ScDPSaveMember
> pMember
);
133 void SetDupFlag(bool bSet
)
136 bool GetDupFlag() const
139 const OUString
& GetName() const
142 bool IsDataLayout() const
143 { return bIsDataLayout
; }
145 void SetName( const OUString
& rNew
); // used if the source dim was renamed (groups)
147 void SetOrientation(css::sheet::DataPilotFieldOrientation nNew
);
148 void SetSubTotals(std::vector
<ScGeneralFunction
> && rFuncs
);
149 tools::Long
GetSubTotalsCount() const
150 { return maSubTotalFuncs
.size(); }
152 ScGeneralFunction
GetSubTotalFunc(tools::Long nIndex
) const
153 { return maSubTotalFuncs
[nIndex
]; }
155 bool HasShowEmpty() const;
156 void SetShowEmpty(bool bSet
);
157 bool GetShowEmpty() const
158 { return bool(nShowEmptyMode
); }
160 void SetRepeatItemLabels(bool bSet
);
161 bool GetRepeatItemLabels() const
162 { return bRepeatItemLabels
; }
164 void SetFunction(ScGeneralFunction nNew
);
165 ScGeneralFunction
GetFunction() const
166 { return nFunction
; }
168 void SetUsedHierarchy(tools::Long nNew
);
169 tools::Long
GetUsedHierarchy() const
170 { return nUsedHierarchy
; }
172 void SetLayoutName(const OUString
& rName
);
173 const std::optional
<OUString
> & GetLayoutName() const;
174 void RemoveLayoutName();
175 void SetSubtotalName(const OUString
& rName
);
176 const std::optional
<OUString
> & GetSubtotalName() const;
177 void RemoveSubtotalName();
179 bool IsMemberNameInUse(const OUString
& rName
) const;
181 const css::sheet::DataPilotFieldReference
* GetReferenceValue() const
182 { return pReferenceValue
.get(); }
184 void SetReferenceValue(const css::sheet::DataPilotFieldReference
* pNew
);
186 const css::sheet::DataPilotFieldSortInfo
* GetSortInfo() const
187 { return pSortInfo
.get(); }
189 void SetSortInfo(const css::sheet::DataPilotFieldSortInfo
* pNew
);
190 const css::sheet::DataPilotFieldAutoShowInfo
* GetAutoShowInfo() const
191 { return pAutoShowInfo
.get(); }
193 void SetAutoShowInfo(const css::sheet::DataPilotFieldAutoShowInfo
* pNew
);
194 const css::sheet::DataPilotFieldLayoutInfo
* GetLayoutInfo() const
195 { return pLayoutInfo
.get(); }
197 void SetLayoutInfo(const css::sheet::DataPilotFieldLayoutInfo
* pNew
);
199 void SetCurrentPage( const OUString
* pPage
); // NULL = no selection (all)
200 OUString
GetCurrentPage() const; // only for ODF compatibility
202 css::sheet::DataPilotFieldOrientation
GetOrientation() const
203 { return nOrientation
; }
205 ScDPSaveMember
* GetExistingMemberByName(const OUString
& rName
);
208 * Get a member object by its name. If one doesn't exist, create a new
209 * object and return it. This class manages the life cycle of all member
210 * objects belonging to it, so <i>don't delete the returned instance.</i>
212 * @param rName member name
214 * @return pointer to the member object.
216 ScDPSaveMember
* GetMemberByName(const OUString
& rName
);
218 void SetMemberPosition( const OUString
& rName
, sal_Int32 nNewPos
);
220 void WriteToSource( const css::uno::Reference
<css::uno::XInterface
>& xDim
);
222 void UpdateMemberVisibility(const std::unordered_map
< OUString
, bool>& rData
);
224 bool HasInvisibleMember() const;
226 void RemoveObsoleteMembers(const MemberSetType
& rMembers
);
229 void Dump(int nIndent
= 0) const;
235 typedef std::unordered_map
<OUString
, size_t> DupNameCountType
;
237 typedef std::unordered_map
<OUString
, size_t> DimOrderType
;
238 typedef std::vector
<std::unique_ptr
<ScDPSaveDimension
>> DimsType
;
242 DupNameCountType maDupNameCounts
; /// keep track of number of duplicates in each name.
243 std::unique_ptr
<ScDPDimensionSaveData
> pDimensionData
; // settings that create new dimensions
244 sal_uInt16 nColumnGrandMode
;
245 sal_uInt16 nRowGrandMode
;
246 sal_uInt16 nIgnoreEmptyMode
;
247 sal_uInt16 nRepeatEmptyMode
;
248 bool bFilterButton
; // not passed to DataPilotSource
249 bool bDrillDown
; // not passed to DataPilotSource
250 bool bExpandCollapse
; // not passed to DataPilotSource
252 /** if true, all dimensions already have all of their member instances
254 bool mbDimensionMembersBuilt
;
256 std::optional
<OUString
> mpGrandTotalName
;
257 mutable std::unique_ptr
<DimOrderType
> mpDimOrder
; // dimension order for row and column dimensions, to traverse result tree.
260 SC_DLLPUBLIC
ScDPSaveData();
261 ScDPSaveData(const ScDPSaveData
& r
);
262 SC_DLLPUBLIC
~ScDPSaveData();
264 ScDPSaveData
& operator= ( const ScDPSaveData
& r
);
266 bool operator== ( const ScDPSaveData
& r
) const;
268 SC_DLLPUBLIC
void SetGrandTotalName(const OUString
& rName
);
269 SC_DLLPUBLIC
const std::optional
<OUString
> & GetGrandTotalName() const;
271 const DimsType
& GetDimensions() const { return m_DimList
; }
274 * Get sort order map to sort row and column dimensions in order of
275 * appearance. Row dimensions get sorted before column dimensions. This
276 * is used to traverse result tree, which is structured following this
279 const DimOrderType
& GetDimensionSortOrder() const;
282 * Get all dimensions in a given orientation. The order represents the
283 * actual order of occurrence. The returned list also includes data
286 * @param eOrientation orientation
287 * @param rDims (out) list of dimensions for specified orientation
289 SC_DLLPUBLIC
void GetAllDimensionsByOrientation(
290 css::sheet::DataPilotFieldOrientation eOrientation
,
291 std::vector
<const ScDPSaveDimension
*>& rDims
) const;
293 void AddDimension(ScDPSaveDimension
* pDim
);
296 * Get a dimension object by its name. <i>If one doesn't exist for the
297 * given name, it creates a new one.</i>
299 * @param rName dimension name
301 * @return pointer to the dimension object. The ScDPSaveData instance
302 * manages its life cycle; hence the caller must
303 * <i>not</i> delete this object.
305 SC_DLLPUBLIC ScDPSaveDimension
* GetDimensionByName(const OUString
& rName
);
306 SC_DLLPUBLIC ScDPSaveDimension
* GetDataLayoutDimension();
307 SC_DLLPUBLIC ScDPSaveDimension
* GetExistingDataLayoutDimension() const;
309 ScDPSaveDimension
* DuplicateDimension(std::u16string_view rName
);
310 SC_DLLPUBLIC ScDPSaveDimension
& DuplicateDimension(const ScDPSaveDimension
& rDim
);
312 SC_DLLPUBLIC ScDPSaveDimension
* GetExistingDimensionByName(std::u16string_view rName
) const;
313 SC_DLLPUBLIC ScDPSaveDimension
* GetNewDimensionByName(const OUString
& rName
);
315 void RemoveDimensionByName(const OUString
& rName
);
317 ScDPSaveDimension
* GetInnermostDimension(css::sheet::DataPilotFieldOrientation nOrientation
);
318 ScDPSaveDimension
* GetFirstDimension(css::sheet::DataPilotFieldOrientation eOrientation
);
319 SC_DLLPUBLIC
tools::Long
GetDataDimensionCount() const;
321 void SetPosition( ScDPSaveDimension
* pDim
, tools::Long nNew
);
322 SC_DLLPUBLIC
void SetColumnGrand( bool bSet
);
323 bool GetColumnGrand() const
324 { return bool(nColumnGrandMode
); }
326 SC_DLLPUBLIC
void SetRowGrand( bool bSet
);
327 bool GetRowGrand() const
328 { return bool(nRowGrandMode
); }
330 SC_DLLPUBLIC
void SetIgnoreEmptyRows( bool bSet
);
331 bool GetIgnoreEmptyRows() const
332 { return bool(nIgnoreEmptyMode
); }
334 SC_DLLPUBLIC
void SetRepeatIfEmpty( bool bSet
);
335 bool GetRepeatIfEmpty() const
336 { return bool(nRepeatEmptyMode
); }
338 SC_DLLPUBLIC
void SetFilterButton( bool bSet
);
339 bool GetFilterButton() const
340 { return bFilterButton
; }
342 SC_DLLPUBLIC
void SetDrillDown( bool bSet
);
343 bool GetDrillDown() const
344 { return bDrillDown
; }
346 SC_DLLPUBLIC
void SetExpandCollapse( bool bSet
);
347 bool GetExpandCollapse() const
348 { return bExpandCollapse
; }
350 void WriteToSource( const css::uno::Reference
<css::sheet::XDimensionsSupplier
>& xSource
);
351 bool IsEmpty() const;
353 const ScDPDimensionSaveData
* GetExistingDimensionData() const
354 { return pDimensionData
.get(); }
356 void RemoveAllGroupDimensions( const OUString
& rSrcDimName
, std::vector
<OUString
>* pDeletedNames
= nullptr );
358 SC_DLLPUBLIC ScDPDimensionSaveData
* GetDimensionData(); // create if not there
359 SC_DLLPUBLIC
void SetDimensionData( const ScDPDimensionSaveData
* pNew
); // copied
360 void BuildAllDimensionMembers(ScDPTableData
* pData
);
361 void SyncAllDimensionMembers(ScDPTableData
* pData
);
364 * Check whether a dimension has one or more invisible members.
366 * @param rDimName dimension name
368 SC_DLLPUBLIC
bool HasInvisibleMember(std::u16string_view rDimName
) const;
375 void CheckDuplicateName(ScDPSaveDimension
& rDim
);
376 void RemoveDuplicateNameCount(const OUString
& rName
);
379 * Append a new original dimension. Not to be called to insert a duplicate
382 * @param rName Dimension name. The name must be the original dimension
383 * name; not a duplicate dimension name.
384 * @param bDataLayout true if this is a data layout dimension, false
387 * @return pointer to the new dimension just inserted.
389 ScDPSaveDimension
* AppendNewDimension(const OUString
& rName
, bool bDataLayout
);
391 void DimensionsChanged();
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */