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