Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / colorscale.hxx
blobf43f533e8f603858542d58f86cfd19a257abdd97
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/.
8 */
10 #pragma once
12 #include <formula/grammar.hxx>
13 #include <tools/color.hxx>
14 #include "conditio.hxx"
15 #include "document.hxx"
17 #include <memory>
18 #include <vector>
19 #include <optional>
21 //TODO: merge this with conditio.hxx
23 class ScFormulaCell;
24 class ScTokenArray;
25 struct ScDataBarInfo;
26 class BitmapEx;
28 // don't change the order
29 // they are also used in the dialog to determine the position
30 // in the list box
31 enum ScColorScaleEntryType
33 COLORSCALE_AUTO,
34 COLORSCALE_MIN,
35 COLORSCALE_MAX,
36 COLORSCALE_PERCENTILE,
37 COLORSCALE_VALUE,
38 COLORSCALE_PERCENT,
39 COLORSCALE_FORMULA,
42 class SC_DLLPUBLIC ScColorScaleEntry
44 private:
45 double mnVal;
46 std::unique_ptr<ScFormulaCell> mpCell;
47 std::unique_ptr<ScFormulaListener> mpListener;
48 ScConditionalFormat* mpFormat;
49 Color maColor;
50 ScColorScaleEntryType meType;
52 void setListener();
54 public:
55 ScColorScaleEntry(double nVal, const Color& rCol, ScColorScaleEntryType eType = COLORSCALE_VALUE);
56 ScColorScaleEntry();
57 ScColorScaleEntry(const ScColorScaleEntry& rEntry);
58 ScColorScaleEntry(ScDocument* pDoc, const ScColorScaleEntry& rEntry);
59 ~ScColorScaleEntry() COVERITY_NOEXCEPT_FALSE;
61 const Color& GetColor() const { return maColor;}
62 void SetColor(const Color&);
63 double GetValue() const;
64 void SetValue(double nValue);
65 void SetFormula(const OUString& rFormula, ScDocument& rDoc, const ScAddress& rAddr,
66 formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT);
68 void UpdateReference( const sc::RefUpdateContext& rCxt );
69 void UpdateInsertTab( const sc::RefUpdateInsertTabContext& rCxt );
70 void UpdateDeleteTab( const sc::RefUpdateDeleteTabContext& rCxt );
71 void UpdateMoveTab( const sc::RefUpdateMoveTabContext& rCxt );
73 const ScTokenArray* GetFormula() const;
74 OUString GetFormula( formula::FormulaGrammar::Grammar eGrammar ) const;
76 ScColorScaleEntryType GetType() const { return meType;}
77 void SetType( ScColorScaleEntryType eType );
79 void SetRepaintCallback(ScConditionalFormat* pParent);
80 void SetRepaintCallback(const std::function<void()>& func);
83 namespace databar
86 enum ScAxisPosition
88 NONE,
89 AUTOMATIC,
90 MIDDLE
95 struct SC_DLLPUBLIC ScDataBarFormatData
97 ScDataBarFormatData():
98 maAxisColor(COL_BLACK),
99 mbGradient(true),
100 mbNeg(true),
101 meAxisPosition(databar::AUTOMATIC),
102 mnMinLength(0),
103 mnMaxLength(100),
104 mbOnlyBar(false),
105 mpUpperLimit(new ScColorScaleEntry()),
106 mpLowerLimit(new ScColorScaleEntry())
109 ScDataBarFormatData(const ScDataBarFormatData& r):
110 maPositiveColor(r.maPositiveColor),
111 maAxisColor(r.maAxisColor),
112 mbGradient(r.mbGradient),
113 mbNeg(r.mbNeg),
114 meAxisPosition(r.meAxisPosition),
115 mnMinLength(r.mnMinLength),
116 mnMaxLength(r.mnMaxLength),
117 mbOnlyBar(r.mbOnlyBar)
119 mxNegativeColor = r.mxNegativeColor;
121 if(r.mpLowerLimit)
122 mpLowerLimit.reset( new ScColorScaleEntry(*r.mpLowerLimit));
123 else
124 mpLowerLimit.reset(new ScColorScaleEntry());
125 if(r.mpUpperLimit)
126 mpUpperLimit.reset( new ScColorScaleEntry(*r.mpUpperLimit));
127 else
128 mpUpperLimit.reset(new ScColorScaleEntry());
132 * Color for all Positive Values and if mbNeg == false also for negative ones
134 Color maPositiveColor;
136 * Specifies the color for negative values. This is optional and depends on mbNeg.
138 * Default color is 0xFF0000, this value is not set
140 std::optional<Color> mxNegativeColor;
142 * Color of the axis if used
143 * Default color is black
145 Color maAxisColor;
147 * Paint the bars with gradient. If this is used the default is to draw with
148 * borders.
150 * Default is true
152 bool mbGradient;
154 * Use different color for negative values. Color is specified in
155 * mpNegativeColor and defaults to 0xFF0000
157 * Default is true
159 bool mbNeg; //differentiate between negative values
161 * Paint negative values into the same direction as positive values
162 * If false we will set the mid point according to the upper and lower limit and negative
163 * values are painted to the left and positive to the right
165 * Default is false
167 databar::ScAxisPosition meAxisPosition;
169 * Minimal length of a databar in percent of cell length
170 * Value has to be in the range [0, 100)
172 double mnMinLength;
174 * Maximal length of a databar in percent of cell length
175 * Value has to be in the range (0, 100]
177 double mnMaxLength;
180 * If TRUE we only show the bar and not the value
182 bool mbOnlyBar;
184 std::unique_ptr<ScColorScaleEntry, o3tl::default_delete<ScColorScaleEntry>> mpUpperLimit;
185 std::unique_ptr<ScColorScaleEntry, o3tl::default_delete<ScColorScaleEntry>> mpLowerLimit;
188 enum ScIconSetType
190 IconSet_3Arrows,
191 IconSet_3ArrowsGray,
192 IconSet_3Flags,
193 IconSet_3TrafficLights1,
194 IconSet_3TrafficLights2,
195 IconSet_3Signs,
196 IconSet_3Symbols,
197 IconSet_3Symbols2,
198 IconSet_3Smilies,
199 IconSet_3Stars,
200 IconSet_3Triangles,
201 IconSet_3ColorSmilies,
202 IconSet_4Arrows,
203 IconSet_4ArrowsGray,
204 IconSet_4RedToBlack,
205 IconSet_4Rating,
206 IconSet_4TrafficLights,
207 IconSet_5Arrows,
208 IconSet_5ArrowsGray,
209 IconSet_5Ratings,
210 IconSet_5Quarters,
211 IconSet_5Boxes
214 struct ScIconSetMap {
215 const char* pName;
216 ScIconSetType eType;
217 sal_Int32 nElements;
220 class SC_DLLPUBLIC ScColorFormat : public ScFormatEntry
222 public:
223 ScColorFormat(ScDocument* pDoc);
224 virtual ~ScColorFormat() override;
226 const ScRangeList& GetRange() const;
227 void SetCache(const std::vector<double>& aValues) const;
228 std::vector<double> GetCache() const;
230 virtual void SetParent(ScConditionalFormat* pParent) override;
232 virtual void startRendering() override;
233 virtual void endRendering() override;
234 virtual void updateValues() override;
236 protected:
237 std::vector<double>& getValues() const;
239 double getMinValue() const;
240 double getMaxValue() const;
242 ScConditionalFormat* mpParent;
245 typedef std::vector<std::unique_ptr<ScColorScaleEntry, o3tl::default_delete<ScColorScaleEntry>>> ScColorScaleEntries;
247 class SC_DLLPUBLIC ScColorScaleFormat final : public ScColorFormat
249 private:
250 ScColorScaleEntries maColorScales;
252 double GetMinValue() const;
253 double GetMaxValue() const;
255 void calcMinMax(double& nMin, double& nMax) const;
256 double CalcValue(double nMin, double nMax, const ScColorScaleEntries::const_iterator& rItr) const;
257 public:
258 ScColorScaleFormat(ScDocument* pDoc);
259 ScColorScaleFormat(ScDocument* pDoc, const ScColorScaleFormat& rFormat);
260 ScColorScaleFormat(const ScColorScaleFormat&) = delete;
261 virtual ~ScColorScaleFormat() override;
262 const ScColorScaleFormat& operator=(const ScColorScaleFormat&) = delete;
264 virtual ScColorFormat* Clone(ScDocument* pDoc) const override;
266 virtual void SetParent(ScConditionalFormat* pParent) override;
268 std::optional<Color> GetColor(const ScAddress& rAddr) const;
269 void AddEntry(ScColorScaleEntry* pEntry);
271 bool IsEqual(const ScFormatEntry& r, bool bIgnoreSrcPos) const override;
273 virtual void UpdateReference( sc::RefUpdateContext& rCxt ) override;
274 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt ) override;
275 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt ) override;
276 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt ) override;
278 virtual Type GetType() const override;
279 ScColorScaleEntries::iterator begin();
280 ScColorScaleEntries::const_iterator begin() const;
281 ScColorScaleEntries::iterator end();
282 ScColorScaleEntries::const_iterator end() const;
284 ScColorScaleEntry* GetEntry(size_t nPos);
285 const ScColorScaleEntry* GetEntry(size_t nPos) const;
287 size_t size() const;
290 * Makes sure that the maColorScales contain at least 2 entries.
291 * Should be called when it is not sure that the format is valid.
293 void EnsureSize();
296 class SC_DLLPUBLIC ScDataBarFormat final : public ScColorFormat
298 public:
299 ScDataBarFormat(ScDocument* pDoc);
300 ScDataBarFormat(ScDocument* pDoc, const ScDataBarFormat& rFormat);
301 virtual ScColorFormat* Clone(ScDocument* pDoc) const override;
303 virtual void SetParent(ScConditionalFormat* pParent) override;
305 std::unique_ptr<ScDataBarInfo> GetDataBarInfo(const ScAddress& rAddr) const;
307 void SetDataBarData( ScDataBarFormatData* pData );
308 const ScDataBarFormatData* GetDataBarData() const;
309 ScDataBarFormatData* GetDataBarData();
311 bool IsEqual(const ScFormatEntry& r, bool bIgnoreSrcPos) const override;
313 virtual void UpdateReference( sc::RefUpdateContext& rCxt ) override;
314 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt ) override;
315 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt ) override;
316 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt ) override;
318 virtual Type GetType() const override;
321 * Makes sure that the mpFormatData does not contain valid entries.
322 * Should be called when it is not sure that the format is valid.
324 void EnsureSize();
326 private:
327 double getMin(double nMin, double nMax) const;
328 double getMax(double nMin, double nMax) const;
330 std::unique_ptr<ScDataBarFormatData> mpFormatData;
333 struct ScIconSetFormatData
335 ScIconSetType eIconSetType;
336 bool mbShowValue;
338 * Specifies whether the icons should be shown in reverse order
340 bool mbReverse;
341 typedef std::vector<std::unique_ptr<ScColorScaleEntry, o3tl::default_delete<ScColorScaleEntry>>> Entries_t;
342 Entries_t m_Entries;
343 bool mbCustom;
344 // the std::pair points to exactly one image
345 // std..pair::second == -1 means no image
346 std::vector<std::pair<ScIconSetType, sal_Int32> > maCustomVector;
348 ScIconSetFormatData(ScIconSetType eType = IconSet_3Arrows):
349 eIconSetType(eType),
350 mbShowValue(true),
351 mbReverse(false),
352 mbCustom(false)
355 ScIconSetFormatData(ScIconSetFormatData const&);
356 ScIconSetFormatData& operator=(ScIconSetFormatData const&) = delete; //TODO?
359 class SC_DLLPUBLIC ScIconSetFormat final : public ScColorFormat
361 public:
362 ScIconSetFormat(ScDocument* pDoc);
363 ScIconSetFormat(ScDocument* pDoc, const ScIconSetFormat& rFormat);
365 virtual ScColorFormat* Clone(ScDocument* pDoc) const override;
367 virtual void SetParent(ScConditionalFormat* pParent) override;
369 std::unique_ptr<ScIconSetInfo> GetIconSetInfo(const ScAddress& rAddr) const;
371 void SetIconSetData( ScIconSetFormatData* pData );
372 const ScIconSetFormatData* GetIconSetData() const;
373 ScIconSetFormatData* GetIconSetData();
375 virtual void UpdateReference( sc::RefUpdateContext& rCxt ) override;
376 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt ) override;
377 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt ) override;
378 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt ) override;
380 virtual Type GetType() const override;
382 static const ScIconSetMap g_IconSetMap[];
383 static const char* getIconSetName( ScIconSetType eType );
384 static sal_Int32 getIconSetElements( ScIconSetType eType );
385 static OUString getIconName(ScIconSetType eType, sal_Int32 nIndex);
386 static BitmapEx& getBitmap(sc::IconSetBitmapMap& rBitmapMap, ScIconSetType eType, sal_Int32 nIndex);
388 typedef ScIconSetFormatData::Entries_t::iterator iterator;
389 typedef ScIconSetFormatData::Entries_t::const_iterator const_iterator;
391 iterator begin();
392 const_iterator begin() const;
393 iterator end();
394 const_iterator end() const;
396 size_t size() const;
399 * Makes sure that the mpFormatData does not contain valid entries.
400 * Should be called when it is not sure that the format is valid.
402 void EnsureSize();
404 private:
406 double GetMinValue() const;
407 double GetMaxValue() const;
408 double CalcValue(double nMin, double nMax, const ScIconSetFormat::const_iterator& itr) const;
410 std::unique_ptr<ScIconSetFormatData> mpFormatData;
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */