merged tag ooo/OOO330_m14
[LibreOffice.git] / sc / source / filter / inc / xepivot.hxx
blob0c730952e69e0db0a4615dde1a615739a8029709
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #ifndef SC_XEPIVOT_HXX
29 #define SC_XEPIVOT_HXX
31 #include <map>
32 #include "xerecord.hxx"
33 #include "xlpivot.hxx"
34 #include "xeroot.hxx"
36 class ScDPObject;
37 class ScDPSaveData;
38 class ScDPSaveDimension;
39 class ScDPSaveMember;
40 class ScDPDimensionSaveData;
41 class ScDPSaveGroupDimension;
42 class ScDPSaveNumGroupDimension;
43 struct ScDPNumGroupInfo;
45 // ============================================================================
46 // Pivot cache
47 // ============================================================================
49 /** Represents a data item in a pivot cache containing data of any type. */
50 class XclExpPCItem : public XclExpRecord, public XclPCItem
52 public:
53 explicit XclExpPCItem( const String& rText );
54 explicit XclExpPCItem( double fValue );
55 explicit XclExpPCItem( const DateTime& rDateTime );
56 explicit XclExpPCItem( sal_Int16 nValue );
57 explicit XclExpPCItem( bool bValue );
59 inline sal_uInt16 GetTypeFlag() const { return mnTypeFlag; }
61 bool EqualsText( const String& rText ) const;
62 bool EqualsDouble( double fValue ) const;
63 bool EqualsDateTime( const DateTime& rDateTime ) const;
64 bool EqualsBool( bool bValue ) const;
66 private:
67 virtual void WriteBody( XclExpStream& rStrm );
69 private:
70 sal_uInt16 mnTypeFlag; /// Data type flag.
73 // ============================================================================
75 class XclExpPivotCache;
77 class XclExpPCField : public XclExpRecord, public XclPCField, protected XclExpRoot
79 public:
80 /** Creates a standard pivot cache field, filled from sheet source data. */
81 explicit XclExpPCField( const XclExpRoot& rRoot,
82 const XclExpPivotCache& rPCache, sal_uInt16 nFieldIdx,
83 const ScDPObject& rDPObj, const ScRange& rRange );
84 /** Creates a child grouping pivot cache field, filled from the passed grouping info. */
85 explicit XclExpPCField( const XclExpRoot& rRoot,
86 const XclExpPivotCache& rPCache, sal_uInt16 nFieldIdx,
87 const ScDPObject& rDPObj, const ScDPSaveGroupDimension& rGroupDim,
88 const XclExpPCField& rBaseField );
89 virtual ~XclExpPCField();
91 /** Sets the passed field as direct grouping child field of this field. */
92 void SetGroupChildField( const XclExpPCField& rChildField );
93 /** Converts this standard field into a numeric grouping field. */
94 void ConvertToNumGroup( const ScDPObject& rDPObj, const ScDPSaveNumGroupDimension& rNumGroupDim );
96 /** Returns the name of this cache field. */
97 inline const String& GetFieldName() const { return maFieldInfo.maName; }
99 /** Returns the number of visible items of this field. */
100 sal_uInt16 GetItemCount() const;
101 /** Returns the specified pivot cache item (returns visible items in groupings). */
102 const XclExpPCItem* GetItem( sal_uInt16 nItemIdx ) const;
103 /** Returns the index of a pivot cache item, or EXC_PC_NOITEM on error. */
104 sal_uInt16 GetItemIndex( const String& rItemName ) const;
106 /** Returns the size an item index needs to write out. */
107 sal_Size GetIndexSize() const;
108 /** Writes the item index at the passed source row position as part of the SXINDEXLIST record. */
109 void WriteIndex( XclExpStream& rStrm, sal_uInt32 nSrcRow ) const;
111 /** Writes the pivot cache field and all items and other related records. */
112 virtual void Save( XclExpStream& rStrm );
114 private:
115 typedef XclExpRecordList< XclExpPCItem > XclExpPCItemList;
117 /** Returns the item list that contains the visible items.
118 @descr Visible items are equal to source items in standard fields,
119 but are generated items in grouping and calculated fields. */
120 const XclExpPCItemList& GetVisItemList() const;
122 /** Initializes a standard field. Inserts all original source items. */
123 void InitStandardField( const ScRange& rRange );
124 /** Initializes a standard grouping field. Inserts all visible grouping items. */
125 void InitStdGroupField( const XclExpPCField& rBaseField, const ScDPSaveGroupDimension& rGroupDim );
126 /** Initializes a numeric grouping field. Inserts all visible grouping items and the limit settings. */
127 void InitNumGroupField( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rNumInfo );
128 /** Initializes a date grouping field. Inserts all visible grouping items and the limit settings. */
129 void InitDateGroupField( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rDateInfo, sal_Int32 nDatePart );
131 /** Inserts the passed index into the item index array of original items. */
132 void InsertItemArrayIndex( size_t nListPos );
133 /** Inserts an original source item. Updates item index array. */
134 void InsertOrigItem( XclExpPCItem* pNewItem );
135 /** Inserts an original text item, if it is not contained already. */
136 void InsertOrigTextItem( const String& rText );
137 /** Inserts an original value item, if it is not contained already. */
138 void InsertOrigDoubleItem( double fValue );
139 /** Inserts an original date/time item, if it is not contained already. */
140 void InsertOrigDateTimeItem( const DateTime& rDateTime );
141 /** Inserts an original boolean item, if it is not contained already. */
142 void InsertOrigBoolItem( bool bValue );
144 /** Inserts an item into the grouping item list. Does not change anything else.
145 @return The list index of the new item. */
146 sal_uInt16 InsertGroupItem( XclExpPCItem* pNewItem );
147 /** Generates and inserts all visible items for numeric or date grouping. */
148 void InsertNumDateGroupItems( const ScDPObject& rDPObj, const ScDPNumGroupInfo& rNumInfo, sal_Int32 nDatePart = 0 );
150 /** Inserts the SXDOUBLE items that specify the limits for a numeric grouping. */
151 void SetNumGroupLimit( const ScDPNumGroupInfo& rNumInfo );
152 /** Inserts the SXDATETIME/SXINTEGER items that specify the limits for a date grouping.
153 @param bUseStep true = Insert the passed step value; false = always insert 1. */
154 void SetDateGroupLimit( const ScDPNumGroupInfo& rDateInfo, bool bUseStep );
156 /** Initializes flags and item count fields. */
157 void Finalize();
159 /** Writes an SXNUMGROUP record and the additional items for a numeric grouping field. */
160 void WriteSxnumgroup( XclExpStream& rStrm );
161 /** Writes an SXGROUPINFO record describing the item order in grouping fields. */
162 void WriteSxgroupinfo( XclExpStream& rStrm );
164 /** Writes the contents of the SXFIELD record for this field. */
165 virtual void WriteBody( XclExpStream& rStrm );
167 private:
168 const XclExpPivotCache& mrPCache; /// Parent pivot cache containing this field.
169 XclExpPCItemList maOrigItemList; /// List with original items.
170 XclExpPCItemList maGroupItemList; /// List with grouping items.
171 ScfUInt16Vec maIndexVec; /// Indexes into maItemList.
172 XclExpPCItemList maNumGroupLimits; /// List with limit values for numeric grouping.
173 sal_uInt16 mnTypeFlags; /// Collected item data type flags.
176 // ============================================================================
178 class XclExpPivotCache : protected XclExpRoot
180 public:
181 explicit XclExpPivotCache( const XclExpRoot& rRoot,
182 const ScDPObject& rDPObj, sal_uInt16 nListIdx );
184 /** Returns true, if the cache has been constructed successfully. */
185 inline bool IsValid() const { return mbValid; }
186 /** Returns true, if the item index list will be written. */
187 bool HasItemIndexList() const;
189 /** Returns the stream identifier used to create the cache stream. */
190 inline sal_uInt16 GetStreamId() const { return maPCInfo.mnStrmId; }
191 /** Returns the list index of the cache used in pivot table records. */
192 inline sal_uInt16 GetCacheIndex() const { return mnListIdx; }
194 /** Returns the number of pivot cache fields. */
195 sal_uInt16 GetFieldCount() const;
196 /** Returns the specified pivot cache field. */
197 const XclExpPCField* GetField( sal_uInt16 nFieldIdx ) const;
198 //UNUSED2009-05 /** Returns a pivot cache field by its name. */
199 //UNUSED2009-05 const XclExpPCField* GetField( const String& rFieldName ) const;
200 /** Returns true, if this pivot cache contains non-standard fields (e.g. grouping fields). */
201 bool HasAddFields() const;
203 /** Returns true, if the passed DP object has the same data source as this cache. */
204 bool HasEqualDataSource( const ScDPObject& rDPObj ) const;
206 /** Writes related records into Workbook stream and creates the pivot cache storage stream. */
207 virtual void Save( XclExpStream& rStrm );
208 virtual void SaveXml( XclExpXmlStream& rStrm );
210 private:
211 /** Returns read/write access to a pivot cache field. */
212 XclExpPCField* GetFieldAcc( sal_uInt16 nFieldIdx );
213 /** Returns read/write access to a pivot cache field. */
214 XclExpPCField* GetFieldAcc( const String& rFieldName );
216 /** Adds all pivot cache fields. */
217 void AddFields( const ScDPObject& rDPObj );
219 /** Adds all standard pivot cache fields based on source data. */
220 void AddStdFields( const ScDPObject& rDPObj );
221 /** Adds all grouping pivot cache fields. */
222 void AddGroupFields( const ScDPObject& rDPObj );
223 /** Adds all calculated pivot cache fields. */
224 void AddCalcFields( const ScDPObject& rDPObj );
226 /** Writes the DCONREF record containing the source range. */
227 void WriteDconref( XclExpStream& rStrm ) const;
229 /** Creates the pivot cache storage stream and writes the cache. */
230 void WriteCacheStream();
231 /** Writes the SXDB record. */
232 void WriteSxdb( XclExpStream& rStrm ) const;
233 /** Writes the SXDBEX record. */
234 void WriteSxdbex( XclExpStream& rStrm ) const;
235 /** Writes the SXINDEXLIST record list containing the item index table. */
236 void WriteSxindexlistList( XclExpStream& rStrm ) const;
238 private:
239 typedef XclExpRecordList< XclExpPCField > XclExpPCFieldList;
240 typedef XclExpPCFieldList::RecordRefType XclExpPCFieldRef;
242 XclPCInfo maPCInfo; /// Pivot cache settings (SXDB record).
243 XclExpPCFieldList maFieldList; /// List of all pivot cache fields.
244 String maTabName; /// Name of source data sheet.
245 ScRange maOrigSrcRange; /// The original sheet source range.
246 ScRange maExpSrcRange; /// The exported sheet source range.
247 ScRange maDocSrcRange; /// The range used to build the cache fields and items.
248 sal_uInt16 mnListIdx; /// List index in pivot cache buffer.
249 bool mbValid; /// true = The cache is valid for export.
252 // ============================================================================
253 // Pivot table
254 // ============================================================================
256 class XclExpPivotTable;
258 /** Data field position specifying the pivot table field index (first) and data info index (second). */
259 typedef ::std::pair< sal_uInt16, sal_uInt16 > XclPTDataFieldPos;
261 // ============================================================================
263 class XclExpPTItem : public XclExpRecord
265 public:
266 explicit XclExpPTItem( const XclExpPCField& rCacheField, sal_uInt16 nCacheIdx );
267 explicit XclExpPTItem( sal_uInt16 nItemType, sal_uInt16 nCacheIdx, bool bUseCache );
269 /** Returns the internal name of this item. */
270 const String& GetItemName() const;
272 /** Fills this item with properties from the passed save member. */
273 void SetPropertiesFromMember( const ScDPSaveMember& rSaveMem );
275 private:
276 /** Writes the SXVI record body describing the pivot table item. */
277 virtual void WriteBody( XclExpStream& rStrm );
279 private:
280 const XclExpPCItem* mpCacheItem; /// The referred pivot cache item.
281 XclPTItemInfo maItemInfo; /// General data for this item.
284 // ============================================================================
286 class XclExpPTField : public XclExpRecordBase
288 public:
289 explicit XclExpPTField( const XclExpPivotTable& rPTable, sal_uInt16 nCacheIdx );
291 // data access ------------------------------------------------------------
293 /** Returns the name of this field. */
294 const String& GetFieldName() const;
295 /** Returns the pivot table field list index of this field. */
296 sal_uInt16 GetFieldIndex() const;
298 /** Returns the index of the last inserted data info struct. */
299 sal_uInt16 GetLastDataInfoIndex() const;
301 //UNUSED2009-05 /** Returns an item by its name. */
302 //UNUSED2009-05 const XclExpPTItem* GetItem( const String& rName ) const;
303 /** Returns the list index of an item by its name.
304 @param nDefaultIdx This value will be returned, if the item could not be found. */
305 sal_uInt16 GetItemIndex( const String& rName, sal_uInt16 nDefaultIdx ) const;
307 // fill data --------------------------------------------------------------
309 /** Fills this field with row/column/page properties from the passed save dimension. */
310 void SetPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
311 /** Fills this field with data field properties from the passed save dimension. */
312 void SetDataPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
314 /** Appends special items describing the field subtotal entries. */
315 void AppendSubtotalItems();
317 // records ----------------------------------------------------------------
319 /** Writes an entry for an SXPI record containing own page field info. */
320 void WriteSxpiEntry( XclExpStream& rStrm ) const;
321 /** Writes an SXDI records containing info about a data field. */
322 void WriteSxdi( XclExpStream& rStrm, sal_uInt16 nDataInfoIdx ) const;
324 /** Writes the entire pivot table field. */
325 virtual void Save( XclExpStream& rStrm );
327 // ------------------------------------------------------------------------
328 private:
329 /** Returns an item by its name. */
330 XclExpPTItem* GetItemAcc( const String& rName );
332 /** Appends a special item describing a field subtotal entry. */
333 void AppendSubtotalItem( sal_uInt16 nItemType );
335 /** Writes the SXVD record introducing the field. */
336 void WriteSxvd( XclExpStream& rStrm ) const;
337 /** Writes the SXVDEX record containing additional settings. */
338 void WriteSxvdex( XclExpStream& rStrm ) const;
340 private:
341 typedef ::std::vector< XclPTDataFieldInfo > XclPTDataFieldInfoVec;
342 typedef XclExpRecordList< XclExpPTItem > XclExpPTItemList;
344 const XclExpPivotTable& mrPTable; /// Parent pivot table containing this field.
345 const XclExpPCField* mpCacheField; /// The referred pivot cache field.
346 XclPTFieldInfo maFieldInfo; /// General field info (SXVD record).
347 XclPTFieldExtInfo maFieldExtInfo; /// Extended field info (SXVDEX record).
348 XclPTPageFieldInfo maPageInfo; /// Page field info (entry in SXPI record).
349 XclPTDataFieldInfoVec maDataInfoVec; /// List of extended data field info (SXDI records).
350 XclExpPTItemList maItemList; /// List of all items of this field.
353 // ============================================================================
355 class XclExpPivotTable : public XclExpRecordBase, protected XclExpRoot
357 public:
358 explicit XclExpPivotTable( const XclExpRoot& rRoot,
359 const ScDPObject& rDPObj, const XclExpPivotCache& rPCache );
361 /** Returns a pivot cache field. */
362 const XclExpPCField* GetCacheField( sal_uInt16 nCacheIdx ) const;
364 /** Returns the output range of the pivot table. */
365 inline SCTAB GetScTab() const { return mnOutScTab; }
367 /** Returns a pivot table field by its name. */
368 const XclExpPTField* GetField( sal_uInt16 nFieldIdx ) const;
369 /** Returns a pivot table field by its name. */
370 const XclExpPTField* GetField( const String& rName ) const;
372 /** Returns the data-field-only index of the first data field with the passed name.
373 @param nDefaultIdx This value will be returned, if the field could not be found. */
374 sal_uInt16 GetDataFieldIndex( const String& rName, sal_uInt16 nDefaultIdx ) const;
376 /** Writes the entire pivot table. */
377 virtual void Save( XclExpStream& rStrm );
379 // ------------------------------------------------------------------------
380 private:
381 /** Returns a pivot table field by its name. */
382 XclExpPTField* GetFieldAcc( const String& rName );
383 /** Returns a pivot table field corresponding to the passed save dimension. */
384 XclExpPTField* GetFieldAcc( const ScDPSaveDimension& rSaveDim );
386 // fill data --------------------------------------------------------------
388 /** Fills internal members with all properties from the passed save data. */
389 void SetPropertiesFromDP( const ScDPSaveData& rSaveData );
390 /** Fills a pivot table field with all properties from the passed save dimension. */
391 void SetFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
392 /** Fills a pivot table data field with all properties from the passed save dimension. */
393 void SetDataFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
395 /** Initializes any data after processing the entire source DataPilot. */
396 void Finalize();
398 // records ----------------------------------------------------------------
400 /** Writes the SXVIEW record starting the pivot table. */
401 void WriteSxview( XclExpStream& rStrm ) const;
402 /** Writes an SXIVD record for row field or column field order. */
403 void WriteSxivd( XclExpStream& rStrm, const ScfUInt16Vec& rFields ) const;
404 /** Writes the SXPI record containing page field info. */
405 void WriteSxpi( XclExpStream& rStrm ) const;
406 /** Writes all SXDI records containing info about the data fields. */
407 void WriteSxdiList( XclExpStream& rStrm ) const;
408 /** Writes a dummy SXLI records containing item layout info. */
409 void WriteSxli( XclExpStream& rStrm, sal_uInt16 nLineCount, sal_uInt16 nIndexCount ) const;
410 /** Writes the SXEX records containing additional pivot table info. */
411 void WriteSxex( XclExpStream& rStrm ) const;
413 void WriteQsiSxTag( XclExpStream& rStrm ) const;
414 /** Writes the SX_AUTOFORMAT records with the autoformat id and header layout */
415 void WriteSxViewEx9( XclExpStream& rStrm ) const;
417 // ------------------------------------------------------------------------
418 private:
419 typedef XclExpRecordList< XclExpPTField > XclExpPTFieldList;
420 typedef XclExpPTFieldList::RecordRefType XclExpPTFieldRef;
421 typedef ::std::vector< XclPTDataFieldPos > XclPTDataFieldPosVec;
423 const XclExpPivotCache& mrPCache; /// The pivot cache this pivot table bases on.
424 XclPTInfo maPTInfo; /// Info about the pivot table (SXVIEW record).
425 XclPTExtInfo maPTExtInfo; /// Extended info about the pivot table (SXEX record).
426 XclPTViewEx9Info maPTViewEx9Info; /// The selected autoformat (SXVIEWEX9)
427 XclExpPTFieldList maFieldList; /// All fields in pivot cache order.
428 ScfUInt16Vec maRowFields; /// Row field indexes.
429 ScfUInt16Vec maColFields; /// Column field indexes.
430 ScfUInt16Vec maPageFields; /// Page field indexes.
431 XclPTDataFieldPosVec maDataFields; /// Data field indexes.
432 XclExpPTField maDataOrientField; /// Special data field orientation field.
433 SCTAB mnOutScTab; /// Sheet index of the output range.
434 bool mbValid; /// true = The pivot table is valid for export.
435 bool mbFilterBtn; /// true = DataPilot has filter button.
438 // ============================================================================
440 /** The main class for pivot table export.
442 This class contains all pivot caches and pivot tables in a Calc document.
443 It creates the pivot cache streams and pivot table records in the main
444 workbook stream. It supports sharing of pivot caches between multiple pivot
445 tables to decrease file size.
447 class XclExpPivotTableManager : protected XclExpRoot
449 public:
450 explicit XclExpPivotTableManager( const XclExpRoot& rRoot );
452 /** Creates all pivot tables and caches from the Calc DataPilot objects. */
453 void CreatePivotTables();
455 /** Creates a record wrapper for exporting all pivot caches. */
456 XclExpRecordRef CreatePivotCachesRecord();
457 /** Creates a record wrapper for exporting all pivot tables of the specified sheet. */
458 XclExpRecordRef CreatePivotTablesRecord( SCTAB nScTab );
460 /** Writes all pivot caches (all Workbook records and cache streams). */
461 void WritePivotCaches( XclExpStream& rStrm );
462 void WritePivotCachesXml( XclExpXmlStream& rStrm );
463 /** Writes all pivot tables of the specified Calc sheet. */
464 void WritePivotTables( XclExpStream& rStrm, SCTAB nScTab );
465 void WritePivotTablesXml( XclExpXmlStream& rStrm, SCTAB nScTab );
467 private:
468 /** Finds an existing (if enabled in mbShareCaches) or creates a new pivot cache.
469 @return Pointer to the pivot cache or 0, if the passed source range was invalid. */
470 const XclExpPivotCache* CreatePivotCache( const ScDPObject& rDPObj );
472 private:
473 typedef XclExpRecordList< XclExpPivotCache > XclExpPivotCacheList;
474 typedef XclExpPivotCacheList::RecordRefType XclExpPivotCacheRef;
475 typedef XclExpRecordList< XclExpPivotTable > XclExpPivotTableList;
476 typedef XclExpPivotTableList::RecordRefType XclExpPivotTableRef;
478 XclExpPivotCacheList maPCacheList; /// List of all pivot caches.
479 XclExpPivotTableList maPTableList; /// List of all pivot tables.
480 bool mbShareCaches; /// true = Tries to share caches between tables.
483 // ============================================================================
485 #endif