update dev300-m58
[ooovba.git] / sc / inc / dptabres.hxx
blob4e05c19b9b7b7f6e8cf6d19cc4701420d412d798
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dptabres.hxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SC_DPTABRES_HXX
32 #define SC_DPTABRES_HXX
34 #include <svtools/svarray.hxx>
35 #include <tools/string.hxx>
36 #include <com/sun/star/sheet/MemberResult.hpp>
37 #include <com/sun/star/sheet/DataResult.hpp>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include "global.hxx" // enum ScSubTotalFunc
40 #include "dpcachetable.hxx"
41 #include <hash_map>
42 #include <hash_set>
43 #include <vector>
44 #include <memory>
46 namespace com { namespace sun { namespace star { namespace sheet {
47 struct DataPilotFieldReference;
48 } } } }
51 class ScAddress;
52 class ScDocument;
53 class ScDPSource;
54 class ScDPDimension;
55 class ScDPDimensions;
56 class ScDPLevel;
57 class ScDPMember;
58 class ScDPAggData;
59 class ScDPResultMember;
60 class ScDPResultVisibilityData;
62 struct ScDPValueData;
63 struct ScDPItemData;
66 // Member names that are being processed for InitFrom/LateInitFrom
67 // (needed for initialization of grouped items)
70 class ScDPInitState
72 long* pIndex; // array
73 ScDPItemData* pData; // array
74 long nCount;
76 public:
77 ScDPInitState();
78 ~ScDPInitState();
80 void AddMember( long nSourceIndex, const ScDPItemData& rName );
81 void RemoveMember();
83 long GetCount() const { return nCount; }
84 const long* GetSource() const { return pIndex; }
85 const ScDPItemData* GetNames() const { return pData; }
87 const ScDPItemData* GetNameForIndex( long nIndexValue ) const;
90 typedef ::std::vector<sal_Int32> ScMemberSortOrder;
93 // selected subtotal information, passed down the dimensions
96 struct ScDPSubTotalState
98 ScSubTotalFunc eColForce;
99 ScSubTotalFunc eRowForce;
100 long nColSubTotalFunc;
101 long nRowSubTotalFunc;
103 ScDPSubTotalState() :
104 eColForce( SUBTOTAL_FUNC_NONE ),
105 eRowForce( SUBTOTAL_FUNC_NONE ),
106 nColSubTotalFunc( -1 ),
107 nRowSubTotalFunc( -1 )
112 // indexes when calculating running totals
113 // Col/RowVisible: simple counts from 0 - without sort order applied - visible index
114 // (only used for running total / relative index)
115 // Col/RowIndexes: with sort order applied - member index
116 // (used otherwise - so other members' children can be accessed)
119 class ScDPRunningTotalState
121 ScDPResultMember* pColResRoot;
122 ScDPResultMember* pRowResRoot;
123 long* pColVisible;
124 long* pColIndexes;
125 long* pRowVisible;
126 long* pRowIndexes;
127 long nColIndexPos;
128 long nRowIndexPos;
130 public:
131 ScDPRunningTotalState( ScDPResultMember* pColRoot, ScDPResultMember* pRowRoot );
132 ~ScDPRunningTotalState();
134 ScDPResultMember* GetColResRoot() const { return pColResRoot; }
135 ScDPResultMember* GetRowResRoot() const { return pRowResRoot; }
137 const long* GetColVisible() const { return pColVisible; }
138 const long* GetColIndexes() const { return pColIndexes; }
139 const long* GetRowVisible() const { return pRowVisible; }
140 const long* GetRowIndexes() const { return pRowIndexes; }
142 void AddColIndex( long nVisible, long nSorted );
143 void AddRowIndex( long nVisible, long nSorted );
144 void RemoveColIndex();
145 void RemoveRowIndex();
148 struct ScDPRelativePos
150 long nBasePos; // simple count, without sort order applied
151 long nDirection;
153 ScDPRelativePos( long nBase, long nDir );
157 // aggregated data
158 //! separate header file?
161 // Possible values for the nCount member:
162 // (greater than 0 counts the collected values)
163 const long SC_DPAGG_EMPTY = 0; // empty during data collection
164 const long SC_DPAGG_DATA_ERROR = -1; // error during data collection
165 const long SC_DPAGG_RESULT_EMPTY = -2; // empty result calculated
166 const long SC_DPAGG_RESULT_VALID = -3; // valid result calculated
167 const long SC_DPAGG_RESULT_ERROR = -4; // error in calculated result
169 class ScDPAggData
171 private:
172 double fVal;
173 double fAux;
174 long nCount;
175 ScDPAggData* pChild;
177 public:
178 ScDPAggData() : fVal(0.0), fAux(0.0), nCount(SC_DPAGG_EMPTY), pChild(NULL) {}
179 ~ScDPAggData() { delete pChild; }
181 void Update( const ScDPValueData& rNext, ScSubTotalFunc eFunc, const ScDPSubTotalState& rSubState );
182 void Calculate( ScSubTotalFunc eFunc, const ScDPSubTotalState& rSubState );
183 BOOL IsCalculated() const;
185 double GetResult() const;
186 BOOL HasError() const;
187 BOOL HasData() const;
189 void SetResult( double fNew );
190 void SetEmpty( BOOL bSet );
191 void SetError();
193 double GetAuxiliary() const;
194 void SetAuxiliary( double fNew );
196 void Reset(); // also deletes children
198 const ScDPAggData* GetExistingChild() const { return pChild; }
199 ScDPAggData* GetChild();
203 // Row and grand total state, passed down (column total is at result member)
206 class ScDPRowTotals
208 ScDPAggData aRowTotal;
209 ScDPAggData aGrandTotal;
210 BOOL bIsInColRoot;
212 public:
213 ScDPRowTotals();
214 ~ScDPRowTotals();
216 ScDPAggData* GetRowTotal( long nMeasure );
217 ScDPAggData* GetGrandTotal( long nMeasure );
219 BOOL IsInColRoot() const { return bIsInColRoot; }
220 void SetInColRoot(BOOL bSet) { bIsInColRoot = bSet; }
223 // --------------------------------------------------------------------
225 // results for a hierarchy dimension
228 #define SC_DP_RES_GROW 16
230 class ScDPResultDimension;
231 class ScDPDataDimension;
232 class ScDPDataMember;
234 #define SC_DPMEASURE_ALL -1
235 #define SC_DPMEASURE_ANY -2
237 class ScDPResultData
239 private:
240 ScDPSource* pSource; //! Ref
241 //! keep things like measure lists here
243 long nMeasCount;
244 ScSubTotalFunc* pMeasFuncs;
245 ::com::sun::star::sheet::DataPilotFieldReference* pMeasRefs;
246 USHORT* pMeasRefOrient;
247 String* pMeasNames;
248 BOOL bLateInit;
249 BOOL bDataAtCol;
250 BOOL bDataAtRow;
252 //! add "displayed values" settings
254 public:
255 ScDPResultData( ScDPSource* pSrc ); //! Ref
256 ~ScDPResultData();
258 void SetMeasureData( long nCount, const ScSubTotalFunc* pFunctions,
259 const ::com::sun::star::sheet::DataPilotFieldReference* pRefs,
260 const USHORT* pRefOrient, const String* pNames );
261 void SetDataLayoutOrientation( USHORT nOrient );
262 void SetLateInit( BOOL bSet );
264 long GetMeasureCount() const { return nMeasCount; }
265 ScSubTotalFunc GetMeasureFunction(long nMeasure) const;
266 String GetMeasureString(long nMeasure, BOOL bForce, ScSubTotalFunc eForceFunc, bool& rbTotalResult) const;
267 String GetMeasureDimensionName(long nMeasure) const;
268 const ::com::sun::star::sheet::DataPilotFieldReference& GetMeasureRefVal(long nMeasure) const;
269 USHORT GetMeasureRefOrient(long nMeasure) const;
271 BOOL IsDataAtCol() const { return bDataAtCol; }
272 BOOL IsDataAtRow() const { return bDataAtRow; }
273 BOOL IsLateInit() const { return bLateInit; }
275 long GetColStartMeasure() const;
276 long GetRowStartMeasure() const;
278 long GetCountForMeasure( long nMeas ) const
279 { return ( nMeas == SC_DPMEASURE_ALL ) ? nMeasCount : 1; }
281 BOOL IsBaseForGroup( long nDim ) const; // any group
282 long GetGroupBase( long nGroupDim ) const;
283 BOOL IsNumOrDateGroup( long nDim ) const;
284 BOOL IsInGroup( const ScDPItemData& rGroupData, long nGroupIndex,
285 const ScDPItemData& rBaseData, long nBaseIndex ) const;
286 BOOL HasCommonElement( const ScDPItemData& rFirstData, long nFirstIndex,
287 const ScDPItemData& rSecondData, long nSecondIndex ) const;
289 const ScDPSource* GetSource() const;
293 class ScDPResultMember
295 private:
296 const ScDPResultData* pResultData;
297 const ScDPDimension* pParentDim; //! Ref
298 const ScDPLevel* pParentLevel; //! Ref
299 const ScDPMember* pMemberDesc; //! Ref
300 ScDPResultDimension* pChildDimension;
301 ScDPDataMember* pDataRoot;
302 BOOL bHasElements;
303 BOOL bForceSubTotal;
304 BOOL bHasHiddenDetails;
305 BOOL bInitialized;
306 BOOL bAutoHidden;
307 ScDPAggData aColTotal; // to store column totals
309 public:
310 ScDPResultMember( const ScDPResultData* pData, const ScDPDimension* pDim,
311 const ScDPLevel* pLev, const ScDPMember* pDesc,
312 BOOL bForceSub ); //! Ref
313 ~ScDPResultMember();
315 void InitFrom( const ::std::vector<ScDPDimension*>& ppDim,
316 const ::std::vector<ScDPLevel*>& ppLev,
317 size_t nPos,
318 ScDPInitState& rInitState );
319 void LateInitFrom( const ::std::vector<ScDPDimension*>& ppDim,
320 const ::std::vector<ScDPLevel*>& ppLev,
321 const ::std::vector<ScDPItemData>& pItemData,
322 size_t nPos,
323 ScDPInitState& rInitState );
324 String GetName() const;
325 void FillItemData( ScDPItemData& rData ) const;
326 BOOL IsValid() const;
327 BOOL IsVisible() const;
328 long GetSize(long nMeasure) const;
329 BOOL HasHiddenDetails() const;
330 BOOL IsSubTotalInTitle(long nMeasure) const;
332 // BOOL SubTotalEnabled() const;
333 long GetSubTotalCount( long* pUserSubStart = NULL ) const;
335 BOOL IsNamedItem( const ScDPItemData& r ) const;
336 bool IsValidEntry( const ::std::vector<ScDPItemData>& aMembers ) const;
338 void SetHasElements() { bHasElements = TRUE; }
339 void SetAutoHidden() { bAutoHidden = TRUE; }
341 void ProcessData( const ::std::vector<ScDPItemData>& aChildMembers,
342 const ScDPResultDimension* pDataDim,
343 const ::std::vector<ScDPItemData>& aDataMembers,
344 const ::std::vector<ScDPValueData>& aValues );
346 void FillMemberResults( com::sun::star::uno::Sequence<
347 com::sun::star::sheet::MemberResult>* pSequences,
348 long& rPos, long nMeasure, BOOL bRoot,
349 const String* pMemberName,
350 const String* pMemberCaption );
352 void FillDataResults( const ScDPResultMember* pRefMember,
353 com::sun::star::uno::Sequence<
354 com::sun::star::uno::Sequence<
355 com::sun::star::sheet::DataResult> >& rSequence,
356 long& rRow, long nMeasure ) const;
358 void UpdateDataResults( const ScDPResultMember* pRefMember, long nMeasure ) const;
359 void UpdateRunningTotals( const ScDPResultMember* pRefMember, long nMeasure,
360 ScDPRunningTotalState& rRunning, ScDPRowTotals& rTotals ) const;
362 void SortMembers( ScDPResultMember* pRefMember );
363 void DoAutoShow( ScDPResultMember* pRefMember );
365 void ResetResults( BOOL bRoot );
367 void DumpState( const ScDPResultMember* pRefMember, ScDocument* pDoc, ScAddress& rPos ) const;
369 //! this will be removed!
370 const ScDPResultDimension* GetChildDimension() const { return pChildDimension; }
371 ScDPResultDimension* GetChildDimension() { return pChildDimension; }
373 ScDPDataMember* GetDataRoot() const { return pDataRoot; }
375 const ScDPDimension* GetParentDim() const { return pParentDim; } //! Ref
376 const ScDPLevel* GetParentLevel() const { return pParentLevel; } //! Ref
378 ScDPAggData* GetColTotal( long nMeasure ) const;
380 void FillVisibilityData(ScDPResultVisibilityData& rData) const;
383 class ScDPDataMember
385 private:
386 const ScDPResultData* pResultData;
387 const ScDPResultMember* pResultMember; //! Ref?
388 ScDPDataDimension* pChildDimension;
389 ScDPAggData aAggregate;
391 void UpdateValues( const ::std::vector<ScDPValueData>& aValues, const ScDPSubTotalState& rSubState );
393 public:
394 ScDPDataMember( const ScDPResultData* pData, const ScDPResultMember* pRes );
395 ~ScDPDataMember();
397 void InitFrom( const ScDPResultDimension* pDim );
399 String GetName() const;
400 BOOL IsVisible() const;
401 BOOL HasData( long nMeasure, const ScDPSubTotalState& rSubState ) const;
403 BOOL IsNamedItem( const ScDPItemData& r ) const;
405 BOOL HasHiddenDetails() const;
407 void ProcessData( const ::std::vector<ScDPItemData>& aChildMembers, const ::std::vector<ScDPValueData>& aValues,
408 const ScDPSubTotalState& rSubState );
410 BOOL HasError( long nMeasure, const ScDPSubTotalState& rSubState ) const;
411 double GetAggregate( long nMeasure, const ScDPSubTotalState& rSubState ) const;
412 const ScDPAggData* GetConstAggData( long nMeasure, const ScDPSubTotalState& rSubState ) const;
413 ScDPAggData* GetAggData( long nMeasure, const ScDPSubTotalState& rSubState );
415 void FillDataRow( const ScDPResultMember* pRefMember,
416 com::sun::star::uno::Sequence<com::sun::star::sheet::DataResult>& rSequence,
417 long& rCol, long nMeasure, BOOL bIsSubTotalRow,
418 const ScDPSubTotalState& rSubState ) const;
420 void UpdateDataRow( const ScDPResultMember* pRefMember, long nMeasure, BOOL bIsSubTotalRow,
421 const ScDPSubTotalState& rSubState );
422 void UpdateRunningTotals( const ScDPResultMember* pRefMember, long nMeasure, BOOL bIsSubTotalRow,
423 const ScDPSubTotalState& rSubState, ScDPRunningTotalState& rRunning,
424 ScDPRowTotals& rTotals, const ScDPResultMember& rRowParent );
426 void SortMembers( ScDPResultMember* pRefMember );
427 void DoAutoShow( ScDPResultMember* pRefMember );
429 void ResetResults();
431 void DumpState( const ScDPResultMember* pRefMember, ScDocument* pDoc, ScAddress& rPos ) const;
433 //! this will be removed!
434 const ScDPDataDimension* GetChildDimension() const { return pChildDimension; }
435 ScDPDataDimension* GetChildDimension() { return pChildDimension; }
438 //! replace PtrArr with 32-bit array ????
440 typedef ScDPDataMember* ScDPDataMemberPtr;
441 SV_DECL_PTRARR_DEL(ScDPDataMembers, ScDPDataMemberPtr, SC_DP_RES_GROW, SC_DP_RES_GROW)
444 // result dimension contains only members
446 class ScDPResultDimension
448 public :
449 struct MemberHashFunc : public std::unary_function< const ScDPItemData &, size_t >
451 size_t operator() (const ScDPItemData &rData) const { return rData.Hash(); }
453 typedef std::vector <ScDPResultMember *> MemberArray;
454 typedef std::hash_map <ScDPItemData, ScDPResultMember *, MemberHashFunc> MemberHash;
456 private:
457 const ScDPResultData* pResultData;
458 MemberArray maMemberArray;
459 MemberHash maMemberHash;
460 BOOL bInitialized;
461 String aDimensionName; //! or ptr to IntDimension?
462 BOOL bIsDataLayout; //! or ptr to IntDimension?
463 BOOL bSortByData;
464 BOOL bSortAscending;
465 long nSortMeasure;
466 ScMemberSortOrder aMemberOrder; // used when sorted by measure
467 BOOL bAutoShow;
468 BOOL bAutoTopItems;
469 long nAutoMeasure;
470 long nAutoCount;
472 ScDPResultMember* FindMember( const ScDPItemData& rData ) const;
473 public:
474 ScDPResultDimension( const ScDPResultData* pData );
475 ~ScDPResultDimension();
477 // allocates new members
478 void InitFrom( const ::std::vector<ScDPDimension*>& ppDim,
479 const ::std::vector<ScDPLevel*>& ppLev,
480 size_t nPos,
481 ScDPInitState& rInitState );
482 void LateInitFrom( const ::std::vector<ScDPDimension*>& ppDim,
483 const ::std::vector<ScDPLevel*>& ppLev,
484 const ::std::vector<ScDPItemData>& pItemData,
485 size_t nPos,
486 ScDPInitState& rInitState );
488 long GetSize(long nMeasure) const;
490 bool IsValidEntry( const ::std::vector<ScDPItemData>& aMembers ) const;
491 // modifies existing members, allocates data dimensions
492 void ProcessData( const ::std::vector<ScDPItemData>& aMembers,
493 const ScDPResultDimension* pDataDim,
494 const ::std::vector<ScDPItemData>& aDataMembers,
495 const ::std::vector<ScDPValueData>& aValues ) const; //! Test
497 void FillMemberResults( com::sun::star::uno::Sequence<
498 com::sun::star::sheet::MemberResult>* pSequences,
499 long nStart, long nMeasure );
501 void FillDataResults( const ScDPResultMember* pRefMember,
502 com::sun::star::uno::Sequence<
503 com::sun::star::uno::Sequence<
504 com::sun::star::sheet::DataResult> >& rSequence,
505 long nRow, long nMeasure ) const;
507 void UpdateDataResults( const ScDPResultMember* pRefMember, long nMeasure ) const;
508 void UpdateRunningTotals( const ScDPResultMember* pRefMember, long nMeasure,
509 ScDPRunningTotalState& rRunning, ScDPRowTotals& rTotals ) const;
511 void SortMembers( ScDPResultMember* pRefMember );
512 long GetSortedIndex( long nUnsorted ) const;
514 void DoAutoShow( ScDPResultMember* pRefMember );
516 void ResetResults();
518 // called for the reference dimension
519 ScDPDataMember* GetRowReferenceMember( const ScDPRelativePos* pMemberPos, const String* pName,
520 const long* pRowIndexes, const long* pColIndexes ) const;
522 // uses row root member from ScDPRunningTotalState
523 static ScDPDataMember* GetColReferenceMember( const ScDPRelativePos* pMemberPos, const String* pName,
524 long nRefDimPos, const ScDPRunningTotalState& rRunning );
526 void DumpState( const ScDPResultMember* pRefMember, ScDocument* pDoc, ScAddress& rPos ) const;
528 // for ScDPDataDimension::InitFrom
529 long GetMemberCount() const;
530 const ScDPResultMember* GetMember(long n) const;
531 ScDPResultMember* GetMember(long n);
533 const ScMemberSortOrder& GetMemberOrder() const { return aMemberOrder; }
534 ScMemberSortOrder& GetMemberOrder() { return aMemberOrder; }
536 BOOL IsDataLayout() const { return bIsDataLayout; }
537 String GetName() const { return aDimensionName; }
539 BOOL IsSortByData() const { return bSortByData; }
540 BOOL IsSortAscending() const { return bSortAscending; }
541 long GetSortMeasure() const { return nSortMeasure; }
543 BOOL IsAutoShow() const { return bAutoShow; }
544 BOOL IsAutoTopItems() const { return bAutoTopItems; }
545 long GetAutoMeasure() const { return nAutoMeasure; }
546 long GetAutoCount() const { return nAutoCount; }
548 ScDPResultDimension* GetFirstChildDimension() const;
550 void FillVisibilityData(ScDPResultVisibilityData& rData) const;
553 class ScDPDataDimension
555 private:
556 const ScDPResultData* pResultData;
557 const ScDPResultDimension* pResultDimension; // column
558 ScDPDataMembers aMembers;
559 BOOL bIsDataLayout; //! or ptr to IntDimension?
561 public:
562 ScDPDataDimension( const ScDPResultData* pData );
563 ~ScDPDataDimension();
565 void InitFrom( const ScDPResultDimension* pDim ); // recursive
566 void ProcessData( const ::std::vector<ScDPItemData>& aDataMembers, const ::std::vector<ScDPValueData>& aValues,
567 const ScDPSubTotalState& rSubState );
569 void FillDataRow( const ScDPResultDimension* pRefDim,
570 com::sun::star::uno::Sequence<com::sun::star::sheet::DataResult>& rSequence,
571 long nCol, long nMeasure, BOOL bIsSubTotalRow,
572 const ScDPSubTotalState& rSubState ) const;
574 void UpdateDataRow( const ScDPResultDimension* pRefDim, long nMeasure, BOOL bIsSubTotalRow,
575 const ScDPSubTotalState& rSubState ) const;
576 void UpdateRunningTotals( const ScDPResultDimension* pRefDim, long nMeasure, BOOL bIsSubTotalRow,
577 const ScDPSubTotalState& rSubState, ScDPRunningTotalState& rRunning,
578 ScDPRowTotals& rTotals, const ScDPResultMember& rRowParent ) const;
580 void SortMembers( ScDPResultDimension* pRefDim );
581 long GetSortedIndex( long nUnsorted ) const;
583 void DoAutoShow( ScDPResultDimension* pRefDim );
585 void ResetResults();
587 void DumpState( const ScDPResultDimension* pRefDim, ScDocument* pDoc, ScAddress& rPos ) const;
589 long GetMemberCount() const;
590 ScDPDataMember* GetMember(long n) const;
593 // ----------------------------------------------------------------------------
595 /**
596 * This class collects visible members of each dimension and uses that
597 * information to create filtering criteria (e.g. for drill-down data).
599 class ScDPResultVisibilityData
601 public:
602 ScDPResultVisibilityData(ScSimpleSharedString& rSharedString, ScDPSource* pSource);
603 ~ScDPResultVisibilityData();
605 void addVisibleMember(const String& rDimName, const ScDPItemData& rMemberItem);
606 void fillFieldFilters(::std::vector<ScDPCacheTable::Criterion>& rFilters) const;
608 private:
609 struct MemberHash
611 size_t operator()(const ScDPItemData& r) const;
613 typedef ::std::hash_set<ScDPItemData, MemberHash> VisibleMemberType;
614 typedef ::std::hash_map<String, VisibleMemberType, ScStringHashCode> DimMemberType;
615 DimMemberType maDimensions;
617 ScSimpleSharedString& mrSharedString;
618 ScDPSource* mpSource;
621 #endif