fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / core / data / dpshttab.cxx
blob08a2421e6df14ab523f32b84d07b53acd1c70be2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
35 #include <vector>
36 #include <set>
38 using namespace ::com::sun::star;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::Sequence;
41 using ::std::vector;
43 ScSheetDPData::ScSheetDPData(ScDocument* pD, const ScSheetSourceDesc& rDesc, const ScDPCache& rCache) :
44 ScDPTableData(pD),
45 aQuery ( rDesc.GetQueryParam() ),
46 bIgnoreEmptyRows( false ),
47 bRepeatIfEmpty(false),
48 aCacheTable(rCache)
50 SCSIZE nEntryCount( aQuery.GetEntryCount());
51 for (SCSIZE j = 0; j < nEntryCount; ++j)
53 ScQueryEntry& rEntry = aQuery.GetEntry(j);
54 if (rEntry.bDoQuery)
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()
74 aCacheTable.clear();
77 long ScSheetDPData::GetColumnCount()
79 CreateCacheTable();
80 return aCacheTable.getColSize();
83 OUString ScSheetDPData::getDimensionName(long nColumn)
85 CreateCacheTable();
86 if (getIsDataLayoutDimension(nColumn))
88 //TODO: different internal and display names?
89 //return "Data";
90 return ScGlobal::GetRscString(STR_PIVOT_DATA);
92 else if (nColumn >= aCacheTable.getColSize())
94 OSL_FAIL("getDimensionName: invalid dimension");
95 return OUString();
97 else
99 return aCacheTable.getFieldName(static_cast<SCCOL>(nColumn));
103 bool ScSheetDPData::IsDateDimension(long nDim)
105 CreateCacheTable();
106 long nColCount = aCacheTable.getColSize();
107 if (getIsDataLayoutDimension(nDim))
109 return false;
111 else if (nDim >= nColCount)
113 OSL_FAIL("IsDateDimension: invalid dimension");
114 return false;
116 else
118 return GetCacheTable().getCache().IsDateDimension( nDim);
122 sal_uLong ScSheetDPData::GetNumberFormat(long nDim)
124 CreateCacheTable();
125 if (getIsDataLayoutDimension(nDim))
127 return 0;
129 else if (nDim >= GetCacheTable().getColSize())
131 OSL_FAIL("GetNumberFormat: invalid dimension");
132 return 0;
134 else
136 return GetCacheTable().getCache().GetNumberFormat( nDim );
139 sal_uInt32 ScDPTableData::GetNumberFormatByIdx( NfIndexTableOffset eIdx )
141 if( !mpDoc )
142 return 0;
144 if ( SvNumberFormatter* pFormatter = mpDoc->GetFormatTable() )
145 return pFormatter->GetFormatIndex( eIdx, LANGUAGE_SYSTEM );
147 return 0;
150 bool ScSheetDPData::getIsDataLayoutDimension(long nColumn)
152 CreateCacheTable();
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())
171 // already cached.
172 return;
174 aCacheTable.fillTable(aQuery, bIgnoreEmptyRows, bRepeatIfEmpty);
177 void ScSheetDPData::FilterCacheTable(const vector<ScDPFilteredCache::Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rCatDims)
179 CreateCacheTable();
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)
186 CreateCacheTable();
187 sal_Int32 nRowSize = aCacheTable.getRowSize();
188 if (!nRowSize)
189 return;
191 aCacheTable.filterTable(
192 rCriteria, rData, IsRepeatIfEmpty() ? rCatDims : std::unordered_set<sal_Int32>());
195 void ScSheetDPData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
197 CreateCacheTable();
198 CalcResultsFromCacheTable(aCacheTable, rInfo, bAutoShow);
201 const ScDPFilteredCache& ScSheetDPData::GetCacheTable() const
203 return aCacheTable;
206 void ScSheetDPData::ReloadCacheTable()
208 aCacheTable.clear();
209 CreateCacheTable();
212 ScSheetSourceDesc::ScSheetSourceDesc(ScDocument* pDoc) :
213 mpDoc(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();
230 if (!pRangeName)
231 break;
233 OUString aUpper = ScGlobal::pCharClass->uppercase(maRangeName);
234 const ScRangeData* pData = pRangeName->findByUpperName(aUpper);
235 if (!pData)
236 break;
238 // range name found. Fow now, we only use the first token and
239 // ignore the rest.
240 ScRange aRange;
241 if (!pData->IsReference(aRange))
242 break;
244 maSourceRange = aRange;
246 while (false);
248 return maSourceRange;
251 void ScSheetSourceDesc::SetRangeName(const OUString& rName)
253 maRangeName = 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
275 if (!mpDoc)
276 return NULL;
278 sal_uLong nErrId = CheckSourceRange();
279 if (nErrId)
281 OSL_FAIL( "Error Create Cache\n" );
282 return NULL;
285 // All cache instances are managed centrally by ScDPCollection.
286 ScDPCollection* pDPs = mpDoc->GetDPCollection();
287 if (HasRangeName())
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
300 if (!mpDoc)
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;
311 return 0;
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */