1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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_CONDITIO_HXX
21 #define INCLUDED_SC_INC_CONDITIO_HXX
24 #include <tools/solar.h>
25 #include <tools/color.hxx>
26 #include "address.hxx"
27 #include <formula/grammar.hxx>
29 #include "rangelst.hxx"
30 #include "tokenarray.hxx"
32 #include <svl/listener.hxx>
34 #include <com/sun/star/sheet/ConditionOperator.hpp>
36 #include <rtl/math.hxx>
37 #include <tools/date.hxx>
38 #include <tools/link.hxx>
47 struct ScRefCellValue
;
51 struct RefUpdateContext
;
52 struct RefUpdateInsertTabContext
;
53 struct RefUpdateDeleteTabContext
;
54 struct RefUpdateMoveTabContext
;
59 #define SC_COND_NOBLANKS 1
61 enum class ScConditionMode
91 // For use in SAL_DEBUG etc. Output format not guaranteed to be stable.
92 template<typename charT
, typename traits
>
93 inline std::basic_ostream
<charT
, traits
> & operator <<(std::basic_ostream
<charT
, traits
> & stream
, const ScConditionMode
& rMode
)
97 case ScConditionMode::Equal
:
100 case ScConditionMode::Less
:
103 case ScConditionMode::Greater
:
106 case ScConditionMode::EqLess
:
109 case ScConditionMode::EqGreater
:
110 stream
<< "EQGREATER";
112 case ScConditionMode::NotEqual
:
113 stream
<< "NOTEQUAL";
115 case ScConditionMode::Between
:
118 case ScConditionMode::NotBetween
:
119 stream
<< "NOTBETWEEN";
121 case ScConditionMode::Duplicate
:
122 stream
<< "DUPLICATE";
124 case ScConditionMode::NotDuplicate
:
125 stream
<< "NOTDUPLICATE";
127 case ScConditionMode::Direct
:
130 case ScConditionMode::Top10
:
133 case ScConditionMode::Bottom10
:
134 stream
<< "BOTTOM10";
136 case ScConditionMode::TopPercent
:
137 stream
<< "TOPPERCENT";
139 case ScConditionMode::BottomPercent
:
140 stream
<< "BOTTOMPERCENT";
142 case ScConditionMode::AboveAverage
:
143 stream
<< "ABOVEAVERAGE";
145 case ScConditionMode::BelowAverage
:
146 stream
<< "BELOWAVERAGE";
148 case ScConditionMode::AboveEqualAverage
:
149 stream
<< "ABOVEEQUALAVERAGE";
151 case ScConditionMode::BelowEqualAverage
:
152 stream
<< "BELOWEQUALAVERAGE";
154 case ScConditionMode::Error
:
157 case ScConditionMode::NoError
:
160 case ScConditionMode::BeginsWith
:
161 stream
<< "BEGINSWITH";
163 case ScConditionMode::EndsWith
:
164 stream
<< "ENDSWITH";
166 case ScConditionMode::ContainsText
:
167 stream
<< "CONTAINSTEXT";
169 case ScConditionMode::NotContainsText
:
170 stream
<< "NOTCONTAINSTEXT";
172 case ScConditionMode::NONE
:
176 stream
<< "?(" << static_cast<int>(rMode
) << ")";
183 class ScFormulaListener final
: public SvtListener
186 mutable bool mbDirty
;
188 std::function
<void()> maCallbackFunction
;
190 void startListening(const ScTokenArray
* pTokens
, const ScRange
& rPos
);
191 void startListening(const ScRangeList
& rPos
);
194 explicit ScFormulaListener(ScFormulaCell
* pCell
);
195 explicit ScFormulaListener(ScDocument
& rDoc
);
196 explicit ScFormulaListener(ScDocument
& rDoc
, const ScRangeList
& rRange
);
197 virtual ~ScFormulaListener() override
;
199 void Notify( const SfxHint
& rHint
) override
;
201 bool NeedsRepaint() const;
203 void addTokenArray(const ScTokenArray
* pTokens
, const ScRange
& rRange
);
204 void stopListening();
205 void setCallback(const std::function
<void()>& aCallbackFunction
);
209 class ScConditionalFormat
;
210 struct ScDataBarInfo
;
211 struct ScIconSetInfo
;
213 struct ScCondFormatData
216 ScCondFormatData(ScCondFormatData
&&);
219 std::optional
<Color
> mxColorScale
;
220 std::unique_ptr
<ScDataBarInfo
> pDataBar
;
221 std::unique_ptr
<ScIconSetInfo
> pIconSet
;
225 class SC_DLLPUBLIC ScFormatEntry
228 ScFormatEntry(ScDocument
* pDoc
);
229 virtual ~ScFormatEntry() {}
241 virtual Type
GetType() const = 0;
242 virtual void UpdateReference( sc::RefUpdateContext
& rCxt
) = 0;
243 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
) = 0;
244 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
) = 0;
245 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
) = 0;
247 virtual ScFormatEntry
* Clone( ScDocument
* pDoc
) const = 0;
249 virtual void SetParent( ScConditionalFormat
* pNew
) = 0;
251 bool operator==( const ScFormatEntry
& ) const;
252 virtual bool IsEqual( const ScFormatEntry
&, bool bIgnoreSrcPos
) const;
254 virtual void startRendering();
255 virtual void endRendering();
261 template<typename charT
, typename traits
>
262 inline std::basic_ostream
<charT
, traits
> & operator <<(std::basic_ostream
<charT
, traits
> & stream
, const ScFormatEntry::Type
& rType
)
266 case ScFormatEntry::Type::Condition
:
267 stream
<< "Condition";
269 case ScFormatEntry::Type::ExtCondition
:
270 stream
<< "ExtCondition";
272 case ScFormatEntry::Type::Colorscale
:
273 stream
<< "Colorscale";
275 case ScFormatEntry::Type::Databar
:
278 case ScFormatEntry::Type::Iconset
:
281 case ScFormatEntry::Type::Date
:
285 stream
<< "?(" << static_cast<int>(rType
) << ")";
294 bool operator() (double nVal1
, double nVal2
) const
296 if(nVal1
< nVal2
&& !rtl::math::approxEqual(nVal1
, nVal2
))
303 class SC_DLLPUBLIC ScConditionEntry
: public ScFormatEntry
308 double nVal1
; // input or calculated
310 OUString aStrVal1
; // input or calculated
312 const OUString aStrNmsp1
; // namespace to be used on (re)compilation, e.g. in XML import
313 const OUString aStrNmsp2
; // namespace to be used on (re)compilation, e.g. in XML import
314 const formula::FormulaGrammar::Grammar eTempGrammar1
; // grammar to be used on (re)compilation, e.g. in XML import
315 const formula::FormulaGrammar::Grammar eTempGrammar2
; // grammar to be used on (re)compilation, e.g. in XML import
316 bool bIsStr1
; // for recognition of empty strings
318 std::unique_ptr
<ScTokenArray
> pFormula1
; // entered formula
319 std::unique_ptr
<ScTokenArray
> pFormula2
;
320 ScAddress aSrcPos
; // source position for formulas
322 OUString aSrcString
; // formula source position as text during XML import
323 std::unique_ptr
<ScFormulaCell
> pFCell1
;
324 std::unique_ptr
<ScFormulaCell
> pFCell2
;
328 std::unique_ptr
<ScFormulaListener
> mpListener
;
329 Type eConditionType
; //It can be Condition or ExtCondition
331 static void SimplifyCompiledFormula( std::unique_ptr
<ScTokenArray
>& rFormula
,
336 void MakeCells( const ScAddress
& rPos
);
337 void Compile( const OUString
& rExpr1
, const OUString
& rExpr2
,
338 const OUString
& rExprNmsp1
, const OUString
& rExprNmsp2
,
339 formula::FormulaGrammar::Grammar eGrammar1
,
340 formula::FormulaGrammar::Grammar eGrammar2
,
342 void Interpret( const ScAddress
& rPos
);
344 bool IsValid( double nArg
, const ScAddress
& rPos
) const;
345 bool IsValidStr( const OUString
& rArg
, const ScAddress
& rPos
) const;
346 void StartListening();
349 ScConditionEntry( ScConditionMode eOper
,
350 const OUString
& rExpr1
, const OUString
& rExpr2
,
351 ScDocument
& rDocument
, const ScAddress
& rPos
,
352 const OUString
& rExprNmsp1
, const OUString
& rExprNmsp2
,
353 formula::FormulaGrammar::Grammar eGrammar1
,
354 formula::FormulaGrammar::Grammar eGrammar2
,
355 Type eType
= Type::Condition
);
356 ScConditionEntry( ScConditionMode eOper
,
357 const ScTokenArray
* pArr1
, const ScTokenArray
* pArr2
,
358 ScDocument
& rDocument
, const ScAddress
& rPos
);
359 ScConditionEntry( const ScConditionEntry
& r
); // flat copy of formulas
360 // true copy of formulas (for Ref-Undo):
361 ScConditionEntry( ScDocument
& rDocument
, const ScConditionEntry
& r
);
362 virtual ~ScConditionEntry() override
;
364 bool IsEqual( const ScFormatEntry
& r
, bool bIgnoreSrcPos
) const override
;
366 virtual void SetParent( ScConditionalFormat
* pNew
) override
;
368 bool IsCellValid( ScRefCellValue
& rCell
, const ScAddress
& rPos
) const;
370 ScConditionMode
GetOperation() const { return eOp
; }
371 void SetOperation(ScConditionMode eMode
);
372 bool IsIgnoreBlank() const { return ( nOptions
& SC_COND_NOBLANKS
) == 0; }
373 void SetIgnoreBlank(bool bSet
);
374 const OUString
& GetSrcString() const { return aSrcString
; }
375 const ScAddress
& GetSrcPos() const { return aSrcPos
; }
377 ScAddress
GetValidSrcPos() const; // adjusted to allow textual representation of expressions
379 void SetSrcString( const OUString
& rNew
); // for XML import
381 void SetFormula1( const ScTokenArray
& rArray
);
382 void SetFormula2( const ScTokenArray
& rArray
);
384 OUString
GetExpression( const ScAddress
& rCursor
, sal_uInt16 nPos
, sal_uInt32 nNumFmt
= 0,
385 const formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_DEFAULT
) const;
387 /** Create a flat copy using ScTokenArray copy-ctor with
389 std::unique_ptr
<ScTokenArray
> CreateFlatCopiedTokenArray( sal_uInt16 nPos
) const;
393 virtual void UpdateReference( sc::RefUpdateContext
& rCxt
) override
;
394 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
) override
;
395 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
) override
;
396 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
) override
;
398 bool MarkUsedExternalReferences() const;
400 virtual Type
GetType() const override
{ return eConditionType
; }
402 virtual ScFormatEntry
* Clone(ScDocument
* pDoc
) const override
;
404 static ScConditionMode
GetModeFromApi(css::sheet::ConditionOperator nOperator
);
406 virtual void endRendering() override
;
407 virtual void startRendering() override
;
409 bool NeedsRepaint() const;
413 virtual void DataChanged() const;
414 ScDocument
* GetDocument() const { return mpDoc
; }
415 ScConditionalFormat
* pCondFormat
;
419 bool IsDuplicate(double nArg
, const OUString
& rStr
) const;
420 bool IsTopNElement( double nArg
) const;
421 bool IsTopNPercent( double nArg
) const;
422 bool IsBottomNElement( double nArg
) const;
423 bool IsBottomNPercent( double nArg
) const;
424 bool IsAboveAverage( double nArg
, bool bEqual
) const;
425 bool IsBelowAverage( double nArg
, bool bEqual
) const;
427 bool IsError( const ScAddress
& rPos
) const;
429 void FillCache() const;
431 struct ScConditionEntryCache
433 typedef std::map
<OUString
, sal_Int32
> StringCacheType
;
434 StringCacheType maStrings
;
435 typedef std::map
<double, sal_Int32
, approx_less
> ValueCacheType
;
436 ValueCacheType maValues
;
438 // cache them for easier access
441 ScConditionEntryCache():
445 mutable std::unique_ptr
<ScConditionEntryCache
> mpCache
;
448 // single condition entry for conditional formatting
449 class SC_DLLPUBLIC ScCondFormatEntry final
: public ScConditionEntry
452 Type eCondFormatType
= Type::Condition
;
455 ScCondFormatEntry( ScConditionMode eOper
,
456 const OUString
& rExpr1
, const OUString
& rExpr2
,
457 ScDocument
& rDocument
, const ScAddress
& rPos
,
458 const OUString
& rStyle
,
459 const OUString
& rExprNmsp1
= EMPTY_OUSTRING
,
460 const OUString
& rExprNmsp2
= EMPTY_OUSTRING
,
461 formula::FormulaGrammar::Grammar eGrammar1
= formula::FormulaGrammar::GRAM_DEFAULT
,
462 formula::FormulaGrammar::Grammar eGrammar2
= formula::FormulaGrammar::GRAM_DEFAULT
,
463 Type eType
= Type::Condition
);
464 ScCondFormatEntry( ScConditionMode eOper
,
465 const ScTokenArray
* pArr1
, const ScTokenArray
* pArr2
,
466 ScDocument
& rDocument
, const ScAddress
& rPos
,
467 const OUString
& rStyle
);
468 ScCondFormatEntry( const ScCondFormatEntry
& r
);
469 ScCondFormatEntry( ScDocument
& rDocument
, const ScCondFormatEntry
& r
);
470 virtual ~ScCondFormatEntry() override
;
472 bool IsEqual( const ScFormatEntry
& r
, bool bIgnoreSrcPos
) const override
;
474 const OUString
& GetStyle() const { return aStyleName
; }
475 void UpdateStyleName(const OUString
& rNew
) { aStyleName
=rNew
; }
476 virtual ScFormatEntry
* Clone(ScDocument
* pDoc
) const override
;
477 virtual Type
GetType() const override
{ return eCondFormatType
; }
480 virtual void DataChanged() const override
;
483 namespace condformat
{
485 enum ScCondFormatDateType
504 class SC_DLLPUBLIC ScCondDateFormatEntry final
: public ScFormatEntry
507 ScCondDateFormatEntry(ScDocument
* pDoc
);
508 ScCondDateFormatEntry(ScDocument
* pDoc
, const ScCondDateFormatEntry
& rEntry
);
510 bool IsValid( const ScAddress
& rPos
) const;
512 void SetDateType(condformat::ScCondFormatDateType eType
);
513 condformat::ScCondFormatDateType
GetDateType() const { return meType
;}
515 const OUString
& GetStyleName() const { return maStyleName
;}
516 void SetStyleName( const OUString
& rStyleName
);
518 virtual Type
GetType() const override
{ return Type::Date
; }
519 virtual void UpdateReference( sc::RefUpdateContext
& ) override
{}
520 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext
& ) override
{}
521 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& ) override
{}
522 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext
& ) override
{}
524 virtual ScFormatEntry
* Clone( ScDocument
* pDoc
) const override
;
526 virtual void SetParent( ScConditionalFormat
* ) override
{}
528 virtual void startRendering() override
;
529 virtual void endRendering() override
;
532 condformat::ScCondFormatDateType meType
;
534 mutable std::unique_ptr
<Date
> mpCache
;
536 OUString maStyleName
;
539 // complete conditional formatting
540 class SC_DLLPUBLIC ScConditionalFormat
543 sal_uInt32 nKey
; // Index in attributes
545 std::vector
<std::unique_ptr
<ScFormatEntry
>> maEntries
;
546 ScRangeList maRanges
; // Ranges for conditional format
549 ScConditionalFormat(sal_uInt32 nNewKey
, ScDocument
* pDocument
);
550 ~ScConditionalFormat();
551 ScConditionalFormat(const ScConditionalFormat
&) = delete;
552 const ScConditionalFormat
& operator=(const ScConditionalFormat
&) = delete;
554 // true copy of formulas (for Ref-Undo / between documents)
555 std::unique_ptr
<ScConditionalFormat
> Clone(ScDocument
* pNewDoc
= nullptr) const;
557 void AddEntry( ScFormatEntry
* pNew
);
558 void RemoveEntry(size_t nIndex
);
559 void SetRange( const ScRangeList
& rRanges
);
560 const ScRangeList
& GetRange() const { return maRanges
; }
561 // don't use the same name as for the const version
562 ScRangeList
& GetRangeList() { return maRanges
; }
564 bool IsEmpty() const;
567 ScDocument
* GetDocument();
571 void UpdateReference( sc::RefUpdateContext
& rCxt
, bool bCopyAsMove
= false );
572 void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
);
573 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
);
574 void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
);
576 void InsertRow(SCTAB nTab
, SCCOL nColStart
, SCCOL nColEnd
, SCROW nRowStart
, SCSIZE nSize
);
577 void InsertCol(SCTAB nTab
, SCROW nRowStart
, SCROW nRowEnd
, SCCOL nColStart
, SCSIZE nSize
);
579 void DeleteArea( SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
);
580 void RenameCellStyle( const OUString
& rOld
, const OUString
& rNew
);
582 const ScFormatEntry
* GetEntry( sal_uInt16 nPos
) const;
584 const OUString
& GetCellStyle( ScRefCellValue
& rCell
, const ScAddress
& rPos
) const;
586 ScCondFormatData
GetData( ScRefCellValue
& rCell
, const ScAddress
& rPos
) const;
588 bool EqualEntries( const ScConditionalFormat
& r
, bool bIgnoreSrcPos
= false ) const;
592 sal_uInt32
GetKey() const { return nKey
; }
593 void SetKey(sal_uInt32 nNew
) { nKey
= nNew
; } // only if not inserted!
595 bool MarkUsedExternalReferences() const;
597 // sorted (via std::set) by Index
598 bool operator < ( const ScConditionalFormat
& r
) const { return nKey
< r
.nKey
; }
600 void startRendering();
603 // Forced recalculation for formulas
607 struct CompareScConditionalFormat
609 using is_transparent
= void;
610 bool operator()(std::unique_ptr
<ScConditionalFormat
> const& lhs
,
611 std::unique_ptr
<ScConditionalFormat
> const& rhs
) const
613 return (*lhs
) < (*rhs
);
615 bool operator()(sal_uInt32 nKey
, std::unique_ptr
<ScConditionalFormat
> const& rpFormat
) const
617 return nKey
< rpFormat
->GetKey();
619 bool operator()(std::unique_ptr
<ScConditionalFormat
> const& rpFormat
, sal_uInt32 nKey
) const
621 return rpFormat
->GetKey() < nKey
;
625 // List of all conditional formats in a sheet
626 class SC_DLLPUBLIC ScConditionalFormatList
629 typedef std::set
<std::unique_ptr
<ScConditionalFormat
>,
630 CompareScConditionalFormat
> ConditionalFormatContainer
;
631 ConditionalFormatContainer m_ConditionalFormats
;
633 void operator =(ScConditionalFormatList
const &) = delete;
636 ScConditionalFormatList() {}
637 ScConditionalFormatList(const ScConditionalFormatList
& rList
);
638 ScConditionalFormatList(ScDocument
& rDoc
, const ScConditionalFormatList
& rList
);
640 void InsertNew( std::unique_ptr
<ScConditionalFormat
> pNew
);
643 * Checks that all cond formats have a non empty range.
644 * Deletes empty cond formats. Optionally call rLink
645 * on the empty format before deleting it.
646 * @return true if all cond formats were valid
648 bool CheckAllEntries(const Link
<ScConditionalFormat
*,void>& rLink
= Link
<ScConditionalFormat
*,void>());
650 ScConditionalFormat
* GetFormat( sal_uInt32 nKey
);
651 const ScConditionalFormat
* GetFormat( sal_uInt32 nKey
) const;
655 void UpdateReference( sc::RefUpdateContext
& rCxt
);
656 void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
);
657 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
);
658 void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
);
660 void InsertRow(SCTAB nTab
, SCCOL nColStart
, SCCOL nColEnd
, SCROW nRowStart
, SCSIZE nSize
);
661 void InsertCol(SCTAB nTab
, SCROW nRowStart
, SCROW nRowEnd
, SCCOL nColStart
, SCSIZE nSize
);
663 void RenameCellStyle( const OUString
& rOld
, const OUString
& rNew
);
664 void DeleteArea( SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
);
666 typedef ConditionalFormatContainer::iterator iterator
;
667 typedef ConditionalFormatContainer::const_iterator const_iterator
;
669 ScRangeList
GetCombinedRange() const;
671 void RemoveFromDocument(ScDocument
& rDoc
) const;
672 void AddToDocument(ScDocument
& rDoc
) const;
675 const_iterator
begin() const;
677 const_iterator
end() const;
682 void erase(sal_uLong nIndex
);
685 void startRendering();
688 sal_uInt32
getMaxKey() const;
690 /// Forced recalculation of formulas
696 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */