Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / data / dpshttab.cxx
blob93af29c10ff69e4f93ef237104980f250eec2d1a
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>
34 #include <vector>
35 #include <set>
37 using namespace ::com::sun::star;
38 using ::com::sun::star::uno::Any;
39 using ::com::sun::star::uno::Sequence;
40 using ::std::vector;
42 // -----------------------------------------------------------------------
44 ScSheetDPData::ScSheetDPData(ScDocument* pD, const ScSheetSourceDesc& rDesc, const ScDPCache& rCache) :
45 ScDPTableData(pD),
46 aQuery ( rDesc.GetQueryParam() ),
47 bIgnoreEmptyRows( false ),
48 bRepeatIfEmpty(false),
49 aCacheTable(rCache)
51 SCSIZE nEntryCount( aQuery.GetEntryCount());
52 for (SCSIZE j = 0; j < nEntryCount; ++j)
54 ScQueryEntry& rEntry = aQuery.GetEntry(j);
55 if (rEntry.bDoQuery)
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()
75 aCacheTable.clear();
78 long ScSheetDPData::GetColumnCount()
80 CreateCacheTable();
81 return aCacheTable.getColSize();
84 OUString ScSheetDPData::getDimensionName(long nColumn)
86 CreateCacheTable();
87 if (getIsDataLayoutDimension(nColumn))
89 //! different internal and display names?
90 //return "Data";
91 return ScGlobal::GetRscString(STR_PIVOT_DATA);
93 else if (nColumn >= aCacheTable.getColSize())
95 OSL_FAIL("getDimensionName: invalid dimension");
96 return OUString();
98 else
100 return aCacheTable.getFieldName(static_cast<SCCOL>(nColumn));
104 sal_Bool ScSheetDPData::IsDateDimension(long nDim)
106 CreateCacheTable();
107 long nColCount = aCacheTable.getColSize();
108 if (getIsDataLayoutDimension(nDim))
110 return false;
112 else if (nDim >= nColCount)
114 OSL_FAIL("IsDateDimension: invalid dimension");
115 return false;
117 else
119 return GetCacheTable().getCache()->IsDateDimension( nDim);
123 sal_uLong ScSheetDPData::GetNumberFormat(long nDim)
125 CreateCacheTable();
126 if (getIsDataLayoutDimension(nDim))
128 return 0;
130 else if (nDim >= GetCacheTable().getColSize())
132 OSL_FAIL("GetNumberFormat: invalid dimension");
133 return 0;
135 else
137 return GetCacheTable().getCache()->GetNumberFormat( nDim );
140 sal_uInt32 ScDPTableData::GetNumberFormatByIdx( NfIndexTableOffset eIdx )
142 if( !mpDoc )
143 return 0;
145 if ( SvNumberFormatter* pFormatter = mpDoc->GetFormatTable() )
146 return pFormatter->GetFormatIndex( eIdx, LANGUAGE_SYSTEM );
148 return 0;
151 sal_Bool ScSheetDPData::getIsDataLayoutDimension(long nColumn)
153 CreateCacheTable();
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())
172 // already cached.
173 return;
175 aCacheTable.fillTable(aQuery, bIgnoreEmptyRows, bRepeatIfEmpty);
178 void ScSheetDPData::FilterCacheTable(const vector<ScDPFilteredCache::Criterion>& rCriteria, const boost::unordered_set<sal_Int32>& rCatDims)
180 CreateCacheTable();
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)
187 CreateCacheTable();
188 sal_Int32 nRowSize = aCacheTable.getRowSize();
189 if (!nRowSize)
190 return;
192 aCacheTable.filterTable(
193 rCriteria, rData, IsRepeatIfEmpty() ? rCatDims : boost::unordered_set<sal_Int32>());
196 void ScSheetDPData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
198 CreateCacheTable();
199 CalcResultsFromCacheTable(aCacheTable, rInfo, bAutoShow);
202 const ScDPFilteredCache& ScSheetDPData::GetCacheTable() const
204 return aCacheTable;
207 void ScSheetDPData::ReloadCacheTable()
209 aCacheTable.clear();
210 CreateCacheTable();
213 ScSheetSourceDesc::ScSheetSourceDesc(ScDocument* pDoc) :
214 mpDoc(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();
231 if (!pRangeName)
232 break;
234 OUString aUpper = ScGlobal::pCharClass->uppercase(maRangeName);
235 const ScRangeData* pData = pRangeName->findByUpperName(aUpper);
236 if (!pData)
237 break;
239 // range name found. Fow now, we only use the first token and
240 // ignore the rest.
241 ScRange aRange;
242 if (!pData->IsReference(aRange))
243 break;
245 maSourceRange = aRange;
247 while (false);
249 return maSourceRange;
252 void ScSheetSourceDesc::SetRangeName(const OUString& rName)
254 maRangeName = rName;
257 const OUString& ScSheetSourceDesc::GetRangeName() const
259 return maRangeName;
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
274 return maQueryParam;
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
286 if (!mpDoc)
287 return NULL;
289 sal_uLong nErrId = CheckSourceRange();
290 if (nErrId)
292 OSL_FAIL( "Error Create Cache\n" );
293 return NULL;
296 // All cache instances are managed centrally by ScDPCollection.
297 ScDPCollection* pDPs = mpDoc->GetDPCollection();
298 if (HasRangeName())
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
311 if (!mpDoc)
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;
322 return 0;
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */