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