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
;
48 namespace sc
{ class PivotTableFormats
; }
50 // classes to save Data Pilot settings
56 std::optional
<OUString
> mpLayoutName
; // custom name to be displayed in the table.
57 sal_uInt16 nVisibleMode
;
58 sal_uInt16 nShowDetailsMode
;
61 ScDPSaveMember(OUString aName
);
62 ScDPSaveMember(const ScDPSaveMember
& r
);
65 bool operator== ( const ScDPSaveMember
& r
) const;
67 const OUString
& GetName() const
70 SC_DLLPUBLIC
bool HasIsVisible() const;
71 SC_DLLPUBLIC
void SetIsVisible(bool bSet
);
72 bool GetIsVisible() const
73 { return bool(nVisibleMode
); }
75 SC_DLLPUBLIC
bool HasShowDetails() const;
76 SC_DLLPUBLIC
void SetShowDetails(bool bSet
);
77 bool GetShowDetails() const
78 { return bool(nShowDetailsMode
); }
80 void SetName( const OUString
& rNew
); // used if the source member was renamed (groups)
82 SC_DLLPUBLIC
void SetLayoutName( const OUString
& rName
);
83 SC_DLLPUBLIC
const std::optional
<OUString
> & GetLayoutName() const;
84 void RemoveLayoutName();
86 void WriteToSource( const css::uno::Reference
<css::uno::XInterface
>& xMember
,
87 sal_Int32 nPosition
);
90 void Dump(int nIndent
= 0) const;
94 class ScDPSaveDimension
98 std::optional
<OUString
> mpLayoutName
;
99 std::optional
<OUString
> mpSubtotalName
;
102 css::sheet::DataPilotFieldOrientation nOrientation
;
103 ScGeneralFunction nFunction
; // for data dimensions
104 tools::Long nUsedHierarchy
;
105 sal_uInt16 nShowEmptyMode
; //! at level
106 bool bRepeatItemLabels
; //! at level
107 bool bSubTotalDefault
; //! at level
108 std::vector
<ScGeneralFunction
> maSubTotalFuncs
;
109 std::unique_ptr
<css::sheet::DataPilotFieldReference
> pReferenceValue
;
110 std::unique_ptr
<css::sheet::DataPilotFieldSortInfo
> pSortInfo
; // (level)
111 std::unique_ptr
<css::sheet::DataPilotFieldAutoShowInfo
> pAutoShowInfo
; // (level)
112 std::unique_ptr
<css::sheet::DataPilotFieldLayoutInfo
> pLayoutInfo
; // (level)
115 typedef std::unordered_set
<OUString
> MemberSetType
;
116 typedef std::vector
<ScDPSaveMember
*> MemberList
;
119 std::unordered_map
<OUString
, std::unique_ptr
<ScDPSaveMember
>> maMemberHash
;
120 MemberList maMemberList
;
123 ScDPSaveDimension(OUString aName
, bool bDataLayout
);
124 ScDPSaveDimension(const ScDPSaveDimension
& r
);
125 ~ScDPSaveDimension();
127 bool operator== ( const ScDPSaveDimension
& r
) const;
129 const MemberList
& GetMembers() const
130 { return maMemberList
; }
132 void AddMember(std::unique_ptr
<ScDPSaveMember
> pMember
);
134 void SetDupFlag(bool bSet
)
137 bool GetDupFlag() const
140 const OUString
& GetName() const
143 bool IsDataLayout() const
144 { return bIsDataLayout
; }
146 void SetName( const OUString
& rNew
); // used if the source dim was renamed (groups)
148 SC_DLLPUBLIC
void SetOrientation(css::sheet::DataPilotFieldOrientation nNew
);
149 SC_DLLPUBLIC
void SetSubTotals(std::vector
<ScGeneralFunction
> && rFuncs
);
150 tools::Long
GetSubTotalsCount() const
151 { return maSubTotalFuncs
.size(); }
153 ScGeneralFunction
GetSubTotalFunc(tools::Long nIndex
) const
154 { return maSubTotalFuncs
[nIndex
]; }
156 SC_DLLPUBLIC
bool HasShowEmpty() const;
157 SC_DLLPUBLIC
void SetShowEmpty(bool bSet
);
158 bool GetShowEmpty() const
159 { return bool(nShowEmptyMode
); }
161 void SetRepeatItemLabels(bool bSet
);
162 bool GetRepeatItemLabels() const
163 { return bRepeatItemLabels
; }
165 SC_DLLPUBLIC
void SetFunction(ScGeneralFunction nNew
);
166 ScGeneralFunction
GetFunction() const
167 { return nFunction
; }
169 void SetUsedHierarchy(tools::Long nNew
);
170 tools::Long
GetUsedHierarchy() const
171 { return nUsedHierarchy
; }
173 SC_DLLPUBLIC
void SetLayoutName(const OUString
& rName
);
174 SC_DLLPUBLIC
const std::optional
<OUString
> & GetLayoutName() const;
175 void RemoveLayoutName();
176 SC_DLLPUBLIC
void SetSubtotalName(const OUString
& rName
);
177 SC_DLLPUBLIC
const std::optional
<OUString
> & GetSubtotalName() const;
178 void RemoveSubtotalName();
180 bool IsMemberNameInUse(const OUString
& rName
) const;
182 const css::sheet::DataPilotFieldReference
* GetReferenceValue() const
183 { return pReferenceValue
.get(); }
185 SC_DLLPUBLIC
void SetReferenceValue(const css::sheet::DataPilotFieldReference
* pNew
);
187 const css::sheet::DataPilotFieldSortInfo
* GetSortInfo() const
188 { return pSortInfo
.get(); }
190 SC_DLLPUBLIC
void SetSortInfo(const css::sheet::DataPilotFieldSortInfo
* pNew
);
191 const css::sheet::DataPilotFieldAutoShowInfo
* GetAutoShowInfo() const
192 { return pAutoShowInfo
.get(); }
194 SC_DLLPUBLIC
void SetAutoShowInfo(const css::sheet::DataPilotFieldAutoShowInfo
* pNew
);
195 const css::sheet::DataPilotFieldLayoutInfo
* GetLayoutInfo() const
196 { return pLayoutInfo
.get(); }
198 SC_DLLPUBLIC
void SetLayoutInfo(const css::sheet::DataPilotFieldLayoutInfo
* pNew
);
200 SC_DLLPUBLIC
void SetCurrentPage( const OUString
* pPage
); // NULL = no selection (all)
201 OUString
GetCurrentPage() const; // only for ODF compatibility
203 css::sheet::DataPilotFieldOrientation
GetOrientation() const
204 { return nOrientation
; }
206 SC_DLLPUBLIC ScDPSaveMember
* GetExistingMemberByName(const OUString
& rName
);
209 * Get a member object by its name. If one doesn't exist, create a new
210 * object and return it. This class manages the life cycle of all member
211 * objects belonging to it, so <i>don't delete the returned instance.</i>
213 * @param rName member name
215 * @return pointer to the member object.
217 SC_DLLPUBLIC ScDPSaveMember
* GetMemberByName(const OUString
& rName
);
219 void SetMemberPosition( const OUString
& rName
, sal_Int32 nNewPos
);
221 void WriteToSource( const css::uno::Reference
<css::uno::XInterface
>& xDim
);
223 void UpdateMemberVisibility(const std::unordered_map
< OUString
, bool>& rData
);
225 SC_DLLPUBLIC
bool HasInvisibleMember() const;
227 void RemoveObsoleteMembers(const MemberSetType
& rMembers
);
230 void Dump(int nIndent
= 0) const;
236 typedef std::unordered_map
<OUString
, size_t> DupNameCountType
;
238 typedef std::unordered_map
<OUString
, size_t> DimOrderType
;
239 typedef std::vector
<std::unique_ptr
<ScDPSaveDimension
>> DimsType
;
243 DupNameCountType maDupNameCounts
; /// keep track of number of duplicates in each name.
244 std::unique_ptr
<ScDPDimensionSaveData
> mpDimensionData
; // settings that create new dimensions
245 sal_uInt16 mnColumnGrandMode
;
246 sal_uInt16 mnRowGrandMode
;
247 sal_uInt16 mnIgnoreEmptyMode
;
248 sal_uInt16 mnRepeatEmptyMode
;
249 bool mbFilterButton
; // not passed to DataPilotSource
250 bool mbDrillDown
; // not passed to DataPilotSource
251 bool mbExpandCollapse
; // not passed to DataPilotSource
253 /** if true, all dimensions already have all of their member instances
255 bool mbDimensionMembersBuilt
;
257 std::unique_ptr
<sc::PivotTableFormats
> mpFormats
;
258 std::optional
<OUString
> mpGrandTotalName
;
259 mutable std::unique_ptr
<DimOrderType
> mpDimOrder
; // dimension order for row and column dimensions, to traverse result tree.
262 SC_DLLPUBLIC
ScDPSaveData();
263 ScDPSaveData(const ScDPSaveData
& r
);
264 SC_DLLPUBLIC
~ScDPSaveData();
266 ScDPSaveData
& operator= ( const ScDPSaveData
& r
);
268 bool operator== ( const ScDPSaveData
& r
) const;
270 SC_DLLPUBLIC
bool hasFormats();
271 SC_DLLPUBLIC
sc::PivotTableFormats
const& getFormats();
272 SC_DLLPUBLIC
void setFormats(sc::PivotTableFormats
const& rPivotTableFormats
);
274 SC_DLLPUBLIC
void SetGrandTotalName(const OUString
& rName
);
275 SC_DLLPUBLIC
const std::optional
<OUString
> & GetGrandTotalName() const;
277 const DimsType
& GetDimensions() const { return m_DimList
; }
280 * Get sort order map to sort row and column dimensions in order of
281 * appearance. Row dimensions get sorted before column dimensions. This
282 * is used to traverse result tree, which is structured following this
285 const DimOrderType
& GetDimensionSortOrder() const;
288 * Get all dimensions in a given orientation. The order represents the
289 * actual order of occurrence. The returned list also includes data
292 * @param eOrientation orientation
293 * @param rDims (out) list of dimensions for specified orientation
295 SC_DLLPUBLIC
void GetAllDimensionsByOrientation(
296 css::sheet::DataPilotFieldOrientation eOrientation
,
297 std::vector
<const ScDPSaveDimension
*>& rDims
) const;
299 void AddDimension(ScDPSaveDimension
* pDim
);
302 * Get a dimension object by its name. <i>If one doesn't exist for the
303 * given name, it creates a new one.</i>
305 * @param rName dimension name
307 * @return pointer to the dimension object. The ScDPSaveData instance
308 * manages its life cycle; hence the caller must
309 * <i>not</i> delete this object.
311 SC_DLLPUBLIC ScDPSaveDimension
* GetDimensionByName(const OUString
& rName
);
312 SC_DLLPUBLIC ScDPSaveDimension
* GetDataLayoutDimension();
313 SC_DLLPUBLIC ScDPSaveDimension
* GetExistingDataLayoutDimension() const;
315 ScDPSaveDimension
* DuplicateDimension(std::u16string_view rName
);
316 SC_DLLPUBLIC ScDPSaveDimension
& DuplicateDimension(const ScDPSaveDimension
& rDim
);
318 SC_DLLPUBLIC ScDPSaveDimension
* GetExistingDimensionByName(std::u16string_view rName
) const;
319 SC_DLLPUBLIC ScDPSaveDimension
* GetNewDimensionByName(const OUString
& rName
);
321 void RemoveDimensionByName(const OUString
& rName
);
323 ScDPSaveDimension
* GetInnermostDimension(css::sheet::DataPilotFieldOrientation nOrientation
);
324 ScDPSaveDimension
* GetFirstDimension(css::sheet::DataPilotFieldOrientation eOrientation
);
325 SC_DLLPUBLIC
tools::Long
GetDataDimensionCount() const;
327 void SetPosition( ScDPSaveDimension
* pDim
, tools::Long nNew
);
328 SC_DLLPUBLIC
void SetColumnGrand( bool bSet
);
329 bool GetColumnGrand() const { return bool(mnColumnGrandMode
); }
331 SC_DLLPUBLIC
void SetRowGrand( bool bSet
);
332 bool GetRowGrand() const { return bool(mnRowGrandMode
); }
334 SC_DLLPUBLIC
void SetIgnoreEmptyRows( bool bSet
);
335 bool GetIgnoreEmptyRows() const { return bool(mnIgnoreEmptyMode
); }
337 SC_DLLPUBLIC
void SetRepeatIfEmpty( bool bSet
);
338 bool GetRepeatIfEmpty() const { return bool(mnRepeatEmptyMode
); }
340 SC_DLLPUBLIC
void SetFilterButton( bool bSet
);
341 bool GetFilterButton() const { return mbFilterButton
; }
343 SC_DLLPUBLIC
void SetDrillDown( bool bSet
);
344 bool GetDrillDown() const { return mbDrillDown
; }
346 SC_DLLPUBLIC
void SetExpandCollapse( bool bSet
);
347 bool GetExpandCollapse() const { return mbExpandCollapse
; }
349 void WriteToSource( const css::uno::Reference
<css::sheet::XDimensionsSupplier
>& xSource
);
350 bool IsEmpty() const;
352 const ScDPDimensionSaveData
* GetExistingDimensionData() const { return mpDimensionData
.get(); }
354 void RemoveAllGroupDimensions( const OUString
& rSrcDimName
, std::vector
<OUString
>* pDeletedNames
= nullptr );
356 SC_DLLPUBLIC ScDPDimensionSaveData
* GetDimensionData(); // create if not there
357 SC_DLLPUBLIC
void SetDimensionData( const ScDPDimensionSaveData
* pNew
); // copied
358 void BuildAllDimensionMembers(ScDPTableData
* pData
);
359 void SyncAllDimensionMembers(ScDPTableData
* pData
);
362 * Check whether a dimension has one or more invisible members.
364 * @param rDimName dimension name
366 SC_DLLPUBLIC
bool HasInvisibleMember(std::u16string_view rDimName
) const;
373 void CheckDuplicateName(ScDPSaveDimension
& rDim
);
374 void RemoveDuplicateNameCount(const OUString
& rName
);
377 * Append a new original dimension. Not to be called to insert a duplicate
380 * @param rName Dimension name. The name must be the original dimension
381 * name; not a duplicate dimension name.
382 * @param bDataLayout true if this is a data layout dimension, false
385 * @return pointer to the new dimension just inserted.
387 ScDPSaveDimension
* AppendNewDimension(const OUString
& rName
, bool bDataLayout
);
389 void DimensionsChanged();
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */