Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / inc / xipivot.hxx
blobf6fa56d3cc0bb46538f7c8a0578183703b40a063
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_XIPIVOT_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XIPIVOT_HXX
23 #include "xlpivot.hxx"
24 #include "xiroot.hxx"
25 #include <vector>
26 #include <memory>
28 class ScDPSaveData;
29 class ScDPSaveDimension;
31 // Pivot cache
33 /** Represents a data item in a pivot cache. */
34 class XclImpPCItem : public XclPCItem
36 public:
37 explicit XclImpPCItem( XclImpStream& rStrm );
39 /** Inserts the item data into the passed document. */
40 void WriteToSource( XclImpRoot& rRoot, const ScAddress& rScPos ) const;
42 private:
43 /** Reads an SXDOUBLE record describing a floating-point item. */
44 void ReadSxdouble( XclImpStream& rStrm );
45 /** Reads an SXBOOLEAN record describing a boolean item. */
46 void ReadSxboolean( XclImpStream& rStrm );
47 /** Reads an SXERROR record describing an error code item. */
48 void ReadSxerror( XclImpStream& rStrm );
49 /** Reads an SXINTEGER record describing an integer item. */
50 void ReadSxinteger( XclImpStream& rStrm );
51 /** Reads an SXSTRING record describing a text item. */
52 void ReadSxstring( XclImpStream& rStrm );
53 /** Reads an SXDATETIME record describing a date/time item. */
54 void ReadSxdatetime( XclImpStream& rStrm );
55 /** Reads an SXEMPTY record describing an empty item. */
56 void ReadSxempty( XclImpStream& rStrm );
59 typedef std::shared_ptr< XclImpPCItem > XclImpPCItemRef;
61 struct ScDPNumGroupInfo;
62 class XclImpPivotCache;
64 /** Represents a field in a pivot cache (a column of data items in the source area). */
65 class XclImpPCField : public XclPCField, protected XclImpRoot
67 public:
68 /** Creates a pivot cache field by reading an SXFIELD record. */
69 explicit XclImpPCField( const XclImpRoot& rRoot,
70 XclImpPivotCache& rPCache, sal_uInt16 nFieldIdx );
71 virtual ~XclImpPCField() override;
73 // general field/item access ----------------------------------------------
75 /** Returns the name of the field, uses the passed visible name if supported. */
76 const OUString& GetFieldName( const ScfStringVec& rVisNames ) const;
78 /** Returns the base field if this is a grouping field. */
79 const XclImpPCField* GetGroupBaseField() const;
81 /** Returns the item at the specified position or 0 on error. */
82 const XclImpPCItem* GetItem( sal_uInt16 nItemIdx ) const;
83 /** Returns the item representing a limit value in numeric/date/time grouping fields.
84 @param nItemIdx One of EXC_SXFIELD_INDEX_MIN, EXC_SXFIELD_INDEX_MAX, or EXC_SXFIELD_INDEX_STEP. */
85 const XclImpPCItem* GetLimitItem( sal_uInt16 nItemIdx ) const;
87 /** Inserts the field name into the document. */
88 void WriteFieldNameToSource( SCCOL nScCol, SCTAB nScTab );
89 /** Inserts the specified item data into the document. */
90 void WriteOrigItemToSource( SCROW nScRow, SCTAB nScTab, sal_uInt16 nItemIdx );
91 /** Inserts the data of the last inserted item into the document. */
92 void WriteLastOrigItemToSource( SCROW nScRow, SCTAB nScTab );
94 // records ----------------------------------------------------------------
96 /** Reads the SXFIELD record describing the field. */
97 void ReadSxfield( XclImpStream& rStrm );
98 /** Reads an item data record describing a new item. */
99 void ReadItem( XclImpStream& rStrm );
100 /** Reads the SXNUMGROUP record describing numeric grouping fields. */
101 void ReadSxnumgroup( XclImpStream& rStrm );
102 /** Reads the SXGROUPINFO record describing the item order in grouping fields. */
103 void ReadSxgroupinfo( XclImpStream& rStrm );
105 // grouping ---------------------------------------------------------------
107 /** Inserts grouping information of this field into the passed ScDPSaveData. */
108 void ConvertGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
110 private:
111 /** Inserts standard grouping information of this field into the passed ScDPSaveData. */
112 void ConvertStdGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
113 /** Inserts numeric grouping information of this field into the passed ScDPSaveData. */
114 void ConvertNumGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
115 /** Inserts date grouping information of this field into the passed ScDPSaveData. */
116 void ConvertDateGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
118 /** Returns a Calc struct with numeric grouping data. */
119 ScDPNumGroupInfo GetScNumGroupInfo() const;
120 /** Returns a Calc struct with date grouping data. */
121 ScDPNumGroupInfo GetScDateGroupInfo() const;
123 /** Returns a limit value for numeric grouping fields. */
124 const double* GetNumGroupLimit( sal_uInt16 nLimitIdx ) const;
125 /** Returns a limit value for date grouping fields (minimum/maximum only). */
126 const DateTime* GetDateGroupLimit( sal_uInt16 nLimitIdx ) const;
127 /** Returns the step value for date grouping fields. */
128 const sal_Int16* GetDateGroupStep() const;
130 private:
131 typedef ::std::vector< XclImpPCItemRef > XclImpPCItemVec;
133 XclImpPivotCache& mrPCache; /// Parent pivot cache containing this field.
134 XclImpPCItemVec maItems; /// List of all displayed data items.
135 XclImpPCItemVec maOrigItems; /// List of all source data items.
136 XclImpPCItemVec maNumGroupItems; /// List of items containing numeric grouping limits.
137 mutable SCCOL mnSourceScCol; /// Column index of source data for this field.
138 bool mbNumGroupInfoRead; /// true = Numeric grouping info read (SXNUMGROUP record).
141 typedef std::shared_ptr< XclImpPCField > XclImpPCFieldRef;
143 class XclImpPivotCache : protected XclImpRoot
145 public:
146 explicit XclImpPivotCache( const XclImpRoot& rRoot );
147 virtual ~XclImpPivotCache() override;
149 // data access ------------------------------------------------------------
151 /** Returns the data source range read from the DCONREF record. */
152 const ScRange& GetSourceRange() const { return maSrcRange; }
154 const OUString& GetSourceRangeName() const { return maSrcRangeName; }
156 /** Returns read-only access to a pivot cache field. */
157 const XclImpPCField* GetField( sal_uInt16 nFieldIdx ) const;
159 // records ----------------------------------------------------------------
161 /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
162 void ReadSxidstm( XclImpStream& rStrm );
163 /** Reads an SXVS record containing the source type of the pivot cache. */
164 void ReadSxvs( XclImpStream& rStrm );
165 /** Reads a DCONREF record containing the source range of the pivot cache. */
166 void ReadDconref( XclImpStream& rStrm );
168 * Read DECONNAME record which contains the defined name of the source
169 * range.
171 void ReadDConName( XclImpStream& rStrm );
172 /** Reads the entire pivot cache stream. Uses decrypter from passed stream. */
173 void ReadPivotCacheStream( const XclImpStream& rStrm );
175 bool IsRefreshOnLoad() const;
176 bool IsValid() const;
178 private:
179 typedef ::std::vector< XclImpPCFieldRef > XclImpPCFieldVec;
181 XclPCInfo maPCInfo; /// Pivot cache settings (SXDB record).
182 XclImpPCFieldVec maFields; /// List of pivot cache fields.
183 ScRange maSrcRange; /// Source range in the spreadsheet.
184 OUString maUrl; /// URL of the source data.
185 OUString maTabName; /// Sheet name of the source data.
186 OUString maSrcRangeName; /// Name of the source data range.
187 sal_uInt16 mnStrmId; /// Pivot cache stream identifier.
188 sal_uInt16 mnSrcType; /// Source data type.
189 bool mbSelfRef; /// true = Source data from own document.
192 typedef std::shared_ptr< XclImpPivotCache > XclImpPivotCacheRef;
194 // Pivot table
196 class XclImpPivotTable;
198 class XclImpPTItem
200 public:
201 explicit XclImpPTItem( const XclImpPCField* pCacheField );
203 /** Returns the internal name of the item or 0, if no name could be found. */
204 const OUString* GetItemName() const;
205 /** Returns the internal name of the item. */
206 std::pair<bool, OUString> GetItemName(const ScDPSaveDimension& rSaveDim, ScDPObject* pObj, const XclImpRoot& rRoot) const;
208 /** Reads an SXVI record containing data of this item. */
209 void ReadSxvi( XclImpStream& rStrm );
211 /** Inserts this item into the passed ScDPSaveDimension. */
212 void ConvertItem( ScDPSaveDimension& rSaveDim, ScDPObject* pObj, const XclImpRoot& rRoot ) const;
214 private:
215 XclPTItemInfo maItemInfo; /// General data for this item.
216 const XclImpPCField* mpCacheField; /// Corresponding pivot cache field.
219 typedef std::shared_ptr< XclImpPTItem > XclImpPTItemRef;
221 class XclImpPTField
223 public:
224 explicit XclImpPTField( const XclImpPivotTable& rPTable, sal_uInt16 nCacheIdx );
226 // general field/item access ----------------------------------------------
228 /** Returns the corresponding pivot cache field of this field. */
229 const XclImpPCField* GetCacheField() const;
230 /** Returns the name of this field that is used to create the Calc dimensions. */
231 OUString GetFieldName() const;
232 /** Returns the internally set visible name of this field. */
233 OUString GetVisFieldName() const;
235 /** Returns the specified item. */
236 const XclImpPTItem* GetItem( sal_uInt16 nItemIdx ) const;
237 /** Returns the internal name of the specified item. */
238 const OUString* GetItemName( sal_uInt16 nItemIdx ) const;
240 /** Returns the flags of the axes this field is part of. */
241 sal_uInt16 GetAxes() const { return maFieldInfo.mnAxes; }
242 /** Sets the flags of the axes this field is part of. */
243 void SetAxes( sal_uInt16 nAxes ) { maFieldInfo.mnAxes = nAxes; }
245 // records ----------------------------------------------------------------
247 /** Reads an SXVD record describing the field. */
248 void ReadSxvd( XclImpStream& rStrm );
249 /** Reads an SXVDEX record describing extended options of the field. */
250 void ReadSxvdex( XclImpStream& rStrm );
251 /** Reads an SXVI record describing a new item of this field. */
252 void ReadSxvi( XclImpStream& rStrm );
254 // row/column fields ------------------------------------------------------
256 void ConvertRowColField( ScDPSaveData& rSaveData ) const;
258 // page fields ------------------------------------------------------------
260 void SetPageFieldInfo( const XclPTPageFieldInfo& rPageInfo );
261 void ConvertPageField( ScDPSaveData& rSaveData ) const;
263 // hidden fields ----------------------------------------------------------
265 void ConvertHiddenField( ScDPSaveData& rSaveData ) const;
267 // data fields ------------------------------------------------------------
269 bool HasDataFieldInfo() const;
270 void AddDataFieldInfo( const XclPTDataFieldInfo& rDataInfo );
271 void ConvertDataField( ScDPSaveData& rSaveData ) const;
273 void ConvertFieldInfo( const ScDPSaveData& rSaveData, ScDPObject* pObj, const XclImpRoot& rRoot, bool bPageField = false ) const;
275 private:
276 void ConvertRCPField( ScDPSaveData& rSaveData ) const;
278 void ConvertDataField( ScDPSaveDimension& rSaveDim, const XclPTDataFieldInfo& rDataInfo ) const;
279 void ConvertDataFieldInfo( ScDPSaveDimension& rSaveDim, const XclPTDataFieldInfo& rDataInfo ) const;
281 private:
282 typedef ::std::vector< XclImpPTItemRef > XclImpPTItemVec;
284 const XclImpPivotTable& mrPTable; /// Parent pivot table containing this field.
285 XclPTFieldInfo maFieldInfo; /// General field info (SXVD record).
286 XclPTFieldExtInfo maFieldExtInfo; /// Extended field info (SXVDEX record).
287 XclPTPageFieldInfo maPageInfo; /// Page field info (entry from SXPI record).
288 std::vector< XclPTDataFieldInfo > maDataInfoVector; /// Vector of extended data field info (SXDI records).
289 XclImpPTItemVec maItems; /// List of all items of this field.
292 typedef std::shared_ptr< XclImpPTField > XclImpPTFieldRef;
294 class XclImpPivotTable : protected XclImpRoot
296 public:
297 explicit XclImpPivotTable( const XclImpRoot& rRoot );
298 virtual ~XclImpPivotTable() override;
300 // cache/field access, misc. ----------------------------------------------
302 const XclImpPivotCacheRef& GetPivotCache() const { return mxPCache; }
303 const ScfStringVec& GetVisFieldNames() const { return maVisFieldNames; }
305 sal_uInt16 GetFieldCount() const;
306 const XclImpPTField* GetField( sal_uInt16 nFieldIdx ) const;
307 XclImpPTField* GetFieldAcc( sal_uInt16 nFieldIdx );
309 const XclImpPTField* GetDataField( sal_uInt16 nDataFieldIdx ) const;
310 OUString GetDataFieldName( sal_uInt16 nDataFieldIdx ) const;
312 // records ----------------------------------------------------------------
314 /** Reads an SXVIEW record starting a new pivot table. */
315 void ReadSxview( XclImpStream& rStrm );
316 /** Reads an SXVD record describing a new field. */
317 void ReadSxvd( XclImpStream& rStrm );
318 /** Reads an SXVI record describing a new item of the current field. */
319 void ReadSxvi( XclImpStream& rStrm );
320 /** Reads an SXVDEX record describing extended options of the current field. */
321 void ReadSxvdex( XclImpStream& rStrm );
322 /** Reads an SXIVD record containing the row field or column field order. */
323 void ReadSxivd( XclImpStream& rStrm );
324 /** Reads an SXPI record containing page field data. */
325 void ReadSxpi( XclImpStream& rStrm );
326 /** Reads an SXDI record containing data field data. */
327 void ReadSxdi( XclImpStream& rStrm );
328 /** Reads an SXEX record containing additional settings for the pivot table. */
329 void ReadSxex( XclImpStream& rStrm );
330 /** Reads an SXVIEWEX9 record that specifies the pivot tables
331 * autoformat. */
332 void ReadSxViewEx9( XclImpStream& rStrm );
334 /** Reads an SXADDL record that specifies additional info for pivot table. */
335 void ReadSxAddl( XclImpStream& rStrm );
337 /** Inserts the pivot table into the Calc document. */
338 void Convert();
340 void MaybeRefresh();
342 void ApplyMergeFlags(const ScRange& rOutRange, const ScDPSaveData& rSaveData);
343 void ApplyFieldInfo();
345 private:
346 typedef ::std::vector< XclImpPTFieldRef > XclImpPTFieldVec;
348 XclImpPivotCacheRef mxPCache; /// Pivot cache containing field/item names.
350 XclPTInfo maPTInfo; /// General info about the pivot table (SXVIEW record).
351 XclPTExtInfo maPTExtInfo; /// Extended info about the pivot table (SXEX record).
352 XclPTViewEx9Info maPTViewEx9Info; /// (SXVIEWEX9 record)
353 XclPTAddl maPTAddlInfo;
354 XclImpPTFieldVec maFields; /// Vector containing all fields.
355 XclImpPTFieldRef mxCurrField; /// Current field for importing additional info.
356 ScfStringVec maVisFieldNames; /// Vector containing all visible field names.
357 ScfUInt16Vec maRowFields; /// Row field indexes.
358 ScfUInt16Vec maColFields; /// Column field indexes.
359 ScfUInt16Vec maPageFields; /// Page field indexes.
360 ScfUInt16Vec maOrigDataFields; /// Original data field indexes.
361 ScfUInt16Vec maFiltDataFields; /// Filtered data field indexes.
362 XclImpPTField maDataOrientField; /// Special data field orientation field.
363 ScRange maOutScRange; /// Output range in the Calc document.
364 ScDPObject* mpDPObj;
367 typedef std::shared_ptr< XclImpPivotTable > XclImpPivotTableRef;
369 /** The main class for pivot table import.
371 This class contains functions to read all records related to pivot tables
372 and pivot caches.
374 class XclImpPivotTableManager : protected XclImpRoot
376 public:
377 explicit XclImpPivotTableManager( const XclImpRoot& rRoot );
378 virtual ~XclImpPivotTableManager() override;
380 // pivot cache records ----------------------------------------------------
382 /** Returns the pivot cache with the specified 0-based index. */
383 XclImpPivotCacheRef GetPivotCache( sal_uInt16 nCacheIdx );
385 /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
386 void ReadSxidstm( XclImpStream& rStrm );
387 /** Reads an SXVS record containing the source type of a pivot cache. */
388 void ReadSxvs( XclImpStream& rStrm );
389 /** Reads a DCONREF record containing the source range of a pivot cache. */
390 void ReadDconref( XclImpStream& rStrm );
391 void ReadDConName( XclImpStream& rStrm );
393 // pivot table records ----------------------------------------------------
395 /** Reads an SXVIEW record describing a new pivot table. */
396 void ReadSxview( XclImpStream& rStrm );
397 /** Reads an SXVD record describing a new field. */
398 void ReadSxvd( XclImpStream& rStrm );
399 /** Reads an SXVDEX record describing extended options of a field. */
400 void ReadSxvdex( XclImpStream& rStrm );
401 /** Reads an SXIVD record containing the row field or column field order. */
402 void ReadSxivd( XclImpStream& rStrm );
403 /** Reads an SXPI record containing page field data. */
404 void ReadSxpi( XclImpStream& rStrm );
405 /** Reads an SXDI record containing data field data. */
406 void ReadSxdi( XclImpStream& rStrm );
407 /** Reads an SXVI record describing a new item of the current field. */
408 void ReadSxvi( XclImpStream& rStrm );
409 /** Reads an SXEX record containing additional settings for a pivot table. */
410 void ReadSxex( XclImpStream& rStrm );
411 /** Reads an SXVIEWEX9 record that specifies the pivot tables
412 * autoformat. */
413 void ReadSxViewEx9( XclImpStream& rStrm );
414 /** Reads an SXADDL record that specifies additional info for pivot table. */
415 void ReadSxAddl( XclImpStream& rStrm );
417 /** Reads all used pivot caches and creates additional sheets for external data sources. */
418 void ReadPivotCaches( const XclImpStream& rStrm );
419 /** Inserts all pivot tables into the Calc document. */
420 void ConvertPivotTables();
422 void MaybeRefreshPivotTables();
424 private:
425 typedef ::std::vector< XclImpPivotCacheRef > XclImpPivotCacheVec;
426 typedef ::std::vector< XclImpPivotTableRef > XclImpPivotTableVec;
428 XclImpPivotCacheVec maPCaches; /// List of all pivot caches.
429 XclImpPivotTableVec maPTables; /// List of all pivot tables.
432 #endif
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */