build fix
[LibreOffice.git] / sc / inc / dpobject.hxx
blob8e4a5f6be3f6017deb990577cdb50f1db900f8f0
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 class Rectangle;
57 class ScDPSaveData;
58 class ScDPOutput;
59 struct ScPivotParam;
60 struct ScImportSourceDesc;
61 class ScSheetSourceDesc;
62 struct ScPivotField;
63 class ScDPTableData;
64 class ScDPDimensionSaveData;
65 class ScRangeList;
67 struct ScDPServiceDesc
69 OUString aServiceName;
70 OUString aParSource;
71 OUString aParName;
72 OUString aParUser;
73 OUString aParPass;
75 ScDPServiceDesc( const OUString& rServ, const OUString& rSrc, const OUString& rNam,
76 const OUString& rUser, const OUString& rPass );
78 bool operator== ( const ScDPServiceDesc& rOther ) const;
81 class SC_DLLPUBLIC ScDPObject
83 private:
84 ScDocument* pDoc;
85 // settings
86 ScDPSaveData* pSaveData;
87 OUString aTableName;
88 OUString aTableTag;
89 ScRange aOutRange;
90 ScSheetSourceDesc* pSheetDesc; // for sheet data
91 ScImportSourceDesc* pImpDesc; // for database data
92 ScDPServiceDesc* pServDesc; // for external service
93 std::shared_ptr<ScDPTableData> mpTableData;
94 // cached data
95 css::uno::Reference<css::sheet::XDimensionsSupplier> xSource;
96 ScDPOutput* pOutput;
97 long nHeaderRows; // page fields plus filter button
98 bool mbHeaderLayout:1; // true : grid, false : standard
99 bool bAllowMove:1;
100 bool bSettingsChanged:1;
101 bool mbEnableGetPivotData:1;
103 SAL_DLLPRIVATE ScDPTableData* GetTableData();
104 SAL_DLLPRIVATE void CreateObjects();
105 SAL_DLLPRIVATE void CreateOutput();
106 SAL_DLLPRIVATE void ClearSource();
107 SAL_DLLPRIVATE bool FillLabelDataForDimension(
108 const css::uno::Reference< css::container::XIndexAccess>& xDims,
109 sal_Int32 nDim, ScDPLabelData& rLabelData);
111 public:
112 ScDPObject(ScDocument* pD);
113 ScDPObject(const ScDPObject& r);
114 ~ScDPObject();
116 ScDPObject& operator= (const ScDPObject& r);
118 void EnableGetPivotData(bool b);
120 void SetAllowMove(bool bSet);
122 void InvalidateData();
123 void Clear();
124 void ClearTableData();
125 void ReloadGroupTableData();
127 void Output( const ScAddress& rPos );
128 ScRange GetNewOutputRange( bool& rOverflow );
130 ScRange GetOutputRangeByType( sal_Int32 nType );
131 ScRange GetOutputRangeByType( sal_Int32 nType ) const;
133 void SetSaveData(const ScDPSaveData& rData);
134 ScDPSaveData* GetSaveData() const { return pSaveData; }
136 void SetOutRange(const ScRange& rRange);
137 const ScRange& GetOutRange() const;
139 void SetHeaderLayout(bool bUseGrid);
140 bool GetHeaderLayout() const { return mbHeaderLayout;}
142 void SetSheetDesc(const ScSheetSourceDesc& rDesc, bool bFromRefUpdate = false);
143 void SetImportDesc(const ScImportSourceDesc& rDesc);
144 void SetServiceData(const ScDPServiceDesc& rDesc);
146 void WriteSourceDataTo( ScDPObject& rDest ) const;
147 void WriteTempDataTo( ScDPObject& rDest ) const;
149 const ScSheetSourceDesc* GetSheetDesc() const { return pSheetDesc; }
150 const ScImportSourceDesc* GetImportSourceDesc() const { return pImpDesc; }
151 const ScDPServiceDesc* GetDPServiceDesc() const { return pServDesc; }
153 css::uno::Reference<css::sheet::XDimensionsSupplier> const & GetSource();
155 bool IsSheetData() const;
156 bool IsImportData() const { return(pImpDesc != nullptr); }
157 bool IsServiceData() const { return(pServDesc != nullptr); }
159 void SetName(const OUString& rNew);
160 const OUString& GetName() const { return aTableName; }
161 void SetTag(const OUString& rNew);
162 const OUString& GetTag() const { return aTableTag; }
165 * Data description cell displays the description of a data dimension if
166 * and only if there is only one data dimension. It's usually located at
167 * the upper-left corner of the table output.
169 bool IsDataDescriptionCell(const ScAddress& rPos);
171 bool IsDimNameInUse(const OUString& rName) const;
172 OUString GetDimName( long nDim, bool& rIsDataLayout, sal_Int32* pFlags = nullptr );
173 bool IsDuplicated( long nDim );
174 long GetDimCount();
175 void GetHeaderPositionData(const ScAddress& rPos, css::sheet::DataPilotTableHeaderData& rData);
176 long GetHeaderDim( const ScAddress& rPos, sal_uInt16& rOrient );
177 bool GetHeaderDrag( const ScAddress& rPos, bool bMouseLeft, bool bMouseTop,
178 long nDragDim,
179 Rectangle& rPosRect, sal_uInt16& rOrient, long& rDimPos );
180 bool IsFilterButton( const ScAddress& rPos );
182 OUString GetFormattedString( const OUString& rDimName, const double fValue );
184 double GetPivotData(
185 const OUString& rDataFieldName,
186 std::vector<css::sheet::DataPilotFieldFilter>& rFilters);
188 bool ParseFilters(
189 OUString& rDataFieldName,
190 std::vector<css::sheet::DataPilotFieldFilter>& rFilters,
191 std::vector<sal_Int16>& rFilterFuncs,
192 const OUString& rFilterList );
194 void GetMemberResultNames(ScDPUniqueStringSet& rNames, long nDimension);
196 void ToggleDetails(const css::sheet::DataPilotTableHeaderData& rElemDesc, ScDPObject* pDestObj);
198 void FillOldParam(ScPivotParam& rParam) const;
199 void FillLabelData(sal_Int32 nDim, ScDPLabelData& Labels);
200 void FillLabelData(ScPivotParam& rParam);
202 bool GetHierarchiesNA( sal_Int32 nDim, css::uno::Reference< css::container::XNameAccess >& xHiers );
203 void GetHierarchies( sal_Int32 nDim, css::uno::Sequence< OUString >& rHiers );
205 sal_Int32 GetUsedHierarchy( sal_Int32 nDim );
207 bool GetMembersNA( sal_Int32 nDim, css::uno::Reference< css::sheet::XMembersAccess >& xMembers );
208 bool GetMembersNA( sal_Int32 nDim, sal_Int32 nHier, css::uno::Reference< css::sheet::XMembersAccess >& xMembers );
210 bool GetMemberNames( sal_Int32 nDim, css::uno::Sequence< OUString >& rNames );
211 bool GetMembers( sal_Int32 nDim, sal_Int32 nHier, ::std::vector<ScDPLabelData::Member>& rMembers );
213 void UpdateReference( UpdateRefMode eUpdateRefMode,
214 const ScRange& r, SCsCOL nDx, SCsROW nDy, SCsTAB nDz );
215 bool RefsEqual( const ScDPObject& r ) const;
216 void WriteRefsTo( ScDPObject& r ) const;
218 void GetPositionData(const ScAddress& rPos, css::sheet::DataPilotTablePositionData& rPosData);
220 bool GetDataFieldPositionData(const ScAddress& rPos,
221 css::uno::Sequence<
222 css::sheet::DataPilotFieldFilter >& rFilters);
224 void GetDrillDownData(const ScAddress& rPos,
225 css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rTableData);
227 // apply drop-down attribute, initialize nHeaderRows, without accessing the source
228 // (button attribute must be present)
229 void RefreshAfterLoad();
231 void BuildAllDimensionMembers();
234 * Remove in the save data entries for members that don't exist anymore.
235 * This is called during pivot table refresh.
237 bool SyncAllDimensionMembers();
239 static bool HasRegisteredSources();
240 static std::vector<OUString> GetRegisteredSources();
241 static css::uno::Reference<css::sheet::XDimensionsSupplier>
242 CreateSource( const ScDPServiceDesc& rDesc );
244 static void ConvertOrientation(
245 ScDPSaveData& rSaveData,
246 const ScPivotFieldVector& rFields, sal_uInt16 nOrient,
247 const css::uno::Reference< css::sheet::XDimensionsSupplier>& xSource,
248 const ScDPLabelDataVector& rLabels,
249 const ScPivotFieldVector* pRefColFields = nullptr,
250 const ScPivotFieldVector* pRefRowFields = nullptr,
251 const ScPivotFieldVector* pRefPageFields = nullptr );
253 static bool IsOrientationAllowed( sal_uInt16 nOrient, sal_Int32 nDimFlags );
255 #if DUMP_PIVOT_TABLE
256 void Dump() const;
257 void DumpCache() const;
258 #endif
261 class ScDPCollection
263 friend class ScDPCache;
264 public:
267 * Stores and manages all caches from internal sheets.
269 class SheetCaches
271 friend class ScDPCollection;
272 typedef std::map<size_t, std::unique_ptr<ScDPCache>> CachesType;
273 typedef std::vector<ScRange> RangeIndexType;
274 CachesType m_Caches;
275 RangeIndexType maRanges;
276 ScDocument* mpDoc;
277 public:
278 SheetCaches(ScDocument* pDoc);
279 bool hasCache(const ScRange& rRange) const;
280 const ScDPCache* getCache(const ScRange& rRange, const ScDPDimensionSaveData* pDimData);
281 SC_DLLPUBLIC size_t size() const;
283 void updateReference(
284 UpdateRefMode eMode, const ScRange& r, SCsCOL nDx, SCsROW nDy, SCsTAB nDz);
286 SC_DLLPUBLIC ScDPCache* getExistingCache(const ScRange& rRange);
287 SC_DLLPUBLIC const ScDPCache* getExistingCache(const ScRange& rRange) const;
289 void updateCache(const ScRange& rRange, std::set<ScDPObject*>& rRefs);
290 bool remove(const ScDPCache* p);
292 SC_DLLPUBLIC const std::vector<ScRange>& getAllRanges() const;
296 * Data caches for range name based source data.
298 class NameCaches
300 friend class ScDPCollection;
301 typedef ::std::map<OUString, std::unique_ptr<ScDPCache>> CachesType;
302 CachesType m_Caches;
303 ScDocument* mpDoc;
304 public:
305 NameCaches(ScDocument* pDoc);
306 bool hasCache(const OUString& rName) const;
307 const ScDPCache* getCache(
308 const OUString& rName, const ScRange& rRange, const ScDPDimensionSaveData* pDimData);
309 size_t size() const;
310 private:
311 ScDPCache* getExistingCache(const OUString& rName);
313 void updateCache(
314 const OUString& rName, const ScRange& rRange, std::set<ScDPObject*>& rRefs);
315 bool remove(const ScDPCache* p);
319 * Defines connection type to external data source. Used as a key to look
320 * up database cache.
322 struct DBType
324 sal_Int32 mnSdbType;
325 OUString maDBName;
326 OUString maCommand;
327 DBType(sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand);
329 struct less : public ::std::binary_function<DBType, DBType, bool>
331 bool operator() (const DBType& left, const DBType& right) const;
336 * Data caches for external database sources.
338 class DBCaches
340 friend class ScDPCollection;
341 typedef ::std::map<DBType, std::unique_ptr<ScDPCache>, DBType::less> CachesType;
342 CachesType m_Caches;
343 ScDocument* mpDoc;
344 public:
345 DBCaches(ScDocument* pDoc);
346 bool hasCache(sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand) const;
347 const ScDPCache* getCache(
348 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand,
349 const ScDPDimensionSaveData* pDimData);
351 private:
352 ScDPCache* getExistingCache(
353 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand);
355 static css::uno::Reference<css::sdbc::XRowSet> createRowSet(
356 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand);
358 void updateCache(
359 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand,
360 std::set<ScDPObject*>& rRefs);
361 bool remove(const ScDPCache* p);
364 ScDPCollection(ScDocument* pDocument);
365 ScDPCollection(const ScDPCollection& r);
366 ~ScDPCollection();
368 sal_uLong ReloadCache(ScDPObject* pDPObj, std::set<ScDPObject*>& rRefs);
369 bool ReloadGroupsInCache(ScDPObject* pDPObj, std::set<ScDPObject*>& rRefs);
370 SC_DLLPUBLIC bool GetReferenceGroups(const ScDPObject& rDPObj, const ScDPDimensionSaveData** pGroups) const;
372 SC_DLLPUBLIC size_t GetCount() const;
373 SC_DLLPUBLIC ScDPObject& operator[](size_t nIndex);
374 SC_DLLPUBLIC const ScDPObject& operator[](size_t nIndex) const;
376 const ScDPObject* GetByName(const OUString& rName) const;
378 void DeleteOnTab( SCTAB nTab );
379 void UpdateReference( UpdateRefMode eUpdateRefMode,
380 const ScRange& r, SCsCOL nDx, SCsROW nDy, SCsTAB nDz );
381 void CopyToTab( SCTAB nOld, SCTAB nNew );
382 bool RefsEqual( const ScDPCollection& r ) const;
383 void WriteRefsTo( ScDPCollection& r ) const;
386 * Create a new name that's not yet used by any existing data pilot
387 * objects. All data pilot names are 'DataPilot' + <num>
389 * @return new name for data pilot object.
391 OUString CreateNewName() const;
393 void FreeTable(ScDPObject* pDPObj);
394 SC_DLLPUBLIC bool InsertNewTable(ScDPObject* pDPObj);
395 SC_DLLPUBLIC bool HasTable(const ScDPObject* pDPObj) const;
397 SC_DLLPUBLIC SheetCaches& GetSheetCaches();
398 SC_DLLPUBLIC const SheetCaches& GetSheetCaches() const;
399 NameCaches& GetNameCaches();
400 SC_DLLPUBLIC const NameCaches& GetNameCaches() const;
401 DBCaches& GetDBCaches();
402 SC_DLLPUBLIC const DBCaches& GetDBCaches() const;
404 ScRangeList GetAllTableRanges( SCTAB nTab ) const;
405 bool IntersectsTableByColumns( SCCOL nCol1, SCCOL nCol2, SCROW nRow, SCTAB nTab ) const;
406 bool IntersectsTableByRows( SCCOL nCol, SCROW nRow1, SCROW nRow2, SCTAB nTab ) const;
407 bool HasTable( const ScRange& rRange ) const;
409 #if DEBUG_PIVOT_TABLE
410 void DumpTables() const;
411 #endif
413 private:
414 /** Only to be called from ScDPCache::RemoveReference(). */
415 void RemoveCache(const ScDPCache* pCache);
417 void GetAllTables(const ScRange& rSrcRange, std::set<ScDPObject*>& rRefs) const;
418 void GetAllTables(const OUString& rSrcName, std::set<ScDPObject*>& rRefs) const;
419 void GetAllTables(
420 sal_Int32 nSdbType, const OUString& rDBName, const OUString& rCommand,
421 std::set<ScDPObject*>& rRefs) const;
423 private:
424 typedef std::vector< std::unique_ptr<ScDPObject> > TablesType;
426 ScDocument* mpDoc;
427 TablesType maTables;
428 SheetCaches maSheetCaches;
429 NameCaches maNameCaches;
430 DBCaches maDBCaches;
433 bool operator<(const ScDPCollection::DBType& left, const ScDPCollection::DBType& right);
435 #endif
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */