Branch libreoffice-24-8-3
[LibreOffice.git] / sc / inc / dociter.hxx
blob190d284a39e89858967091d889dcb7c0a58fa382
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 "address.hxx"
23 #include "formulagroup.hxx"
24 #include "global.hxx"
25 #include "scdllapi.h"
26 #include "cellvalue.hxx"
27 #include "mtvelements.hxx"
28 #include "queryparam.hxx"
29 #include "attarray.hxx"
30 #include <vcl/outdev.hxx>
31 #include <vcl/vclptr.hxx>
33 #include <memory>
34 #include <set>
35 #include <vector>
36 #include <optional>
38 class ScDocument;
39 class ScPatternAttr;
40 class ScAttrArray;
41 class ScAttrIterator;
42 class ScFlatBoolRowSegments;
43 class ScMatrix;
44 struct ScDBQueryParamBase;
45 struct ScQueryParam;
46 struct ScDBQueryParamInternal;
47 struct ScDBQueryParamMatrix;
48 class ScFormulaCell;
49 class OutputDevice;
50 struct ScInterpreterContext;
51 enum class SvNumFormatType : sal_Int16;
53 class ScValueIterator // walk through all values in an area
55 typedef sc::CellStoreType::const_position_type PositionType;
57 const ScDocument& mrDoc;
58 ScInterpreterContext& mrContext;
59 const ScAttrArray* pAttrArray;
60 sal_uInt32 nNumFormat; // for CalcAsShown
61 sal_uInt32 nNumFmtIndex;
62 ScAddress maStartPos;
63 ScAddress maEndPos;
64 SCCOL mnCol;
65 SCTAB mnTab;
66 SCROW nAttrEndRow;
67 SubtotalFlags mnSubTotalFlags;
68 SvNumFormatType nNumFmtType;
69 bool bNumValid;
70 bool bCalcAsShown;
71 bool bTextAsZero;
73 const sc::CellStoreType* mpCells;
74 PositionType maCurPos;
76 SCROW GetRow() const;
77 void IncBlock();
78 void IncPos();
80 /**
81 * See if the cell at the current position is a non-empty cell. If not,
82 * move to the next non-empty cell position.
84 bool GetThis( double& rValue, FormulaError& rErr );
86 public:
88 ScValueIterator(ScInterpreterContext& rContext,
89 const ScRange& rRange, SubtotalFlags nSubTotalFlags = SubtotalFlags::NONE,
90 bool bTextAsZero = false );
92 void GetCurNumFmtInfo( SvNumFormatType& nType, sal_uInt32& nIndex );
94 /// Does NOT reset rValue if no value found!
95 bool GetFirst( double& rValue, FormulaError& rErr );
97 /// Does NOT reset rValue if no value found!
98 bool GetNext( double& rValue, FormulaError& rErr );
101 class ScDBQueryDataIterator
103 public:
104 struct Value
106 OUString maString;
107 double mfValue;
108 FormulaError mnError;
109 bool mbIsNumber;
111 Value();
114 private:
115 static const sc::CellStoreType* GetColumnCellStore(ScDocument& rDoc, SCTAB nTab, SCCOL nCol);
116 static const ScAttrArray* GetAttrArrayByCol(ScDocument& rDoc, SCTAB nTab, SCCOL nCol);
117 static bool IsQueryValid(ScDocument& rDoc, const ScQueryParam& rParam, SCTAB nTab, SCROW nRow, const ScRefCellValue* pCell);
119 class DataAccess
121 public:
122 DataAccess();
123 virtual ~DataAccess() = 0;
124 virtual bool getCurrent(Value& rValue) = 0;
125 virtual bool getFirst(Value& rValue) = 0;
126 virtual bool getNext(Value& rValue) = 0;
129 class DataAccessInternal final : public DataAccess
131 typedef std::pair<sc::CellStoreType::const_iterator,size_t> PositionType;
132 public:
133 DataAccessInternal(ScDBQueryParamInternal* pParam, ScDocument& rDoc, const ScInterpreterContext& rContext);
134 virtual ~DataAccessInternal() override;
135 virtual bool getCurrent(Value& rValue) override;
136 virtual bool getFirst(Value& rValue) override;
137 virtual bool getNext(Value& rValue) override;
139 private:
140 void incBlock();
141 void incPos();
143 const sc::CellStoreType* mpCells;
144 PositionType maCurPos;
145 ScDBQueryParamInternal* mpParam;
146 ScDocument& mrDoc;
147 const ScInterpreterContext& mrContext;
148 const ScAttrArray* pAttrArray;
149 sal_uInt32 nNumFormat; // for CalcAsShown
150 sal_uInt32 nNumFmtIndex;
151 SCCOL nCol;
152 SCROW nRow;
153 SCROW nAttrEndRow;
154 SCTAB nTab;
155 SvNumFormatType nNumFmtType;
156 bool bCalcAsShown;
159 class DataAccessMatrix final : public DataAccess
161 public:
162 DataAccessMatrix(ScDBQueryParamMatrix* pParam);
163 virtual ~DataAccessMatrix() override;
164 virtual bool getCurrent(Value& rValue) override;
165 virtual bool getFirst(Value& rValue) override;
166 virtual bool getNext(Value& rValue) override;
168 private:
169 bool isValidQuery(SCROW mnRow, const ScMatrix& rMat) const;
171 ScDBQueryParamMatrix* mpParam;
172 SCROW mnCurRow;
173 SCROW mnRows;
176 ::std::unique_ptr<ScDBQueryParamBase> mpParam;
177 ::std::unique_ptr<DataAccess> mpData;
179 public:
180 ScDBQueryDataIterator(ScDocument& rDocument, const ScInterpreterContext& rContext, std::unique_ptr<ScDBQueryParamBase> pParam);
181 /// Does NOT reset rValue if no value found!
182 bool GetFirst(Value& rValue);
183 /// Does NOT reset rValue if no value found!
184 bool GetNext(Value& rValue);
187 class ScFormulaGroupIterator
189 private:
190 ScDocument& mrDoc;
191 SCTAB mnTab;
192 SCCOL mnCol;
193 bool mbNullCol;
194 size_t mnIndex;
195 std::vector<sc::FormulaGroupEntry> maEntries;
197 public:
198 ScFormulaGroupIterator( ScDocument& rDoc );
200 sc::FormulaGroupEntry* first();
201 sc::FormulaGroupEntry* next();
205 * Walk through all cells in an area. For SubTotal and Aggregate depending on mnSubTotalFlags.
207 class ScCellIterator
209 typedef std::pair<sc::CellStoreType::const_iterator, size_t> PositionType;
211 ScDocument& mrDoc;
212 ScAddress maStartPos;
213 ScAddress maEndPos;
214 ScAddress maCurPos;
216 PositionType maCurColPos;
217 SubtotalFlags mnSubTotalFlags;
219 ScRefCellValue maCurCell;
221 void incBlock();
222 void incPos();
223 void setPos(size_t nPos);
225 const ScColumn* getColumn() const;
227 void init();
228 bool getCurrent();
230 public:
231 ScCellIterator( ScDocument& rDoc, const ScRange& rRange, SubtotalFlags nSubTotalFlags = SubtotalFlags::NONE );
233 const ScAddress& GetPos() const { return maCurPos; }
235 CellType getType() const { return maCurCell.getType();}
236 OUString getString() const;
237 const EditTextObject* getEditText() const { return maCurCell.getEditText();}
238 ScFormulaCell* getFormulaCell() { return maCurCell.getFormula();}
239 const ScFormulaCell* getFormulaCell() const { return maCurCell.getFormula();}
240 ScCellValue getCellValue() const;
241 const ScRefCellValue& getRefCellValue() const { return maCurCell;}
243 bool hasString() const;
244 bool isEmpty() const;
245 bool equalsWithoutFormat( const ScAddress& rPos ) const;
247 bool first();
248 bool next();
251 class ScDocAttrIterator // all attribute areas
253 private:
254 ScDocument& rDoc;
255 SCTAB nTab;
256 SCCOL nEndCol;
257 SCROW nStartRow;
258 SCROW nEndRow;
259 SCCOL nCol;
260 std::optional<ScAttrIterator> moColIter;
262 public:
263 ScDocAttrIterator(ScDocument& rDocument, SCTAB nTable,
264 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
266 const ScPatternAttr* GetNext( SCCOL& rCol, SCROW& rRow1, SCROW& rRow2 );
269 class ScAttrRectIterator // all attribute areas, including areas stretching
270 // across more than one column
272 private:
273 ScDocument& rDoc;
274 SCTAB nTab;
275 SCCOL nEndCol;
276 SCROW nStartRow;
277 SCROW nEndRow;
278 SCCOL nIterStartCol;
279 SCCOL nIterEndCol;
280 std::optional<ScAttrIterator>
281 moColIter;
283 public:
284 ScAttrRectIterator(ScDocument& rDocument, SCTAB nTable,
285 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
287 void DataChanged();
288 const ScPatternAttr* GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow1, SCROW& rRow2 );
291 class ScHorizontalCellIterator // walk through all non empty cells in an area
292 { // row by row
293 struct ColParam
295 sc::CellStoreType::const_iterator maPos;
296 sc::CellStoreType::const_iterator maEnd;
297 SCCOL mnCol;
300 std::vector<ColParam>::iterator maColPos;
301 std::vector<ColParam> maColPositions;
303 ScDocument& rDoc;
304 SCTAB mnTab;
305 SCCOL nStartCol;
306 SCCOL nEndCol;
307 SCROW nStartRow;
308 SCROW nEndRow;
309 SCCOL mnCol;
310 SCROW mnRow;
311 ScRefCellValue maCurCell;
312 bool mbMore;
314 public:
315 ScHorizontalCellIterator(ScDocument& rDocument, SCTAB nTable,
316 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
317 ~ScHorizontalCellIterator();
319 ScRefCellValue* GetNext( SCCOL& rCol, SCROW& rRow );
320 bool GetPos( SCCOL& rCol, SCROW& rRow );
321 /// Set a(nother) sheet and (re)init.
322 void SetTab( SCTAB nTab );
324 private:
325 void Advance();
326 void SkipInvalid();
327 bool SkipInvalidInRow();
328 SCROW FindNextNonEmptyRow();
331 /** Row-wise value iterator. */
332 class ScHorizontalValueIterator
334 private:
335 ScDocument& rDoc;
336 const ScAttrArray* pAttrArray;
337 std::unique_ptr<ScHorizontalCellIterator>
338 pCellIter;
339 sal_uInt32 nNumFormat; // for CalcAsShown
340 SCTAB nEndTab;
341 SCCOL nCurCol;
342 SCROW nCurRow;
343 SCTAB nCurTab;
344 SCROW nAttrEndRow;
345 bool bCalcAsShown;
347 public:
349 ScHorizontalValueIterator( ScDocument& rDocument,
350 const ScRange& rRange );
351 ~ScHorizontalValueIterator();
352 /// Does NOT reset rValue if no value found!
353 bool GetNext( double& rValue, FormulaError& rErr );
356 class ScHorizontalAttrIterator
358 private:
359 ScDocument& rDoc;
360 SCTAB nTab;
361 SCCOL nStartCol;
362 SCROW nStartRow;
363 SCCOL nEndCol;
364 SCROW nEndRow;
366 std::unique_ptr<SCROW[]> pNextEnd;
367 std::unique_ptr<SCCOL[]> pHorizEnd;
368 std::unique_ptr<SCSIZE[]> pIndices;
369 std::unique_ptr<const ScPatternAttr*[]>
370 ppPatterns;
371 SCCOL nCol;
372 SCROW nRow;
373 SCROW nMinNextEnd;
375 void InitForNextRow(bool bInitialization);
377 public:
378 ScHorizontalAttrIterator( ScDocument& rDocument, SCTAB nTable,
379 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 );
380 ~ScHorizontalAttrIterator();
382 const ScPatternAttr* GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow );
385 // returns non-empty cells and areas with formatting (horizontal)
387 class SC_DLLPUBLIC ScUsedAreaIterator
389 private:
390 ScHorizontalCellIterator aCellIter;
391 ScHorizontalAttrIterator aAttrIter;
393 SCCOL nNextCol;
394 SCROW nNextRow;
396 SCCOL nCellCol;
397 SCROW nCellRow;
398 ScRefCellValue* pCell;
399 SCCOL nAttrCol1;
400 SCCOL nAttrCol2;
401 SCROW nAttrRow;
402 const ScPatternAttr* pPattern;
404 SCCOL nFoundStartCol; // results after GetNext
405 SCCOL nFoundEndCol;
406 SCROW nFoundRow;
407 const ScPatternAttr* pFoundPattern;
409 ScRefCellValue maFoundCell;
411 public:
412 ScUsedAreaIterator( ScDocument& rDocument, SCTAB nTable,
413 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 );
414 ~ScUsedAreaIterator();
416 bool GetNext();
418 SCCOL GetStartCol() const { return nFoundStartCol; }
419 SCCOL GetEndCol() const { return nFoundEndCol; }
420 SCROW GetRow() const { return nFoundRow; }
421 const ScPatternAttr* GetPattern() const { return pFoundPattern; }
422 const ScRefCellValue& GetCell() const { return maFoundCell;}
425 class ScRowBreakIterator
427 public:
428 static constexpr SCROW NOT_FOUND = -1;
430 explicit ScRowBreakIterator(::std::set<SCROW>& rBreaks);
431 SCROW first();
432 SCROW next();
434 private:
435 ::std::set<SCROW>& mrBreaks;
436 ::std::set<SCROW>::const_iterator maItr;
437 ::std::set<SCROW>::const_iterator maEnd;
440 class ScDocRowHeightUpdater
442 public:
443 struct TabRanges
445 SCTAB mnTab;
446 ScFlatBoolRowSegments maRanges;
448 TabRanges(SCTAB nTab, SCROW nMaxRow);
452 * Passing a NULL pointer to pTabRangesArray forces the heights of all
453 * rows in all tables to be updated.
455 explicit ScDocRowHeightUpdater(
456 ScDocument& rDoc, OutputDevice* pOutDev, double fPPTX, double fPPTY,
457 const ::std::vector<TabRanges>* pTabRangesArray);
459 void update(const bool bOnlyUsedRows = false);
461 private:
462 void updateAll(const bool bOnlyUsedRows);
464 private:
465 ScDocument& mrDoc;
466 VclPtr<OutputDevice> mpOutDev;
467 double mfPPTX;
468 double mfPPTY;
469 const ::std::vector<TabRanges>* mpTabRangesArray;
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */