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