sc: extend backcolor area
[LibreOffice.git] / sc / inc / dbdata.hxx
blobe03e41f6562a49893b5a072e20d730b61b95da9d
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 #pragma once
22 #include "scdllapi.h"
23 #include "refreshtimer.hxx"
24 #include "address.hxx"
25 #include "global.hxx"
26 #include "rangelst.hxx"
28 #include <svl/listener.hxx>
30 #include <memory>
31 #include <set>
32 #include <vector>
34 class ScDocument;
35 struct ScSortParam;
36 struct ScQueryParam;
37 struct ScSubTotalParam;
39 /** Enum used to indicate which portion of the DBArea is to be considered. */
40 enum class ScDBDataPortion
42 TOP_LEFT, ///< top left cell of area
43 AREA ///< entire area
46 /** Container base class to provide selected access for ScDBData. */
47 class ScDBDataContainerBase
49 public:
50 ScDBDataContainerBase( ScDocument& rDoc ) : mrDoc(rDoc) {}
51 virtual ~ScDBDataContainerBase() {}
52 ScDocument& GetDocument() const;
53 ScRangeList& GetDirtyTableColumnNames();
55 protected:
56 ScDocument& mrDoc;
57 ScRangeList maDirtyTableColumnNames;
60 class SAL_DLLPUBLIC_RTTI ScDBData final : public SvtListener, public ScRefreshTimer
62 private:
63 std::unique_ptr<ScSortParam> mpSortParam;
64 std::unique_ptr<ScQueryParam> mpQueryParam;
65 std::unique_ptr<ScSubTotalParam> mpSubTotal;
66 std::unique_ptr<ScImportParam> mpImportParam;
68 ScDBDataContainerBase* mpContainer;
70 /// DBParam
71 const OUString aName;
72 OUString aUpper;
73 SCTAB nTable;
74 SCCOL nStartCol;
75 SCROW nStartRow;
76 SCCOL nEndCol;
77 SCROW nEndRow;
78 bool bByRow;
79 bool bHasHeader;
80 bool bHasTotals;
81 bool bDoSize;
82 bool bKeepFmt;
83 bool bStripData;
85 /// QueryParam
86 bool bIsAdvanced; ///< true if created by advanced filter
87 ScRange aAdvSource; ///< source range
89 bool bDBSelection; ///< not in Param: if selection, block update
91 sal_uInt16 nIndex; ///< unique index formulas
92 bool bAutoFilter; ///< AutoFilter? (not saved)
93 bool bModified; ///< is set/cleared for/by(?) UpdateReference
95 ::std::vector< OUString > maTableColumnNames; ///< names of table columns
96 bool mbTableColumnNamesDirty;
97 SCSIZE nFilteredRowCount;
99 using ScRefreshTimer::operator==;
101 public:
102 struct less
104 bool operator() (const std::unique_ptr<ScDBData>& left, const std::unique_ptr<ScDBData>& right) const;
107 SC_DLLPUBLIC ScDBData(const OUString& rName,
108 SCTAB nTab,
109 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2,
110 bool bByR = true, bool bHasH = true, bool bTotals = false);
111 ScDBData(const ScDBData& rData);
112 ScDBData(const OUString& rName, const ScDBData& rData);
113 SC_DLLPUBLIC virtual ~ScDBData() override;
115 virtual void Notify( const SfxHint& rHint ) override;
117 ScDBData& operator= (const ScDBData& rData) ;
119 bool operator== (const ScDBData& rData) const;
121 const OUString& GetName() const { return aName; }
122 const OUString& GetUpperName() const { return aUpper; }
123 SCTAB GetTab() const { return nTable; }
124 void GetArea(SCTAB& rTab, SCCOL& rCol1, SCROW& rRow1, SCCOL& rCol2, SCROW& rRow2) const;
125 SC_DLLPUBLIC void GetArea(ScRange& rRange) const;
126 void SetArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
127 void MoveTo(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2,
128 SCCOL nUpdateCol = -1);
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( ::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 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 bool UpdateReference(const ScDocument* pDoc, UpdateRefMode eUpdateRefMode, SCCOL nCol1,
212 SCROW nRow1, SCTAB nTab1, SCCOL nCol2, SCROW nRow2, SCTAB nTab2,
213 SCCOL nDx, SCROW nDy, SCTAB nDz);
215 void ExtendDataArea(const ScDocument& rDoc);
216 void ExtendBackColorArea(const ScDocument& rDoc);
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 iterator 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 SAL_DLLPRIVATE 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 iterator erase(const iterator& itr);
300 bool empty() const;
301 bool has( const ScDBData* p ) const;
302 bool operator== (const AnonDBs& r) const;
305 private:
306 Link<Timer *, void> aRefreshHandler;
307 ScDocument& rDoc;
308 sal_uInt16 nEntryIndex; ///< counter for unique indices
309 NamedDBs maNamedDBs;
310 AnonDBs maAnonDBs;
312 public:
313 ScDBCollection(ScDocument& rDocument);
314 ScDBCollection(const ScDBCollection& r);
316 NamedDBs& getNamedDBs() { return maNamedDBs;}
317 const NamedDBs& getNamedDBs() const { return maNamedDBs;}
319 AnonDBs& getAnonDBs() { return maAnonDBs;}
320 const AnonDBs& getAnonDBs() const { return maAnonDBs;}
322 const ScDBData* GetDBAtCursor(SCCOL nCol, SCROW nRow, SCTAB nTab, ScDBDataPortion ePortion) const;
323 ScDBData* GetDBAtCursor(SCCOL nCol, SCROW nRow, SCTAB nTab, ScDBDataPortion ePortion);
324 const ScDBData* GetDBAtArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) const;
325 ScDBData* GetDBAtArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
326 ScDBData* GetDBNearCursor(SCCOL nCol, SCROW nRow, SCTAB nTab );
327 std::vector<ScDBData*> GetAllDBsFromTab(SCTAB nTab);
329 void RefreshDirtyTableColumnNames();
331 void DeleteOnTab( SCTAB nTab );
332 void UpdateReference(UpdateRefMode eUpdateRefMode,
333 SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
334 SCCOL nCol2, SCROW nRow2, SCTAB nTab2,
335 SCCOL nDx, SCROW nDy, SCTAB nDz);
336 void UpdateMoveTab( SCTAB nOldPos, SCTAB nNewPos );
338 void SetRefreshHandler( const Link<Timer *, void>& rLink )
339 { aRefreshHandler = rLink; }
340 const Link<Timer *, void>& GetRefreshHandler() const { return aRefreshHandler; }
342 bool empty() const;
343 bool operator== (const ScDBCollection& r) const;
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */