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