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 <svx/colorbox.hxx>
16 #include <vcl/svapp.hxx>
17 #include <vcl/weld.hxx>
18 
19 namespace {
20 
21 void SetType(const ScColorScaleEntry* pEntry, weld::ComboBox& rLstBox)
22 {
23  rLstBox.set_active(pEntry->GetType());
24 }
25 
26 void GetType(const weld::ComboBox& rLstBox, const weld::Entry& rEd, ScColorScaleEntry* pEntry, SvNumberFormatter* pNumberFormatter,
27  ScDocument* pDoc, const ScAddress& rPos )
28 {
29  double nVal = 0;
30  sal_uInt32 nIndex = 0;
31  pEntry->SetType(static_cast<ScColorScaleEntryType>(rLstBox.get_active()));
32  switch (rLstBox.get_active())
33  {
34  case COLORSCALE_AUTO:
35  case COLORSCALE_MIN:
36  case COLORSCALE_MAX:
37  break;
39  case COLORSCALE_VALUE:
40  case COLORSCALE_PERCENT:
41  (void)pNumberFormatter->IsNumberFormat( rEd.get_text(), nIndex, nVal );
42  pEntry->SetValue(nVal);
43  break;
44  case COLORSCALE_FORMULA:
45  pEntry->SetFormula(rEd.get_text(), *pDoc, rPos);
46  break;
47  }
48 }
49 
50 OUString convertNumberToString(double nVal, const ScDocument* pDoc)
51 {
52  SvNumberFormatter* pNumberFormatter = pDoc->GetFormatTable();
53  OUString aText;
54  pNumberFormatter->GetInputLineString(nVal, 0, aText);
55  return aText;
56 }
57 
58 void SetValue( const ScDocument* pDoc, const ScColorScaleEntry* pEntry, weld::Entry& rEdit)
59 {
60  if(pEntry->GetType() == COLORSCALE_FORMULA)
62  else if(pEntry->GetType() != COLORSCALE_MIN && pEntry->GetType() != COLORSCALE_MAX)
63  rEdit.set_text(convertNumberToString(pEntry->GetValue(), pDoc));
64  else
65  rEdit.set_sensitive(false);
66 }
67 
68 }
69 
71  : GenericDialogController(pParent, "modules/scalc/ui/databaroptions.ui", "DataBarOptions")
72  , mpNumberFormatter(pDoc->GetFormatTable())
73  , mpDoc(pDoc)
74  , maPos(rPos)
75  , mxBtnOk(m_xBuilder->weld_button("ok"))
76  , mxBtnCancel(m_xBuilder->weld_button("cancel"))
77  , mxLbPos(new ColorListBox(m_xBuilder->weld_menu_button("positive_colour"), pParent))
78  , mxLbNeg(new ColorListBox(m_xBuilder->weld_menu_button("negative_colour"), pParent))
79  , mxLbAxisCol(new ColorListBox(m_xBuilder->weld_menu_button("axis_colour"), pParent))
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;
105  case databar::AUTOMATIC:
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 
142 namespace {
143 
144 void GetAxesPosition(ScDataBarFormatData* pData, const weld::ComboBox& rLbox)
145 {
146  switch (rLbox.get_active())
147  {
148  case 0:
150  break;
151  case 1:
153  break;
154  case 2:
155  pData->meAxisPosition = databar::NONE;
156  break;
157  }
158 }
159 
160 void 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 
double mnMaxLength
Maximal length of a databar in percent of cell length Value has to be in the range (0...
Definition: colorscale.hxx:178
std::unique_ptr< weld::Entry > mxLenMin
Definition: colorformat.hxx:44
sal_Int32 nIndex
std::unique_ptr< weld::ComboBox > mxLbTypeMin
Definition: colorformat.hxx:38
std::unique_ptr< weld::ComboBox > mxLbFillType
Definition: colorformat.hxx:37
SvNumberFormatter * mpNumberFormatter
Definition: colorformat.hxx:25
std::unique_ptr< ContentProperties > pData
double mnMinLength
Minimal length of a databar in percent of cell length Value has to be in the range [0...
Definition: colorscale.hxx:173
std::unique_ptr< weld::Entry > mxLenMax
Definition: colorformat.hxx:45
std::unique_ptr< weld::CheckButton > mxCbOnlyBar
Definition: colorformat.hxx:47
std::unique_ptr< ColorListBox > mxLbNeg
Definition: colorformat.hxx:34
virtual ~ScDataBarSettingsDlg() override
bool mbOnlyBar
If TRUE we only show the bar and not the value.
Definition: colorscale.hxx:183
void SetValue(double nValue)
Definition: colorscale.cxx:251
virtual int get_active() const =0
virtual void set_text(const OUString &rText)=0
std::optional< Color > mxNegativeColor
Specifies the color for negative values.
Definition: colorscale.hxx:141
std::unique_ptr< weld::Entry > mxEdMax
Definition: colorformat.hxx:43
const ScTokenArray * GetFormula() const
Definition: colorscale.cxx:216
ScDataBarFormatData * GetData()
ScColorScaleEntryType GetType() const
Definition: colorscale.hxx:77
std::unique_ptr< weld::ComboBox > mxLbAxisPos
Definition: colorformat.hxx:40
std::unique_ptr< weld::Label > mxStrSameValueFT
Definition: colorformat.hxx:49
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_LIGHTRED
std::unique_ptr< weld::Entry > mxEdMin
Definition: colorformat.hxx:42
Color maPositiveColor
Color for all Positive Values and if mbNeg == false also for negative ones.
Definition: colorscale.hxx:135
Color maAxisColor
Color of the axis if used Default color is black.
Definition: colorscale.hxx:146
ScDocument * mpDoc
Definition: colorformat.hxx:27
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:438
bool mbGradient
Paint the bars with gradient.
Definition: colorscale.hxx:153
IMPL_LINK_NOARG(ScDataBarSettingsDlg, OkBtnHdl, weld::Button &, void)
bool IsNumberFormat(const OUString &sString, sal_uInt32 &F_Index, double &fOutNumber, SvNumInputOptions eInputOptions=SvNumInputOptions::NONE)
virtual void set_active(int pos)=0
ScDataBarSettingsDlg(weld::Window *pParent, const ScDataBarFormatData &rData, ScDocument *pDoc, const ScAddress &rPos)
Definition: colorformat.cxx:70
std::unique_ptr< ColorListBox > mxLbAxisCol
Definition: colorformat.hxx:35
void GetInputLineString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &rOutString)
void SetFormula(const OUString &rFormula, ScDocument &rDoc, const ScAddress &rAddr, formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT)
Definition: colorscale.cxx:207
virtual OUString get_text() const =0
databar::ScAxisPosition meAxisPosition
Paint negative values into the same direction as positive values If false we will set the mid point a...
Definition: colorscale.hxx:168
std::unique_ptr< ScColorScaleEntry, o3tl::default_delete< ScColorScaleEntry > > mpUpperLimit
Definition: colorscale.hxx:185
RET_OK
Reference< XExecutableDialog > m_xDialog
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
std::unique_ptr< weld::Button > mxBtnOk
Definition: colorformat.hxx:30
std::unique_ptr< weld::ComboBox > mxLbTypeMax
Definition: colorformat.hxx:39
void SetType(ScColorScaleEntryType eType)
Definition: colorscale.cxx:324
std::unique_ptr< ScColorScaleEntry, o3tl::default_delete< ScColorScaleEntry > > mpLowerLimit
Definition: colorscale.hxx:186
OUString maStrWarnSameValue
Definition: colorformat.hxx:24
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
double GetValue() const
Definition: colorscale.cxx:237
virtual void set_sensitive(bool sensitive)=0
std::unique_ptr< ColorListBox > mxLbPos
Definition: colorformat.hxx:33
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo