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>
33 #include <osl/diagnose.h>
38 using namespace ::com::sun::star
;
39 using ::com::sun::star::uno::Any
;
40 using ::com::sun::star::uno::Sequence
;
43 ScSheetDPData::ScSheetDPData(ScDocument
* pD
, const ScSheetSourceDesc
& rDesc
, const ScDPCache
& rCache
) :
45 aQuery ( rDesc
.GetQueryParam() ),
46 bIgnoreEmptyRows( false ),
47 bRepeatIfEmpty(false),
50 SCSIZE
nEntryCount( aQuery
.GetEntryCount());
51 for (SCSIZE j
= 0; j
< nEntryCount
; ++j
)
53 ScQueryEntry
& rEntry
= aQuery
.GetEntry(j
);
56 ScQueryEntry::Item
& rItem
= rEntry
.GetQueryItem();
57 if (rItem
.meType
== ScQueryEntry::ByString
)
59 sal_uInt32 nIndex
= 0;
60 bool bNumber
= pD
->GetFormatTable()->IsNumberFormat(
61 rItem
.maString
.getString(), nIndex
, rItem
.mfVal
);
62 rItem
.meType
= bNumber
? ScQueryEntry::ByValue
: ScQueryEntry::ByString
;
68 ScSheetDPData::~ScSheetDPData()
72 void ScSheetDPData::DisposeData()
77 long ScSheetDPData::GetColumnCount()
80 return aCacheTable
.getColSize();
83 OUString
ScSheetDPData::getDimensionName(long nColumn
)
86 if (getIsDataLayoutDimension(nColumn
))
88 //TODO: different internal and display names?
90 return ScGlobal::GetRscString(STR_PIVOT_DATA
);
92 else if (nColumn
>= aCacheTable
.getColSize())
94 OSL_FAIL("getDimensionName: invalid dimension");
99 return aCacheTable
.getFieldName(static_cast<SCCOL
>(nColumn
));
103 bool ScSheetDPData::IsDateDimension(long nDim
)
106 long nColCount
= aCacheTable
.getColSize();
107 if (getIsDataLayoutDimension(nDim
))
111 else if (nDim
>= nColCount
)
113 OSL_FAIL("IsDateDimension: invalid dimension");
118 return GetCacheTable().getCache().IsDateDimension( nDim
);
122 sal_uLong
ScSheetDPData::GetNumberFormat(long nDim
)
125 if (getIsDataLayoutDimension(nDim
))
129 else if (nDim
>= GetCacheTable().getColSize())
131 OSL_FAIL("GetNumberFormat: invalid dimension");
136 return GetCacheTable().getCache().GetNumberFormat( nDim
);
139 sal_uInt32
ScDPTableData::GetNumberFormatByIdx( NfIndexTableOffset eIdx
)
144 if ( SvNumberFormatter
* pFormatter
= mpDoc
->GetFormatTable() )
145 return pFormatter
->GetFormatIndex( eIdx
, LANGUAGE_SYSTEM
);
150 bool ScSheetDPData::getIsDataLayoutDimension(long nColumn
)
153 return (nColumn
==(long)( aCacheTable
.getColSize()));
156 void ScSheetDPData::SetEmptyFlags( bool bIgnoreEmptyRowsP
, bool bRepeatIfEmptyP
)
158 bIgnoreEmptyRows
= bIgnoreEmptyRowsP
;
159 bRepeatIfEmpty
= bRepeatIfEmptyP
;
162 bool ScSheetDPData::IsRepeatIfEmpty()
164 return bRepeatIfEmpty
;
167 void ScSheetDPData::CreateCacheTable()
169 // Scan and store the data from the source range.
170 if (!aCacheTable
.empty())
174 aCacheTable
.fillTable(aQuery
, bIgnoreEmptyRows
, bRepeatIfEmpty
);
177 void ScSheetDPData::FilterCacheTable(const vector
<ScDPFilteredCache::Criterion
>& rCriteria
, const std::unordered_set
<sal_Int32
>& rCatDims
)
180 aCacheTable
.filterByPageDimension(
181 rCriteria
, (IsRepeatIfEmpty() ? rCatDims
: std::unordered_set
<sal_Int32
>()));
184 void ScSheetDPData::GetDrillDownData(const vector
<ScDPFilteredCache::Criterion
>& rCriteria
, const std::unordered_set
<sal_Int32
>& rCatDims
, Sequence
< Sequence
<Any
> >& rData
)
187 sal_Int32 nRowSize
= aCacheTable
.getRowSize();
191 aCacheTable
.filterTable(
192 rCriteria
, rData
, IsRepeatIfEmpty() ? rCatDims
: std::unordered_set
<sal_Int32
>());
195 void ScSheetDPData::CalcResults(CalcInfo
& rInfo
, bool bAutoShow
)
198 CalcResultsFromCacheTable(aCacheTable
, rInfo
, bAutoShow
);
201 const ScDPFilteredCache
& ScSheetDPData::GetCacheTable() const
206 void ScSheetDPData::ReloadCacheTable()
212 ScSheetSourceDesc::ScSheetSourceDesc(ScDocument
* pDoc
) :
215 void ScSheetSourceDesc::SetSourceRange(const ScRange
& rRange
)
217 maSourceRange
= rRange
;
218 maRangeName
.clear(); // overwrite existing range name if any.
221 const ScRange
& ScSheetSourceDesc::GetSourceRange() const
223 if (!maRangeName
.isEmpty())
225 // Obtain the source range from the range name first.
226 maSourceRange
= ScRange();
227 ScRangeName
* pRangeName
= mpDoc
->GetRangeName();
233 OUString aUpper
= ScGlobal::pCharClass
->uppercase(maRangeName
);
234 const ScRangeData
* pData
= pRangeName
->findByUpperName(aUpper
);
238 // range name found. Fow now, we only use the first token and
241 if (!pData
->IsReference(aRange
))
244 maSourceRange
= aRange
;
248 return maSourceRange
;
251 void ScSheetSourceDesc::SetRangeName(const OUString
& rName
)
256 bool ScSheetSourceDesc::HasRangeName() const
258 return !maRangeName
.isEmpty();
261 void ScSheetSourceDesc::SetQueryParam(const ScQueryParam
& rParam
)
263 maQueryParam
= rParam
;
266 bool ScSheetSourceDesc::operator== (const ScSheetSourceDesc
& rOther
) const
268 return maSourceRange
== rOther
.maSourceRange
&&
269 maRangeName
== rOther
.maRangeName
&&
270 maQueryParam
== rOther
.maQueryParam
;
273 const ScDPCache
* ScSheetSourceDesc::CreateCache(const ScDPDimensionSaveData
* pDimData
) const
278 sal_uLong nErrId
= CheckSourceRange();
281 OSL_FAIL( "Error Create Cache\n" );
285 // All cache instances are managed centrally by ScDPCollection.
286 ScDPCollection
* pDPs
= mpDoc
->GetDPCollection();
289 // Name-based data source.
290 ScDPCollection::NameCaches
& rCaches
= pDPs
->GetNameCaches();
291 return rCaches
.getCache(GetRangeName(), GetSourceRange(), pDimData
);
294 ScDPCollection::SheetCaches
& rCaches
= pDPs
->GetSheetCaches();
295 return rCaches
.getCache(GetSourceRange(), pDimData
);
298 sal_uLong
ScSheetSourceDesc::CheckSourceRange() const
301 return STR_ERR_DATAPILOTSOURCE
;
303 // Make sure the range is valid and sane.
304 const ScRange
& rSrcRange
= GetSourceRange();
305 if (!rSrcRange
.IsValid())
306 return STR_ERR_DATAPILOTSOURCE
;
308 if (rSrcRange
.aStart
.Col() > rSrcRange
.aEnd
.Col() || rSrcRange
.aStart
.Row() > rSrcRange
.aEnd
.Row())
309 return STR_ERR_DATAPILOTSOURCE
;
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */