Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sc / inc / dpobject.hxx
blob2e02ac88eed838494d20313cde22b98d394819c6
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 #ifndef INCLUDED_SC_INC_DPOBJECT_HXX
21 #define INCLUDED_SC_INC_DPOBJECT_HXX
23 #include "scdllapi.h"
24 #include "global.hxx"
25 #include "address.hxx"
26 #include "dpoutput.hxx"
27 #include "dptypes.hxx"
28 #include "pivot.hxx"
29 #include "calcmacros.hxx"
31 #include <com/sun/star/sheet/XDimensionsSupplier.hpp>
33 #include <memory>
34 #include <set>
35 #include <vector>
36 #include <map>
38 namespace com { namespace sun { namespace star {
40 namespace container {
41 class XIndexAccess;
44 namespace sdbc {
45 class XRowSet;
48 namespace sheet {
49 class XMembersAccess;
50 struct DataPilotTablePositionData;
51 struct DataPilotTableHeaderData;
52 struct DataPilotFieldFilter;
54 }}}
56 namespace tools { class Rectangle; }
57 class ScDPSaveData;
58 class ScDPOutput;
59 struct ScImportSourceDesc;
60 class ScSheetSourceDesc;
61 class ScDPTableData;
62 class ScDPDimensionSaveData;
63 class ScRangeList;
65 struct ScDPServiceDesc
67 OUString aServiceName;
68 OUString aParSource;
69 OUString aParName;
70 OUString aParUser;
71 OUString aParPass;
73 ScDPServiceDesc( const OUString& rServ, const OUString& rSrc, const OUString& rNam,
74 const OUString& rUser, const OUString& rPass );
76 bool operator== ( const ScDPServiceDesc& rOther ) const;
79 class SC_DLLPUBLIC ScDPObject
81 private:
82 ScDocument* pDoc;
83 // settings
84 ScDPSaveData* pSaveData;
85 OUString aTableName;
86 OUString aTableTag;
87 ScRange aOutRange;
88 ScSheetSourceDesc* pSheetDesc; // for sheet data
89 ScImportSourceDesc* pImpDesc; // for database data
90 ScDPServiceDesc* pServDesc; // for external service
91 std::shared_ptr<ScDPTableData> mpTableData;
92 // cached data
93 css::uno::Reference<css::sheet::XDimensionsSupplier> xSource;
94 ScDPOutput* pOutput;
95 long nHeaderRows; // page fields plus filter button
96 bool mbHeaderLayout:1; // true : grid, false : standard
97 bool bAllowMove:1;
98 bool bSettingsChanged:1;
99 bool mbEnableGetPivotData:1;
101 SAL_DLLPRIVATE ScDPTableData* GetTableData();
102 SAL_DLLPRIVATE void CreateObjects();
103 SAL_DLLPRIVATE void CreateOutput();
104 SAL_DLLPRIVATE void ClearSource();
105 SAL_DLLPRIVATE void FillLabelDataForDimension(
106 const css::uno::Reference< css::container::XIndexAccess>& xDims,
107 sal_Int32 nDim, ScDPLabelData& rLabelData);
109 public:
110 ScDPObject(ScDocument* pD);
111 ScDPObject(const ScDPObject& r);
112 ~ScDPObject();
114 ScDPObject& operator= (const ScDPObject& r);
116 void EnableGetPivotData(bool b);
118 void SetAllowMove(bool bSet);
120 void InvalidateData();
121 void Clear();
122 void ClearTableData();
123 void ReloadGroupTableData();
125 void Output( const ScAddress& rPos );
126 ScRange GetNewOutputRange( bool& rOverflow );
128 ScRange GetOutputRangeByType( sal_Int32 nType );
129 ScRange GetOutputRangeByType( sal_Int32 nType ) const;
131 void SetSaveData(const ScDPSaveData& rData);
132 ScDPSaveData* GetSaveData() const { return pSaveData; }
134 void SetOutRange(const ScRange& rRange);
135 const ScRange& GetOutRange() const;
137 void SetHeaderLayout(bool bUseGrid);
138 bool GetHeaderLayout() const { return mbHeaderLayout;}
140 void SetSheetDesc(const ScSheetSourceDesc& rDesc);
141 void SetImportDesc(const ScImportSourceDesc& rDesc);
142 void SetServiceData(const ScDPServiceDesc& rDesc);
144 void WriteSourceDataTo( ScDPObject& rDest ) const;
145 void WriteTempDataTo( ScDPObject& rDest ) const;
147 const ScSheetSourceDesc* GetSheetDesc() const { return pSheetDesc; }
148 const ScImportSourceDesc* GetImportSourceDesc() const { return pImpDesc; }
149 const ScDPServiceDesc* GetDPServiceDesc() const { return pServDesc; }
151 css::uno::Reference<css::sheet::XDimensionsSupplier> const & GetSource();
153 bool IsSheetData() const;
154 bool IsImportData() const { return(pImpDesc != nullptr); }
155 bool IsServiceData() const { return(pServDesc != nullptr); }
157 void SetName(const OUString& rNew);
158 const OUString& GetName() const { return aTableName; }
159 void SetTag(const OUString& rNew);
160 const OUString& GetTag() const { return aTableTag; }
163 * Data description cell displays the description of a data dimension if
164 * and only if there is only one data dimension. It's usually located at
165 * the upper-left corner of the table output.
167 bool IsDataDescriptionCell(const ScAddress& rPos);
169 bool IsDimNameInUse(const OUString& rName) const;
170 OUString GetDimName( long nDim, bool& rIsDataLayout, sal_Int32* pFlags = nullptr );
171 bool IsDuplicated( long nDim );
172 long GetDimCount();
173 void GetHeaderPositionData(const ScAddress& rPos, css::sheet::DataPilotTableHeaderData& rData);
174 long GetHeaderDim( const ScAddress& rPos, css::sheet::DataPilotFieldOrientation& rOrient );
175 bool GetHeaderDrag( const ScAddress& rPos, bool bMouseLeft, bool bMouseTop,
176 long nDragDim,
177 tools::Rectangle& rPosRect, css::sheet::DataPilotFieldOrientation& rOrient, long& rDimPos );
178 bool IsFilterButton( const ScAddress& rPos );
180 OUString GetFormattedString( const OUString& rDimName, const double fValue );
182 double GetPivotData(
183 const OUString& rDataFieldName,
184 std::vector<css::sheet::DataPilotFieldFilter>& rFilters);
186 bool ParseFilters(
187 OUString& rDataFieldName,
188 std::vector<css::sheet::DataPilotFieldFilter>& rFilters,
189 std::vector<sal_Int16>& rFilterFuncs,
190 const OUString& rFilterList );
192 void GetMemberResultNames(ScDPUniqueStringSet& rNames, long nDimension);
194 void ToggleDetails(const css::sheet::DataPilotTableHeaderData& rElemDesc, ScDPObject* pDestObj);
196 void FillOldParam(ScPivotParam& rParam) const;
197 void FillLabelData(sal_Int32 nDim, ScDPLabelData& Labels);
198 void FillLabelData(ScPivotParam& rParam);
200 bool GetHierarchiesNA( sal_Int32 nDim, css::uno::Reference< css::container::XNameAccess >& xHiers );
201 void GetHierarchies( sal_Int32 nDim, css::uno::Sequence< OUString >& rHiers );
203 sal_Int32 GetUsedHierarchy( sal_Int32 nDim );
205 bool GetMembersNA( sal_Int32 nDim, css::uno::Reference< css::sheet::XMembersAccess >& xMembers );
206 bool GetMembersNA( sal_Int32 nDim, sal_Int32 nHier, css::uno::Reference< css::sheet::XMembersAccess >& xMembers );
208 bool GetMemberNames( sal_Int32 nDim, css::uno::Sequence< OUString >& rNames );
209 bool GetMembers( sal_Int32 nDim, sal_Int32 nHier, ::std::vector<ScDPLabelData::Member>& rMembers );
211 void UpdateReference( UpdateRefMode eUpdateRefMode,
212 const ScRange& r, SCCOL nDx, SCROW nDy, SCTAB nDz );
213 bool RefsEqual( const ScDPObject& r ) const;
214 void WriteRefsTo( ScDPObject& r ) const;
216 void GetPositionData(const ScAddress& rPos, css::sheet::DataPilotTablePositionData& rPosData);
218 bool GetDataFieldPositionData(const ScAddress& rPos,
219 css::uno::Sequence<
220 css::sheet::DataPilotFieldFilter >& rFilters);
222 void GetDrillDownData(const ScAddress& rPos,
223 css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rTableData);
225 // apply drop-down attribute, initialize nHeaderRows, without accessing the source
226 // (button attribute must be present)
227 void RefreshAfterLoad();
229 void BuildAllDimensionMembers();
232 * Remove in the save data entries for members that don't exist anymore.
233 * This is called during pivot table refresh.
235 bool SyncAllDimensionMembers();
237 static bool HasRegisteredSources();
238 static std::vector<OUString> GetRegisteredSources();
239 static css::uno::Reference<css::sheet::XDimensionsSupplier>
240 CreateSource( const ScDPServiceDesc& rDesc );
242 static void ConvertOrientation(
243 ScDPSaveData& rSaveData,
244 const ScPivotFieldVector& rFields, css::sheet::DataPilotFieldOrientation nOrient,
245 const css::uno::Reference< css::sheet::XDimensionsSupplier>& xSource,
246 const ScDPLabelDataVector& rLabels,
247 const ScPivotFieldVector* pRefColFields = nullptr,
248 const ScPivotFieldVector* pRefRowFields = nullptr,
249 const ScPivotFieldVector* pRefPageFields = nullptr );
251 static bool IsOrientationAllowed( css::sheet::DataPilotFieldOrientation nOrient, sal_Int32 nDimFlags );
253 #if DUMP_PIVOT_TABLE
254 void Dump() const;
255 void DumpCache() const;
256 #endif
259 class ScDPCollection
261 friend class ScDPCache;
262 public:
265 * Stores and manages all caches from internal sheets.
267 class SheetCaches
269 friend class ScDPCollection;
270 typedef std::map<size_t, std::unique_ptr<ScDPCache>> CachesType;
271 typedef std::vector<ScRange> RangeIndexType;
272 CachesType m_Caches;
273 RangeIndexType maRanges;
274 ScDocument* mpDoc;
275 public:
276 SheetCaches(ScDocument* pDoc);
277 bool hasCache(const ScRange& rRange) const;
278 const ScDPCache* getCache(const ScRange& rRange, const ScDPDimensionSaveData* pDimData);
279 SC_DLLPUBLIC size_t size() const;
281 void updateReference(
282 UpdateRefMode eMode, const ScRange& r, SCCOL nDx, SCROW nDy, SCTAB nDz);
284 SC_DLLPUBLIC ScDPCache* getExistingCache(const ScRange& rRange);
285 SC_DLLPUBLIC const ScDPCache* getExistingCache(const ScRange& rRange) const;
287 void updateCache(const ScRange& rRange, std::set<ScDPObject*>& rRefs);
288 bool remove(const ScDPCache* p);
290 SC_DLLPUBLIC const std::vector<ScRange>& getAllRanges() const;
294 * Data caches for range name based source data.
296 class NameCaches
298 friend class ScDPCollection;
299 typedef ::std::map<OUString, std::unique_ptr<ScDPCache>> CachesType;
300 CachesType m_Caches;
301 ScDocument* mpDoc;
302 public:
303 NameCaches(ScDocument* pDoc);
304 bool hasCache(const OUString& rName) const;
305 const ScDPCache* getCache(
306 const OUString& rName, const ScRange& rRange, const ScDPDimensionSaveData* pDimData);
307 size_t size() const;
308 private:
309 ScDPCache* getExistingCache(const OUString& rName);
311 void updateCache(
312 const OUString& rName, const ScRange& rRange, std::set<ScDPObject*>& rRefs);
313 bool remove(const ScDPCache* p);
317 * Defines connection type to external data source. Used as a key to look
318 * up database cache.
320 struct DBType
322 sal_Int32 mnSdbType;
323 OUString maDBName;
324 OUString maCommand;
325 DBType(sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand);
327 struct less
329 bool operator() (const DBType& left, const DBType& right) const;
334 * Data caches for external database sources.
336 class DBCaches
338 friend class ScDPCollection;
339 typedef ::std::map<DBType, std::unique_ptr<ScDPCache>, DBType::less> CachesType;
340 CachesType m_Caches;
341 ScDocument* mpDoc;
342 public:
343 DBCaches(ScDocument* pDoc);
344 bool hasCache(sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand) const;
345 const ScDPCache* getCache(
346 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand,
347 const ScDPDimensionSaveData* pDimData);
349 private:
350 ScDPCache* getExistingCache(
351 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand);
353 static css::uno::Reference<css::sdbc::XRowSet> createRowSet(
354 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand);
356 void updateCache(
357 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand,
358 std::set<ScDPObject*>& rRefs);
359 bool remove(const ScDPCache* p);
362 ScDPCollection(ScDocument* pDocument);
363 ScDPCollection(const ScDPCollection& r);
364 ~ScDPCollection();
366 const char* ReloadCache(const ScDPObject* pDPObj, std::set<ScDPObject*>& rRefs);
367 bool ReloadGroupsInCache(const ScDPObject* pDPObj, std::set<ScDPObject*>& rRefs);
368 SC_DLLPUBLIC bool GetReferenceGroups(const ScDPObject& rDPObj, const ScDPDimensionSaveData** pGroups) const;
370 SC_DLLPUBLIC size_t GetCount() const;
371 SC_DLLPUBLIC ScDPObject& operator[](size_t nIndex);
372 SC_DLLPUBLIC const ScDPObject& operator[](size_t nIndex) const;
374 ScDPObject* GetByName(const OUString& rName) const;
376 void DeleteOnTab( SCTAB nTab );
377 void UpdateReference( UpdateRefMode eUpdateRefMode,
378 const ScRange& r, SCCOL nDx, SCROW nDy, SCTAB nDz );
379 void CopyToTab( SCTAB nOld, SCTAB nNew );
380 bool RefsEqual( const ScDPCollection& r ) const;
381 void WriteRefsTo( ScDPCollection& r ) const;
384 * Create a new name that's not yet used by any existing data pilot
385 * objects. All data pilot names are 'DataPilot' + <num>
387 * @return new name for data pilot object.
389 OUString CreateNewName() const;
391 void FreeTable(const ScDPObject* pDPObj);
392 SC_DLLPUBLIC bool InsertNewTable(ScDPObject* pDPObj);
393 SC_DLLPUBLIC bool HasTable(const ScDPObject* pDPObj) const;
395 SC_DLLPUBLIC SheetCaches& GetSheetCaches();
396 SC_DLLPUBLIC const SheetCaches& GetSheetCaches() const;
397 NameCaches& GetNameCaches();
398 SC_DLLPUBLIC const NameCaches& GetNameCaches() const;
399 DBCaches& GetDBCaches();
400 SC_DLLPUBLIC const DBCaches& GetDBCaches() const;
402 ScRangeList GetAllTableRanges( SCTAB nTab ) const;
403 bool IntersectsTableByColumns( SCCOL nCol1, SCCOL nCol2, SCROW nRow, SCTAB nTab ) const;
404 bool IntersectsTableByRows( SCCOL nCol, SCROW nRow1, SCROW nRow2, SCTAB nTab ) const;
405 bool HasTable( const ScRange& rRange ) const;
407 #if DEBUG_PIVOT_TABLE
408 void DumpTables() const;
409 #endif
411 private:
412 /** Only to be called from ScDPCache::RemoveReference(). */
413 void RemoveCache(const ScDPCache* pCache);
415 void GetAllTables(const ScRange& rSrcRange, std::set<ScDPObject*>& rRefs) const;
416 void GetAllTables(const OUString& rSrcName, std::set<ScDPObject*>& rRefs) const;
417 void GetAllTables(
418 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand,
419 std::set<ScDPObject*>& rRefs) const;
421 private:
422 typedef std::vector< std::unique_ptr<ScDPObject> > TablesType;
424 ScDocument* mpDoc;
425 TablesType maTables;
426 SheetCaches maSheetCaches;
427 NameCaches maNameCaches;
428 DBCaches maDBCaches;
431 bool operator<(const ScDPCollection::DBType& left, const ScDPCollection::DBType& right);
433 #endif
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */