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