Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / inc / xepivot.hxx
blob1fb61696c9f688112edc242985e0387c480b75cf
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 "xerecord.hxx"
23 #include "xlpivot.hxx"
24 #include "xeroot.hxx"
26 class ScDPObject;
27 class ScDPSaveData;
28 class ScDPSaveDimension;
29 class ScDPSaveMember;
30 class ScDPSaveGroupDimension;
31 struct ScDPNumGroupInfo;
33 // Pivot cache
35 /** Represents a data item in a pivot cache containing data of any type. */
36 class XclExpPCItem : public XclExpRecord, public XclPCItem
38 public:
39 explicit XclExpPCItem( const OUString& rText );
40 explicit XclExpPCItem( double fValue, const OUString& rText = OUString() );
41 explicit XclExpPCItem( const DateTime& rDateTime, const OUString& rText = OUString() );
42 explicit XclExpPCItem( sal_Int16 nValue );
43 explicit XclExpPCItem( bool bValue, const OUString& rText );
45 sal_uInt16 GetTypeFlag() const { return mnTypeFlag; }
47 bool EqualsText( std::u16string_view rText ) const;
48 bool EqualsDouble( double fValue ) const;
49 bool EqualsDateTime( const DateTime& rDateTime ) const;
50 bool EqualsBool( bool bValue ) const;
52 private:
53 virtual void WriteBody( XclExpStream& rStrm ) override;
55 private:
56 sal_uInt16 mnTypeFlag; /// Data type flag.
59 class XclExpPCField : public XclExpRecord, public XclPCField, protected XclExpRoot
61 public:
62 /** Creates a standard pivot cache field, filled from sheet source data. */
63 explicit XclExpPCField( const XclExpRoot& rRoot,
64 sal_uInt16 nFieldIdx,
65 const ScDPObject& rDPObj, const ScRange& rRange );
66 /** Creates a child grouping pivot cache field, filled from the passed grouping info. */
67 explicit XclExpPCField( const XclExpRoot& rRoot,
68 sal_uInt16 nFieldIdx,
69 const ScDPObject& rDPObj, const ScDPSaveGroupDimension& rGroupDim,
70 const XclExpPCField& rBaseField );
71 virtual ~XclExpPCField() override;
73 /** Sets the passed field as direct grouping child field of this field. */
74 void SetGroupChildField( const XclExpPCField& rChildField );
76 /** Returns the name of this cache field. */
77 const OUString& GetFieldName() const { return maFieldInfo.maName; }
79 /** Returns the number of visible items of this field. */
80 sal_uInt16 GetItemCount() const;
81 /** Returns the specified pivot cache item (returns visible items in groupings). */
82 const XclExpPCItem* GetItem( sal_uInt16 nItemIdx ) const;
83 /** Returns the index of a pivot cache item, or EXC_PC_NOITEM on error. */
84 sal_uInt16 GetItemIndex( std::u16string_view rItemName ) const;
86 /** Returns the size an item index needs to write out. */
87 std::size_t GetIndexSize() const;
88 /** Writes the item index at the passed source row position as part of the SXINDEXLIST record. */
89 void WriteIndex( XclExpStream& rStrm, sal_uInt32 nSrcRow ) const;
91 /** Writes the pivot cache field and all items and other related records. */
92 virtual void Save( XclExpStream& rStrm ) override;
94 private:
95 typedef XclExpRecordList< XclExpPCItem > XclExpPCItemList;
97 /** Returns the item list that contains the visible items.
98 @descr Visible items are equal to source items in standard fields,
99 but are generated items in grouping and calculated fields. */
100 const XclExpPCItemList& GetVisItemList() const;
102 /** Initializes a standard field. Inserts all original source items. */
103 void InitStandardField( const ScRange& rRange );
104 /** Initializes a standard grouping field. Inserts all visible grouping items. */
105 void InitStdGroupField( const XclExpPCField& rBaseField, const ScDPSaveGroupDimension& rGroupDim );
106 /** Initializes a numeric grouping field. Inserts all visible grouping items and the limit settings. */
107 void InitNumGroupField( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rNumInfo );
108 /** Initializes a date grouping field. Inserts all visible grouping items and the limit settings. */
109 void InitDateGroupField( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rDateInfo, sal_Int32 nDatePart );
111 /** Inserts the passed index into the item index array of original items. */
112 void InsertItemArrayIndex( size_t nListPos );
113 /** Inserts an original source item. Updates item index array. */
114 void InsertOrigItem( XclExpPCItem* pNewItem );
115 /** Inserts an original text item, if it is not contained already. */
116 void InsertOrigTextItem( const OUString& rText );
117 /** Inserts an original value item, if it is not contained already. */
118 void InsertOrigDoubleItem( double fValue, const OUString& rText );
119 /** Inserts an original date/time item, if it is not contained already. */
120 void InsertOrigDateTimeItem( const DateTime& rDateTime, const OUString& rText );
121 /** Inserts an original boolean item, if it is not contained already. */
122 void InsertOrigBoolItem( bool bValue, const OUString& rText );
124 /** Inserts an item into the grouping item list. Does not change anything else.
125 @return The list index of the new item. */
126 sal_uInt16 InsertGroupItem( XclExpPCItem* pNewItem );
127 /** Generates and inserts all visible items for numeric or date grouping. */
128 void InsertNumDateGroupItems( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rNumInfo, sal_Int32 nDatePart = 0 );
130 /** Inserts the SXDOUBLE items that specify the limits for a numeric grouping. */
131 void SetNumGroupLimit( const ScDPNumGroupInfo& rNumInfo );
132 /** Inserts the SXDATETIME/SXINTEGER items that specify the limits for a date grouping.
133 @param bUseStep true = Insert the passed step value; false = always insert 1. */
134 void SetDateGroupLimit( const ScDPNumGroupInfo& rDateInfo, bool bUseStep );
136 /** Initializes flags and item count fields. */
137 void Finalize();
139 /** Writes an SXNUMGROUP record and the additional items for a numeric grouping field. */
140 void WriteSxnumgroup( XclExpStream& rStrm );
141 /** Writes an SXGROUPINFO record describing the item order in grouping fields. */
142 void WriteSxgroupinfo( XclExpStream& rStrm );
144 /** Writes the contents of the SXFIELD record for this field. */
145 virtual void WriteBody( XclExpStream& rStrm ) override;
147 private:
148 XclExpPCItemList maOrigItemList; /// List with original items.
149 XclExpPCItemList maGroupItemList; /// List with grouping items.
150 ScfUInt16Vec maIndexVec; /// Indexes into maItemList.
151 XclExpPCItemList maNumGroupLimits; /// List with limit values for numeric grouping.
152 sal_uInt16 mnTypeFlags; /// Collected item data type flags.
155 class XclExpPivotCache : public salhelper::SimpleReferenceObject, protected XclExpRoot
157 public:
158 explicit XclExpPivotCache( const XclExpRoot& rRoot,
159 const ScDPObject& rDPObj, sal_uInt16 nListIdx );
161 /** Returns true, if the cache has been constructed successfully. */
162 bool IsValid() const { return mbValid; }
163 /** Returns true, if the item index list will be written. */
164 bool HasItemIndexList() const;
166 /** Returns the list index of the cache used in pivot table records. */
167 sal_uInt16 GetCacheIndex() const { return mnListIdx; }
169 /** Returns the number of pivot cache fields. */
170 sal_uInt16 GetFieldCount() const;
171 /** Returns the specified pivot cache field. */
172 const XclExpPCField* GetField( sal_uInt16 nFieldIdx ) const;
173 /** Returns true, if this pivot cache contains non-standard fields (e.g. grouping fields). */
174 bool HasAddFields() const;
176 /** Returns true, if the passed DP object has the same data source as this cache. */
177 bool HasEqualDataSource( const ScDPObject& rDPObj ) const;
179 /** Writes related records into Workbook stream and creates the pivot cache storage stream. */
180 void Save( XclExpStream& rStrm );
181 static void SaveXml( XclExpXmlStream& rStrm );
183 private:
184 /** Adds all pivot cache fields. */
185 void AddFields( const ScDPObject& rDPObj );
187 /** Adds all standard pivot cache fields based on source data. */
188 void AddStdFields( const ScDPObject& rDPObj );
189 /** Adds all grouping pivot cache fields. */
190 void AddGroupFields( const ScDPObject& rDPObj );
192 /** Writes the DCONREF record containing the source range. */
193 void WriteDconref( XclExpStream& rStrm ) const;
194 /** DCONNAME record contains range name source. */
195 void WriteDConName( XclExpStream& rStrm ) const;
197 /** Creates the pivot cache storage stream and writes the cache. */
198 void WriteCacheStream();
199 /** Writes the SXDB record. */
200 void WriteSxdb( XclExpStream& rStrm ) const;
201 /** Writes the SXDBEX record. */
202 static void WriteSxdbex( XclExpStream& rStrm );
203 /** Writes the SXINDEXLIST record list containing the item index table. */
204 void WriteSxindexlistList( XclExpStream& rStrm ) const;
206 private:
207 typedef XclExpRecordList< XclExpPCField > XclExpPCFieldList;
208 typedef XclExpPCFieldList::RecordRefType XclExpPCFieldRef;
210 XclPCInfo maPCInfo; /// Pivot cache settings (SXDB record).
211 XclExpPCFieldList maFieldList; /// List of all pivot cache fields.
212 OUString maTabName; /// Name of source data sheet.
213 OUString maSrcRangeName; /// Range name for source data.
214 ScRange maOrigSrcRange; /// The original sheet source range.
215 ScRange maExpSrcRange; /// The exported sheet source range.
216 ScRange maDocSrcRange; /// The range used to build the cache fields and items.
217 sal_uInt16 mnListIdx; /// List index in pivot cache buffer.
218 bool mbValid; /// true = The cache is valid for export.
221 typedef rtl::Reference<XclExpPivotCache> XclExpPivotCacheRef;
223 // Pivot table
225 class XclExpPivotTable;
227 /** Data field position specifying the pivot table field index (first) and data info index (second). */
228 typedef ::std::pair< sal_uInt16, sal_uInt16 > XclPTDataFieldPos;
230 class XclExpPTItem : public XclExpRecord
232 public:
233 explicit XclExpPTItem( const XclExpPCField& rCacheField, sal_uInt16 nCacheIdx );
234 explicit XclExpPTItem( sal_uInt16 nItemType, sal_uInt16 nCacheIdx );
236 /** Returns the internal name of this item. */
237 OUString GetItemName() const;
239 /** Fills this item with properties from the passed save member. */
240 void SetPropertiesFromMember( const ScDPSaveMember& rSaveMem );
242 private:
243 /** Writes the SXVI record body describing the pivot table item. */
244 virtual void WriteBody( XclExpStream& rStrm ) override;
246 private:
247 const XclExpPCItem* mpCacheItem; /// The referred pivot cache item.
248 XclPTItemInfo maItemInfo; /// General data for this item.
251 class XclExpPTField : public XclExpRecordBase
253 public:
254 explicit XclExpPTField( const XclExpPivotTable& rPTable, sal_uInt16 nCacheIdx );
256 // data access ------------------------------------------------------------
258 /** Returns the name of this field. */
259 OUString GetFieldName() const;
260 /** Returns the pivot table field list index of this field.
261 * The field index is always equal to cache index.
263 sal_uInt16 GetFieldIndex() const { return maFieldInfo.mnCacheIdx; }
265 /** Returns the index of the last inserted data info struct. */
266 sal_uInt16 GetLastDataInfoIndex() const;
268 /** Returns the list index of an item by its name.
269 @param nDefaultIdx This value will be returned, if the item could not be found. */
270 sal_uInt16 GetItemIndex( std::u16string_view rName, sal_uInt16 nDefaultIdx ) const;
272 // fill data --------------------------------------------------------------
274 /** Fills this field with row/column/page properties from the passed save dimension. */
275 void SetPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
276 /** Fills this field with data field properties from the passed save dimension. */
277 void SetDataPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
279 /** Appends special items describing the field subtotal entries. */
280 void AppendSubtotalItems();
282 // records ----------------------------------------------------------------
284 /** Writes an entry for an SXPI record containing own page field info. */
285 void WriteSxpiEntry( XclExpStream& rStrm ) const;
286 /** Writes an SXDI records containing info about a data field. */
287 void WriteSxdi( XclExpStream& rStrm, sal_uInt16 nDataInfoIdx ) const;
289 /** Writes the entire pivot table field. */
290 virtual void Save( XclExpStream& rStrm ) override;
292 private:
293 /** Returns an item by its name. */
294 XclExpPTItem* GetItemAcc( std::u16string_view rName );
296 /** Appends a special item describing a field subtotal entry. */
297 void AppendSubtotalItem( sal_uInt16 nItemType );
299 /** Writes the SXVD record introducing the field. */
300 void WriteSxvd( XclExpStream& rStrm ) const;
301 /** Writes the SXVDEX record containing additional settings. */
302 void WriteSxvdex( XclExpStream& rStrm ) const;
304 private:
306 const XclExpPivotTable& mrPTable; /// Parent pivot table containing this field.
307 const XclExpPCField* mpCacheField; /// The referred pivot cache field.
308 XclPTFieldInfo maFieldInfo; /// General field info (SXVD record).
309 XclPTFieldExtInfo maFieldExtInfo; /// Extended field info (SXVDEX record).
310 XclPTPageFieldInfo maPageInfo; /// Page field info (entry in SXPI record).
311 std::vector< XclPTDataFieldInfo >
312 maDataInfoVec; /// List of extended data field info (SXDI records).
313 XclExpRecordList< XclExpPTItem >
314 maItemList; /// List of all items of this field.
317 class XclExpPivotTable : public XclExpRecordBase, protected XclExpRoot
319 public:
320 explicit XclExpPivotTable( const XclExpRoot& rRoot,
321 const ScDPObject& rDPObj, const XclExpPivotCache& rPCache );
323 /** Returns a pivot cache field. */
324 const XclExpPCField* GetCacheField( sal_uInt16 nCacheIdx ) const;
326 /** Returns the output range of the pivot table. */
327 SCTAB GetScTab() const { return mnOutScTab; }
329 /** Returns a pivot table field by its name. */
330 const XclExpPTField* GetField( sal_uInt16 nFieldIdx ) const;
331 /** Returns a pivot table field by its name. */
332 const XclExpPTField* GetField( std::u16string_view rName ) const;
334 /** Returns the data-field-only index of the first data field with the passed name.
335 @param nDefaultIdx This value will be returned, if the field could not be found. */
336 sal_uInt16 GetDataFieldIndex( const OUString& rName, sal_uInt16 nDefaultIdx ) const;
338 /** Writes the entire pivot table. */
339 virtual void Save( XclExpStream& rStrm ) override;
341 private:
342 /** Returns a pivot table field by its name. */
343 XclExpPTField* GetFieldAcc( std::u16string_view rName );
344 /** Returns a pivot table field corresponding to the passed save dimension. */
345 XclExpPTField* GetFieldAcc( const ScDPSaveDimension& rSaveDim );
347 // fill data --------------------------------------------------------------
349 /** Fills internal members with all properties from the passed save data. */
350 void SetPropertiesFromDP( const ScDPSaveData& rSaveData );
351 /** Fills a pivot table field with all properties from the passed save dimension. */
352 void SetFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
353 /** Fills a pivot table data field with all properties from the passed save dimension. */
354 void SetDataFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
356 /** Initializes any data after processing the entire source DataPilot. */
357 void Finalize();
359 // records ----------------------------------------------------------------
361 /** Writes the SXVIEW record starting the pivot table. */
362 void WriteSxview( XclExpStream& rStrm ) const;
363 /** Writes an SXIVD record for row field or column field order. */
364 static void WriteSxivd( XclExpStream& rStrm, const ScfUInt16Vec& rFields );
365 /** Writes the SXPI record containing page field info. */
366 void WriteSxpi( XclExpStream& rStrm ) const;
367 /** Writes all SXDI records containing info about the data fields. */
368 void WriteSxdiList( XclExpStream& rStrm ) const;
369 /** Writes a dummy SXLI records containing item layout info. */
370 static void WriteSxli( XclExpStream& rStrm, sal_uInt16 nLineCount, sal_uInt16 nIndexCount );
371 /** Writes the SXEX records containing additional pivot table info. */
372 void WriteSxex( XclExpStream& rStrm ) const;
374 void WriteQsiSxTag( XclExpStream& rStrm ) const;
375 /** Writes the SX_AUTOFORMAT records with the autoformat id and header layout */
376 void WriteSxViewEx9( XclExpStream& rStrm ) const;
378 private:
379 typedef XclExpRecordList< XclExpPTField > XclExpPTFieldList;
380 typedef XclExpPTFieldList::RecordRefType XclExpPTFieldRef;
382 const XclExpPivotCache& mrPCache; /// The pivot cache this pivot table bases on.
383 XclPTInfo maPTInfo; /// Info about the pivot table (SXVIEW record).
384 XclPTExtInfo maPTExtInfo; /// Extended info about the pivot table (SXEX record).
385 XclPTViewEx9Info maPTViewEx9Info; /// The selected autoformat (SXVIEWEX9)
386 XclExpPTFieldList maFieldList; /// All fields in pivot cache order.
387 ScfUInt16Vec maRowFields; /// Row field indexes.
388 ScfUInt16Vec maColFields; /// Column field indexes.
389 ScfUInt16Vec maPageFields; /// Page field indexes.
390 std::vector< XclPTDataFieldPos >
391 maDataFields; /// Data field indexes.
392 XclExpPTField maDataOrientField; /// Special data field orientation field.
393 SCTAB mnOutScTab; /// Sheet index of the output range.
394 bool mbValid; /// true = The pivot table is valid for export.
395 bool mbFilterBtn; /// true = DataPilot has filter button.
398 /** The main class for pivot table export.
400 This class contains all pivot caches and pivot tables in a Calc document.
401 It creates the pivot cache streams and pivot table records in the main
402 workbook stream. It supports sharing of pivot caches between multiple pivot
403 tables to decrease file size.
405 class XclExpPivotTableManager : protected XclExpRoot
407 public:
408 explicit XclExpPivotTableManager( const XclExpRoot& rRoot );
410 /** Creates all pivot tables and caches from the Calc DataPilot objects. */
411 void CreatePivotTables();
413 /** Creates a record wrapper for exporting all pivot caches. */
414 XclExpRecordRef CreatePivotCachesRecord();
415 /** Creates a record wrapper for exporting all pivot tables of the specified sheet. */
416 XclExpRecordRef CreatePivotTablesRecord( SCTAB nScTab );
418 /** Writes all pivot caches (all Workbook records and cache streams). */
419 void WritePivotCaches( XclExpStream& rStrm );
420 /** Writes all pivot tables of the specified Calc sheet. */
421 void WritePivotTables( XclExpStream& rStrm, SCTAB nScTab );
423 private:
424 /** Finds an existing (if enabled in mbShareCaches) or creates a new pivot cache.
425 @return Pointer to the pivot cache or 0, if the passed source range was invalid. */
426 const XclExpPivotCache* CreatePivotCache( const ScDPObject& rDPObj );
428 private:
429 typedef XclExpRecordList< XclExpPivotTable > XclExpPivotTableList;
430 typedef XclExpPivotTableList::RecordRefType XclExpPivotTableRef;
432 XclExpRecordList< XclExpPivotCache > maPCacheList; /// List of all pivot caches.
433 XclExpPivotTableList maPTableList; /// List of all pivot tables.
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */