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/numformat.hxx>
21 #include <svl/zforlist.hxx>
22 #include <unotools/charclass.hxx>
24 #include <dpcache.hxx>
25 #include <dpshttab.hxx>
26 #include <document.hxx>
27 #include <dpfilteredcache.hxx>
28 #include <dpobject.hxx>
29 #include <globstr.hrc>
30 #include <scresid.hxx>
31 #include <rangenam.hxx>
32 #include <queryentry.hxx>
34 #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(const 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 sal_Int32
ScSheetDPData::GetColumnCount()
80 return aCacheTable
.getColSize();
83 OUString
ScSheetDPData::getDimensionName(sal_Int32 nColumn
)
86 if (getIsDataLayoutDimension(nColumn
))
88 //TODO: different internal and display names?
90 return ScResId(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(sal_Int32 nDim
)
106 tools::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_uInt32
ScSheetDPData::GetNumberFormat(sal_Int32 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(sal_Int32 nColumn
)
153 return (nColumn
==static_cast<tools::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(std::vector
<ScDPFilteredCache::Criterion
>&& rCriteria
, std::unordered_set
<sal_Int32
>&& rCatDims
)
180 aCacheTable
.filterByPageDimension(
181 rCriteria
, (IsRepeatIfEmpty() ? std::move(rCatDims
) : std::unordered_set
<sal_Int32
>()));
184 void ScSheetDPData::GetDrillDownData(std::vector
<ScDPFilteredCache::Criterion
>&& rCriteria
, std::unordered_set
<sal_Int32
>&& rCatDims
, Sequence
< Sequence
<Any
> >& rData
)
187 sal_Int32 nRowSize
= aCacheTable
.getRowSize();
191 aCacheTable
.filterTable(
192 rCriteria
, rData
, IsRepeatIfEmpty() ? std::move(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()
214 void ScSheetDPData::Dump() const
216 // TODO : Implement this.
221 ScSheetSourceDesc::ScSheetSourceDesc(ScDocument
* pDoc
) :
224 void ScSheetSourceDesc::SetSourceRange(const ScRange
& rRange
)
226 maSourceRange
= rRange
;
227 maRangeName
.clear(); // overwrite existing range name if any.
230 const ScRange
& ScSheetSourceDesc::GetSourceRange() const
232 if (!maRangeName
.isEmpty())
234 // Obtain the source range from the range name first.
235 maSourceRange
= ScRange();
236 ScRangeName
* pRangeName
= mpDoc
->GetRangeName();
242 OUString aUpper
= ScGlobal::getCharClass().uppercase(maRangeName
);
243 const ScRangeData
* pData
= pRangeName
->findByUpperName(aUpper
);
247 // range name found. Fow now, we only use the first token and
250 if (!pData
->IsReference(aRange
))
253 maSourceRange
= aRange
;
257 return maSourceRange
;
260 void ScSheetSourceDesc::SetRangeName(const OUString
& rName
)
265 bool ScSheetSourceDesc::HasRangeName() const
267 return !maRangeName
.isEmpty();
270 void ScSheetSourceDesc::SetQueryParam(const ScQueryParam
& rParam
)
272 maQueryParam
= rParam
;
275 bool ScSheetSourceDesc::operator== (const ScSheetSourceDesc
& rOther
) const
277 return maSourceRange
== rOther
.maSourceRange
&&
278 maRangeName
== rOther
.maRangeName
&&
279 maQueryParam
== rOther
.maQueryParam
;
282 const ScDPCache
* ScSheetSourceDesc::CreateCache(const ScDPDimensionSaveData
* pDimData
) const
287 TranslateId pErrId
= CheckSourceRange();
290 OSL_FAIL( "Error Create Cache" );
294 // All cache instances are managed centrally by ScDPCollection.
295 ScDPCollection
* pDPs
= mpDoc
->GetDPCollection();
298 // Name-based data source.
299 ScDPCollection::NameCaches
& rCaches
= pDPs
->GetNameCaches();
300 return rCaches
.getCache(GetRangeName(), GetSourceRange(), pDimData
);
303 ScDPCollection::SheetCaches
& rCaches
= pDPs
->GetSheetCaches();
304 return rCaches
.getCache(GetSourceRange(), pDimData
);
307 TranslateId
ScSheetSourceDesc::CheckSourceRange() const
310 return STR_ERR_DATAPILOTSOURCE
;
312 // Make sure the range is valid and sane.
313 const ScRange
& rSrcRange
= GetSourceRange();
314 if (!rSrcRange
.IsValid())
315 return STR_ERR_DATAPILOTSOURCE
;
317 if (rSrcRange
.aStart
.Col() > rSrcRange
.aEnd
.Col() || rSrcRange
.aStart
.Row() > rSrcRange
.aEnd
.Row())
318 return STR_ERR_DATAPILOTSOURCE
;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */