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 INCLUDED_SC_SOURCE_FILTER_INC_XEPIVOT_HXX
21 : #define INCLUDED_SC_SOURCE_FILTER_INC_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 : // Pivot cache
37 :
38 : /** Represents a data item in a pivot cache containing data of any type. */
39 0 : class XclExpPCItem : public XclExpRecord, public XclPCItem
40 : {
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 );
47 :
48 0 : inline sal_uInt16 GetTypeFlag() const { return mnTypeFlag; }
49 :
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;
54 :
55 : private:
56 : virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
57 :
58 : private:
59 : sal_uInt16 mnTypeFlag; /// Data type flag.
60 : };
61 :
62 : class XclExpPivotCache;
63 :
64 : class XclExpPCField : public XclExpRecord, public XclPCField, protected XclExpRoot
65 : {
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();
77 :
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 );
82 :
83 : /** Returns the name of this cache field. */
84 0 : inline const OUString& GetFieldName() const { return maFieldInfo.maName; }
85 :
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;
92 :
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;
97 :
98 : /** Writes the pivot cache field and all items and other related records. */
99 : virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
100 :
101 : private:
102 : typedef XclExpRecordList< XclExpPCItem > XclExpPCItemList;
103 :
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;
108 :
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 );
117 :
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 );
130 :
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 );
136 :
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 );
142 :
143 : /** Initializes flags and item count fields. */
144 : void Finalize();
145 :
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 );
150 :
151 : /** Writes the contents of the SXFIELD record for this field. */
152 : virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
153 :
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.
161 : };
162 :
163 0 : class XclExpPivotCache : protected XclExpRoot
164 : {
165 : public:
166 : explicit XclExpPivotCache( const XclExpRoot& rRoot,
167 : const ScDPObject& rDPObj, sal_uInt16 nListIdx );
168 :
169 : /** Returns true, if the cache has been constructed successfully. */
170 0 : inline bool IsValid() const { return mbValid; }
171 : /** Returns true, if the item index list will be written. */
172 : bool HasItemIndexList() const;
173 :
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 0 : inline sal_uInt16 GetCacheIndex() const { return mnListIdx; }
178 :
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;
185 :
186 : /** Returns true, if the passed DP object has the same data source as this cache. */
187 : bool HasEqualDataSource( const ScDPObject& rDPObj ) const;
188 :
189 : /** Writes related records into Workbook stream and creates the pivot cache storage stream. */
190 : virtual void Save( XclExpStream& rStrm );
191 : virtual void SaveXml( XclExpXmlStream& rStrm );
192 :
193 : private:
194 : /** Returns read/write access to a pivot cache field. */
195 : XclExpPCField* GetFieldAcc( sal_uInt16 nFieldIdx );
196 :
197 : /** Adds all pivot cache fields. */
198 : void AddFields( const ScDPObject& rDPObj );
199 :
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 );
204 : /** Adds all calculated pivot cache fields. */
205 : void AddCalcFields( const ScDPObject& rDPObj );
206 :
207 : /** Writes the DCONREF record containing the source range. */
208 : void WriteDconref( XclExpStream& rStrm ) const;
209 : /** DCONNAME record contains range name source. */
210 : void WriteDConName( XclExpStream& rStrm ) const;
211 :
212 : /** Creates the pivot cache storage stream and writes the cache. */
213 : void WriteCacheStream();
214 : /** Writes the SXDB record. */
215 : void WriteSxdb( XclExpStream& rStrm ) const;
216 : /** Writes the SXDBEX record. */
217 : void WriteSxdbex( XclExpStream& rStrm ) const;
218 : /** Writes the SXINDEXLIST record list containing the item index table. */
219 : void WriteSxindexlistList( XclExpStream& rStrm ) const;
220 :
221 : private:
222 : typedef XclExpRecordList< XclExpPCField > XclExpPCFieldList;
223 : typedef XclExpPCFieldList::RecordRefType XclExpPCFieldRef;
224 :
225 : XclPCInfo maPCInfo; /// Pivot cache settings (SXDB record).
226 : XclExpPCFieldList maFieldList; /// List of all pivot cache fields.
227 : OUString maTabName; /// Name of source data sheet.
228 : OUString maSrcRangeName; /// Range name for source data.
229 : ScRange maOrigSrcRange; /// The original sheet source range.
230 : ScRange maExpSrcRange; /// The exported sheet source range.
231 : ScRange maDocSrcRange; /// The range used to build the cache fields and items.
232 : sal_uInt16 mnListIdx; /// List index in pivot cache buffer.
233 : bool mbValid; /// true = The cache is valid for export.
234 : };
235 :
236 : // Pivot table
237 :
238 : class XclExpPivotTable;
239 :
240 : /** Data field position specifying the pivot table field index (first) and data info index (second). */
241 : typedef ::std::pair< sal_uInt16, sal_uInt16 > XclPTDataFieldPos;
242 :
243 0 : class XclExpPTItem : public XclExpRecord
244 : {
245 : public:
246 : explicit XclExpPTItem( const XclExpPCField& rCacheField, sal_uInt16 nCacheIdx );
247 : explicit XclExpPTItem( sal_uInt16 nItemType, sal_uInt16 nCacheIdx, bool bUseCache );
248 :
249 : /** Returns the internal name of this item. */
250 : OUString GetItemName() const;
251 :
252 : /** Fills this item with properties from the passed save member. */
253 : void SetPropertiesFromMember( const ScDPSaveMember& rSaveMem );
254 :
255 : private:
256 : /** Writes the SXVI record body describing the pivot table item. */
257 : virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
258 :
259 : private:
260 : const XclExpPCItem* mpCacheItem; /// The referred pivot cache item.
261 : XclPTItemInfo maItemInfo; /// General data for this item.
262 : };
263 :
264 0 : class XclExpPTField : public XclExpRecordBase
265 : {
266 : public:
267 : explicit XclExpPTField( const XclExpPivotTable& rPTable, sal_uInt16 nCacheIdx );
268 :
269 : // data access ------------------------------------------------------------
270 :
271 : /** Returns the name of this field. */
272 : OUString GetFieldName() const;
273 : /** Returns the pivot table field list index of this field.
274 : * The field index is always equal to cache index.
275 : */
276 0 : sal_uInt16 GetFieldIndex() const { return maFieldInfo.mnCacheIdx; }
277 :
278 : /** Returns the index of the last inserted data info struct. */
279 : sal_uInt16 GetLastDataInfoIndex() const;
280 :
281 : /** Returns the list index of an item by its name.
282 : @param nDefaultIdx This value will be returned, if the item could not be found. */
283 : sal_uInt16 GetItemIndex( const OUString& rName, sal_uInt16 nDefaultIdx ) const;
284 :
285 : // fill data --------------------------------------------------------------
286 :
287 : /** Fills this field with row/column/page properties from the passed save dimension. */
288 : void SetPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
289 : /** Fills this field with data field properties from the passed save dimension. */
290 : void SetDataPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
291 :
292 : /** Appends special items describing the field subtotal entries. */
293 : void AppendSubtotalItems();
294 :
295 : // records ----------------------------------------------------------------
296 :
297 : /** Writes an entry for an SXPI record containing own page field info. */
298 : void WriteSxpiEntry( XclExpStream& rStrm ) const;
299 : /** Writes an SXDI records containing info about a data field. */
300 : void WriteSxdi( XclExpStream& rStrm, sal_uInt16 nDataInfoIdx ) const;
301 :
302 : /** Writes the entire pivot table field. */
303 : virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
304 :
305 : private:
306 : /** Returns an item by its name. */
307 : XclExpPTItem* GetItemAcc( const OUString& rName );
308 :
309 : /** Appends a special item describing a field subtotal entry. */
310 : void AppendSubtotalItem( sal_uInt16 nItemType );
311 :
312 : /** Writes the SXVD record introducing the field. */
313 : void WriteSxvd( XclExpStream& rStrm ) const;
314 : /** Writes the SXVDEX record containing additional settings. */
315 : void WriteSxvdex( XclExpStream& rStrm ) const;
316 :
317 : private:
318 : typedef ::std::vector< XclPTDataFieldInfo > XclPTDataFieldInfoVec;
319 : typedef XclExpRecordList< XclExpPTItem > XclExpPTItemList;
320 :
321 : const XclExpPivotTable& mrPTable; /// Parent pivot table containing this field.
322 : const XclExpPCField* mpCacheField; /// The referred pivot cache field.
323 : XclPTFieldInfo maFieldInfo; /// General field info (SXVD record).
324 : XclPTFieldExtInfo maFieldExtInfo; /// Extended field info (SXVDEX record).
325 : XclPTPageFieldInfo maPageInfo; /// Page field info (entry in SXPI record).
326 : XclPTDataFieldInfoVec maDataInfoVec; /// List of extended data field info (SXDI records).
327 : XclExpPTItemList maItemList; /// List of all items of this field.
328 : };
329 :
330 0 : class XclExpPivotTable : public XclExpRecordBase, protected XclExpRoot
331 : {
332 : public:
333 : explicit XclExpPivotTable( const XclExpRoot& rRoot,
334 : const ScDPObject& rDPObj, const XclExpPivotCache& rPCache, size_t nId );
335 :
336 : /** Returns a pivot cache field. */
337 : const XclExpPCField* GetCacheField( sal_uInt16 nCacheIdx ) const;
338 :
339 : /** Returns the output range of the pivot table. */
340 0 : inline SCTAB GetScTab() const { return mnOutScTab; }
341 :
342 : /** Returns a pivot table field by its name. */
343 : const XclExpPTField* GetField( sal_uInt16 nFieldIdx ) const;
344 : /** Returns a pivot table field by its name. */
345 : const XclExpPTField* GetField( const OUString& rName ) const;
346 :
347 : /** Returns the data-field-only index of the first data field with the passed name.
348 : @param nDefaultIdx This value will be returned, if the field could not be found. */
349 : sal_uInt16 GetDataFieldIndex( const OUString& rName, sal_uInt16 nDefaultIdx ) const;
350 :
351 : /** Writes the entire pivot table. */
352 : virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
353 :
354 : private:
355 : /** Returns a pivot table field by its name. */
356 : XclExpPTField* GetFieldAcc( const OUString& rName );
357 : /** Returns a pivot table field corresponding to the passed save dimension. */
358 : XclExpPTField* GetFieldAcc( const ScDPSaveDimension& rSaveDim );
359 :
360 : // fill data --------------------------------------------------------------
361 :
362 : /** Fills internal members with all properties from the passed save data. */
363 : void SetPropertiesFromDP( const ScDPSaveData& rSaveData );
364 : /** Fills a pivot table field with all properties from the passed save dimension. */
365 : void SetFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
366 : /** Fills a pivot table data field with all properties from the passed save dimension. */
367 : void SetDataFieldPropertiesFromDim( const ScDPSaveDimension& rSaveDim );
368 :
369 : /** Initializes any data after processing the entire source DataPilot. */
370 : void Finalize();
371 :
372 : // records ----------------------------------------------------------------
373 :
374 : /** Writes the SXVIEW record starting the pivot table. */
375 : void WriteSxview( XclExpStream& rStrm ) const;
376 : /** Writes an SXIVD record for row field or column field order. */
377 : void WriteSxivd( XclExpStream& rStrm, const ScfUInt16Vec& rFields ) const;
378 : /** Writes the SXPI record containing page field info. */
379 : void WriteSxpi( XclExpStream& rStrm ) const;
380 : /** Writes all SXDI records containing info about the data fields. */
381 : void WriteSxdiList( XclExpStream& rStrm ) const;
382 : /** Writes a dummy SXLI records containing item layout info. */
383 : void WriteSxli( XclExpStream& rStrm, sal_uInt16 nLineCount, sal_uInt16 nIndexCount ) const;
384 : /** Writes the SXEX records containing additional pivot table info. */
385 : void WriteSxex( XclExpStream& rStrm ) const;
386 :
387 : void WriteQsiSxTag( XclExpStream& rStrm ) const;
388 : /** Writes the SX_AUTOFORMAT records with the autoformat id and header layout */
389 : void WriteSxViewEx9( XclExpStream& rStrm ) const;
390 :
391 : private:
392 : typedef XclExpRecordList< XclExpPTField > XclExpPTFieldList;
393 : typedef XclExpPTFieldList::RecordRefType XclExpPTFieldRef;
394 : typedef ::std::vector< XclPTDataFieldPos > XclPTDataFieldPosVec;
395 :
396 : const XclExpPivotCache& mrPCache; /// The pivot cache this pivot table bases on.
397 : XclPTInfo maPTInfo; /// Info about the pivot table (SXVIEW record).
398 : XclPTExtInfo maPTExtInfo; /// Extended info about the pivot table (SXEX record).
399 : XclPTViewEx9Info maPTViewEx9Info; /// The selected autoformat (SXVIEWEX9)
400 : XclExpPTFieldList maFieldList; /// All fields in pivot cache order.
401 : ScfUInt16Vec maRowFields; /// Row field indexes.
402 : ScfUInt16Vec maColFields; /// Column field indexes.
403 : ScfUInt16Vec maPageFields; /// Page field indexes.
404 : XclPTDataFieldPosVec maDataFields; /// Data field indexes.
405 : XclExpPTField maDataOrientField; /// Special data field orientation field.
406 : SCTAB mnOutScTab; /// Sheet index of the output range.
407 : bool mbValid; /// true = The pivot table is valid for export.
408 : bool mbFilterBtn; /// true = DataPilot has filter button.
409 : };
410 :
411 : /** The main class for pivot table export.
412 :
413 : This class contains all pivot caches and pivot tables in a Calc document.
414 : It creates the pivot cache streams and pivot table records in the main
415 : workbook stream. It supports sharing of pivot caches between multiple pivot
416 : tables to decrease file size.
417 : */
418 128 : class XclExpPivotTableManager : protected XclExpRoot
419 : {
420 : public:
421 : explicit XclExpPivotTableManager( const XclExpRoot& rRoot );
422 :
423 : /** Creates all pivot tables and caches from the Calc DataPilot objects. */
424 : void CreatePivotTables();
425 :
426 : /** Creates a record wrapper for exporting all pivot caches. */
427 : XclExpRecordRef CreatePivotCachesRecord();
428 : /** Creates a record wrapper for exporting all pivot tables of the specified sheet. */
429 : XclExpRecordRef CreatePivotTablesRecord( SCTAB nScTab );
430 :
431 : /** Writes all pivot caches (all Workbook records and cache streams). */
432 : void WritePivotCaches( XclExpStream& rStrm );
433 : /** Writes all pivot tables of the specified Calc sheet. */
434 : void WritePivotTables( XclExpStream& rStrm, SCTAB nScTab );
435 :
436 : private:
437 : /** Finds an existing (if enabled in mbShareCaches) or creates a new pivot cache.
438 : @return Pointer to the pivot cache or 0, if the passed source range was invalid. */
439 : const XclExpPivotCache* CreatePivotCache( const ScDPObject& rDPObj );
440 :
441 : private:
442 : typedef XclExpRecordList< XclExpPivotCache > XclExpPivotCacheList;
443 : typedef XclExpPivotCacheList::RecordRefType XclExpPivotCacheRef;
444 : typedef XclExpRecordList< XclExpPivotTable > XclExpPivotTableList;
445 : typedef XclExpPivotTableList::RecordRefType XclExpPivotTableRef;
446 :
447 : XclExpPivotCacheList maPCacheList; /// List of all pivot caches.
448 : XclExpPivotTableList maPTableList; /// List of all pivot tables.
449 : bool mbShareCaches; /// true = Tries to share caches between tables.
450 : };
451 :
452 : #endif
453 :
454 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|