Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / inc / xipivot.hxx
blob611de505e8515b4487897cf9862d4a52ec74ac5f
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 #pragma once
22 #include "xlpivot.hxx"
23 #include "xiroot.hxx"
24 #include <vector>
25 #include <memory>
27 class ScDPSaveData;
28 class ScDPSaveDimension;
30 // Pivot cache
32 /** Represents a data item in a pivot cache. */
33 class XclImpPCItem : public XclPCItem
35 public:
36 explicit XclImpPCItem( XclImpStream& rStrm );
38 /** Inserts the item data into the passed document. */
39 void WriteToSource( XclImpRoot& rRoot, const ScAddress& rScPos ) const;
41 private:
42 /** Reads an SXDOUBLE record describing a floating-point item. */
43 void ReadSxdouble( XclImpStream& rStrm );
44 /** Reads an SXBOOLEAN record describing a boolean item. */
45 void ReadSxboolean( XclImpStream& rStrm );
46 /** Reads an SXERROR record describing an error code item. */
47 void ReadSxerror( XclImpStream& rStrm );
48 /** Reads an SXINTEGER record describing an integer item. */
49 void ReadSxinteger( XclImpStream& rStrm );
50 /** Reads an SXSTRING record describing a text item. */
51 void ReadSxstring( XclImpStream& rStrm );
52 /** Reads an SXDATETIME record describing a date/time item. */
53 void ReadSxdatetime( XclImpStream& rStrm );
54 /** Reads an SXEMPTY record describing an empty item. */
55 void ReadSxempty( XclImpStream& rStrm );
58 typedef std::shared_ptr< XclImpPCItem > XclImpPCItemRef;
60 struct ScDPNumGroupInfo;
61 class XclImpPivotCache;
63 /** Represents a field in a pivot cache (a column of data items in the source area). */
64 class XclImpPCField : public XclPCField, protected XclImpRoot
66 public:
67 /** Creates a pivot cache field by reading an SXFIELD record. */
68 explicit XclImpPCField( const XclImpRoot& rRoot,
69 XclImpPivotCache& rPCache, sal_uInt16 nFieldIdx );
70 virtual ~XclImpPCField() override;
72 // general field/item access ----------------------------------------------
74 /** Returns the name of the field, uses the passed visible name if supported. */
75 const OUString& GetFieldName( const ScfStringVec& rVisNames ) const;
77 /** Returns the base field if this is a grouping field. */
78 const XclImpPCField* GetGroupBaseField() const;
80 /** Returns the item at the specified position or 0 on error. */
81 const XclImpPCItem* GetItem( sal_uInt16 nItemIdx ) const;
82 /** Returns the item representing a limit value in numeric/date/time grouping fields.
83 @param nItemIdx One of EXC_SXFIELD_INDEX_MIN, EXC_SXFIELD_INDEX_MAX, or EXC_SXFIELD_INDEX_STEP. */
84 const XclImpPCItem* GetLimitItem( sal_uInt16 nItemIdx ) const;
86 /** Inserts the field name into the document. */
87 void WriteFieldNameToSource( SCCOL nScCol, SCTAB nScTab );
88 /** Inserts the specified item data into the document. */
89 void WriteOrigItemToSource( SCROW nScRow, SCTAB nScTab, sal_uInt16 nItemIdx );
90 /** Inserts the data of the last inserted item into the document. */
91 void WriteLastOrigItemToSource( SCROW nScRow, SCTAB nScTab );
93 // records ----------------------------------------------------------------
95 /** Reads the SXFIELD record describing the field. */
96 void ReadSxfield( XclImpStream& rStrm );
97 /** Reads an item data record describing a new item. */
98 void ReadItem( XclImpStream& rStrm );
99 /** Reads the SXNUMGROUP record describing numeric grouping fields. */
100 void ReadSxnumgroup( XclImpStream& rStrm );
101 /** Reads the SXGROUPINFO record describing the item order in grouping fields. */
102 void ReadSxgroupinfo( XclImpStream& rStrm );
104 // grouping ---------------------------------------------------------------
106 /** Inserts grouping information of this field into the passed ScDPSaveData. */
107 void ConvertGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
109 private:
110 /** Inserts standard grouping information of this field into the passed ScDPSaveData. */
111 void ConvertStdGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
112 /** Inserts numeric grouping information of this field into the passed ScDPSaveData. */
113 void ConvertNumGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
114 /** Inserts date grouping information of this field into the passed ScDPSaveData. */
115 void ConvertDateGroupField( ScDPSaveData& rSaveData, const ScfStringVec& rVisNames ) const;
117 /** Returns a Calc struct with numeric grouping data. */
118 ScDPNumGroupInfo GetScNumGroupInfo() const;
119 /** Returns a Calc struct with date grouping data. */
120 ScDPNumGroupInfo GetScDateGroupInfo() const;
122 /** Returns a limit value for numeric grouping fields. */
123 const double* GetNumGroupLimit( sal_uInt16 nLimitIdx ) const;
124 /** Returns a limit value for date grouping fields (minimum/maximum only). */
125 const DateTime* GetDateGroupLimit( sal_uInt16 nLimitIdx ) const;
126 /** Returns the step value for date grouping fields. */
127 const sal_Int16* GetDateGroupStep() const;
129 private:
130 typedef ::std::vector< XclImpPCItemRef > XclImpPCItemVec;
132 XclImpPivotCache& mrPCache; /// Parent pivot cache containing this field.
133 XclImpPCItemVec maItems; /// List of all displayed data items.
134 XclImpPCItemVec maOrigItems; /// List of all source data items.
135 XclImpPCItemVec maNumGroupItems; /// List of items containing numeric grouping limits.
136 mutable SCCOL mnSourceScCol; /// Column index of source data for this field.
137 bool mbNumGroupInfoRead; /// true = Numeric grouping info read (SXNUMGROUP record).
140 typedef std::shared_ptr< XclImpPCField > XclImpPCFieldRef;
142 class XclImpPivotCache : protected XclImpRoot
144 public:
145 explicit XclImpPivotCache( const XclImpRoot& rRoot );
146 virtual ~XclImpPivotCache() override;
148 // data access ------------------------------------------------------------
150 /** Returns the data source range read from the DCONREF record. */
151 const ScRange& GetSourceRange() const { return maSrcRange; }
153 const OUString& GetSourceRangeName() const { return maSrcRangeName; }
155 /** Returns read-only access to a pivot cache field. */
156 const XclImpPCField* GetField( sal_uInt16 nFieldIdx ) const;
158 // records ----------------------------------------------------------------
160 /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
161 void ReadSxidstm( XclImpStream& rStrm );
162 /** Reads an SXVS record containing the source type of the pivot cache. */
163 void ReadSxvs( XclImpStream& rStrm );
164 /** Reads a DCONREF record containing the source range of the pivot cache. */
165 void ReadDconref( XclImpStream& rStrm );
167 * Read DECONNAME record which contains the defined name of the source
168 * range.
170 void ReadDConName( XclImpStream& rStrm );
171 /** Reads the entire pivot cache stream. Uses decrypter from passed stream. */
172 void ReadPivotCacheStream( const XclImpStream& rStrm );
174 bool IsRefreshOnLoad() const;
175 bool IsValid() const;
177 private:
178 typedef ::std::vector< XclImpPCFieldRef > XclImpPCFieldVec;
180 XclPCInfo maPCInfo; /// Pivot cache settings (SXDB record).
181 XclImpPCFieldVec maFields; /// List of pivot cache fields.
182 ScRange maSrcRange; /// Source range in the spreadsheet.
183 OUString maUrl; /// URL of the source data.
184 OUString maTabName; /// Sheet name of the source data.
185 OUString maSrcRangeName; /// Name of the source data range.
186 sal_uInt16 mnStrmId; /// Pivot cache stream identifier.
187 sal_uInt16 mnSrcType; /// Source data type.
188 bool mbSelfRef; /// true = Source data from own document.
191 typedef std::shared_ptr< XclImpPivotCache > XclImpPivotCacheRef;
193 // Pivot table
195 class XclImpPivotTable;
197 class XclImpPTItem
199 public:
200 explicit XclImpPTItem( const XclImpPCField* pCacheField );
202 /** Returns the internal name of the item or 0, if no name could be found. */
203 const OUString* GetItemName() const;
204 /** Returns the internal name of the item. */
205 std::pair<bool, OUString> GetItemName(const ScDPSaveDimension& rSaveDim, ScDPObject* pObj, const XclImpRoot& rRoot) const;
207 /** Reads an SXVI record containing data of this item. */
208 void ReadSxvi( XclImpStream& rStrm );
210 /** Inserts this item into the passed ScDPSaveDimension. */
211 void ConvertItem( ScDPSaveDimension& rSaveDim, ScDPObject* pObj, const XclImpRoot& rRoot ) const;
213 private:
214 XclPTItemInfo maItemInfo; /// General data for this item.
215 const XclImpPCField* mpCacheField; /// Corresponding pivot cache field.
218 typedef std::shared_ptr< XclImpPTItem > XclImpPTItemRef;
220 class XclImpPTField
222 public:
223 explicit XclImpPTField( const XclImpPivotTable& rPTable, sal_uInt16 nCacheIdx );
225 // general field/item access ----------------------------------------------
227 /** Returns the corresponding pivot cache field of this field. */
228 const XclImpPCField* GetCacheField() const;
229 /** Returns the name of this field that is used to create the Calc dimensions. */
230 OUString GetFieldName() const;
231 /** Returns the internally set visible name of this field. */
232 OUString GetVisFieldName() const;
234 /** Returns the specified item. */
235 const XclImpPTItem* GetItem( sal_uInt16 nItemIdx ) const;
236 /** Returns the internal name of the specified item. */
237 const OUString* GetItemName( sal_uInt16 nItemIdx ) const;
239 /** Returns the flags of the axes this field is part of. */
240 sal_uInt16 GetAxes() const { return maFieldInfo.mnAxes; }
241 /** Sets the flags of the axes this field is part of. */
242 void SetAxes( sal_uInt16 nAxes ) { maFieldInfo.mnAxes = nAxes; }
244 // records ----------------------------------------------------------------
246 /** Reads an SXVD record describing the field. */
247 void ReadSxvd( XclImpStream& rStrm );
248 /** Reads an SXVDEX record describing extended options of the field. */
249 void ReadSxvdex( XclImpStream& rStrm );
250 /** Reads an SXVI record describing a new item of this field. */
251 void ReadSxvi( XclImpStream& rStrm );
253 // row/column fields ------------------------------------------------------
255 void ConvertRowColField( ScDPSaveData& rSaveData ) const;
257 // page fields ------------------------------------------------------------
259 void SetPageFieldInfo( const XclPTPageFieldInfo& rPageInfo );
260 void ConvertPageField( ScDPSaveData& rSaveData ) const;
262 // hidden fields ----------------------------------------------------------
264 void ConvertHiddenField( ScDPSaveData& rSaveData ) const;
266 // data fields ------------------------------------------------------------
268 bool HasDataFieldInfo() const;
269 void AddDataFieldInfo( const XclPTDataFieldInfo& rDataInfo );
270 void ConvertDataField( ScDPSaveData& rSaveData ) const;
272 void ConvertFieldInfo( const ScDPSaveData& rSaveData, ScDPObject* pObj, const XclImpRoot& rRoot, bool bPageField = false ) const;
274 private:
275 void ConvertRCPField( ScDPSaveData& rSaveData ) const;
277 void ConvertDataField( ScDPSaveDimension& rSaveDim, const XclPTDataFieldInfo& rDataInfo ) const;
278 void ConvertDataFieldInfo( ScDPSaveDimension& rSaveDim, const XclPTDataFieldInfo& rDataInfo ) const;
280 private:
282 const XclImpPivotTable& mrPTable; /// Parent pivot table containing this field.
283 XclPTFieldInfo maFieldInfo; /// General field info (SXVD record).
284 XclPTFieldExtInfo maFieldExtInfo; /// Extended field info (SXVDEX record).
285 XclPTPageFieldInfo maPageInfo; /// Page field info (entry from SXPI record).
286 std::vector< XclPTDataFieldInfo > maDataInfoVector; /// Vector of extended data field info (SXDI records).
287 std::vector< XclImpPTItemRef > maItems; /// List of all items of this field.
290 typedef std::shared_ptr< XclImpPTField > XclImpPTFieldRef;
292 class XclImpPivotTable : protected XclImpRoot
294 public:
295 explicit XclImpPivotTable( const XclImpRoot& rRoot );
296 virtual ~XclImpPivotTable() override;
298 // cache/field access, misc. ----------------------------------------------
300 const XclImpPivotCacheRef& GetPivotCache() const { return mxPCache; }
301 const ScfStringVec& GetVisFieldNames() const { return maVisFieldNames; }
303 sal_uInt16 GetFieldCount() const;
304 const XclImpPTField* GetField( sal_uInt16 nFieldIdx ) const;
305 XclImpPTField* GetFieldAcc( sal_uInt16 nFieldIdx );
307 const XclImpPTField* GetDataField( sal_uInt16 nDataFieldIdx ) const;
308 OUString GetDataFieldName( sal_uInt16 nDataFieldIdx ) const;
310 // records ----------------------------------------------------------------
312 /** Reads an SXVIEW record starting a new pivot table. */
313 void ReadSxview( XclImpStream& rStrm );
314 /** Reads an SXVD record describing a new field. */
315 void ReadSxvd( XclImpStream& rStrm );
316 /** Reads an SXVI record describing a new item of the current field. */
317 void ReadSxvi( XclImpStream& rStrm );
318 /** Reads an SXVDEX record describing extended options of the current field. */
319 void ReadSxvdex( XclImpStream& rStrm );
320 /** Reads an SXIVD record containing the row field or column field order. */
321 void ReadSxivd( XclImpStream& rStrm );
322 /** Reads an SXPI record containing page field data. */
323 void ReadSxpi( XclImpStream& rStrm );
324 /** Reads an SXDI record containing data field data. */
325 void ReadSxdi( XclImpStream& rStrm );
326 /** Reads an SXEX record containing additional settings for the pivot table. */
327 void ReadSxex( XclImpStream& rStrm );
328 /** Reads an SXVIEWEX9 record that specifies the pivot tables
329 * autoformat. */
330 void ReadSxViewEx9( XclImpStream& rStrm );
332 /** Reads an SXADDL record that specifies additional info for pivot table. */
333 void ReadSxAddl( XclImpStream& rStrm );
335 /** Inserts the pivot table into the Calc document. */
336 void Convert();
338 void MaybeRefresh();
340 void ApplyMergeFlags(const ScRange& rOutRange, const ScDPSaveData& rSaveData);
341 void ApplyFieldInfo();
343 private:
344 XclImpPivotCacheRef mxPCache; /// Pivot cache containing field/item names.
346 XclPTInfo maPTInfo; /// General info about the pivot table (SXVIEW record).
347 XclPTExtInfo maPTExtInfo; /// Extended info about the pivot table (SXEX record).
348 XclPTViewEx9Info maPTViewEx9Info; /// (SXVIEWEX9 record)
349 XclPTAddl maPTAddlInfo;
350 std::vector< XclImpPTFieldRef >
351 maFields; /// Vector containing all fields.
352 XclImpPTFieldRef mxCurrField; /// Current field for importing additional info.
353 ScfStringVec maVisFieldNames; /// Vector containing all visible field names.
354 ScfUInt16Vec maRowFields; /// Row field indexes.
355 ScfUInt16Vec maColFields; /// Column field indexes.
356 ScfUInt16Vec maPageFields; /// Page field indexes.
357 ScfUInt16Vec maOrigDataFields; /// Original data field indexes.
358 ScfUInt16Vec maFiltDataFields; /// Filtered data field indexes.
359 XclImpPTField maDataOrientField; /// Special data field orientation field.
360 ScRange maOutScRange; /// Output range in the Calc document.
361 ScDPObject* mpDPObj;
364 typedef std::shared_ptr< XclImpPivotTable > XclImpPivotTableRef;
366 /** The main class for pivot table import.
368 This class contains functions to read all records related to pivot tables
369 and pivot caches.
371 class XclImpPivotTableManager : protected XclImpRoot
373 public:
374 explicit XclImpPivotTableManager( const XclImpRoot& rRoot );
375 virtual ~XclImpPivotTableManager() override;
377 // pivot cache records ----------------------------------------------------
379 /** Returns the pivot cache with the specified 0-based index. */
380 XclImpPivotCacheRef GetPivotCache( sal_uInt16 nCacheIdx );
382 /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
383 void ReadSxidstm( XclImpStream& rStrm );
384 /** Reads an SXVS record containing the source type of a pivot cache. */
385 void ReadSxvs( XclImpStream& rStrm );
386 /** Reads a DCONREF record containing the source range of a pivot cache. */
387 void ReadDconref( XclImpStream& rStrm );
388 void ReadDConName( XclImpStream& rStrm );
390 // pivot table records ----------------------------------------------------
392 /** Reads an SXVIEW record describing a new pivot table. */
393 void ReadSxview( XclImpStream& rStrm );
394 /** Reads an SXVD record describing a new field. */
395 void ReadSxvd( XclImpStream& rStrm );
396 /** Reads an SXVDEX record describing extended options of a field. */
397 void ReadSxvdex( XclImpStream& rStrm );
398 /** Reads an SXIVD record containing the row field or column field order. */
399 void ReadSxivd( XclImpStream& rStrm );
400 /** Reads an SXPI record containing page field data. */
401 void ReadSxpi( XclImpStream& rStrm );
402 /** Reads an SXDI record containing data field data. */
403 void ReadSxdi( XclImpStream& rStrm );
404 /** Reads an SXVI record describing a new item of the current field. */
405 void ReadSxvi( XclImpStream& rStrm );
406 /** Reads an SXEX record containing additional settings for a pivot table. */
407 void ReadSxex( XclImpStream& rStrm );
408 /** Reads an SXVIEWEX9 record that specifies the pivot tables
409 * autoformat. */
410 void ReadSxViewEx9( XclImpStream& rStrm );
411 /** Reads an SXADDL record that specifies additional info for pivot table. */
412 void ReadSxAddl( XclImpStream& rStrm );
414 /** Reads all used pivot caches and creates additional sheets for external data sources. */
415 void ReadPivotCaches( const XclImpStream& rStrm );
416 /** Inserts all pivot tables into the Calc document. */
417 void ConvertPivotTables();
419 void MaybeRefreshPivotTables();
421 private:
423 std::vector< XclImpPivotCacheRef > maPCaches; /// List of all pivot caches.
424 std::vector< XclImpPivotTableRef > maPTables; /// List of all pivot tables.
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */