Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / dbgui / dpgroupdlg.cxx
blob6ade3f464178fa36284e722e195bf543a3f7443b
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifdef SC_DLLIMPLEMENTATION
21 #undef SC_DLLIMPLEMENTATION
22 #endif
24 #include "dpgroupdlg.hxx"
25 #include "scresid.hxx"
26 #include "sc.hrc"
27 #include "globstr.hrc"
29 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
31 namespace {
33 /** Date part flags in order of the list box entries. */
34 static const sal_Int32 spnDateParts[] =
36 css::sheet::DataPilotFieldGroupBy::SECONDS,
37 css::sheet::DataPilotFieldGroupBy::MINUTES,
38 css::sheet::DataPilotFieldGroupBy::HOURS,
39 css::sheet::DataPilotFieldGroupBy::DAYS,
40 css::sheet::DataPilotFieldGroupBy::MONTHS,
41 css::sheet::DataPilotFieldGroupBy::QUARTERS,
42 css::sheet::DataPilotFieldGroupBy::YEARS
45 static const sal_uInt16 nDatePartResIds[] =
47 STR_DPFIELD_GROUP_BY_SECONDS,
48 STR_DPFIELD_GROUP_BY_MINUTES,
49 STR_DPFIELD_GROUP_BY_HOURS,
50 STR_DPFIELD_GROUP_BY_DAYS,
51 STR_DPFIELD_GROUP_BY_MONTHS,
52 STR_DPFIELD_GROUP_BY_QUARTERS,
53 STR_DPFIELD_GROUP_BY_YEARS
56 } // namespace
58 ScDPGroupEditHelper::ScDPGroupEditHelper( RadioButton* pRbAuto, RadioButton* pRbMan, Edit* pEdValue ) :
59 mpRbAuto( pRbAuto ),
60 mpRbMan( pRbMan ),
61 mpEdValue( pEdValue )
63 mpRbAuto->SetClickHdl( LINK( this, ScDPGroupEditHelper, ClickHdl ) );
64 mpRbMan->SetClickHdl( LINK( this, ScDPGroupEditHelper, ClickHdl ) );
67 bool ScDPGroupEditHelper::IsAuto() const
69 return mpRbAuto->IsChecked();
72 double ScDPGroupEditHelper::GetValue() const
74 double fValue;
75 if( !ImplGetValue( fValue ) )
76 fValue = 0.0;
77 return fValue;
80 void ScDPGroupEditHelper::SetValue( bool bAuto, double fValue )
82 if( bAuto )
84 mpRbAuto->Check();
85 ClickHdl( mpRbAuto );
87 else
89 mpRbMan->Check();
90 ClickHdl( mpRbMan );
92 ImplSetValue( fValue );
95 IMPL_LINK_TYPED( ScDPGroupEditHelper, ClickHdl, Button*, pButton, void )
97 if( pButton == mpRbAuto )
99 // disable edit field on clicking "automatic" radio button
100 mpEdValue->Disable();
102 else if( pButton == mpRbMan )
104 // enable and set focus to edit field on clicking "manual" radio button
105 mpEdValue->Enable();
106 mpEdValue->GrabFocus();
110 ScDPNumGroupEditHelper::ScDPNumGroupEditHelper(
111 RadioButton* pRbAuto, RadioButton* pRbMan, ScDoubleField* pEdValue ) :
112 ScDPGroupEditHelper( pRbAuto, pRbMan, pEdValue ),
113 mpEdValue( pEdValue )
117 bool ScDPNumGroupEditHelper::ImplGetValue( double& rfValue ) const
119 return mpEdValue->GetValue( rfValue );
122 void ScDPNumGroupEditHelper::ImplSetValue( double fValue )
124 mpEdValue->SetValue( fValue );
127 ScDPDateGroupEditHelper::ScDPDateGroupEditHelper(
128 RadioButton* pRbAuto, RadioButton* pRbMan, DateField* pEdValue, const Date& rNullDate ) :
129 ScDPGroupEditHelper( pRbAuto, pRbMan, pEdValue ),
130 mpEdValue( pEdValue ),
131 maNullDate( rNullDate )
135 bool ScDPDateGroupEditHelper::ImplGetValue( double& rfValue ) const
137 rfValue = mpEdValue->GetDate() - maNullDate;
138 return true;
141 void ScDPDateGroupEditHelper::ImplSetValue( double fValue )
143 Date aDate( maNullDate );
144 aDate += static_cast< sal_Int32 >( fValue );
145 mpEdValue->SetDate( aDate );
148 ScDPNumGroupDlg::ScDPNumGroupDlg( vcl::Window* pParent, const ScDPNumGroupInfo& rInfo ) :
149 ModalDialog ( pParent, "PivotTableGroupByNumber", "modules/scalc/ui/groupbynumber.ui" ),
150 mpRbAutoStart ( get<RadioButton>("auto_start") ),
151 mpRbManStart ( get<RadioButton>("manual_start") ),
152 mpEdStart ( get<ScDoubleField> ("edit_start") ),
153 mpRbAutoEnd ( get<RadioButton> ( "auto_end" ) ),
154 mpRbManEnd ( get<RadioButton> ("manual_end") ),
155 mpEdEnd ( get<ScDoubleField>( "edit_end") ),
156 mpEdBy ( get<ScDoubleField> ("edit_by") ),
157 maStartHelper ( mpRbAutoStart, mpRbManStart, mpEdStart ),
158 maEndHelper ( mpRbAutoEnd, mpRbManEnd, mpEdEnd )
161 maStartHelper.SetValue( rInfo.mbAutoStart, rInfo.mfStart );
162 maEndHelper.SetValue( rInfo.mbAutoEnd, rInfo.mfEnd );
163 mpEdBy->SetValue( (rInfo.mfStep <= 0.0) ? 1.0 : rInfo.mfStep );
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( mpEdStart->IsEnabled() )
168 mpEdStart->GrabFocus();
169 else if( mpEdEnd->IsEnabled() )
170 mpEdEnd->GrabFocus();
171 else
172 mpEdBy->GrabFocus();
175 ScDPNumGroupDlg::~ScDPNumGroupDlg()
177 disposeOnce();
180 void ScDPNumGroupDlg::dispose()
182 mpRbAutoStart.clear();
183 mpRbManStart.clear();
184 mpEdStart.clear();
185 mpRbAutoEnd.clear();
186 mpRbManEnd.clear();
187 mpEdEnd.clear();
188 mpEdBy.clear();
189 ModalDialog::dispose();
193 ScDPNumGroupInfo ScDPNumGroupDlg::GetGroupInfo() const
195 ScDPNumGroupInfo aInfo;
196 aInfo.mbEnable = true;
197 aInfo.mbDateValues = false;
198 aInfo.mbAutoStart = maStartHelper.IsAuto();
199 aInfo.mbAutoEnd = maEndHelper.IsAuto();
201 // get values and silently auto-correct them, if they are not valid
202 // TODO: error messages in OK event?
203 aInfo.mfStart = maStartHelper.GetValue();
204 aInfo.mfEnd = maEndHelper.GetValue();
205 if( !mpEdBy->GetValue( aInfo.mfStep ) || (aInfo.mfStep <= 0.0) )
206 aInfo.mfStep = 1.0;
207 if( aInfo.mfEnd <= aInfo.mfStart )
208 aInfo.mfEnd = aInfo.mfStart + aInfo.mfStep;
210 return aInfo;
213 ScDPDateGroupDlg::ScDPDateGroupDlg( vcl::Window* pParent,
214 const ScDPNumGroupInfo& rInfo, sal_Int32 nDatePart, const Date& rNullDate ) :
215 ModalDialog( pParent, "PivotTableGroupByDate", "modules/scalc/ui/groupbydate.ui" ),
216 mpRbAutoStart ( get<RadioButton>("auto_start") ),
217 mpRbManStart ( get<RadioButton>("manual_start") ),
218 mpEdStart ( get<DateField>("start_date") ),
219 mpRbAutoEnd ( get<RadioButton>("auto_end") ),
220 mpRbManEnd ( get<RadioButton>("manual_end") ),
221 mpEdEnd ( get<DateField>("end_date") ),
222 mpRbNumDays ( get<RadioButton>("days") ),
223 mpRbUnits ( get<RadioButton>("intervals") ),
224 mpEdNumDays ( get<NumericField>("days_value") ),
225 mpLbUnits ( get<SvxCheckListBox>("interval_list") ),
226 mpBtnOk ( get<OKButton>("ok") ),
227 maStartHelper ( mpRbAutoStart, mpRbManStart, mpEdStart, rNullDate ),
228 maEndHelper ( mpRbAutoEnd, mpRbManEnd, mpEdEnd, rNullDate )
230 static const size_t nCount = SAL_N_ELEMENTS( nDatePartResIds );
231 for(sal_uInt16 nDatePartResId : nDatePartResIds)
232 mpLbUnits->InsertEntry( ScGlobal::GetRscString( nDatePartResId ) );
234 mpEdStart->SetShowDateCentury( true );
235 mpEdEnd->SetShowDateCentury( true );
237 maStartHelper.SetValue( rInfo.mbAutoStart, rInfo.mfStart );
238 maEndHelper.SetValue( rInfo.mbAutoEnd, rInfo.mfEnd );
240 if( nDatePart == 0 )
241 nDatePart = css::sheet::DataPilotFieldGroupBy::MONTHS;
242 for( size_t nIdx = 0; nIdx < nCount; ++nIdx )
243 mpLbUnits->CheckEntryPos( static_cast< sal_uInt16 >( nIdx ), (nDatePart & spnDateParts[ nIdx ]) != 0 );
245 if( rInfo.mbDateValues )
247 mpRbNumDays->Check();
248 ClickHdl( mpRbNumDays );
250 double fNumDays = rInfo.mfStep;
251 if( fNumDays < 1.0 )
252 fNumDays = 1.0;
253 else if( fNumDays > 32767.0 )
254 fNumDays = 32767.0;
255 mpEdNumDays->SetValue( static_cast< long >( fNumDays ) );
257 else
259 mpRbUnits->Check();
260 ClickHdl( mpRbUnits );
263 /* Set the initial focus, currently it is somewhere after calling all the radio
264 button click handlers. Now the first enabled editable control is focused. */
265 if( mpEdStart->IsEnabled() )
266 mpEdStart->GrabFocus();
267 else if( mpEdEnd->IsEnabled() )
268 mpEdEnd->GrabFocus();
269 else if( mpEdNumDays->IsEnabled() )
270 mpEdNumDays->GrabFocus();
271 else if( mpLbUnits->IsEnabled() )
272 mpLbUnits->GrabFocus();
274 mpRbNumDays->SetClickHdl( LINK( this, ScDPDateGroupDlg, ClickHdl ) );
275 mpRbUnits->SetClickHdl( LINK( this, ScDPDateGroupDlg, ClickHdl ) );
276 mpLbUnits->SetCheckButtonHdl( LINK( this, ScDPDateGroupDlg, CheckHdl ) );
279 ScDPDateGroupDlg::~ScDPDateGroupDlg()
281 disposeOnce();
284 void ScDPDateGroupDlg::dispose()
286 mpRbAutoStart.clear();
287 mpRbManStart.clear();
288 mpEdStart.clear();
289 mpRbAutoEnd.clear();
290 mpRbManEnd.clear();
291 mpEdEnd.clear();
292 mpRbNumDays.clear();
293 mpRbUnits.clear();
294 mpEdNumDays.clear();
295 mpLbUnits.clear();
296 mpBtnOk.clear();
297 ModalDialog::dispose();
300 ScDPNumGroupInfo ScDPDateGroupDlg::GetGroupInfo() const
302 ScDPNumGroupInfo aInfo;
303 aInfo.mbEnable = true;
304 aInfo.mbDateValues = mpRbNumDays->IsChecked();
305 aInfo.mbAutoStart = maStartHelper.IsAuto();
306 aInfo.mbAutoEnd = maEndHelper.IsAuto();
308 // get values and silently auto-correct them, if they are not valid
309 // TODO: error messages in OK event?
310 aInfo.mfStart = maStartHelper.GetValue();
311 aInfo.mfEnd = maEndHelper.GetValue();
312 sal_Int64 nNumDays = mpEdNumDays->GetValue();
313 aInfo.mfStep = static_cast<double>( aInfo.mbDateValues ? nNumDays : 0L );
314 if( aInfo.mfEnd <= aInfo.mfStart )
315 aInfo.mfEnd = aInfo.mfStart + nNumDays;
317 return aInfo;
320 sal_Int32 ScDPDateGroupDlg::GetDatePart() const
322 // return DAYS for special "number of days" mode
323 if( mpRbNumDays->IsChecked() )
324 return css::sheet::DataPilotFieldGroupBy::DAYS;
326 // return listbox contents for "units" mode
327 sal_Int32 nDatePart = 0;
328 for( sal_uLong nIdx = 0, nCount = mpLbUnits->GetEntryCount(); nIdx < nCount; ++nIdx )
329 if( mpLbUnits->IsChecked( static_cast< sal_uInt16 >( nIdx ) ) )
330 nDatePart |= spnDateParts[ nIdx ];
331 return nDatePart;
334 IMPL_LINK_TYPED( ScDPDateGroupDlg, ClickHdl, Button*, pButton, void )
336 if( pButton == mpRbNumDays )
338 mpLbUnits->Disable();
339 // enable and set focus to edit field on clicking "num of days" radio button
340 mpEdNumDays->Enable();
341 mpEdNumDays->GrabFocus();
342 mpBtnOk->Enable();
344 else if( pButton == mpRbUnits )
346 mpEdNumDays->Disable();
347 // enable and set focus to listbox on clicking "units" radio button
348 mpLbUnits->Enable();
349 mpLbUnits->GrabFocus();
350 // disable OK button if no date part selected
351 CheckHdl( mpLbUnits );
355 IMPL_LINK_TYPED( ScDPDateGroupDlg, CheckHdl, SvTreeListBox*, pListBox, void )
357 // enable/disable OK button on modifying check list box
358 if( pListBox == mpLbUnits )
359 mpBtnOk->Enable( mpLbUnits->GetCheckedEntryCount() > 0 );
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */