1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <svl/zforlist.hxx>
22 #include "dpshttab.hxx"
23 #include "dptabres.hxx"
24 #include "document.hxx"
25 #include "formulacell.hxx"
26 #include "dpfilteredcache.hxx"
27 #include "dpobject.hxx"
28 #include "globstr.hrc"
29 #include "rangenam.hxx"
30 #include "queryentry.hxx"
32 #include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
37 using namespace ::com::sun::star
;
38 using ::com::sun::star::uno::Any
;
39 using ::com::sun::star::uno::Sequence
;
42 // -----------------------------------------------------------------------
44 ScSheetDPData::ScSheetDPData(ScDocument
* pD
, const ScSheetSourceDesc
& rDesc
, const ScDPCache
& rCache
) :
46 aQuery ( rDesc
.GetQueryParam() ),
47 bIgnoreEmptyRows( false ),
48 bRepeatIfEmpty(false),
51 SCSIZE
nEntryCount( aQuery
.GetEntryCount());
52 for (SCSIZE j
= 0; j
< nEntryCount
; ++j
)
54 ScQueryEntry
& rEntry
= aQuery
.GetEntry(j
);
57 ScQueryEntry::Item
& rItem
= rEntry
.GetQueryItem();
58 if (rItem
.meType
== ScQueryEntry::ByString
)
60 sal_uInt32 nIndex
= 0;
61 bool bNumber
= pD
->GetFormatTable()->IsNumberFormat(
62 rItem
.maString
.getString(), nIndex
, rItem
.mfVal
);
63 rItem
.meType
= bNumber
? ScQueryEntry::ByValue
: ScQueryEntry::ByString
;
69 ScSheetDPData::~ScSheetDPData()
73 void ScSheetDPData::DisposeData()
78 long ScSheetDPData::GetColumnCount()
81 return aCacheTable
.getColSize();
84 OUString
ScSheetDPData::getDimensionName(long nColumn
)
87 if (getIsDataLayoutDimension(nColumn
))
89 //! different internal and display names?
91 return ScGlobal::GetRscString(STR_PIVOT_DATA
);
93 else if (nColumn
>= aCacheTable
.getColSize())
95 OSL_FAIL("getDimensionName: invalid dimension");
100 return aCacheTable
.getFieldName(static_cast<SCCOL
>(nColumn
));
104 sal_Bool
ScSheetDPData::IsDateDimension(long nDim
)
107 long nColCount
= aCacheTable
.getColSize();
108 if (getIsDataLayoutDimension(nDim
))
112 else if (nDim
>= nColCount
)
114 OSL_FAIL("IsDateDimension: invalid dimension");
119 return GetCacheTable().getCache()->IsDateDimension( nDim
);
123 sal_uLong
ScSheetDPData::GetNumberFormat(long nDim
)
126 if (getIsDataLayoutDimension(nDim
))
130 else if (nDim
>= GetCacheTable().getColSize())
132 OSL_FAIL("GetNumberFormat: invalid dimension");
137 return GetCacheTable().getCache()->GetNumberFormat( nDim
);
140 sal_uInt32
ScDPTableData::GetNumberFormatByIdx( NfIndexTableOffset eIdx
)
145 if ( SvNumberFormatter
* pFormatter
= mpDoc
->GetFormatTable() )
146 return pFormatter
->GetFormatIndex( eIdx
, LANGUAGE_SYSTEM
);
151 sal_Bool
ScSheetDPData::getIsDataLayoutDimension(long nColumn
)
154 return (nColumn
==(long)( aCacheTable
.getColSize()));
157 void ScSheetDPData::SetEmptyFlags( sal_Bool bIgnoreEmptyRowsP
, sal_Bool bRepeatIfEmptyP
)
159 bIgnoreEmptyRows
= bIgnoreEmptyRowsP
;
160 bRepeatIfEmpty
= bRepeatIfEmptyP
;
163 bool ScSheetDPData::IsRepeatIfEmpty()
165 return bRepeatIfEmpty
;
168 void ScSheetDPData::CreateCacheTable()
170 // Scan and store the data from the source range.
171 if (!aCacheTable
.empty())
175 aCacheTable
.fillTable(aQuery
, bIgnoreEmptyRows
, bRepeatIfEmpty
);
178 void ScSheetDPData::FilterCacheTable(const vector
<ScDPFilteredCache::Criterion
>& rCriteria
, const boost::unordered_set
<sal_Int32
>& rCatDims
)
181 aCacheTable
.filterByPageDimension(
182 rCriteria
, (IsRepeatIfEmpty() ? rCatDims
: boost::unordered_set
<sal_Int32
>()));
185 void ScSheetDPData::GetDrillDownData(const vector
<ScDPFilteredCache::Criterion
>& rCriteria
, const boost::unordered_set
<sal_Int32
>& rCatDims
, Sequence
< Sequence
<Any
> >& rData
)
188 sal_Int32 nRowSize
= aCacheTable
.getRowSize();
192 aCacheTable
.filterTable(
193 rCriteria
, rData
, IsRepeatIfEmpty() ? rCatDims
: boost::unordered_set
<sal_Int32
>());
196 void ScSheetDPData::CalcResults(CalcInfo
& rInfo
, bool bAutoShow
)
199 CalcResultsFromCacheTable(aCacheTable
, rInfo
, bAutoShow
);
202 const ScDPFilteredCache
& ScSheetDPData::GetCacheTable() const
207 void ScSheetDPData::ReloadCacheTable()
213 ScSheetSourceDesc::ScSheetSourceDesc(ScDocument
* pDoc
) :
216 void ScSheetSourceDesc::SetSourceRange(const ScRange
& rRange
)
218 maSourceRange
= rRange
;
219 maRangeName
= OUString(); // overwrite existing range name if any.
222 const ScRange
& ScSheetSourceDesc::GetSourceRange() const
224 if (!maRangeName
.isEmpty())
226 // Obtain the source range from the range name first.
227 maSourceRange
= ScRange();
228 ScRangeName
* pRangeName
= mpDoc
->GetRangeName();
234 OUString aUpper
= ScGlobal::pCharClass
->uppercase(maRangeName
);
235 const ScRangeData
* pData
= pRangeName
->findByUpperName(aUpper
);
239 // range name found. Fow now, we only use the first token and
242 if (!pData
->IsReference(aRange
))
245 maSourceRange
= aRange
;
249 return maSourceRange
;
252 void ScSheetSourceDesc::SetRangeName(const OUString
& rName
)
257 const OUString
& ScSheetSourceDesc::GetRangeName() const
262 bool ScSheetSourceDesc::HasRangeName() const
264 return !maRangeName
.isEmpty();
267 void ScSheetSourceDesc::SetQueryParam(const ScQueryParam
& rParam
)
269 maQueryParam
= rParam
;
272 const ScQueryParam
& ScSheetSourceDesc::GetQueryParam() const
277 bool ScSheetSourceDesc::operator== (const ScSheetSourceDesc
& rOther
) const
279 return maSourceRange
== rOther
.maSourceRange
&&
280 maRangeName
== rOther
.maRangeName
&&
281 maQueryParam
== rOther
.maQueryParam
;
284 const ScDPCache
* ScSheetSourceDesc::CreateCache(const ScDPDimensionSaveData
* pDimData
) const
289 sal_uLong nErrId
= CheckSourceRange();
292 OSL_FAIL( "Error Create Cache\n" );
296 // All cache instances are managed centrally by ScDPCollection.
297 ScDPCollection
* pDPs
= mpDoc
->GetDPCollection();
300 // Name-based data source.
301 ScDPCollection::NameCaches
& rCaches
= pDPs
->GetNameCaches();
302 return rCaches
.getCache(GetRangeName(), GetSourceRange(), pDimData
);
305 ScDPCollection::SheetCaches
& rCaches
= pDPs
->GetSheetCaches();
306 return rCaches
.getCache(GetSourceRange(), pDimData
);
309 sal_uLong
ScSheetSourceDesc::CheckSourceRange() const
312 return STR_ERR_DATAPILOTSOURCE
;
314 // Make sure the range is valid and sane.
315 const ScRange
& rSrcRange
= GetSourceRange();
316 if (!rSrcRange
.IsValid())
317 return STR_ERR_DATAPILOTSOURCE
;
319 if (rSrcRange
.aStart
.Col() > rSrcRange
.aEnd
.Col() || rSrcRange
.aStart
.Row() > rSrcRange
.aEnd
.Row())
320 return STR_ERR_DATAPILOTSOURCE
;
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */