Bump version to 6.4-15
[LibreOffice.git] / sc / inc / dbdata.hxx
blob24eaa3be7d189b987f193f553915c0d413c42a3d
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_DBDATA_HXX
21 #define INCLUDED_SC_INC_DBDATA_HXX
23 #include "scdllapi.h"
24 #include "refreshtimer.hxx"
25 #include "address.hxx"
26 #include "global.hxx"
27 #include "rangelst.hxx"
29 #include <svl/listener.hxx>
31 #include <memory>
32 #include <set>
33 #include <vector>
35 class ScDocument;
36 struct ScSortParam;
37 struct ScQueryParam;
38 struct ScSubTotalParam;
40 /** Enum used to indicate which portion of the DBArea is to be considered. */
41 enum class ScDBDataPortion
43 TOP_LEFT, ///< top left cell of area
44 AREA ///< entire area
47 /** Container base class to provide selected access for ScDBData. */
48 class ScDBDataContainerBase
50 public:
51 ScDBDataContainerBase( ScDocument& rDoc ) : mrDoc(rDoc) {}
52 virtual ~ScDBDataContainerBase() {}
53 ScDocument& GetDocument() const;
54 ScRangeList& GetDirtyTableColumnNames();
56 protected:
57 ScDocument& mrDoc;
58 ScRangeList maDirtyTableColumnNames;
61 class SAL_DLLPUBLIC_RTTI ScDBData final : public SvtListener, public ScRefreshTimer
63 private:
64 std::unique_ptr<ScSortParam> mpSortParam;
65 std::unique_ptr<ScQueryParam> mpQueryParam;
66 std::unique_ptr<ScSubTotalParam> mpSubTotal;
67 std::unique_ptr<ScImportParam> mpImportParam;
69 ScDBDataContainerBase* mpContainer;
71 /// DBParam
72 const OUString aName;
73 OUString aUpper;
74 SCTAB nTable;
75 SCCOL nStartCol;
76 SCROW nStartRow;
77 SCCOL nEndCol;
78 SCROW nEndRow;
79 bool bByRow;
80 bool bHasHeader;
81 bool bHasTotals;
82 bool bDoSize;
83 bool bKeepFmt;
84 bool bStripData;
86 /// QueryParam
87 bool bIsAdvanced; ///< true if created by advanced filter
88 ScRange aAdvSource; ///< source range
90 bool bDBSelection; ///< not in Param: if selection, block update
92 sal_uInt16 nIndex; ///< unique index formulas
93 bool bAutoFilter; ///< AutoFilter? (not saved)
94 bool bModified; ///< is set/cleared for/by(?) UpdateReference
96 ::std::vector< OUString > maTableColumnNames; ///< names of table columns
97 bool mbTableColumnNamesDirty;
98 SCSIZE nFilteredRowCount;
100 using ScRefreshTimer::operator==;
102 public:
103 struct less
105 bool operator() (const std::unique_ptr<ScDBData>& left, const std::unique_ptr<ScDBData>& right) const;
108 SC_DLLPUBLIC ScDBData(const OUString& rName,
109 SCTAB nTab,
110 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2,
111 bool bByR = true, bool bHasH = true, bool bTotals = false);
112 ScDBData(const ScDBData& rData);
113 ScDBData(const OUString& rName, const ScDBData& rData);
114 SC_DLLPUBLIC virtual ~ScDBData() override;
116 virtual void Notify( const SfxHint& rHint ) override;
118 ScDBData& operator= (const ScDBData& rData) ;
120 bool operator== (const ScDBData& rData) const;
122 const OUString& GetName() const { return aName; }
123 const OUString& GetUpperName() const { return aUpper; }
124 SCTAB GetTab() const { return nTable; }
125 void GetArea(SCTAB& rTab, SCCOL& rCol1, SCROW& rRow1, SCCOL& rCol2, SCROW& rRow2) const;
126 SC_DLLPUBLIC void GetArea(ScRange& rRange) const;
127 void SetArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
128 void MoveTo(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
129 void SetByRow(bool bByR) { bByRow = bByR; }
130 bool HasHeader() const { return bHasHeader; }
131 void SetHeader(bool bHasH) { bHasHeader = bHasH; }
132 bool HasTotals() const { return bHasTotals; }
133 void SetTotals(bool bTotals) { bHasTotals = bTotals; }
134 void SetIndex(sal_uInt16 nInd) { nIndex = nInd; }
135 sal_uInt16 GetIndex() const { return nIndex; }
136 bool IsDoSize() const { return bDoSize; }
137 void SetDoSize(bool bSet) { bDoSize = bSet; }
138 bool IsKeepFmt() const { return bKeepFmt; }
139 void SetKeepFmt(bool bSet) { bKeepFmt = bSet; }
140 bool IsStripData() const { return bStripData; }
141 void SetStripData(bool bSet) { bStripData = bSet; }
143 void SetContainer( ScDBDataContainerBase* pContainer ) { mpContainer = pContainer; }
144 /** Returns header row range if has headers, else invalid range. */
145 ScRange GetHeaderArea() const;
146 void StartTableColumnNamesListener();
147 void EndTableColumnNamesListener();
148 SC_DLLPUBLIC void SetTableColumnNames( const ::std::vector< OUString >& rNames );
149 SC_DLLPUBLIC const ::std::vector< OUString >& GetTableColumnNames() const { return maTableColumnNames; }
150 bool AreTableColumnNamesDirty() const { return mbTableColumnNamesDirty; }
152 /** Refresh/update the column names with the header row's cell contents. */
153 SC_DLLPUBLIC void RefreshTableColumnNames( ScDocument* pDoc );
155 /** Refresh/update the column names with the header row's cell contents
156 within the given range. */
157 void RefreshTableColumnNames( ScDocument* pDoc, const ScRange& rRange );
159 /** Finds the column named rName and returns the corresponding offset
160 within the table.
161 @returns -1 if not found.
163 XXX NOTE: there is no refresh of names or anything implemented yet, use
164 this only during document load time.
166 sal_Int32 GetColumnNameOffset( const OUString& rName ) const;
168 /** Returns table column name if nCol is within column range and name
169 is stored, else empty string. */
170 const OUString& GetTableColumnName( SCCOL nCol ) const;
172 OUString GetSourceString() const;
173 OUString GetOperations() const;
175 SC_DLLPUBLIC void GetSortParam(ScSortParam& rSortParam) const;
176 SC_DLLPUBLIC void SetSortParam(const ScSortParam& rSortParam);
178 /** Remember some more settings of ScSortParam, only to be called at
179 anonymous DB ranges as it at least overwrites bHasHeader. */
180 void UpdateFromSortParam( const ScSortParam& rSortParam );
182 SC_DLLPUBLIC void GetQueryParam(ScQueryParam& rQueryParam) const;
183 SC_DLLPUBLIC void SetQueryParam(const ScQueryParam& rQueryParam);
184 SC_DLLPUBLIC bool GetAdvancedQuerySource(ScRange& rSource) const;
185 SC_DLLPUBLIC void SetAdvancedQuerySource(const ScRange* pSource);
187 void GetSubTotalParam(ScSubTotalParam& rSubTotalParam) const;
188 void SetSubTotalParam(const ScSubTotalParam& rSubTotalParam);
190 void GetImportParam(ScImportParam& rImportParam) const;
191 void SetImportParam(const ScImportParam& rImportParam);
193 bool IsDBAtCursor(SCCOL nCol, SCROW nRow, SCTAB nTab, ScDBDataPortion ePortion) const;
194 bool IsDBAtArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) const;
196 bool HasImportParam() const;
197 SC_DLLPUBLIC bool HasQueryParam() const;
198 bool HasSortParam() const;
199 bool HasSubTotalParam() const;
201 bool HasImportSelection() const { return bDBSelection; }
202 void SetImportSelection(bool bSet) { bDBSelection = bSet; }
204 bool HasAutoFilter() const { return bAutoFilter; }
205 void SetAutoFilter(bool bSet) { bAutoFilter = bSet; }
207 bool IsModified() const { return bModified; }
208 void SetModified(bool bMod) { bModified = bMod; }
210 void UpdateMoveTab( SCTAB nOldPos, SCTAB nNewPos );
211 void UpdateReference(const ScDocument* pDoc, UpdateRefMode eUpdateRefMode,
212 SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
213 SCCOL nCol2, SCROW nRow2, SCTAB nTab2,
214 SCCOL nDx, SCROW nDy, SCTAB nDz);
216 void ExtendDataArea(const ScDocument* pDoc);
217 void CalcSaveFilteredCount(SCSIZE nNonFilteredRowCount);
218 void GetFilterSelCount(SCSIZE& nSelected, SCSIZE& nTotal);
220 private:
222 void AdjustTableColumnNames( UpdateRefMode eUpdateRefMode, SCCOL nDx, SCCOL nCol1,
223 SCCOL nOldCol1, SCCOL nOldCol2, SCCOL nNewCol1, SCCOL nNewCol2 );
224 void InvalidateTableColumnNames( bool bSwapToEmptyNames );
227 class SC_DLLPUBLIC ScDBCollection
229 public:
230 enum RangeType { GlobalNamed, GlobalAnonymous, SheetAnonymous };
233 * Stores global named database ranges.
235 class SC_DLLPUBLIC NamedDBs final : public ScDBDataContainerBase
237 friend class ScDBCollection;
239 typedef ::std::set<std::unique_ptr<ScDBData>, ScDBData::less> DBsType;
240 DBsType m_DBs;
241 ScDBCollection& mrParent;
242 NamedDBs(ScDBCollection& rParent, ScDocument& rDoc);
243 NamedDBs(const NamedDBs& r, ScDBCollection& rParent);
244 NamedDBs(const NamedDBs&) = delete;
245 virtual ~NamedDBs() override;
246 NamedDBs & operator=(NamedDBs const&) = delete;
247 void initInserted( ScDBData* p );
249 public:
250 typedef DBsType::iterator iterator;
251 typedef DBsType::const_iterator const_iterator;
253 iterator begin();
254 iterator end();
255 const_iterator begin() const;
256 const_iterator end() const;
257 ScDBData* findByIndex(sal_uInt16 nIndex);
258 ScDBData* findByUpperName(const OUString& rName);
259 iterator findByUpperName2(const OUString& rName);
261 /** Takes ownership of p and attempts to insert it into the collection.
262 Deletes p if it could not be inserted, i.e. duplicate name.
263 @return <TRUE/> if inserted, else <FALSE/>.
265 bool insert(std::unique_ptr<ScDBData> p);
267 void erase(const iterator& itr);
268 bool empty() const;
269 size_t size() const;
270 bool operator== (const NamedDBs& r) const;
274 * Stores global anonymous database ranges.
276 class AnonDBs
278 typedef ::std::vector<std::unique_ptr<ScDBData>> DBsType;
279 DBsType m_DBs;
281 AnonDBs& operator=(AnonDBs const&) = delete;
283 public:
284 AnonDBs();
285 AnonDBs(AnonDBs const&);
287 typedef DBsType::iterator iterator;
288 typedef DBsType::const_iterator const_iterator;
290 iterator begin();
291 iterator end();
292 const_iterator begin() const;
293 const_iterator end() const;
294 const ScDBData* findAtCursor(SCCOL nCol, SCROW nRow, SCTAB nTab, ScDBDataPortion ePortion) const;
295 const ScDBData* findByRange(const ScRange& rRange) const;
296 void deleteOnTab(SCTAB nTab);
297 ScDBData* getByRange(const ScRange& rRange);
298 void insert(ScDBData* p);
299 bool empty() const;
300 bool has( const ScDBData* p ) const;
301 bool operator== (const AnonDBs& r) const;
304 private:
305 Link<Timer *, void> aRefreshHandler;
306 ScDocument* pDoc;
307 sal_uInt16 nEntryIndex; ///< counter for unique indices
308 NamedDBs maNamedDBs;
309 AnonDBs maAnonDBs;
311 public:
312 ScDBCollection(ScDocument* pDocument);
313 ScDBCollection(const ScDBCollection& r);
315 NamedDBs& getNamedDBs() { return maNamedDBs;}
316 const NamedDBs& getNamedDBs() const { return maNamedDBs;}
318 AnonDBs& getAnonDBs() { return maAnonDBs;}
319 const AnonDBs& getAnonDBs() const { return maAnonDBs;}
321 const ScDBData* GetDBAtCursor(SCCOL nCol, SCROW nRow, SCTAB nTab, ScDBDataPortion ePortion) const;
322 ScDBData* GetDBAtCursor(SCCOL nCol, SCROW nRow, SCTAB nTab, ScDBDataPortion ePortion);
323 const ScDBData* GetDBAtArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) const;
324 ScDBData* GetDBAtArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
325 ScDBData* GetDBNearCursor(SCCOL nCol, SCROW nRow, SCTAB nTab );
327 void RefreshDirtyTableColumnNames();
329 void DeleteOnTab( SCTAB nTab );
330 void UpdateReference(UpdateRefMode eUpdateRefMode,
331 SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
332 SCCOL nCol2, SCROW nRow2, SCTAB nTab2,
333 SCCOL nDx, SCROW nDy, SCTAB nDz);
334 void UpdateMoveTab( SCTAB nOldPos, SCTAB nNewPos );
336 void SetRefreshHandler( const Link<Timer *, void>& rLink )
337 { aRefreshHandler = rLink; }
338 const Link<Timer *, void>& GetRefreshHandler() const { return aRefreshHandler; }
340 bool empty() const;
341 bool operator== (const ScDBCollection& r) const;
344 #endif
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */