fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / inc / xepivot.hxx
blobdafd0884d37f48a7a435e43f89ba456ef3c01b01
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 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_XEPIVOT_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XEPIVOT_HXX
23 #include <map>
24 #include "xerecord.hxx"
25 #include "xlpivot.hxx"
26 #include "xeroot.hxx"
28 class ScDPObject;
29 class ScDPSaveData;
30 class ScDPSaveDimension;
31 class ScDPSaveMember;
32 class ScDPSaveGroupDimension;
33 class ScDPSaveNumGroupDimension;
34 struct ScDPNumGroupInfo;
36 // Pivot cache
38 /** Represents a data item in a pivot cache containing data of any type. */
39 class XclExpPCItem : public XclExpRecord, public XclPCItem
41 public:
42 explicit XclExpPCItem( const OUString& rText );
43 explicit XclExpPCItem( double fValue );
44 explicit XclExpPCItem( const DateTime& rDateTime );
45 explicit XclExpPCItem( sal_Int16 nValue );
46 explicit XclExpPCItem( bool bValue );
48 inline sal_uInt16 GetTypeFlag() const { return mnTypeFlag; }
50 bool EqualsText( const OUString& rText ) const;
51 bool EqualsDouble( double fValue ) const;
52 bool EqualsDateTime( const DateTime& rDateTime ) const;
53 bool EqualsBool( bool bValue ) const;
55 private:
56 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
58 private:
59 sal_uInt16 mnTypeFlag; /// Data type flag.
62 class XclExpPivotCache;
64 class XclExpPCField : public XclExpRecord, public XclPCField, protected XclExpRoot
66 public:
67 /** Creates a standard pivot cache field, filled from sheet source data. */
68 explicit XclExpPCField( const XclExpRoot& rRoot,
69 const XclExpPivotCache& rPCache, sal_uInt16 nFieldIdx,
70 const ScDPObject& rDPObj, const ScRange& rRange );
71 /** Creates a child grouping pivot cache field, filled from the passed grouping info. */
72 explicit XclExpPCField( const XclExpRoot& rRoot,
73 const XclExpPivotCache& rPCache, sal_uInt16 nFieldIdx,
74 const ScDPObject& rDPObj, const ScDPSaveGroupDimension& rGroupDim,
75 const XclExpPCField& rBaseField );
76 virtual ~XclExpPCField();
78 /** Sets the passed field as direct grouping child field of this field. */
79 void SetGroupChildField( const XclExpPCField& rChildField );
80 /** Converts this standard field into a numeric grouping field. */
81 void ConvertToNumGroup( const ScDPObject& rDPObj, const ScDPSaveNumGroupDimension& rNumGroupDim );
83 /** Returns the name of this cache field. */
84 inline const OUString& GetFieldName() const { return maFieldInfo.maName; }
86 /** Returns the number of visible items of this field. */
87 sal_uInt16 GetItemCount() const;
88 /** Returns the specified pivot cache item (returns visible items in groupings). */
89 const XclExpPCItem* GetItem( sal_uInt16 nItemIdx ) const;
90 /** Returns the index of a pivot cache item, or EXC_PC_NOITEM on error. */
91 sal_uInt16 GetItemIndex( const OUString& rItemName ) const;
93 /** Returns the size an item index needs to write out. */
94 sal_Size GetIndexSize() const;
95 /** Writes the item index at the passed source row position as part of the SXINDEXLIST record. */
96 void WriteIndex( XclExpStream& rStrm, sal_uInt32 nSrcRow ) const;
98 /** Writes the pivot cache field and all items and other related records. */
99 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
101 private:
102 typedef XclExpRecordList< XclExpPCItem > XclExpPCItemList;
104 /** Returns the item list that contains the visible items.
105 @descr Visible items are equal to source items in standard fields,
106 but are generated items in grouping and calculated fields. */
107 const XclExpPCItemList& GetVisItemList() const;
109 /** Initializes a standard field. Inserts all original source items. */
110 void InitStandardField( const ScRange& rRange );
111 /** Initializes a standard grouping field. Inserts all visible grouping items. */
112 void InitStdGroupField( const XclExpPCField& rBaseField, const ScDPSaveGroupDimension& rGroupDim );
113 /** Initializes a numeric grouping field. Inserts all visible grouping items and the limit settings. */
114 void InitNumGroupField( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rNumInfo );
115 /** Initializes a date grouping field. Inserts all visible grouping items and the limit settings. */
116 void InitDateGroupField( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rDateInfo, sal_Int32 nDatePart );
118 /** Inserts the passed index into the item index array of original items. */
119 void InsertItemArrayIndex( size_t nListPos );
120 /** Inserts an original source item. Updates item index array. */
121 void InsertOrigItem( XclExpPCItem* pNewItem );
122 /** Inserts an original text item, if it is not contained already. */
123 void InsertOrigTextItem( const OUString& rText );
124 /** Inserts an original value item, if it is not contained already. */
125 void InsertOrigDoubleItem( double fValue );
126 /** Inserts an original date/time item, if it is not contained already. */
127 void InsertOrigDateTimeItem( const DateTime& rDateTime );
128 /** Inserts an original boolean item, if it is not contained already. */
129 void InsertOrigBoolItem( bool bValue );
131 /** Inserts an item into the grouping item list. Does not change anything else.
132 @return The list index of the new item. */
133 sal_uInt16 InsertGroupItem( XclExpPCItem* pNewItem );
134 /** Generates and inserts all visible items for numeric or date grouping. */
135 void InsertNumDateGroupItems( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rNumInfo, sal_Int32 nDatePart = 0 );
137 /** Inserts the SXDOUBLE items that specify the limits for a numeric grouping. */
138 void SetNumGroupLimit( const ScDPNumGroupInfo& rNumInfo );
139 /** Inserts the SXDATETIME/SXINTEGER items that specify the limits for a date grouping.
140 @param bUseStep true = Insert the passed step value; false = always insert 1. */
141 void SetDateGroupLimit( const ScDPNumGroupInfo& rDateInfo, bool bUseStep );
143 /** Initializes flags and item count fields. */
144 void Finalize();
146 /** Writes an SXNUMGROUP record and the additional items for a numeric grouping field. */
147 void WriteSxnumgroup( XclExpStream& rStrm );
148 /** Writes an SXGROUPINFO record describing the item order in grouping fields. */
149 void WriteSxgroupinfo( XclExpStream& rStrm );
151 /** Writes the contents of the SXFIELD record for this field. */
152 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
154 private:
155 const XclExpPivotCache& mrPCache; /// Parent pivot cache containing this field.
156 XclExpPCItemList maOrigItemList; /// List with original items.
157 XclExpPCItemList maGroupItemList; /// List with grouping items.
158 ScfUInt16Vec maIndexVec; /// Indexes into maItemList.
159 XclExpPCItemList maNumGroupLimits; /// List with limit values for numeric grouping.
160 sal_uInt16 mnTypeFlags; /// Collected item data type flags.
163 class XclExpPivotCache : protected XclExpRoot
165 public:
166 explicit XclExpPivotCache( const XclExpRoot& rRoot,
167 const ScDPObject& rDPObj, sal_uInt16 nListIdx );
169 /** Returns true, if the cache has been constructed successfully. */
170 inline bool IsValid() const { return mbValid; }
171 /** Returns true, if the item index list will be written. */
172 bool HasItemIndexList() const;
174 /** Returns the stream identifier used to create the cache stream. */
175 inline sal_uInt16 GetStreamId() const { return maPCInfo.mnStrmId; }
176 /** Returns the list index of the cache used in pivot table records. */
177 inline sal_uInt16 GetCacheIndex() const { return mnListIdx; }
179 /** Returns the number of pivot cache fields. */
180 sal_uInt16 GetFieldCount() const;
181 /** Returns the specified pivot cache field. */
182 const XclExpPCField* GetField( sal_uInt16 nFieldIdx ) const;
183 /** Returns true, if this pivot cache contains non-standard fields (e.g. grouping fields). */
184 bool HasAddFields() const;
186 /** Returns true, if the passed DP object has the same data source as this cache. */
187 bool HasEqualDataSource( const ScDPObject& rDPObj ) const;
189 /** Writes related records into Workbook stream and creates the pivot cache storage stream. */
190 void Save( XclExpStream& rStrm );
191 static void SaveXml( XclExpXmlStream& rStrm );
193 private:
194 /** Returns read/write access to a pivot cache field. */
195 XclExpPCField* GetFieldAcc( sal_uInt16 nFieldIdx );
197 /** Adds all pivot cache fields. */
198 void AddFields( const ScDPObject& rDPObj );
200 /** Adds all standard pivot cache fields based on source data. */
201 void AddStdFields( const ScDPObject& rDPObj );
202 /** Adds all grouping pivot cache fields. */
203 void AddGroupFields( const ScDPObject& rDPObj );
205 /** Writes the DCONREF record containing the source range. */
206 void WriteDconref( XclExpStream& rStrm ) const;
207 /** DCONNAME record contains range name source. */
208 void WriteDConName( XclExpStream& rStrm ) const;
210 /** Creates the pivot cache storage stream and writes the cache. */
211 void WriteCacheStream();
212 /** Writes the SXDB record. */
213 void WriteSxdb( XclExpStream& rStrm ) const;
214 /** Writes the SXDBEX record. */
215 static void WriteSxdbex( XclExpStream& rStrm );
216 /** Writes the SXINDEXLIST record list containing the item index table. */
217 void WriteSxindexlistList( XclExpStream& rStrm ) const;
219 private:
220 typedef XclExpRecordList< XclExpPCField > XclExpPCFieldList;
221 typedef XclExpPCFieldList::RecordRefType XclExpPCFieldRef;
223 XclPCInfo maPCInfo; /// Pivot cache settings (SXDB record).
224 XclExpPCFieldList maFieldList; /// List of all pivot cache fields.
225 OUString maTabName; /// Name of source data sheet.
226 OUString maSrcRangeName; /// Range name for source data.
227 ScRange maOrigSrcRange; /// The original sheet source range.
228 ScRange maExpSrcRange; /// The exported sheet source range.
229 ScRange maDocSrcRange; /// The range used to build the cache fields and items.
230 sal_uInt16 mnListIdx; /// List index in pivot cache buffer.
231 bool mbValid; /// true = The cache is valid for export.
234 // Pivot table
236 class XclExpPivotTable;
238 /** Data field position specifying the pivot table field index (first) and data info index (second). */
239 typedef ::std::pair< sal_uInt16, sal_uInt16 > XclPTDataFieldPos;
241 class XclExpPTItem : public XclExpRecord
243 public:
244 explicit XclExpPTItem( const XclExpPCField& rCacheField, sal_uInt16 nCacheIdx );
245 explicit XclExpPTItem( sal_uInt16 nItemType, sal_uInt16 nCacheIdx, bool bUseCache );
247 /** Returns the internal name of this item. */
248 OUString GetItemName() const;
250 /** Fills this item with properties from the passed save member. */
251 void SetPropertiesFromMember( const ScDPSaveMember& rSaveMem );
253 private:
254 /** Writes the SXVI record body describing the pivot table item. */
255 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
257 private:
258 const XclExpPCItem* mpCacheItem; /// The referred pivot cache item.
259 XclPTItemInfo maItemInfo; /// General data for this item.
262 class XclExpPTField : public XclExpRecordBase
264 public:
265 explicit XclExpPTField( const XclExpPivotTable& rPTable, sal_uInt16 nCacheIdx );
267 // data access ------------------------------------------------------------
269 /** Returns the name of this field. */
270 OUString GetFieldName() const;
271 /** Returns the pivot table field list index of this field.
272 * The field index is always equal to cache index.
274 sal_uInt16 GetFieldIndex() const { return maFieldInfo.mnCacheIdx; }
276 /** Returns the index of the last inserted data info struct. */
277 sal_uInt16 GetLastDataInfoIndex() const;
279 /** Returns the list index of an item by its name.
280 @param nDefaultIdx This value will be returned, if the item could not be found. */
281 sal_uInt16 GetItemIndex( const OUString& rName, sal_uInt16 nDefaultIdx ) const;
283 // fill data --------------------------------------------------------------
285 /** Fills this field with row/column/page properties from the passed save dimension. */
286 void SetPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
287 /** Fills this field with data field properties from the passed save dimension. */
288 void SetDataPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
290 /** Appends special items describing the field subtotal entries. */
291 void AppendSubtotalItems();
293 // records ----------------------------------------------------------------
295 /** Writes an entry for an SXPI record containing own page field info. */
296 void WriteSxpiEntry( XclExpStream& rStrm ) const;
297 /** Writes an SXDI records containing info about a data field. */
298 void WriteSxdi( XclExpStream& rStrm, sal_uInt16 nDataInfoIdx ) const;
300 /** Writes the entire pivot table field. */
301 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
303 private:
304 /** Returns an item by its name. */
305 XclExpPTItem* GetItemAcc( const OUString& rName );
307 /** Appends a special item describing a field subtotal entry. */
308 void AppendSubtotalItem( sal_uInt16 nItemType );
310 /** Writes the SXVD record introducing the field. */
311 void WriteSxvd( XclExpStream& rStrm ) const;
312 /** Writes the SXVDEX record containing additional settings. */
313 void WriteSxvdex( XclExpStream& rStrm ) const;
315 private:
316 typedef ::std::vector< XclPTDataFieldInfo > XclPTDataFieldInfoVec;
317 typedef XclExpRecordList< XclExpPTItem > XclExpPTItemList;
319 const XclExpPivotTable& mrPTable; /// Parent pivot table containing this field.
320 const XclExpPCField* mpCacheField; /// The referred pivot cache field.
321 XclPTFieldInfo maFieldInfo; /// General field info (SXVD record).
322 XclPTFieldExtInfo maFieldExtInfo; /// Extended field info (SXVDEX record).
323 XclPTPageFieldInfo maPageInfo; /// Page field info (entry in SXPI record).
324 XclPTDataFieldInfoVec maDataInfoVec; /// List of extended data field info (SXDI records).
325 XclExpPTItemList maItemList; /// List of all items of this field.
328 class XclExpPivotTable : public XclExpRecordBase, protected XclExpRoot
330 public:
331 explicit XclExpPivotTable( const XclExpRoot& rRoot,
332 const ScDPObject& rDPObj, const XclExpPivotCache& rPCache, size_t nId );
334 /** Returns a pivot cache field. */
335 const XclExpPCField* GetCacheField( sal_uInt16 nCacheIdx ) const;
337 /** Returns the output range of the pivot table. */
338 inline SCTAB GetScTab() const { return mnOutScTab; }
340 /** Returns a pivot table field by its name. */
341 const XclExpPTField* GetField( sal_uInt16 nFieldIdx ) const;
342 /** Returns a pivot table field by its name. */
343 const XclExpPTField* GetField( const OUString& rName ) const;
345 /** Returns the data-field-only index of the first data field with the passed name.
346 @param nDefaultIdx This value will be returned, if the field could not be found. */
347 sal_uInt16 GetDataFieldIndex( const OUString& rName, sal_uInt16 nDefaultIdx ) const;
349 /** Writes the entire pivot table. */
350 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
352 private:
353 /** Returns a pivot table field by its name. */
354 XclExpPTField* GetFieldAcc( const OUString& rName );
355 /** Returns a pivot table field corresponding to the passed save dimension. */
356 XclExpPTField* GetFieldAcc( const ScDPSaveDimension& rSaveDim );
358 // fill data --------------------------------------------------------------
360 /** Fills internal members with all properties from the passed save data. */
361 void SetPropertiesFromDP( const ScDPSaveData& rSaveData );
362 /** Fills a pivot table field with all properties from the passed save dimension. */
363 void SetFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
364 /** Fills a pivot table data field with all properties from the passed save dimension. */
365 void SetDataFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
367 /** Initializes any data after processing the entire source DataPilot. */
368 void Finalize();
370 // records ----------------------------------------------------------------
372 /** Writes the SXVIEW record starting the pivot table. */
373 void WriteSxview( XclExpStream& rStrm ) const;
374 /** Writes an SXIVD record for row field or column field order. */
375 static void WriteSxivd( XclExpStream& rStrm, const ScfUInt16Vec& rFields );
376 /** Writes the SXPI record containing page field info. */
377 void WriteSxpi( XclExpStream& rStrm ) const;
378 /** Writes all SXDI records containing info about the data fields. */
379 void WriteSxdiList( XclExpStream& rStrm ) const;
380 /** Writes a dummy SXLI records containing item layout info. */
381 static void WriteSxli( XclExpStream& rStrm, sal_uInt16 nLineCount, sal_uInt16 nIndexCount );
382 /** Writes the SXEX records containing additional pivot table info. */
383 void WriteSxex( XclExpStream& rStrm ) const;
385 void WriteQsiSxTag( XclExpStream& rStrm ) const;
386 /** Writes the SX_AUTOFORMAT records with the autoformat id and header layout */
387 void WriteSxViewEx9( XclExpStream& rStrm ) const;
389 private:
390 typedef XclExpRecordList< XclExpPTField > XclExpPTFieldList;
391 typedef XclExpPTFieldList::RecordRefType XclExpPTFieldRef;
392 typedef ::std::vector< XclPTDataFieldPos > XclPTDataFieldPosVec;
394 const XclExpPivotCache& mrPCache; /// The pivot cache this pivot table bases on.
395 XclPTInfo maPTInfo; /// Info about the pivot table (SXVIEW record).
396 XclPTExtInfo maPTExtInfo; /// Extended info about the pivot table (SXEX record).
397 XclPTViewEx9Info maPTViewEx9Info; /// The selected autoformat (SXVIEWEX9)
398 XclExpPTFieldList maFieldList; /// All fields in pivot cache order.
399 ScfUInt16Vec maRowFields; /// Row field indexes.
400 ScfUInt16Vec maColFields; /// Column field indexes.
401 ScfUInt16Vec maPageFields; /// Page field indexes.
402 XclPTDataFieldPosVec maDataFields; /// Data field indexes.
403 XclExpPTField maDataOrientField; /// Special data field orientation field.
404 SCTAB mnOutScTab; /// Sheet index of the output range.
405 bool mbValid; /// true = The pivot table is valid for export.
406 bool mbFilterBtn; /// true = DataPilot has filter button.
409 /** The main class for pivot table export.
411 This class contains all pivot caches and pivot tables in a Calc document.
412 It creates the pivot cache streams and pivot table records in the main
413 workbook stream. It supports sharing of pivot caches between multiple pivot
414 tables to decrease file size.
416 class XclExpPivotTableManager : protected XclExpRoot
418 public:
419 explicit XclExpPivotTableManager( const XclExpRoot& rRoot );
421 /** Creates all pivot tables and caches from the Calc DataPilot objects. */
422 void CreatePivotTables();
424 /** Creates a record wrapper for exporting all pivot caches. */
425 XclExpRecordRef CreatePivotCachesRecord();
426 /** Creates a record wrapper for exporting all pivot tables of the specified sheet. */
427 XclExpRecordRef CreatePivotTablesRecord( SCTAB nScTab );
429 /** Writes all pivot caches (all Workbook records and cache streams). */
430 void WritePivotCaches( XclExpStream& rStrm );
431 /** Writes all pivot tables of the specified Calc sheet. */
432 void WritePivotTables( XclExpStream& rStrm, SCTAB nScTab );
434 private:
435 /** Finds an existing (if enabled in mbShareCaches) or creates a new pivot cache.
436 @return Pointer to the pivot cache or 0, if the passed source range was invalid. */
437 const XclExpPivotCache* CreatePivotCache( const ScDPObject& rDPObj );
439 private:
440 typedef XclExpRecordList< XclExpPivotCache > XclExpPivotCacheList;
441 typedef XclExpPivotCacheList::RecordRefType XclExpPivotCacheRef;
442 typedef XclExpRecordList< XclExpPivotTable > XclExpPivotTableList;
443 typedef XclExpPivotTableList::RecordRefType XclExpPivotTableRef;
445 XclExpPivotCacheList maPCacheList; /// List of all pivot caches.
446 XclExpPivotTableList maPTableList; /// List of all pivot tables.
447 bool mbShareCaches; /// true = Tries to share caches between tables.
450 #endif
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */