LibreOffice Module sc (master) 1
colorformat.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 <colorformat.hxx>
11#include <colorscale.hxx>
12
13#include <document.hxx>
14
15#include <svl/numformat.hxx>
16#include <svx/colorbox.hxx>
17#include <vcl/svapp.hxx>
18#include <vcl/weld.hxx>
19
20namespace {
21
22void SetType(const ScColorScaleEntry* pEntry, weld::ComboBox& rLstBox)
23{
24 rLstBox.set_active(pEntry->GetType());
25}
26
27void GetType(const weld::ComboBox& rLstBox, const weld::Entry& rEd, ScColorScaleEntry* pEntry, SvNumberFormatter* pNumberFormatter,
28 ScDocument* pDoc, const ScAddress& rPos )
29{
30 double nVal = 0;
31 sal_uInt32 nIndex = 0;
32 pEntry->SetType(static_cast<ScColorScaleEntryType>(rLstBox.get_active()));
33 switch (rLstBox.get_active())
34 {
35 case COLORSCALE_AUTO:
36 case COLORSCALE_MIN:
37 case COLORSCALE_MAX:
38 break;
42 (void)pNumberFormatter->IsNumberFormat( rEd.get_text(), nIndex, nVal );
43 pEntry->SetValue(nVal);
44 break;
46 pEntry->SetFormula(rEd.get_text(), *pDoc, rPos);
47 break;
48 }
49}
50
51OUString convertNumberToString(double nVal, const ScDocument* pDoc)
52{
53 SvNumberFormatter* pNumberFormatter = pDoc->GetFormatTable();
54 OUString aText;
55 pNumberFormatter->GetInputLineString(nVal, 0, aText);
56 return aText;
57}
58
59void SetValue( const ScDocument* pDoc, const ScColorScaleEntry* pEntry, weld::Entry& rEdit)
60{
61 if(pEntry->GetType() == COLORSCALE_FORMULA)
63 else if(pEntry->GetType() != COLORSCALE_MIN && pEntry->GetType() != COLORSCALE_MAX)
64 rEdit.set_text(convertNumberToString(pEntry->GetValue(), pDoc));
65 else
66 rEdit.set_sensitive(false);
67}
68
69}
70
72 : GenericDialogController(pParent, "modules/scalc/ui/databaroptions.ui", "DataBarOptions")
73 , mpNumberFormatter(pDoc->GetFormatTable())
74 , mpDoc(pDoc)
75 , maPos(rPos)
76 , mxBtnOk(m_xBuilder->weld_button("ok"))
77 , mxLbPos(new ColorListBox(m_xBuilder->weld_menu_button("positive_colour"), [this]{ return m_xDialog.get(); }))
78 , mxLbNeg(new ColorListBox(m_xBuilder->weld_menu_button("negative_colour"), [this]{ return m_xDialog.get(); }))
79 , mxLbAxisCol(new ColorListBox(m_xBuilder->weld_menu_button("axis_colour"), [this]{ return m_xDialog.get(); }))
80 , mxLbFillType(m_xBuilder->weld_combo_box("fill_type"))
81 , mxLbTypeMin(m_xBuilder->weld_combo_box("min"))
82 , mxLbTypeMax(m_xBuilder->weld_combo_box("max"))
83 , mxLbAxisPos(m_xBuilder->weld_combo_box("axis_pos"))
84 , mxEdMin(m_xBuilder->weld_entry("min_value"))
85 , mxEdMax(m_xBuilder->weld_entry("max_value"))
86 , mxLenMin(m_xBuilder->weld_entry("min_length"))
87 , mxLenMax(m_xBuilder->weld_entry("max_length"))
88 , mxCbOnlyBar(m_xBuilder->weld_check_button("only_bar"))
89 , mxStrSameValueFT(m_xBuilder->weld_label("str_same_value"))
90{
91 maStrWarnSameValue = mxStrSameValueFT->get_label();
92
93 Init();
94
95 mxLbPos->SelectEntry(rData.maPositiveColor);
96 mxLbFillType->set_active( rData.mbGradient ? 1 : 0 );
97 if (rData.mxNegativeColor)
98 mxLbNeg->SelectEntry(*rData.mxNegativeColor);
99
100 switch (rData.meAxisPosition)
101 {
102 case databar::NONE:
103 mxLbAxisPos->set_active(2);
104 break;
106 mxLbAxisPos->set_active(0);
107 break;
108 case databar::MIDDLE:
109 mxLbAxisPos->set_active(1);
110 break;
111 }
112 ::SetType(rData.mpLowerLimit.get(), *mxLbTypeMin);
113 ::SetType(rData.mpUpperLimit.get(), *mxLbTypeMax);
114 SetValue(mpDoc, rData.mpLowerLimit.get(), *mxEdMin);
115 SetValue(mpDoc, rData.mpUpperLimit.get(), *mxEdMax);
116 mxLenMin->set_text(convertNumberToString(rData.mnMinLength, mpDoc));
117 mxLenMax->set_text(convertNumberToString(rData.mnMaxLength, mpDoc));
118 mxLbAxisCol->SelectEntry(rData.maAxisColor);
119 mxCbOnlyBar->set_active(rData.mbOnlyBar);
120
121 TypeSelectHdl(*mxLbTypeMin);
122 PosSelectHdl(*mxLbTypeMin);
123}
124
126{
127}
128
130{
131 mxLbNeg->SelectEntry(COL_LIGHTRED);
132 mxLbAxisCol->SelectEntry(COL_BLACK);
133 mxLbPos->SelectEntry(0x2a6099);
134 mxBtnOk->connect_clicked( LINK( this, ScDataBarSettingsDlg, OkBtnHdl ) );
135
136 mxLbTypeMin->connect_changed( LINK( this, ScDataBarSettingsDlg, TypeSelectHdl ) );
137 mxLbTypeMax->connect_changed( LINK( this, ScDataBarSettingsDlg, TypeSelectHdl ) );
138 mxLbAxisPos->connect_changed( LINK( this, ScDataBarSettingsDlg, PosSelectHdl ) );
139
140}
141
142namespace {
143
144void GetAxesPosition(ScDataBarFormatData* pData, const weld::ComboBox& rLbox)
145{
146 switch (rLbox.get_active())
147 {
148 case 0:
149 pData->meAxisPosition = databar::AUTOMATIC;
150 break;
151 case 1:
152 pData->meAxisPosition = databar::MIDDLE;
153 break;
154 case 2:
155 pData->meAxisPosition = databar::NONE;
156 break;
157 }
158}
159
160void SetBarLength(ScDataBarFormatData* pData, const OUString& minStr, const OUString& maxStr, SvNumberFormatter* mpNumberFormatter)
161{
162 double nMinValue = 0;
163 sal_uInt32 nIndex = 0;
164 (void)mpNumberFormatter->IsNumberFormat(minStr, nIndex, nMinValue);
165 nIndex = 0;
166 double nMaxValue = 0;
167 (void)mpNumberFormatter->IsNumberFormat(maxStr, nIndex, nMaxValue);
168 pData->mnMinLength = nMinValue;
169 pData->mnMaxLength = nMaxValue;
170}
171
172}
173
175{
177 pData->maPositiveColor = mxLbPos->GetSelectEntryColor();
178 pData->mxNegativeColor = mxLbNeg->GetSelectEntryColor();
179 pData->mbGradient = ( mxLbFillType->get_active() == 1 );
180 pData->mpUpperLimit.reset(new ScColorScaleEntry());
181 pData->mpLowerLimit.reset(new ScColorScaleEntry());
182 pData->maAxisColor = mxLbAxisCol->GetSelectEntryColor();
183 pData->mbOnlyBar = mxCbOnlyBar->get_active();
184
185 ::GetType(*mxLbTypeMin, *mxEdMin, pData->mpLowerLimit.get(), mpNumberFormatter, mpDoc, maPos);
186 ::GetType(*mxLbTypeMax, *mxEdMax, pData->mpUpperLimit.get(), mpNumberFormatter, mpDoc, maPos);
187 GetAxesPosition(pData, *mxLbAxisPos);
188 SetBarLength(pData, mxLenMin->get_text(), mxLenMax->get_text(), mpNumberFormatter);
189
190 return pData;
191}
192
194{
195 //check that min < max
196 bool bWarn = false;
197 int nSelectMin = mxLbTypeMin->get_active();
198 if( nSelectMin == COLORSCALE_MAX )
199 bWarn = true;
200 int nSelectMax = mxLbTypeMax->get_active();
201 if( nSelectMax == COLORSCALE_MIN )
202 bWarn = true;
203 if(!bWarn) // databar length checks
204 {
205 OUString aMinString = mxLenMin->get_text();
206 OUString aMaxString = mxLenMax->get_text();
207 double nMinValue = 0;
208 sal_uInt32 nIndex = 0;
209 (void)mpNumberFormatter->IsNumberFormat(aMinString, nIndex, nMinValue);
210 nIndex = 0;
211 double nMaxValue = 0;
212 (void)mpNumberFormatter->IsNumberFormat(aMaxString, nIndex, nMaxValue);
213 if(rtl::math::approxEqual(nMinValue, nMaxValue) || nMinValue > nMaxValue || nMaxValue > 100 || nMinValue < 0)
214 bWarn = true;
215 }
216 if (!bWarn && mxLbTypeMin->get_active() == mxLbTypeMax->get_active())
217 {
218
219 if(nSelectMax != COLORSCALE_FORMULA && nSelectMax != COLORSCALE_AUTO)
220 {
221 OUString aMinString = mxEdMin->get_text();
222 OUString aMaxString = mxEdMax->get_text();
223 double nMinValue = 0;
224 sal_uInt32 nIndex = 0;
225 (void)mpNumberFormatter->IsNumberFormat(aMinString, nIndex, nMinValue);
226 nIndex = 0;
227 double nMaxValue = 0;
228 (void)mpNumberFormatter->IsNumberFormat(aMaxString, nIndex, nMaxValue);
229 if(rtl::math::approxEqual(nMinValue, nMaxValue) || nMinValue > nMaxValue)
230 bWarn = true;
231 }
232 }
233
234 if(bWarn)
235 {
236 //show warning message and don't close
237 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(m_xDialog.get(),
238 VclMessageType::Warning, VclButtonsType::Ok,
239 maStrWarnSameValue));
240 xWarn->run();
241 }
242 else
243 {
244 m_xDialog->response(RET_OK);
245 }
246}
247
249{
250 int nSelectMin = mxLbTypeMin->get_active();
251 if( nSelectMin <= COLORSCALE_MAX)
252 mxEdMin->set_sensitive(false);
253 else
254 {
255 mxEdMin->set_sensitive(true);
256 if(mxEdMin->get_text().isEmpty())
257 {
258 if(nSelectMin == COLORSCALE_PERCENTILE || nSelectMin == COLORSCALE_PERCENT)
259 mxEdMin->set_text(OUString::number(50));
260 else
261 mxEdMin->set_text(OUString::number(0));
262 }
263 }
264
265 int nSelectMax = mxLbTypeMax->get_active();
266 if (nSelectMax <= COLORSCALE_MAX)
267 mxEdMax->set_sensitive(false);
268 else
269 {
270 mxEdMax->set_sensitive(true);
271 if (mxEdMax->get_text().isEmpty())
272 {
273 if(nSelectMax == COLORSCALE_PERCENTILE || nSelectMax == COLORSCALE_PERCENT)
274 mxEdMax->set_text(OUString::number(50));
275 else
276 mxEdMax->set_text(OUString::number(0));
277 }
278 }
279}
280
282{
283 int axisPos = mxLbAxisPos->get_active();
284 if(axisPos != 2 && axisPos != 1) // disable if axis vertical position is automatic
285 {
286 mxLenMin->set_sensitive(false);
287 mxLenMax->set_sensitive(false);
288 }
289 else
290 {
291 mxLenMin->set_sensitive(true);
292 mxLenMax->set_sensitive(true);
293 if(mxLenMin->get_text().isEmpty())
294 {
295 mxLenMin->set_text(OUString::number(0));
296 mxLenMax->set_text(OUString::number(100));
297 }
298 }
299}
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
302
Reference< XExecutableDialog > m_xDialog
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
double GetValue() const
Definition: colorscale.cxx:236
const ScTokenArray * GetFormula() const
Definition: colorscale.cxx:216
void SetType(ScColorScaleEntryType eType)
Definition: colorscale.cxx:323
ScColorScaleEntryType GetType() const
Definition: colorscale.hxx:76
void SetFormula(const OUString &rFormula, ScDocument &rDoc, const ScAddress &rAddr, formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT)
Definition: colorscale.cxx:207
void SetValue(double nValue)
Definition: colorscale.cxx:250
std::unique_ptr< ColorListBox > mxLbNeg
Definition: colorformat.hxx:32
std::unique_ptr< weld::CheckButton > mxCbOnlyBar
Definition: colorformat.hxx:45
std::unique_ptr< ColorListBox > mxLbAxisCol
Definition: colorformat.hxx:33
virtual ~ScDataBarSettingsDlg() override
ScDataBarSettingsDlg(weld::Window *pParent, const ScDataBarFormatData &rData, ScDocument *pDoc, const ScAddress &rPos)
Definition: colorformat.cxx:71
std::unique_ptr< weld::Button > mxBtnOk
Definition: colorformat.hxx:29
std::unique_ptr< weld::Entry > mxLenMin
Definition: colorformat.hxx:42
SvNumberFormatter * mpNumberFormatter
Definition: colorformat.hxx:24
std::unique_ptr< weld::Entry > mxEdMax
Definition: colorformat.hxx:41
std::unique_ptr< weld::ComboBox > mxLbAxisPos
Definition: colorformat.hxx:38
ScDocument * mpDoc
Definition: colorformat.hxx:26
std::unique_ptr< weld::Entry > mxEdMin
Definition: colorformat.hxx:40
std::unique_ptr< ColorListBox > mxLbPos
Definition: colorformat.hxx:31
ScDataBarFormatData * GetData()
std::unique_ptr< weld::ComboBox > mxLbTypeMax
Definition: colorformat.hxx:37
std::unique_ptr< weld::ComboBox > mxLbFillType
Definition: colorformat.hxx:35
std::unique_ptr< weld::ComboBox > mxLbTypeMin
Definition: colorformat.hxx:36
std::unique_ptr< weld::Entry > mxLenMax
Definition: colorformat.hxx:43
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:467
void GetInputLineString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &rOutString, bool bFiltering=false, bool bForceSystemLocale=false)
bool IsNumberFormat(const OUString &sString, sal_uInt32 &F_Index, double &fOutNumber, SvNumInputOptions eInputOptions=SvNumInputOptions::NONE)
virtual void set_active(int pos)=0
virtual int get_active() const=0
virtual void set_text(const OUString &rText)=0
virtual OUString get_text() const=0
std::shared_ptr< weld::Dialog > m_xDialog
virtual void set_sensitive(bool sensitive)=0
constexpr ::Color COL_LIGHTRED(0xFF, 0x00, 0x00)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
IMPL_LINK_NOARG(ScDataBarSettingsDlg, OkBtnHdl, weld::Button &, void)
ScColorScaleEntryType
Definition: colorscale.hxx:32
@ COLORSCALE_MAX
Definition: colorscale.hxx:35
@ COLORSCALE_VALUE
Definition: colorscale.hxx:37
@ COLORSCALE_FORMULA
Definition: colorscale.hxx:39
@ COLORSCALE_PERCENT
Definition: colorscale.hxx:38
@ COLORSCALE_PERCENTILE
Definition: colorscale.hxx:36
@ COLORSCALE_AUTO
Definition: colorscale.hxx:33
@ COLORSCALE_MIN
Definition: colorscale.hxx:34
void Init()
virtual void SetValue(tools::Long nNew) override
sal_Int32 nIndex
std::unique_ptr< sal_Int32[]> pData
RET_OK