1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_XLS_PIVOTTABLEBUFFER_HXX
25 #define OOX_XLS_PIVOTTABLEBUFFER_HXX
26 
27 #include <com/sun/star/table/CellRangeAddress.hpp>
28 #include "oox/xls/pivotcachebuffer.hxx"
29 #include "oox/xls/stylesbuffer.hxx"
30 
31 namespace com { namespace sun { namespace star {
32     namespace sheet { class XDataPilotDescriptor; }
33     namespace sheet { class XDataPilotField; }
34 } } }
35 
36 namespace oox {
37 namespace xls {
38 
39 class PivotTable;
40 
41 // ============================================================================
42 
43 struct PTFieldItemModel
44 {
45     sal_Int32           mnCacheItem;        /// Index to shared item in pivot cache.
46     sal_Int32           mnType;             /// Type of the item.
47     bool                mbShowDetails;      /// True = show item details (items of child fields).
48     bool                mbHidden;           /// True = item is hidden.
49 
50     explicit            PTFieldItemModel();
51 
52     /** Sets item type for BIFF import. */
53     void                setBiffType( sal_uInt16 nType );
54 };
55 
56 // ----------------------------------------------------------------------------
57 
58 struct PTFieldModel
59 {
60     sal_Int32           mnAxis;             /// Axis this field is assigned to (none, row, column, page).
61     sal_Int32           mnNumFmtId;         /// Number format for field items.
62     sal_Int32           mnAutoShowItems;    /// Number of items (or percent/sum) to be shown in auto show filter.
63     sal_Int32           mnAutoShowRankBy;   /// Index of the data field auto show filter is based on.
64     sal_Int32           mnSortType;         /// Autosorting type.
65     sal_Int32           mnSortRefField;     /// Reference field for autosorting.
66     sal_Int32           mnSortRefItem;      /// Item in reference field for autosorting.
67     bool                mbDataField;        /// True = field appears in data area.
68     bool                mbDefaultSubtotal;  /// True = show default subtotals.
69     bool                mbSumSubtotal;      /// True = show sum subtotals.
70     bool                mbCountASubtotal;   /// True = show count all subtotals.
71     bool                mbAverageSubtotal;  /// True = show average subtotals.
72     bool                mbMaxSubtotal;      /// True = show maximum subtotals.
73     bool                mbMinSubtotal;      /// True = show minimum subtotals.
74     bool                mbProductSubtotal;  /// True = show product subtotals.
75     bool                mbCountSubtotal;    /// True = show count numbers subtotals.
76     bool                mbStdDevSubtotal;   /// True = show standard deviation subtotals.
77     bool                mbStdDevPSubtotal;  /// True = show standard deviation of population subtotals.
78     bool                mbVarSubtotal;      /// True = show variance subtotals.
79     bool                mbVarPSubtotal;     /// True = show variance of population subtotals.
80     bool                mbShowAll;          /// True = show items without data.
81     bool                mbOutline;          /// True = show in outline view, false = show in tabular view.
82     bool                mbSubtotalTop;      /// True = show subtotals on top of items in outline or compact mode.
83     bool                mbInsertBlankRow;   /// True = insert blank rows after items.
84     bool                mbInsertPageBreak;  /// True = insert page breaks after items.
85     bool                mbAutoShow;         /// True = auto show (top 10) filter enabled.
86     bool                mbTopAutoShow;      /// True = auto show filter shows top entries, false = bottom.
87     bool                mbMultiPageItems;   /// True = multiple items selectable in page diemsion.
88 
89     explicit            PTFieldModel();
90 
91     /** Sets axis type for BIFF import. */
92     void                setBiffAxis( sal_uInt8 nAxisFlags );
93 };
94 
95 // ----------------------------------------------------------------------------
96 
97 struct PTPageFieldModel
98 {
99     ::rtl::OUString     maName;             /// Unique name of the page field.
100     sal_Int32           mnField;            /// Base pivot field.
101     sal_Int32           mnItem;             /// Index of field item that is shown by the page field.
102 
103     explicit            PTPageFieldModel();
104 };
105 
106 // ----------------------------------------------------------------------------
107 
108 struct PTDataFieldModel
109 {
110     ::rtl::OUString     maName;             /// Name of the data field.
111     sal_Int32           mnField;            /// Base pivot field.
112     sal_Int32           mnSubtotal;         /// Subtotal aggregation function.
113     sal_Int32           mnShowDataAs;       /// Show data as, based on another field.
114     sal_Int32           mnBaseField;        /// Base field for 'show data as'.
115     sal_Int32           mnBaseItem;         /// Base item for 'show data as'.
116     sal_Int32           mnNumFmtId;         /// Number format for the result.
117 
118     explicit            PTDataFieldModel();
119 
120     /** Sets the subtotal aggregation function for BIFF import. */
121     void                setBiffSubtotal( sal_Int32 nSubtotal );
122     /** Sets the 'show data as' type for BIFF import. */
123     void                setBiffShowDataAs( sal_Int32 nShowDataAs );
124 };
125 
126 // ----------------------------------------------------------------------------
127 
128 class PivotTableField : public WorkbookHelper
129 {
130 public:
131     explicit            PivotTableField( PivotTable& rPivotTable, sal_Int32 nFieldIndex );
132 
133     /** Imports pivot field settings from the pivotField element. */
134     void                importPivotField( const AttributeList& rAttribs );
135     /** Imports settings of an item in this pivot field from the item element. */
136     void                importItem( const AttributeList& rAttribs );
137     /** Imports pivot field reference settings from the reference element. */
138     void                importReference( const AttributeList& rAttribs );
139     /** Imports pivot field item reference settings from the x element. */
140     void                importReferenceItem( const AttributeList& rAttribs );
141 
142     /** Imports pivot field settings from the PTFIELD record. */
143     void                importPTField( SequenceInputStream& rStrm );
144     /** Imports settings of an item in this pivot field from the PTFITEM record. */
145     void                importPTFItem( SequenceInputStream& rStrm );
146     /** Imports pivot field reference settings from the PTREFERENCE record. */
147     void                importPTReference( SequenceInputStream& rStrm );
148     /** Imports pivot field item reference settings from the PTREFERENCEITEM record. */
149     void                importPTReferenceItem( SequenceInputStream& rStrm );
150 
151     /** Imports pivot field settings from the PTFIELD and following records. */
152     void                importPTField( BiffInputStream& rStrm );
153     /** Imports pivot field settings from the PTFIELD2 record. */
154     void                importPTField2( BiffInputStream& rStrm );
155     /** Imports settings of an item in this pivot field from the PTFITEM record. */
156     void                importPTFItem( BiffInputStream& rStrm );
157 
158     /** Finalizes the field after import, creates grouping and other settings. */
159     void                finalizeImport(
160                             const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotDescriptor >& rxDPDesc );
161     /** Finalizes the grouped date field after import. */
162     void                finalizeDateGroupingImport(
163                             const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxBaseDPField,
164                             sal_Int32 nBaseFieldIdx );
165     /** Finalizes the grouped field after import. */
166     void                finalizeParentGroupingImport(
167                             const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxBaseDPField,
168                             PivotCacheGroupItemVector& orItemNames );
169 
170     /** Returns the name of the DataPilot field in the fields collection. */
getDPFieldName() const171     inline const ::rtl::OUString& getDPFieldName() const { return maDPFieldName; }
172 
173     /** Converts dimension and other settings for a row field. */
174     void                convertRowField();
175     /** Converts dimension and other settings for a column field. */
176     void                convertColField();
177     /** Converts dimension and other settings for a hidden field. */
178     void                convertHiddenField();
179     /** Converts dimension and other settings for a page field */
180     void                convertPageField( const PTPageFieldModel& rPageField );
181     /** Converts dimension and other settings for a data field. */
182     void                convertDataField( const PTDataFieldModel& rDataField );
183 
184 private:
185     /** Converts dimension and other settings for row, column, page, or hidden fields. */
186     ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >
187                         convertRowColPageField( sal_Int32 nAxis );
188 
189 private:
190     typedef ::std::vector< PTFieldItemModel > ItemModelVector;
191 
192     PivotTable&         mrPivotTable;       /// The parent pivot table object.
193     ItemModelVector     maItems;            /// All items of this field.
194     PTFieldModel        maModel;            /// Pivot field settings.
195     ::rtl::OUString     maDPFieldName;      /// Name of the field in DataPilot field collection.
196     sal_Int32           mnFieldIndex;       /// Zero-based index of this field.
197 };
198 
199 // ============================================================================
200 
201 struct PTFilterModel
202 {
203     ::rtl::OUString     maName;             /// Name of the field filter.
204     ::rtl::OUString     maDescription;      /// Description of the field filter.
205     ::rtl::OUString     maStrValue1;        /// First string value for label filter.
206     ::rtl::OUString     maStrValue2;        /// Second string value for label filter.
207     double              mfValue;            /// Number of items or percent or sum to be shown.
208     sal_Int32           mnField;            /// Base pivot field.
209     sal_Int32           mnMemPropField;     /// Member property field.
210     sal_Int32           mnType;             /// Filter type.
211     sal_Int32           mnEvalOrder;        /// Evaluation order index.
212     sal_Int32           mnId;               /// Unique identifier.
213     sal_Int32           mnMeasureField;     /// Data field for filter calculation.
214     sal_Int32           mnMeasureHier;      /// Hierarchy for filter calculation.
215     bool                mbTopFilter;        /// True = filter shows top entries, false = bottom.
216 
217     explicit            PTFilterModel();
218 };
219 
220 // ----------------------------------------------------------------------------
221 
222 class PivotTableFilter : public WorkbookHelper
223 {
224 public:
225     explicit            PivotTableFilter( const PivotTable& rPivotTable );
226 
227     /** Reads the settings of a field filter from the filter element. */
228     void                importFilter( const AttributeList& rAttribs );
229     /** Reads additional settings of a field filter from the top10 element. */
230     void                importTop10( const AttributeList& rAttribs );
231 
232     /** Reads the settings of a field filter from the PTFILTER record. */
233     void                importPTFilter( SequenceInputStream& rStrm );
234     /** Reads additional settings of a field filter from the TOP10FILTER record. */
235     void                importTop10Filter( SequenceInputStream& rStrm );
236 
237     /** Applies the filter to the associated pivot table field if possible. */
238     void                finalizeImport();
239 
240 private:
241     const PivotTable&   mrPivotTable;
242     PTFilterModel       maModel;
243 };
244 
245 // ============================================================================
246 
247 struct PTDefinitionModel : public AutoFormatModel
248 {
249     ::rtl::OUString     maName;
250     ::rtl::OUString     maDataCaption;
251     ::rtl::OUString     maGrandTotalCaption;
252     ::rtl::OUString     maRowHeaderCaption;
253     ::rtl::OUString     maColHeaderCaption;
254     ::rtl::OUString     maErrorCaption;
255     ::rtl::OUString     maMissingCaption;
256     ::rtl::OUString     maPageStyle;
257     ::rtl::OUString     maPivotTableStyle;
258     ::rtl::OUString     maVacatedStyle;
259     ::rtl::OUString     maTag;
260     sal_Int32           mnCacheId;
261     sal_Int32           mnDataPosition;
262     sal_Int32           mnPageWrap;
263     sal_Int32           mnIndent;
264     sal_Int32           mnChartFormat;
265     sal_uInt16          mnRowFields;
266     sal_uInt16          mnColFields;
267     bool                mbDataOnRows;
268     bool                mbShowError;
269     bool                mbShowMissing;
270     bool                mbShowItems;
271     bool                mbDisableFieldList;
272     bool                mbShowCalcMembers;
273     bool                mbVisualTotals;
274     bool                mbShowDataDropDown;
275     bool                mbShowDrill;
276     bool                mbPrintDrill;
277     bool                mbEnableDrill;
278     bool                mbPreserveFormatting;
279     bool                mbUseAutoFormat;
280     bool                mbPageOverThenDown;
281     bool                mbSubtotalHiddenItems;
282     bool                mbRowGrandTotals;
283     bool                mbColGrandTotals;
284     bool                mbFieldPrintTitles;
285     bool                mbItemPrintTitles;
286     bool                mbMergeItem;
287     bool                mbShowEmptyRow;
288     bool                mbShowEmptyCol;
289     bool                mbShowHeaders;
290     bool                mbFieldListSortAsc;
291     bool                mbCustomListSort;
292 
293     explicit            PTDefinitionModel();
294 };
295 
296 // ----------------------------------------------------------------------------
297 
298 struct PTLocationModel
299 {
300     ::com::sun::star::table::CellRangeAddress
301                         maRange;            /// Target cell range for the pivot table.
302     sal_Int32           mnFirstHeaderRow;   /// First row of header cells (relative in pivot table).
303     sal_Int32           mnFirstDataRow;     /// First row of data cells (relative in pivot table).
304     sal_Int32           mnFirstDataCol;     /// First column of data cells (relative in pivot table).
305     sal_Int32           mnRowPageCount;     /// Number of rows in page filter area.
306     sal_Int32           mnColPageCount;     /// Number of columns in page filter area.
307 
308     explicit            PTLocationModel();
309 };
310 
311 // ----------------------------------------------------------------------------
312 
313 class PivotTable : public WorkbookHelper
314 {
315 public:
316     explicit            PivotTable( const WorkbookHelper& rHelper );
317 
318     /** Reads global pivot table settings from the pivotTableDefinition element. */
319     void                importPivotTableDefinition( const AttributeList& rAttribs );
320     /** Reads the location of the pivot table from the location element. */
321     void                importLocation( const AttributeList& rAttribs, sal_Int16 nSheet );
322     /** Reads the index of a field located in the row dimension. */
323     void                importRowField( const AttributeList& rAttribs );
324     /** Reads the index of a field located in the column dimension. */
325     void                importColField( const AttributeList& rAttribs );
326     /** Reads the settings of a field located in the page dimension from the pageField element. */
327     void                importPageField( const AttributeList& rAttribs );
328     /** Reads the settings of a field located in the data dimension from the dataField element. */
329     void                importDataField( const AttributeList& rAttribs );
330 
331     /** Reads global pivot table settings from the PTDEFINITION record. */
332     void                importPTDefinition( SequenceInputStream& rStrm );
333     /** Reads the location of the pivot table from the PTLOCATION record. */
334     void                importPTLocation( SequenceInputStream& rStrm, sal_Int16 nSheet );
335     /** Reads the indexes of all fields located in the row dimension from a PTROWFIELDS record. */
336     void                importPTRowFields( SequenceInputStream& rStrm );
337     /** Reads the indexes of all fields located in the column dimension from a PTCOLFIELDS record. */
338     void                importPTColFields( SequenceInputStream& rStrm );
339     /** Reads the settings of a field located in the page dimension from the PTPAGEFIELD record. */
340     void                importPTPageField( SequenceInputStream& rStrm );
341     /** Reads the settings of a field located in the data dimension from the PTDATAFIELD record. */
342     void                importPTDataField( SequenceInputStream& rStrm );
343 
344     /** Reads global pivot table settings from the PTDEFINITION record. */
345     void                importPTDefinition( BiffInputStream& rStrm, sal_Int16 nSheet );
346     /** Reads additional global pivot table settings from the PTDEFINITION2 record. */
347     void                importPTDefinition2( BiffInputStream& rStrm );
348     /** Reads the indexes of all fields located in the row or column dimension from a PTROWCOLFIELDS record. */
349     void                importPTRowColFields( BiffInputStream& rStrm );
350     /** Reads the settings of all fields located in the page dimension from a PTPAGEFIELDS record. */
351     void                importPTPageFields( BiffInputStream& rStrm );
352     /** Reads the settings of a field located in the data dimension from a PTDATAFIELD record. */
353     void                importPTDataField( BiffInputStream& rStrm );
354 
355     /** Creates and returns a new pivot table field. */
356     PivotTableField&    createTableField();
357     /** Creates and returns a new pivot table filter. */
358     PivotTableFilter&   createTableFilter();
359     /** Inserts the pivot table into the sheet. */
360     void                finalizeImport();
361     /** Creates all date group fields for the specified cache field after import. */
362     void                finalizeDateGroupingImport(
363                             const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxBaseDPField,
364                             sal_Int32 nBaseFieldIdx );
365     /** Creates all grouped fields for the specified cache field after import. */
366     void                finalizeParentGroupingImport(
367                             const ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >& rxBaseDPField,
368                             const PivotCacheField& rBaseCacheField,
369                             PivotCacheGroupItemVector& orItemNames );
370 
371     /** Returns the associated data pilot field for the specified pivot table field. */
372     ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >
373                         getDataPilotField( const ::rtl::OUString& rFieldName ) const;
374     /** Returns the associated data pilot field for the specified pivot table field. */
375     ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >
376                         getDataPilotField( sal_Int32 nFieldIdx ) const;
377     /** Returns the data layout field used to store all data fields in row/col dimension. */
378     ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotField >
379                         getDataLayoutField() const;
380 
381     /** Returns the cache field with the specified index. */
382     const PivotCacheField* getCacheField( sal_Int32 nFieldIdx ) const;
383     /** Returns the base cache field of the data field item with the specified index. */
384     const PivotCacheField* getCacheFieldOfDataField( sal_Int32 nDataItemIdx ) const;
385     /** Returns the source column index of the pivot field with the passed index, or -1. */
386     sal_Int32           getCacheDatabaseIndex( sal_Int32 nFieldIdx ) const;
387 
388 private:
389     typedef RefVector< PivotTableField >        PivotTableFieldVector;
390     typedef RefVector< PivotTableFilter >       PivotTableFilterVector;
391     typedef ::std::vector< sal_Int32 >          IndexVector;
392     typedef ::std::vector< PTPageFieldModel >   PageFieldVector;
393     typedef ::std::vector< PTDataFieldModel >   DataFieldVector;
394 
395 private:
396     /** Returns a pivot table field by its index. */
397     PivotTableField*    getTableField( sal_Int32 nFieldIdx );
398 
399     /** Reads a field index for the row or column dimension. */
400     static void         importField( IndexVector& orFields, const AttributeList& rAttribs );
401     /** Reads an array of field indexes for the row or column dimension. */
402     static void         importFields( IndexVector& orFields, SequenceInputStream& rStrm );
403     /** Reads an array of field indexes for the row or column dimension. */
404     static void         importFields( IndexVector& orFields, BiffInputStream& rStrm, sal_Int32 nCount );
405 
406 private:
407     PivotTableFieldVector maFields;         /// All pivot table fields.
408     PivotTableField     maDataField;        /// Data layout field.
409     IndexVector         maRowFields;        /// Indexes to fields in row dimension.
410     IndexVector         maColFields;        /// Indexes to fields in column dimension.
411     PageFieldVector     maPageFields;       /// Settings for all fields in page dimension.
412     DataFieldVector     maDataFields;       /// Settings for all fields in data area.
413     PivotTableFilterVector maFilters;       /// All field filters.
414     PTDefinitionModel   maDefModel;         /// Global pivot table settings.
415     PTLocationModel     maLocationModel;    /// Location settings of the pivot table.
416     const PivotCache*   mpPivotCache;       /// The pivot cache this table is based on.
417     ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDataPilotDescriptor >
418                         mxDPDescriptor;     /// Descriptor of the DataPilot object.
419 };
420 
421 // ============================================================================
422 
423 class PivotTableBuffer : public WorkbookHelper
424 {
425 public:
426     explicit            PivotTableBuffer( const WorkbookHelper& rHelper );
427 
428     /** Creates and returns a new pivot table. */
429     PivotTable&         createPivotTable();
430 
431     /** Inserts all pivot tables into the sheet. */
432     void                finalizeImport();
433 
434 private:
435     typedef RefVector< PivotTable > PivotTableVector;
436     PivotTableVector    maTables;
437 };
438 
439 // ============================================================================
440 
441 } // namespace xls
442 } // namespace oox
443 
444 #endif
445