Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / controller / dialogs / tp_RangeChooser.cxx
blobddd8500423a08ca1878ca4ba5bb64cc9dcd1b702
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 #include "tp_RangeChooser.hxx"
21 #include <DataSourceHelper.hxx>
22 #include <DiagramHelper.hxx>
23 #include "ChartTypeTemplateProvider.hxx"
24 #include "DialogModel.hxx"
25 #include <RangeSelectionHelper.hxx>
26 #include <com/sun/star/awt/XTopWindow.hpp>
27 #include <com/sun/star/embed/EmbedStates.hpp>
28 #include <com/sun/star/embed/XComponentSupplier.hpp>
29 #include <svtools/miscopt.hxx>
31 namespace
33 void lcl_ShowChooserButton(
34 PushButton& rChooserButton,
35 bool bShow)
37 if( rChooserButton.IsVisible() != bShow )
39 rChooserButton.Show( bShow );
43 void lcl_enableRangeChoosing( bool bEnable, Dialog * pDialog )
45 if( pDialog )
47 pDialog->Show( !bEnable );
48 pDialog->SetModalInputMode( !bEnable );
52 } // anonymous namespace
54 namespace chart
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::chart2;
59 using ::com::sun::star::uno::Sequence;
61 RangeChooserTabPage::RangeChooserTabPage( vcl::Window* pParent
62 , DialogModel & rDialogModel
63 , ChartTypeTemplateProvider* pTemplateProvider
64 , Dialog * pParentDialog
65 , bool bHideDescription /* = false */ )
66 : OWizardPage( pParent
67 ,"tp_RangeChooser"
68 ,"modules/schart/ui/tp_RangeChooser.ui")
69 , m_nChangingControlCalls(0)
70 , m_bIsDirty(false)
71 , m_aLastValidRangeString()
72 , m_xCurrentChartTypeTemplate(nullptr)
73 , m_pTemplateProvider(pTemplateProvider)
74 , m_rDialogModel( rDialogModel )
75 , m_pParentDialog( pParentDialog )
76 , m_pTabPageNotifiable( dynamic_cast< TabPageNotifiable * >( pParentDialog ))
78 get(m_pFT_Caption, "FT_CAPTION_FOR_WIZARD");
79 get(m_pFT_Range, "FT_RANGE");
80 get(m_pED_Range, "ED_RANGE");
81 get(m_pIB_Range, "IB_RANGE");
82 get(m_pRB_Rows, "RB_DATAROWS");
83 get(m_pRB_Columns, "RB_DATACOLS");
84 get(m_pCB_FirstRowAsLabel, "CB_FIRST_ROW_ASLABELS");
85 get(m_pCB_FirstColumnAsLabel, "CB_FIRST_COLUMN_ASLABELS");
86 get(m_pFTTitle, "STR_PAGE_DATA_RANGE");// OH:remove later with dialog title
87 get(m_pFL_TimeBased, "separator1");
88 get(m_pCB_TimeBased, "CB_TIME_BASED");
89 get(m_pFT_TimeStart, "label1");
90 get(m_pEd_TimeStart, "ED_TIME_BASED_START");
91 get(m_pFT_TimeEnd, "label2");
92 get(m_pEd_TimeEnd, "ED_TIME_BASED_END");
94 m_pFT_Caption->Show(!bHideDescription);
96 SetText( m_pFTTitle->GetText());// OH:remove later with dialog
98 // set defaults as long as DetectArguments does not work
99 m_pRB_Columns->Check();
100 m_pCB_FirstColumnAsLabel->Check();
101 m_pCB_FirstRowAsLabel->Check();
103 // BM: Note, that the range selection is not available, if there is no view.
104 // This happens for charts having their own embedded spreadsheet. If you
105 // force to get the range selection here, this would mean when entering this
106 // page the calc view would be created in this case. So, I enable the
107 // button here, and in the worst case nothing happens when it is pressed.
108 // Not nice, but I see no better solution for the moment.
109 m_pIB_Range->SetClickHdl( LINK( this, RangeChooserTabPage, ChooseRangeHdl ));
111 // #i75179# enable setting the background to a different color
112 m_pED_Range->SetForceControlBackground(true);
114 m_pED_Range->SetUpdateDataHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ));
115 m_pED_Range->SetModifyHdl( LINK( this, RangeChooserTabPage, ControlEditedHdl ));
116 m_pRB_Rows->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedRadioHdl ) );
117 m_pCB_FirstRowAsLabel->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedCheckBoxHdl ) );
118 m_pCB_FirstColumnAsLabel->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedCheckBoxHdl ) );
119 m_pCB_TimeBased->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedCheckBoxHdl ) );
120 m_pEd_TimeStart->SetModifyHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
121 m_pEd_TimeEnd->SetModifyHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
123 SvtMiscOptions aOpts;
124 if ( !aOpts.IsExperimentalMode() )
126 m_pFL_TimeBased->Hide();
127 m_pCB_TimeBased->Hide();
128 m_pFT_TimeStart->Hide();
129 m_pEd_TimeStart->Hide();
130 m_pFT_TimeEnd->Hide();
131 m_pEd_TimeEnd->Hide();
135 RangeChooserTabPage::~RangeChooserTabPage()
137 disposeOnce();
140 void RangeChooserTabPage::dispose()
142 m_pFT_Caption.clear();
143 m_pFT_Range.clear();
144 m_pED_Range.clear();
145 m_pIB_Range.clear();
146 m_pRB_Rows.clear();
147 m_pRB_Columns.clear();
148 m_pCB_FirstRowAsLabel.clear();
149 m_pCB_FirstColumnAsLabel.clear();
150 m_pFTTitle.clear();
151 m_pFL_TimeBased.clear();
152 m_pCB_TimeBased.clear();
153 m_pFT_TimeStart.clear();
154 m_pEd_TimeStart.clear();
155 m_pFT_TimeEnd.clear();
156 m_pEd_TimeEnd.clear();
157 m_pParentDialog.clear();
158 OWizardPage::dispose();
162 void RangeChooserTabPage::ActivatePage()
164 OWizardPage::ActivatePage();
165 initControlsFromModel();
168 void RangeChooserTabPage::initControlsFromModel()
170 m_nChangingControlCalls++;
172 if(m_pTemplateProvider)
173 m_xCurrentChartTypeTemplate = m_pTemplateProvider->getCurrentTemplate();
175 bool bUseColumns = ! m_pRB_Rows->IsChecked();
176 bool bFirstCellAsLabel = bUseColumns ? m_pCB_FirstRowAsLabel->IsChecked() : m_pCB_FirstColumnAsLabel->IsChecked();
177 bool bHasCategories = bUseColumns ? m_pCB_FirstColumnAsLabel->IsChecked() : m_pCB_FirstRowAsLabel->IsChecked();
179 bool bIsValid = m_rDialogModel.allArgumentsForRectRangeDetected();
180 if( bIsValid )
181 m_rDialogModel.detectArguments(m_aLastValidRangeString, bUseColumns, bFirstCellAsLabel, bHasCategories );
182 else
183 m_aLastValidRangeString.clear();
185 m_pED_Range->SetText( m_aLastValidRangeString );
187 m_pRB_Rows->Check( !bUseColumns );
188 m_pRB_Columns->Check( bUseColumns );
190 m_pCB_FirstRowAsLabel->Check( m_pRB_Rows->IsChecked()?bHasCategories:bFirstCellAsLabel );
191 m_pCB_FirstColumnAsLabel->Check( m_pRB_Columns->IsChecked()?bHasCategories:bFirstCellAsLabel );
193 isValid();
195 m_nChangingControlCalls--;
198 void RangeChooserTabPage::DeactivatePage()
200 commitPage();
201 svt::OWizardPage::DeactivatePage();
204 void RangeChooserTabPage::commitPage()
206 commitPage(::svt::WizardTypes::eFinish);
209 bool RangeChooserTabPage::commitPage( ::svt::WizardTypes::CommitPageReason /*eReason*/ )
211 //ranges may have been edited in the meanwhile (dirty is true in that case here)
212 if( isValid() )
214 changeDialogModelAccordingToControls();
215 return true; // return false if this page should not be left
217 else
218 return false;
221 void RangeChooserTabPage::changeDialogModelAccordingToControls()
223 if(m_nChangingControlCalls>0)
224 return;
226 if( !m_xCurrentChartTypeTemplate.is() )
228 if(m_pTemplateProvider)
229 m_xCurrentChartTypeTemplate.set( m_pTemplateProvider->getCurrentTemplate());
230 if( !m_xCurrentChartTypeTemplate.is())
232 OSL_FAIL( "Need a template to change data source" );
233 return;
237 if( m_bIsDirty )
239 bool bFirstCellAsLabel = ( m_pCB_FirstColumnAsLabel->IsChecked() && !m_pRB_Columns->IsChecked() )
240 || ( m_pCB_FirstRowAsLabel->IsChecked() && !m_pRB_Rows->IsChecked() );
241 bool bHasCategories = ( m_pCB_FirstColumnAsLabel->IsChecked() && m_pRB_Columns->IsChecked() )
242 || ( m_pCB_FirstRowAsLabel->IsChecked() && m_pRB_Rows->IsChecked() );
243 bool bTimeBased = m_pCB_TimeBased->IsChecked();
245 Sequence< beans::PropertyValue > aArguments(
246 DataSourceHelper::createArguments(
247 m_pRB_Columns->IsChecked(), bFirstCellAsLabel, bHasCategories ) );
249 if(bTimeBased)
251 aArguments.realloc( aArguments.getLength() + 1 );
252 aArguments[aArguments.getLength() - 1] =
253 beans::PropertyValue( "TimeBased", -1, uno::Any(bTimeBased),
254 beans::PropertyState_DIRECT_VALUE );
257 // only if range is valid
258 if( m_aLastValidRangeString == m_pED_Range->GetText())
260 m_rDialogModel.setTemplate( m_xCurrentChartTypeTemplate );
261 aArguments.realloc( aArguments.getLength() + 1 );
262 aArguments[aArguments.getLength() - 1] =
263 beans::PropertyValue( "CellRangeRepresentation" , -1,
264 uno::Any( m_aLastValidRangeString ),
265 beans::PropertyState_DIRECT_VALUE );
266 m_rDialogModel.setData( aArguments );
267 m_bIsDirty = false;
269 if(bTimeBased)
271 sal_Int32 nStart = m_pEd_TimeStart->GetText().toInt32();
272 sal_Int32 nEnd = m_pEd_TimeEnd->GetText().toInt32();
273 m_rDialogModel.setTimeBasedRange(true, nStart, nEnd);
277 //@todo warn user that the selected range is not valid
278 //@todo better: disable OK-Button if range is invalid
282 bool RangeChooserTabPage::isValid()
284 OUString aRange( m_pED_Range->GetText());
285 bool bFirstCellAsLabel = ( m_pCB_FirstColumnAsLabel->IsChecked() && !m_pRB_Columns->IsChecked() )
286 || ( m_pCB_FirstRowAsLabel->IsChecked() && !m_pRB_Rows->IsChecked() );
287 bool bHasCategories = ( m_pCB_FirstColumnAsLabel->IsChecked() && m_pRB_Columns->IsChecked() )
288 || ( m_pCB_FirstRowAsLabel->IsChecked() && m_pRB_Rows->IsChecked() );
289 bool bIsValid = ( aRange.isEmpty() ) ||
290 m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
291 DataSourceHelper::createArguments(
292 aRange, Sequence< sal_Int32 >(), m_pRB_Columns->IsChecked(), bFirstCellAsLabel, bHasCategories ));
294 if( bIsValid )
296 m_pED_Range->SetControlForeground();
297 m_pED_Range->SetControlBackground();
298 if( m_pTabPageNotifiable )
299 m_pTabPageNotifiable->setValidPage( this );
300 m_aLastValidRangeString = aRange;
302 else
304 m_pED_Range->SetControlBackground( RANGE_SELECTION_INVALID_RANGE_BACKGROUND_COLOR );
305 m_pED_Range->SetControlForeground( RANGE_SELECTION_INVALID_RANGE_FOREGROUND_COLOR );
306 if( m_pTabPageNotifiable )
307 m_pTabPageNotifiable->setInvalidPage( this );
310 // enable/disable controls
311 // #i79531# if the range is valid but an action of one of these buttons
312 // would render it invalid, the button should be disabled
313 if( bIsValid )
315 bool bDataInColumns = m_pRB_Columns->IsChecked();
316 bool bIsSwappedRangeValid = m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
317 DataSourceHelper::createArguments(
318 aRange, Sequence< sal_Int32 >(), ! bDataInColumns, bHasCategories, bFirstCellAsLabel ));
319 m_pRB_Rows->Enable( bIsSwappedRangeValid );
320 m_pRB_Columns->Enable( bIsSwappedRangeValid );
322 m_pCB_FirstRowAsLabel->Enable(
323 m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
324 DataSourceHelper::createArguments(
325 aRange, Sequence< sal_Int32 >(), m_pRB_Columns->IsChecked(),
326 bDataInColumns ? ! bFirstCellAsLabel : bFirstCellAsLabel,
327 bDataInColumns ? bHasCategories : ! bHasCategories )));
328 m_pCB_FirstColumnAsLabel->Enable(
329 m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
330 DataSourceHelper::createArguments(
331 aRange, Sequence< sal_Int32 >(), m_pRB_Columns->IsChecked(),
332 bDataInColumns ? bFirstCellAsLabel : ! bFirstCellAsLabel,
333 bDataInColumns ? ! bHasCategories : bHasCategories )));
335 else
337 m_pRB_Rows->Enable( bIsValid );
338 m_pRB_Columns->Enable( bIsValid );
339 m_pCB_FirstRowAsLabel->Enable( bIsValid );
340 m_pCB_FirstColumnAsLabel->Enable( bIsValid );
343 bool bShowIB = m_rDialogModel.getRangeSelectionHelper()->hasRangeSelection();
344 lcl_ShowChooserButton( *m_pIB_Range, bShowIB );
346 return bIsValid;
349 IMPL_LINK_NOARG(RangeChooserTabPage, ControlEditedHdl, Edit&, void)
351 setDirty();
352 isValid();
355 IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedRadioHdl, RadioButton&, void)
357 ControlChangedHdl(*m_pED_Range);
360 IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedCheckBoxHdl, CheckBox&, void)
362 ControlChangedHdl(*m_pED_Range);
365 IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedHdl, Edit&, void)
367 setDirty();
368 if( isValid())
369 changeDialogModelAccordingToControls();
372 IMPL_LINK_NOARG(RangeChooserTabPage, ChooseRangeHdl, Button*, void)
374 OUString aRange = m_pED_Range->GetText();
375 OUString aTitle = m_pFTTitle->GetText();
377 lcl_enableRangeChoosing( true, m_pParentDialog );
378 m_rDialogModel.getRangeSelectionHelper()->chooseRange( aRange, aTitle, *this );
381 void RangeChooserTabPage::listeningFinished( const OUString & rNewRange )
383 //user has selected a new range
385 // rNewRange becomes invalid after removing the listener
386 OUString aRange( rNewRange );
388 m_rDialogModel.startControllerLockTimer();
390 // stop listening
391 m_rDialogModel.getRangeSelectionHelper()->stopRangeListening();
393 //update dialog state
394 ToTop();
395 GrabFocus();
396 m_pED_Range->SetText( aRange );
397 m_pED_Range->GrabFocus();
399 setDirty();
400 if( isValid())
401 changeDialogModelAccordingToControls();
403 lcl_enableRangeChoosing( false, m_pParentDialog );
405 void RangeChooserTabPage::disposingRangeSelection()
407 m_rDialogModel.getRangeSelectionHelper()->stopRangeListening( false );
410 void RangeChooserTabPage::setDirty()
412 if( m_nChangingControlCalls == 0 )
413 m_bIsDirty = true;
416 } //namespace chart
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */