tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / core / data / dpshttab.cxx
blobae9407a6a64c94a860b4c2c1d180ac73058a6c25
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/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>
36 #include <vector>
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(const 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 sal_Int32 ScSheetDPData::GetColumnCount()
79 CreateCacheTable();
80 return aCacheTable.getColSize();
83 OUString ScSheetDPData::getDimensionName(sal_Int32 nColumn)
85 CreateCacheTable();
86 if (getIsDataLayoutDimension(nColumn))
88 //TODO: different internal and display names?
89 //return "Data";
90 return ScResId(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(sal_Int32 nDim)
105 CreateCacheTable();
106 tools::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_uInt32 ScSheetDPData::GetNumberFormat(sal_Int32 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(sal_Int32 nColumn)
152 CreateCacheTable();
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())
171 // already cached.
172 return;
174 aCacheTable.fillTable(aQuery, bIgnoreEmptyRows, bRepeatIfEmpty);
177 void ScSheetDPData::FilterCacheTable(std::vector<ScDPFilteredCache::Criterion>&& rCriteria, std::unordered_set<sal_Int32>&& rCatDims)
179 CreateCacheTable();
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)
186 CreateCacheTable();
187 sal_Int32 nRowSize = aCacheTable.getRowSize();
188 if (!nRowSize)
189 return;
191 aCacheTable.filterTable(
192 rCriteria, rData, IsRepeatIfEmpty() ? std::move(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 #if DUMP_PIVOT_TABLE
214 void ScSheetDPData::Dump() const
216 // TODO : Implement this.
219 #endif
221 ScSheetSourceDesc::ScSheetSourceDesc(ScDocument* pDoc) :
222 mpDoc(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();
239 if (!pRangeName)
240 break;
242 OUString aUpper = ScGlobal::getCharClass().uppercase(maRangeName);
243 const ScRangeData* pData = pRangeName->findByUpperName(aUpper);
244 if (!pData)
245 break;
247 // range name found. Fow now, we only use the first token and
248 // ignore the rest.
249 ScRange aRange;
250 if (!pData->IsReference(aRange))
251 break;
253 maSourceRange = aRange;
255 while (false);
257 return maSourceRange;
260 void ScSheetSourceDesc::SetRangeName(const OUString& rName)
262 maRangeName = 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
284 if (!mpDoc)
285 return nullptr;
287 TranslateId pErrId = CheckSourceRange();
288 if (pErrId)
290 OSL_FAIL( "Error Create Cache" );
291 return nullptr;
294 // All cache instances are managed centrally by ScDPCollection.
295 ScDPCollection* pDPs = mpDoc->GetDPCollection();
296 if (HasRangeName())
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
309 if (!mpDoc)
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;
320 return {};
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */