LibreOffice Module sc (master) 1
condformathelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10#include <sal/config.h>
11
12#include <o3tl/safeint.hxx>
13#include <rtl/ustrbuf.hxx>
14#include <condformathelper.hxx>
15#include <globstr.hrc>
16#include <scresid.hxx>
17#include <conditio.hxx>
18
19namespace {
20
21OUString getTextForType(ScCondFormatEntryType eType)
22{
23 switch(eType)
24 {
25 case CONDITION:
26 return ScResId(STR_COND_CONDITION);
27 case COLORSCALE:
28 return ScResId(STR_COND_COLORSCALE);
29 case DATABAR:
30 return ScResId(STR_COND_DATABAR);
31 case FORMULA:
32 return ScResId(STR_COND_FORMULA);
33 case ICONSET:
34 return ScResId(STR_COND_ICONSET);
35 case DATE:
36 return ScResId(STR_COND_DATE);
37 default:
38 break;
39 }
40
41 return OUString();
42}
43
44OUString getExpression(sal_Int32 nIndex)
45{
46 switch(nIndex)
47 {
48 case 0:
49 return "=";
50 case 1:
51 return "<";
52 case 2:
53 return ">";
54 case 3:
55 return "<=";
56 case 4:
57 return ">=";
58 case 5:
59 return "!=";
60 case 6:
61 return ScResId(STR_COND_BETWEEN);
62 case 7:
63 return ScResId(STR_COND_NOTBETWEEN);
64 case 8:
65 return ScResId(STR_COND_DUPLICATE);
66 case 9:
67 return ScResId(STR_COND_UNIQUE);
68
69 case 11:
70 return ScResId(STR_COND_TOP10);
71 case 12:
72 return ScResId(STR_COND_BOTTOM10);
73 case 13:
74 return ScResId(STR_COND_TOP_PERCENT);
75 case 14:
76 return ScResId(STR_COND_BOTTOM_PERCENT);
77 case 15:
78 return ScResId(STR_COND_ABOVE_AVERAGE);
79 case 16:
80 return ScResId(STR_COND_BELOW_AVERAGE);
81 case 17:
82 return ScResId(STR_COND_ABOVE_EQUAL_AVERAGE);
83 case 18:
84 return ScResId(STR_COND_BELOW_EQUAL_AVERAGE);
85 case 19:
86 return ScResId(STR_COND_ERROR);
87 case 20:
88 return ScResId(STR_COND_NOERROR);
89 case 21:
90 return ScResId(STR_COND_BEGINS_WITH);
91 case 22:
92 return ScResId(STR_COND_ENDS_WITH);
93 case 23:
94 return ScResId(STR_COND_CONTAINS);
95 case 24:
96 return ScResId(STR_COND_NOT_CONTAINS);
97
98 case 10:
99 assert(false);
100 }
101 return OUString();
102}
103
104OUString getDateString(sal_Int32 nIndex)
105{
106 const TranslateId aCondStrs[] =
107 {
108 STR_COND_TODAY,
109 STR_COND_YESTERDAY,
110 STR_COND_TOMORROW,
111 STR_COND_LAST7DAYS,
112 STR_COND_THISWEEK,
113 STR_COND_LASTWEEK,
114 STR_COND_NEXTWEEK,
115 STR_COND_THISMONTH,
116 STR_COND_LASTMONTH,
117 STR_COND_NEXTMONTH,
118 STR_COND_THISYEAR,
119 STR_COND_LASTYEAR,
120 STR_COND_NEXTYEAR
121 };
122
123 if (nIndex >= 0 && o3tl::make_unsigned(nIndex) < SAL_N_ELEMENTS(aCondStrs))
124 return ScResId(aCondStrs[nIndex]);
125 assert(false);
126 return OUString();
127}
128
129}
130
132{
133 OUStringBuffer aBuffer;
134 if(!rFormat.IsEmpty())
135 {
136 switch(rFormat.GetEntry(0)->GetType())
137 {
140 {
141 const ScConditionEntry* pEntry = static_cast<const ScConditionEntry*>(rFormat.GetEntry(0));
144 {
145 aBuffer.append(getTextForType(FORMULA)
146 + " "
147 + pEntry->GetExpression(rPos, 0));
148 }
149 else
150 {
151 aBuffer.append(getTextForType(CONDITION)
152 + " "
153 + getExpression(static_cast<sal_Int32>(eMode))
154 + " ");
156 {
157 aBuffer.append(pEntry->GetExpression(rPos, 0)
158 + " "
159 + ScResId(STR_COND_AND)
160 + " "
161 + pEntry->GetExpression(rPos, 1));
162 }
163 else if(eMode <= ScConditionMode::NotEqual || eMode >= ScConditionMode::BeginsWith)
164 {
165 aBuffer.append(pEntry->GetExpression(rPos, 0));
166 }
167 }
168 }
169
170 break;
172 aBuffer.append(getTextForType(DATABAR));
173 break;
175 aBuffer.append(getTextForType(COLORSCALE));
176 break;
178 aBuffer.append(getTextForType(ICONSET));
179 break;
181 {
182 sal_Int32 nDateEntry = static_cast<sal_Int32>(static_cast<const ScCondDateFormatEntry*>(rFormat.GetEntry(0))->GetDateType());
183 aBuffer.append(getTextForType(DATE)
184 + " "
185 + getDateString(nDateEntry));
186 }
187 break;
188 }
189 }
190 return aBuffer.makeStringAndClear();
191}
192
194 std::u16string_view aStr1, std::u16string_view aStr2 )
195{
196 OUStringBuffer aBuffer(getTextForType(eType) + " ");
197 if(eType == CONDITION)
198 {
199 // workaround missing FORMULA option in the conditions case
200 // FORMULA is handled later
201 if(nIndex > 9)
202 ++nIndex;
204 if(nIndex <= 7 || nIndex >= 19)
205 {
206 aBuffer.append(OUString::Concat(" ") + aStr1);
207 if(nIndex == 6 || nIndex == 7)
208 {
209 aBuffer.append(" " + ScResId(STR_COND_AND) + " " + aStr2);
210 }
211 }
212 }
213 else if(eType == FORMULA)
214 {
215 aBuffer.append(OUString::Concat(" ") + aStr1);
216 }
217 else if(eType == DATE)
218 {
219 aBuffer.append(getDateString(nIndex));
220 }
221
222 return aBuffer.makeStringAndClear();
223}
224
225/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SC_DLLPUBLIC OUString GetExpression(const ScConditionalFormat &rFormat, const ScAddress &rPos)
ScConditionMode GetOperation() const
Definition: conditio.hxx:369
OUString GetExpression(const ScAddress &rCursor, sal_uInt16 nPos, sal_uInt32 nNumFmt=0, const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT) const
Definition: conditio.cxx:1256
bool IsEmpty() const
Definition: conditio.cxx:1783
const ScFormatEntry * GetEntry(sal_uInt16 nPos) const
Definition: conditio.cxx:1802
virtual Type GetType() const =0
ScCondFormatEntryType
@ CONDITION
@ ICONSET
@ DATABAR
@ COLORSCALE
ScConditionMode
Definition: conditio.hxx:60
DocumentType eType
sal_Int32 nIndex
Mode eMode
#define SAL_N_ELEMENTS(arr)
OUString getExpression(const sal_Unicode *&rpcString, const sal_Unicode *pcEnd, sal_Unicode cEndChar)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
std::unique_ptr< char[]> aBuffer