LibreOffice Module sc (master)  1
dpgroupdlg.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  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifdef SC_DLLIMPLEMENTATION
21 #undef SC_DLLIMPLEMENTATION
22 #endif
23 
24 #include <dpgroupdlg.hxx>
25 #include <globstr.hrc>
26 #include <scresid.hxx>
27 #include <editfield.hxx>
28 
29 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
30 #include <svtools/ctrlbox.hxx>
31 
32 namespace {
33 
35 const sal_Int32 spnDateParts[] =
36 {
37  css::sheet::DataPilotFieldGroupBy::SECONDS,
38  css::sheet::DataPilotFieldGroupBy::MINUTES,
39  css::sheet::DataPilotFieldGroupBy::HOURS,
40  css::sheet::DataPilotFieldGroupBy::DAYS,
41  css::sheet::DataPilotFieldGroupBy::MONTHS,
42  css::sheet::DataPilotFieldGroupBy::QUARTERS,
43  css::sheet::DataPilotFieldGroupBy::YEARS
44 };
45 
46 const char* aDatePartResIds[] =
47 {
48  STR_DPFIELD_GROUP_BY_SECONDS,
49  STR_DPFIELD_GROUP_BY_MINUTES,
50  STR_DPFIELD_GROUP_BY_HOURS,
51  STR_DPFIELD_GROUP_BY_DAYS,
52  STR_DPFIELD_GROUP_BY_MONTHS,
53  STR_DPFIELD_GROUP_BY_QUARTERS,
54  STR_DPFIELD_GROUP_BY_YEARS
55 };
56 
57 } // namespace
58 
60  : mrRbAuto(rRbAuto)
61  , mrRbMan(rRbMan)
62  , mrEdValue(rEdValue)
63 {
64  mrRbAuto.connect_clicked( LINK( this, ScDPGroupEditHelper, ClickHdl ) );
65  mrRbMan.connect_clicked( LINK( this, ScDPGroupEditHelper, ClickHdl ) );
66 }
67 
69 {
70  return mrRbAuto.get_active();
71 }
72 
74 {
75  double fValue;
76  if( !ImplGetValue( fValue ) )
77  fValue = 0.0;
78  return fValue;
79 }
80 
81 void ScDPGroupEditHelper::SetValue( bool bAuto, double fValue )
82 {
83  if( bAuto )
84  {
85  mrRbAuto.set_active(true);
86  ClickHdl(mrRbAuto);
87  }
88  else
89  {
90  mrRbMan.set_active(true);
91  ClickHdl(mrRbMan);
92  }
93  ImplSetValue( fValue );
94 }
95 
96 IMPL_LINK(ScDPGroupEditHelper, ClickHdl, weld::Button&, rButton, void)
97 {
98  if (&rButton == &mrRbAuto)
99  {
100  // disable edit field on clicking "automatic" radio button
101  mrEdValue.set_sensitive(false);
102  }
103  else if (&rButton == &mrRbMan)
104  {
105  // enable and set focus to edit field on clicking "manual" radio button
106  mrEdValue.set_sensitive(true);
107  mrEdValue.grab_focus();
108  }
109 }
110 
112  weld::RadioButton& rRbMan, ScDoubleField& rEdValue)
113  : ScDPGroupEditHelper(rRbAuto, rRbMan, rEdValue.get_widget())
114  , mrEdValue(rEdValue)
115 {
116 }
117 
118 bool ScDPNumGroupEditHelper::ImplGetValue( double& rfValue ) const
119 {
120  return mrEdValue.GetValue(rfValue);
121 }
122 
124 {
125  mrEdValue.SetValue(fValue);
126 }
127 
129  SvtCalendarBox& rEdValue, const Date& rNullDate)
130  : ScDPGroupEditHelper(rRbAuto, rRbMan, rEdValue.get_button())
131  , mrEdValue(rEdValue)
132  , maNullDate(rNullDate)
133 {
134 }
135 
136 bool ScDPDateGroupEditHelper::ImplGetValue( double& rfValue ) const
137 {
138  rfValue = mrEdValue.get_date() - maNullDate;
139  return true;
140 }
141 
143 {
144  Date aDate( maNullDate );
145  aDate.AddDays( fValue );
146  mrEdValue.set_date( aDate );
147 }
148 
150  : GenericDialogController(pParent, "modules/scalc/ui/groupbynumber.ui", "PivotTableGroupByNumber")
151  , mxRbAutoStart(m_xBuilder->weld_radio_button("auto_start"))
152  , mxRbManStart(m_xBuilder->weld_radio_button("manual_start"))
153  , mxEdStart(new ScDoubleField(m_xBuilder->weld_entry("edit_start")))
154  , mxRbAutoEnd(m_xBuilder->weld_radio_button("auto_end"))
155  , mxRbManEnd(m_xBuilder->weld_radio_button("manual_end"))
156  , mxEdEnd(new ScDoubleField(m_xBuilder->weld_entry("edit_end")))
157  , mxEdBy(new ScDoubleField(m_xBuilder->weld_entry("edit_by")))
158  , maStartHelper(*mxRbAutoStart, *mxRbManStart, *mxEdStart)
159  , maEndHelper(*mxRbAutoEnd, *mxRbManEnd, *mxEdEnd)
160 {
161  maStartHelper.SetValue( rInfo.mbAutoStart, rInfo.mfStart );
162  maEndHelper.SetValue( rInfo.mbAutoEnd, rInfo.mfEnd );
163  mxEdBy->SetValue( (rInfo.mfStep <= 0.0) ? 1.0 : rInfo.mfStep );
164 
165  /* Set the initial focus, currently it is somewhere after calling all the radio
166  button click handlers. Now the first enabled editable control is focused. */
167  if (mxEdStart->get_sensitive())
168  mxEdStart->grab_focus();
169  else if (mxEdEnd->get_sensitive())
170  mxEdEnd->grab_focus();
171  else
172  mxEdBy->grab_focus();
173 }
174 
176 {
177 }
178 
180 {
181  ScDPNumGroupInfo aInfo;
182  aInfo.mbEnable = true;
183  aInfo.mbDateValues = false;
184  aInfo.mbAutoStart = maStartHelper.IsAuto();
185  aInfo.mbAutoEnd = maEndHelper.IsAuto();
186 
187  // get values and silently auto-correct them, if they are not valid
188  // TODO: error messages in OK event?
189  aInfo.mfStart = maStartHelper.GetValue();
190  aInfo.mfEnd = maEndHelper.GetValue();
191  if( !mxEdBy->GetValue( aInfo.mfStep ) || (aInfo.mfStep <= 0.0) )
192  aInfo.mfStep = 1.0;
193  if( aInfo.mfEnd <= aInfo.mfStart )
194  aInfo.mfEnd = aInfo.mfStart + aInfo.mfStep;
195 
196  return aInfo;
197 }
198 
200  const ScDPNumGroupInfo& rInfo, sal_Int32 nDatePart, const Date& rNullDate)
201  : GenericDialogController(pParent, "modules/scalc/ui/groupbydate.ui", "PivotTableGroupByDate")
202  , mxRbAutoStart(m_xBuilder->weld_radio_button("auto_start"))
203  , mxRbManStart(m_xBuilder->weld_radio_button("manual_start"))
204  , mxEdStart(new SvtCalendarBox(m_xBuilder->weld_menu_button("start_date")))
205  , mxRbAutoEnd(m_xBuilder->weld_radio_button("auto_end"))
206  , mxRbManEnd(m_xBuilder->weld_radio_button("manual_end"))
207  , mxEdEnd(new SvtCalendarBox(m_xBuilder->weld_menu_button("end_date")))
208  , mxRbNumDays(m_xBuilder->weld_radio_button("days"))
209  , mxRbUnits(m_xBuilder->weld_radio_button("intervals"))
210  , mxEdNumDays(m_xBuilder->weld_spin_button("days_value"))
211  , mxLbUnits(m_xBuilder->weld_tree_view("interval_list"))
212  , mxBtnOk(m_xBuilder->weld_button("ok"))
213  , maStartHelper(*mxRbAutoStart, *mxRbManStart, *mxEdStart, rNullDate)
214  , maEndHelper(*mxRbAutoEnd, *mxRbManEnd, *mxEdEnd, rNullDate)
215 {
216  maStartHelper.SetValue( rInfo.mbAutoStart, rInfo.mfStart );
217  maEndHelper.SetValue( rInfo.mbAutoEnd, rInfo.mfEnd );
218 
219  mxLbUnits->enable_toggle_buttons(weld::ColumnToggleType::Check);
220 
221  if( nDatePart == 0 )
222  nDatePart = css::sheet::DataPilotFieldGroupBy::MONTHS;
223  for (size_t nIdx = 0; nIdx < SAL_N_ELEMENTS(aDatePartResIds); ++nIdx)
224  {
225  mxLbUnits->append();
226  mxLbUnits->set_toggle(nIdx, (nDatePart & spnDateParts[ nIdx ]) ? TRISTATE_TRUE : TRISTATE_FALSE);
227  mxLbUnits->set_text(nIdx, ScResId(aDatePartResIds[nIdx]), 0);
228  }
229 
230  if( rInfo.mbDateValues )
231  {
232  mxRbNumDays->set_active(true);
233  ClickHdl(*mxRbNumDays );
234 
235  double fNumDays = rInfo.mfStep;
236  if( fNumDays < 1.0 )
237  fNumDays = 1.0;
238  else if( fNumDays > 32767.0 )
239  fNumDays = 32767.0;
240  mxEdNumDays->set_value(fNumDays);
241  }
242  else
243  {
244  mxRbUnits->set_active(true);
245  ClickHdl(*mxRbUnits);
246  }
247 
248  /* Set the initial focus, currently it is somewhere after calling all the radio
249  button click handlers. Now the first enabled editable control is focused. */
250  if( mxEdStart->get_sensitive() )
251  mxEdStart->grab_focus();
252  else if( mxEdEnd->get_sensitive() )
253  mxEdEnd->grab_focus();
254  else if( mxEdNumDays->get_sensitive() )
255  mxEdNumDays->grab_focus();
256  else if( mxLbUnits->get_sensitive() )
257  mxLbUnits->grab_focus();
258 
259  mxRbNumDays->connect_clicked( LINK( this, ScDPDateGroupDlg, ClickHdl ) );
260  mxRbUnits->connect_clicked( LINK( this, ScDPDateGroupDlg, ClickHdl ) );
261  mxLbUnits->connect_toggled( LINK( this, ScDPDateGroupDlg, CheckHdl ) );
262 }
263 
265 {
266 }
267 
269 {
270  ScDPNumGroupInfo aInfo;
271  aInfo.mbEnable = true;
272  aInfo.mbDateValues = mxRbNumDays->get_active();
273  aInfo.mbAutoStart = maStartHelper.IsAuto();
274  aInfo.mbAutoEnd = maEndHelper.IsAuto();
275 
276  // get values and silently auto-correct them, if they are not valid
277  // TODO: error messages in OK event?
278  aInfo.mfStart = maStartHelper.GetValue();
279  aInfo.mfEnd = maEndHelper.GetValue();
280  sal_Int64 nNumDays = mxEdNumDays->get_value();
281  aInfo.mfStep = static_cast<double>( aInfo.mbDateValues ? nNumDays : 0L );
282  if( aInfo.mfEnd <= aInfo.mfStart )
283  aInfo.mfEnd = aInfo.mfStart + nNumDays;
284 
285  return aInfo;
286 }
287 
289 {
290  // return DAYS for special "number of days" mode
291  if( mxRbNumDays->get_active() )
292  return css::sheet::DataPilotFieldGroupBy::DAYS;
293 
294  // return listbox contents for "units" mode
295  sal_Int32 nDatePart = 0;
296  for (int nIdx = 0, nCount = mxLbUnits->n_children(); nIdx < nCount; ++nIdx )
297  if (mxLbUnits->get_toggle(nIdx) == TRISTATE_TRUE)
298  nDatePart |= spnDateParts[ nIdx ];
299  return nDatePart;
300 }
301 
302 IMPL_LINK(ScDPDateGroupDlg, ClickHdl, weld::Button&, rButton, void)
303 {
304  if (&rButton == mxRbNumDays.get())
305  {
306  mxLbUnits->set_sensitive(false);
307  // enable and set focus to edit field on clicking "num of days" radio button
308  mxEdNumDays->set_sensitive(true);
309  mxEdNumDays->grab_focus();
310  mxBtnOk->set_sensitive(true);
311  }
312  else if (&rButton == mxRbUnits.get())
313  {
314  mxEdNumDays->set_sensitive(false);
315  // enable and set focus to listbox on clicking "units" radio button
316  mxLbUnits->set_sensitive(true);
317  mxLbUnits->grab_focus();
318  // disable OK button if no date part selected
319  Check();
320  }
321 }
322 
323 namespace
324 {
325  bool HasCheckedEntryCount(const weld::TreeView& rView)
326  {
327  for (int i = 0; i < rView.n_children(); ++i)
328  {
329  if (rView.get_toggle(i) == TRISTATE_TRUE)
330  return true;
331  }
332  return false;
333  }
334 }
335 
337 {
338  Check();
339 }
340 
342 {
343  // enable/disable OK button on modifying check list box
344  mxBtnOk->set_sensitive(HasCheckedEntryCount(*mxLbUnits));
345 }
346 
347 
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScDPDateGroupEditHelper maEndHelper
Definition: dpgroupdlg.hxx:135
virtual bool ImplGetValue(double &rfValue) const override
Definition: dpgroupdlg.cxx:118
virtual ~ScDPNumGroupDlg() override
Definition: dpgroupdlg.cxx:175
ScDPNumGroupInfo GetGroupInfo() const
Definition: dpgroupdlg.cxx:179
virtual ~ScDPDateGroupDlg() override
Definition: dpgroupdlg.cxx:264
ScDPNumGroupEditHelper maEndHelper
Definition: dpgroupdlg.hxx:105
ScDPNumGroupEditHelper maStartHelper
Definition: dpgroupdlg.hxx:104
bool IsAuto() const
Definition: dpgroupdlg.cxx:68
ScDoubleField & mrEdValue
Definition: dpgroupdlg.hxx:67
std::unique_ptr< ScDoubleField > mxEdStart
Definition: dpgroupdlg.hxx:99
ScDPDateGroupEditHelper maStartHelper
Definition: dpgroupdlg.hxx:134
ScDPNumGroupDlg(weld::Window *pParent, const ScDPNumGroupInfo &rInfo)
Definition: dpgroupdlg.cxx:149
virtual bool ImplGetValue(double &rfValue) const =0
IMPL_LINK_NOARG(ScDPDateGroupDlg, CheckHdl, const weld::TreeView::iter_col &, void)
Definition: dpgroupdlg.cxx:336
bool GetValue(double &rfValue) const
Definition: editfield.cxx:43
std::unique_ptr< weld::RadioButton > mxRbUnits
Definition: dpgroupdlg.hxx:130
Date get_date() const
ScDPDateGroupEditHelper(weld::RadioButton &rRbAuto, weld::RadioButton &rRbMan, SvtCalendarBox &rEdValue, const Date &rNullDate)
Definition: dpgroupdlg.cxx:128
TRISTATE_TRUE
virtual bool get_active() const =0
std::unique_ptr< weld::SpinButton > mxEdNumDays
Definition: dpgroupdlg.hxx:131
void SetValue(bool bAuto, double fValue)
Definition: dpgroupdlg.cxx:81
virtual TriState get_toggle(int row, int col=-1) const =0
std::unique_ptr< ScDoubleField > mxEdEnd
Definition: dpgroupdlg.hxx:102
void SetValue(double fValue, sal_Int32 nDecPlaces=12)
Definition: editfield.cxx:57
ScDPDateGroupDlg(weld::Window *pParent, const ScDPNumGroupInfo &rInfo, sal_Int32 nDatePart, const Date &rNullDate)
Definition: dpgroupdlg.cxx:199
sal_Int32 GetDatePart() const
Definition: dpgroupdlg.cxx:288
std::pair< const TreeIter &, int > iter_col
int nCount
virtual int n_children() const =0
virtual void ImplSetValue(double fValue)=0
std::unique_ptr< SvtCalendarBox > mxEdStart
Definition: dpgroupdlg.hxx:125
virtual void set_active(bool active)=0
#define SAL_N_ELEMENTS(arr)
weld::RadioButton & mrRbAuto
Definition: dpgroupdlg.hxx:49
void connect_clicked(const Link< Button &, void > &rLink)
ScDPGroupEditHelper(weld::RadioButton &rRbAuto, weld::RadioButton &rRbMan, weld::Widget &rEdValue)
Definition: dpgroupdlg.cxx:59
int i
ScDPNumGroupEditHelper(weld::RadioButton &rRbAuto, weld::RadioButton &rRbMan, ScDoubleField &rEdValue)
Definition: dpgroupdlg.cxx:111
void set_date(const Date &rDate)
weld::RadioButton & mrRbMan
Definition: dpgroupdlg.hxx:50
TRISTATE_FALSE
An edit control that contains a double precision floating-point value.
Definition: editfield.hxx:25
OUString ScResId(const char *pId)
Definition: scdll.cxx:89
void AddDays(sal_Int32 nAddDays)
std::unique_ptr< SvtCalendarBox > mxEdEnd
Definition: dpgroupdlg.hxx:128
std::unique_ptr< weld::TreeView > mxLbUnits
Definition: dpgroupdlg.hxx:132
ScDPNumGroupInfo GetGroupInfo() const
Definition: dpgroupdlg.cxx:268
virtual void ImplSetValue(double fValue) override
Definition: dpgroupdlg.cxx:142
std::unique_ptr< weld::Button > mxBtnOk
Definition: dpgroupdlg.hxx:133
std::unique_ptr< weld::RadioButton > mxRbNumDays
Definition: dpgroupdlg.hxx:129
virtual bool ImplGetValue(double &rfValue) const override
Definition: dpgroupdlg.cxx:136
double GetValue() const
Definition: dpgroupdlg.cxx:73
std::unique_ptr< ScDoubleField > mxEdBy
Definition: dpgroupdlg.hxx:103
virtual void ImplSetValue(double fValue) override
Definition: dpgroupdlg.cxx:123
IMPL_LINK(ScDPGroupEditHelper, ClickHdl, weld::Button &, rButton, void)
Definition: dpgroupdlg.cxx:96
SvtCalendarBox & mrEdValue
Definition: dpgroupdlg.hxx:85