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