xref: /trunk/main/sc/source/filter/inc/xipivot.hxx (revision fd04a84d)
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 #ifndef SC_XIPIVOT_HXX
23 #define SC_XIPIVOT_HXX
24 
25 #include <list>
26 #include "xlpivot.hxx"
27 #include "xiroot.hxx"
28 
29 class ScDPSaveData;
30 class ScDPSaveDimension;
31 
32 // ============================================================================
33 // Pivot cache
34 // ============================================================================
35 
36 /** Represents a data item in a pivot cache. */
37 class XclImpPCItem : public XclPCItem
38 {
39 public:
40     explicit            XclImpPCItem( XclImpStream& rStrm );
41 
42     /** Inserts the item data into the passed document. */
43     void                WriteToSource( const XclImpRoot& rRoot, const ScAddress& rScPos ) const;
44 
45 private:
46     /** Reads an SXDOUBLE record describing a floating-point item. */
47     void                ReadSxdouble( XclImpStream& rStrm );
48     /** Reads an SXBOOLEAN record describing a boolean item. */
49     void                ReadSxboolean( XclImpStream& rStrm );
50     /** Reads an SXERROR record describing an error code item. */
51     void                ReadSxerror( XclImpStream& rStrm );
52     /** Reads an SXINTEGER record describing an integer item. */
53     void                ReadSxinteger( XclImpStream& rStrm );
54     /** Reads an SXSTRING record describing a text item. */
55     void                ReadSxstring( XclImpStream& rStrm );
56     /** Reads an SXDATETIME record describing a date/time item. */
57     void                ReadSxdatetime( XclImpStream& rStrm );
58     /** Reads an SXEMPTY record describing an empty item. */
59     void                ReadSxempty( XclImpStream& rStrm );
60 };
61 
62 typedef ScfRef< XclImpPCItem > XclImpPCItemRef;
63 
64 // ============================================================================
65 
66 struct ScDPNumGroupInfo;
67 class XclImpPivotCache;
68 
69 /** Represents a field in a pivot cache (a column of data items in the source area). */
70 class XclImpPCField : public XclPCField, protected XclImpRoot
71 {
72 public:
73     /** Creates a pivot cache field by reading an SXFIELD record. */
74     explicit            XclImpPCField( const XclImpRoot& rRoot,
75                             XclImpPivotCache& rPCache, sal_uInt16 nFieldIdx );
76     virtual             ~XclImpPCField();
77 
78     // general field/item access ----------------------------------------------
79 
80     /** Returns the name of the field, uses the passed visible name if supported. */
81     const String&       GetFieldName( const ScfStringVec& rVisNames ) const;
82 
83     /** Returns the base field if this is a grouping field. */
84     const XclImpPCField* GetGroupBaseField() const;
85 
86     /** Returns the number of items of this field. */
87     sal_uInt16          GetItemCount() const;
88     /** Returns the item at the specified position or 0 on error. */
89     const XclImpPCItem* GetItem( sal_uInt16 nItemIdx ) const;
90     /** Returns the item representing a limit value in numeric/date/time grouping fields.
91         @param nItemIdx  One of EXC_SXFIELD_INDEX_MIN, EXC_SXFIELD_INDEX_MAX, or EXC_SXFIELD_INDEX_STEP. */
92     const XclImpPCItem* GetLimitItem( sal_uInt16 nItemIdx ) const;
93 
94     /** Inserts the field name into the document. */
95     void                WriteFieldNameToSource( SCCOL nScCol, SCTAB nScTab ) const;
96     /** Inserts the specified item data into the document. */
97     void                WriteOrigItemToSource( SCROW nScRow, SCTAB nScTab, sal_uInt16 nItemIdx ) const;
98     /** Inserts the data of the last inserted item into the document. */
99     void                WriteLastOrigItemToSource( SCROW nScRow, SCTAB nScTab ) const;
100 
101     // records ----------------------------------------------------------------
102 
103     /** Reads the SXFIELD record describing the field. */
104     void                ReadSxfield( XclImpStream& rStrm );
105     /** Reads an item data record describing a new item. */
106     void                ReadItem( XclImpStream& rStrm );
107     /** Reads the SXNUMGROUP record describing numeric grouping fields. */
108     void                ReadSxnumgroup( XclImpStream& rStrm );
109     /** Reads the SXGROUPINFO record describing the item order in grouping fields. */
110     void                ReadSxgroupinfo( XclImpStream& rStrm );
111 
112     // grouping ---------------------------------------------------------------
113 
114     /** Inserts grouping information of this field into the passed ScDPSaveData. */
115     void                ConvertGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
116 
117     // ------------------------------------------------------------------------
118 private:
119     /** Inserts standard grouping information of this field into the passed ScDPSaveData. */
120     void                ConvertStdGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
121     /** Inserts numeric grouping information of this field into the passed ScDPSaveData. */
122     void                ConvertNumGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
123     /** Inserts date grouping information of this field into the passed ScDPSaveData. */
124     void                ConvertDateGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
125 
126     /** Returns a Calc struct with numeric grouping data. */
127     ScDPNumGroupInfo    GetScNumGroupInfo() const;
128     /** Returns a Calc struct with date grouping data. */
129     ScDPNumGroupInfo    GetScDateGroupInfo() const;
130 
131     /** Returns a limit value for numeric grouping fields. */
132     const double*       GetNumGroupLimit( sal_uInt16 nLimitIdx ) const;
133     /** Returns a limit value for date grouping fields (minimum/maximum only). */
134     const DateTime*     GetDateGroupLimit( sal_uInt16 nLimitIdx ) const;
135     /** Returns the step value for date grouping fields. */
136     const sal_Int16*    GetDateGroupStep() const;
137 
138 private:
139     typedef ::std::vector< XclImpPCItemRef > XclImpPCItemVec;
140 
141     XclImpPivotCache&   mrPCache;           /// Parent pivot cache containing this field.
142     XclImpPCItemVec     maItems;            /// List of all displayed data items.
143     XclImpPCItemVec     maOrigItems;        /// List of all source data items.
144     XclImpPCItemVec     maNumGroupItems;    /// List of items containing numeric grouping limits.
145     mutable SCCOL       mnSourceScCol;      /// Column index of source data for this field.
146     bool                mbNumGroupInfoRead; /// true = Numeric grouping info read (SXNUMGROUP record).
147 };
148 
149 typedef ScfRef< XclImpPCField > XclImpPCFieldRef;
150 
151 // ============================================================================
152 
153 class XclImpPivotCache : protected XclImpRoot
154 {
155 public:
156     explicit            XclImpPivotCache( const XclImpRoot& rRoot );
157     virtual             ~XclImpPivotCache();
158 
159     // data access ------------------------------------------------------------
160 
161     /** Returns the data source range read from the DCONREF record. */
GetSourceRange() const162     inline const ScRange& GetSourceRange() const { return maSrcRange; }
163 
164     /** Returns the number of pivot cache fields. */
165     sal_uInt16          GetFieldCount() const;
166     /** Returns read-only access to a pivot cache field. */
167     const XclImpPCField* GetField( sal_uInt16 nFieldIdx ) const;
168 
169     // records ----------------------------------------------------------------
170 
171     /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
172     void                ReadSxidstm( XclImpStream& rStrm );
173     /** Reads an SXVS record containing the source type of the pivot cache. */
174     void                ReadSxvs( XclImpStream& rStrm );
175     /** Reads a DCONREF record containing the source range of the pivot cache. */
176     void                ReadDconref( XclImpStream& rStrm );
177     /** Reads the entire pivot cache stream. Uses decrypter from passed stream. */
178     void                ReadPivotCacheStream( XclImpStream& rStrm );
179 
180     bool                IsRefreshOnLoad() const;
181 
182 private:
183     typedef ::std::vector< XclImpPCFieldRef > XclImpPCFieldVec;
184 
185     XclPCInfo           maPCInfo;           /// Pivot cache settings (SXDB record).
186     XclImpPCFieldVec    maFields;           /// List of pivot cache fields.
187     ScRange             maSrcRange;         /// Source range in the spreadsheet.
188     String              maUrl;              /// URL of the source data.
189     String              maTabName;          /// Sheet name of the source data.
190     sal_uInt16          mnStrmId;           /// Pivot cache stream identifier.
191     sal_uInt16          mnSrcType;          /// Source data type.
192     bool                mbSelfRef;          /// true = Source data from own document.
193 };
194 
195 typedef ScfRef< XclImpPivotCache > XclImpPivotCacheRef;
196 
197 // ============================================================================
198 // Pivot table
199 // ============================================================================
200 
201 class XclImpPivotTable;
202 class XclImpPTField;
203 
204 // ============================================================================
205 
206 class XclImpPTItem
207 {
208 public:
209     explicit            XclImpPTItem( const XclImpPTField & );
210 
211     /** Returns the internal name of the item or 0, if no name could be found. */
212     const String*       GetItemName() const;
213     /** Returns the displayed name of the item or 0, if no name could be found. */
214     const String*       GetVisItemName() const;
215 
216     /** Reads an SXVI record containing data of this item. */
217     void                ReadSxvi( XclImpStream& rStrm );
218 
219     /** Inserts this item into the passed ScDPSaveDimension. */
220     void                ConvertItem( ScDPSaveDimension& rSaveDim ) const;
221 
222 private:
223     XclPTItemInfo       maItemInfo;         /// General data for this item.
224     //const XclImpPCField* mpCacheField;      /// Corresponding pivot cache field.
225     const XclImpPTField & mrPTField;
226 };
227 
228 typedef ScfRef< XclImpPTItem > XclImpPTItemRef;
229 
230 // ============================================================================
231 
232 class XclImpPTField
233 {
234 public:
235     explicit            XclImpPTField( const XclImpPivotTable& rPTable, sal_uInt16 nCacheIdx );
236 
237     // general field/item access ----------------------------------------------
238 
239     /** Returns the corresponding pivot cache field of this field. */
240     const XclImpPCField* GetCacheField() const;
241     /** Returns the name of this field that is used to create the Calc dimensions. */
242     const String&       GetFieldName() const;
243     /** Returns the internally set visible name of this field. */
244     const String&       GetVisFieldName() const;
245 
246     /** Returns the specified item. */
247     const XclImpPTItem* GetItem( sal_uInt16 nItemIdx ) const;
248     /** Returns the internal name of the specified item. */
249     const String*       GetItemName( sal_uInt16 nItemIdx ) const;
250     /** Returns the displayed name of the specified item. */
251     const String*       GetVisItemName( sal_uInt16 nItemIdx ) const;
252 
253     /** Returns the flags of the axes this field is part of. */
GetAxes() const254     inline sal_uInt16   GetAxes() const { return maFieldInfo.mnAxes; }
255     /** Sets the flags of the axes this field is part of. */
SetAxes(sal_uInt16 nAxes)256     inline void         SetAxes( sal_uInt16 nAxes ) { maFieldInfo.mnAxes = nAxes; }
257 
258     // records ----------------------------------------------------------------
259 
260     /** Reads an SXVD record describing the field. */
261     void                ReadSxvd( XclImpStream& rStrm );
262     /** Reads an SXVDEX record describing extended options of the field. */
263     void                ReadSxvdex( XclImpStream& rStrm );
264     /** Reads an SXVI record describing a new item of this field. */
265     void                ReadSxvi( XclImpStream& rStrm );
266 
267     // row/column fields ------------------------------------------------------
268 
269     void                ConvertRowColField( ScDPSaveData& rSaveData ) const;
270 
271     // page fields ------------------------------------------------------------
272 
273     void                SetPageFieldInfo( const XclPTPageFieldInfo& rPageInfo );
274     void                ConvertPageField( ScDPSaveData& rSaveData ) const;
275 
276     // hidden fields ----------------------------------------------------------
277 
278     void                ConvertHiddenField( ScDPSaveData& rSaveData ) const;
279 
280     // data fields ------------------------------------------------------------
281 
282     bool                HasDataFieldInfo() const;
283     void                AddDataFieldInfo( const XclPTDataFieldInfo& rDataInfo );
284     void                ConvertDataField( ScDPSaveData& rSaveData ) const;
285 
286     // ------------------------------------------------------------------------
287 private:
288     ScDPSaveDimension*  ConvertRCPField( ScDPSaveData& rSaveData ) const;
289     void                ConvertFieldInfo( ScDPSaveDimension& rSaveDim ) const;
290 
291     void                ConvertDataField( ScDPSaveDimension& rSaveDim, const XclPTDataFieldInfo& rDataInfo ) const;
292     void                ConvertDataFieldInfo( ScDPSaveDimension& rSaveDim, const XclPTDataFieldInfo& rDataInfo ) const;
293     void                ConvertItems( ScDPSaveDimension& rSaveDim ) const;
294 
295 private:
296     typedef ::std::list< XclPTDataFieldInfo >   XclPTDataFieldInfoList;
297     typedef ::std::vector< XclImpPTItemRef >    XclImpPTItemVec;
298 
299     const XclImpPivotTable& mrPTable;       /// Parent pivot table containing this field.
300     XclPTFieldInfo      maFieldInfo;        /// General field info (SXVD record).
301     XclPTFieldExtInfo   maFieldExtInfo;     /// Extended field info (SXVDEX record).
302     XclPTPageFieldInfo  maPageInfo;         /// Page field info (entry from SXPI record).
303     XclPTDataFieldInfoList maDataInfoList;  /// List of extended data field info (SXDI records).
304     XclImpPTItemVec     maItems;            /// List of all items of this field.
305 };
306 
307 typedef ScfRef< XclImpPTField > XclImpPTFieldRef;
308 
309 // ============================================================================
310 
311 class XclImpPivotTable : protected XclImpRoot
312 {
313 public:
314     explicit            XclImpPivotTable( const XclImpRoot& rRoot );
315     virtual             ~XclImpPivotTable();
316 
317     // cache/field access, misc. ----------------------------------------------
318 
GetPivotCache() const319     inline XclImpPivotCacheRef GetPivotCache() const { return mxPCache; }
GetVisFieldNames() const320     inline const ScfStringVec& GetVisFieldNames() const { return maVisFieldNames; }
321 
322     sal_uInt16          GetFieldCount() const;
323     const XclImpPTField* GetField( sal_uInt16 nFieldIdx ) const;
324     XclImpPTField*      GetFieldAcc( sal_uInt16 nFieldIdx );
325     const String&       GetFieldName( sal_uInt16 nFieldIdx ) const;
326 
327     const XclImpPTField* GetDataField( sal_uInt16 nDataFieldIdx ) const;
328     const String&       GetDataFieldName( sal_uInt16 nDataFieldIdx ) const;
329 
330     // records ----------------------------------------------------------------
331 
332     /** Reads an SXVIEW record starting a new pivot table. */
333     void                ReadSxview( XclImpStream& rStrm );
334     /** Reads an SXVD record describing a new field. */
335     void                ReadSxvd( XclImpStream& rStrm );
336     /** Reads an SXVI record describing a new item of the current field. */
337     void                ReadSxvi( XclImpStream& rStrm );
338     /** Reads an SXVDEX record describing extended options of the current field. */
339     void                ReadSxvdex( XclImpStream& rStrm );
340     /** Reads an SXIVD record containing the row field or column field order. */
341     void                ReadSxivd( XclImpStream& rStrm );
342     /** Reads an SXPI record containing page field data. */
343     void                ReadSxpi( XclImpStream& rStrm );
344     /** Reads an SXDI record containing data field data. */
345     void                ReadSxdi( XclImpStream& rStrm );
346     /** Reads an SXEX record containing additional settings for the pivot table. */
347     void                ReadSxex( XclImpStream& rStrm );
348     /** Reads an SXVIEWEX9 record that specifies the pivot tables
349      *  autoformat. */
350     void                ReadSxViewEx9( XclImpStream& rStrm );
351 
352     // ------------------------------------------------------------------------
353 
354     /** Inserts the pivot table into the Calc document. */
355     void                Convert();
356 
357     void                MaybeRefresh();
358 
359     void                ApplyMergeFlags(const ScRange& rOutRange, const ScDPSaveData& rSaveData);
360 
GetCacheId() const361     sal_uInt16          GetCacheId() const { return maPTInfo.mnCacheIdx; };
362 
363     // ------------------------------------------------------------------------
364 private:
365     typedef ::std::vector< XclImpPTFieldRef > XclImpPTFieldVec;
366 
367     XclImpPivotCacheRef mxPCache;           /// Pivot cache containing field/item names.
368 
369     XclPTInfo           maPTInfo;           /// General info about the pivot table (SXVIEW record).
370     XclPTExtInfo        maPTExtInfo;        /// Extended info about the pivot table (SXEX record).
371     XclPTViewEx9Info    maPTViewEx9Info;     /// (SXVIEWEX9 record)
372     XclImpPTFieldVec    maFields;           /// Vector containing all fields.
373     XclImpPTFieldRef    mxCurrField;        /// Current field for importing additional info.
374     ScfStringVec        maVisFieldNames;    /// Vector containing all visible field names.
375     ScfUInt16Vec        maRowFields;        /// Row field indexes.
376     ScfUInt16Vec        maColFields;        /// Column field indexes.
377     ScfUInt16Vec        maPageFields;       /// Page field indexes.
378     ScfUInt16Vec        maOrigDataFields;   /// Original data field indexes.
379     ScfUInt16Vec        maFiltDataFields;   /// Filtered data field indexes.
380     XclImpPTField       maDataOrientField;  /// Special data field orientation field.
381     ScRange             maOutScRange;       /// Output range in the Calc document.
382     ScDPObject*         mpDPObj;
383 };
384 
385 typedef ScfRef< XclImpPivotTable > XclImpPivotTableRef;
386 
387 // ============================================================================
388 // ============================================================================
389 
390 /** The main class for pivot table import.
391 
392     This class contains functions to read all records related to pivot tables
393     and pivot caches.
394  */
395 class XclImpPivotTableManager : protected XclImpRoot
396 {
397 public:
398     explicit            XclImpPivotTableManager( const XclImpRoot& rRoot );
399     virtual             ~XclImpPivotTableManager();
400 
401     // pivot cache records ----------------------------------------------------
402 
403     /** Returns the pivot cache with the specified 0-based index. */
404     XclImpPivotCacheRef GetPivotCache( sal_uInt16 nCacheIdx );
405 
406     /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
407     void                ReadSxidstm( XclImpStream& rStrm );
408     /** Reads an SXVS record containing the source type of a pivot cache. */
409     void                ReadSxvs( XclImpStream& rStrm );
410     /** Reads a DCONREF record containing the source range of a pivot cache. */
411     void                ReadDconref( XclImpStream& rStrm );
412 
413     // pivot table records ----------------------------------------------------
414 
415     /** Reads an SXVIEW record describing a new pivot table. */
416     void                ReadSxview( XclImpStream& rStrm );
417     /** Reads an SXVD record describing a new field. */
418     void                ReadSxvd( XclImpStream& rStrm );
419     /** Reads an SXVDEX record describing extended options of a field. */
420     void                ReadSxvdex( XclImpStream& rStrm );
421     /** Reads an SXIVD record containing the row field or column field order. */
422     void                ReadSxivd( XclImpStream& rStrm );
423     /** Reads an SXPI record containing page field data. */
424     void                ReadSxpi( XclImpStream& rStrm );
425     /** Reads an SXDI record containing data field data. */
426     void                ReadSxdi( XclImpStream& rStrm );
427     /** Reads an SXVI record describing a new item of the current field. */
428     void                ReadSxvi( XclImpStream& rStrm );
429     /** Reads an SXEX record containing additional settings for a pivot table. */
430     void                ReadSxex( XclImpStream& rStrm );
431     /** Reads an SXVIEWEX9 record that specifies the pivot tables
432      *  autoformat. */
433     void                ReadSxViewEx9( XclImpStream& rStrm );
434 
435     // ------------------------------------------------------------------------
436 
437     /** Reads all used pivot caches and creates additional sheets for external data sources. */
438 //    void                ReadPivotCaches( XclImpStream& rStrm );
439     /** Inserts all pivot tables into the Calc document. */
440 //    void                ConvertPivotTables();
441     void                ConvertPivotTables( XclImpStream & );
442 //    void                MaybeRefreshPivotTables();
443 
444 private:
445     typedef ::std::vector< XclImpPivotCacheRef >    XclImpPivotCacheVec;
446     typedef ::std::vector< XclImpPivotTableRef >    XclImpPivotTableVec;
447 
448     XclImpPivotCacheVec maPCaches;          /// List of all pivot caches.
449     XclImpPivotTableVec maPTables;          /// List of all pivot tables.
450 };
451 
452 // ============================================================================
453 
454 #endif
455 
456