Line data Source code
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 : #include "tp_RangeChooser.hxx"
21 : #include "Strings.hrc"
22 : #include "ResId.hxx"
23 : #include "macros.hxx"
24 : #include "DataSourceHelper.hxx"
25 : #include "DiagramHelper.hxx"
26 : #include "ChartTypeTemplateProvider.hxx"
27 : #include "DialogModel.hxx"
28 : #include "RangeSelectionHelper.hxx"
29 : #include <com/sun/star/awt/XTopWindow.hpp>
30 : #include <com/sun/star/embed/EmbedStates.hpp>
31 : #include <com/sun/star/embed/XComponentSupplier.hpp>
32 :
33 : namespace
34 : {
35 0 : void lcl_ShowChooserButton(
36 : PushButton& rChooserButton,
37 : bool bShow)
38 : {
39 0 : if( rChooserButton.IsVisible() != bShow )
40 : {
41 0 : rChooserButton.Show( bShow );
42 : }
43 0 : }
44 :
45 0 : void lcl_enableRangeChoosing( bool bEnable, Dialog * pDialog )
46 : {
47 0 : if( pDialog )
48 : {
49 0 : pDialog->Show( bEnable ? sal_False : sal_True );
50 0 : pDialog->SetModalInputMode( bEnable ? sal_False : sal_True );
51 : }
52 0 : }
53 :
54 : } // anonymous namespace
55 :
56 : namespace chart
57 : {
58 : using namespace ::com::sun::star;
59 : using namespace ::com::sun::star::chart2;
60 :
61 : using ::com::sun::star::uno::Reference;
62 : using ::com::sun::star::uno::Sequence;
63 :
64 0 : RangeChooserTabPage::RangeChooserTabPage( vcl::Window* pParent
65 : , DialogModel & rDialogModel
66 : , ChartTypeTemplateProvider* pTemplateProvider
67 : , Dialog * pParentDialog
68 : , bool bHideDescription /* = false */ )
69 : : OWizardPage( pParent
70 : ,"tp_RangeChooser"
71 : ,"modules/schart/ui/tp_RangeChooser.ui")
72 : , m_nChangingControlCalls(0)
73 : , m_bIsDirty(false)
74 : , m_xDataProvider( 0 )
75 : , m_aLastValidRangeString()
76 : , m_xCurrentChartTypeTemplate(0)
77 : , m_pTemplateProvider(pTemplateProvider)
78 : , m_rDialogModel( rDialogModel )
79 : , m_pParentDialog( pParentDialog )
80 0 : , m_pTabPageNotifiable( dynamic_cast< TabPageNotifiable * >( pParentDialog ))
81 : {
82 0 : get(m_pFT_Caption, "FT_CAPTION_FOR_WIZARD");
83 0 : get(m_pFT_Range, "FT_RANGE");
84 0 : get(m_pED_Range, "ED_RANGE");
85 0 : get(m_pIB_Range, "IB_RANGE");
86 0 : get(m_pRB_Rows, "RB_DATAROWS");
87 0 : get(m_pRB_Columns, "RB_DATACOLS");
88 0 : get(m_pCB_FirstRowAsLabel, "CB_FIRST_ROW_ASLABELS");
89 0 : get(m_pCB_FirstColumnAsLabel, "CB_FIRST_COLUMN_ASLABELS");
90 0 : get(m_pFTTitle, "STR_PAGE_DATA_RANGE");// OH:remove later with dialog title
91 0 : get(m_pCB_TimeBased, "CB_TIME_BASED");
92 0 : get(m_pEd_TimeStart, "ED_TIME_BASED_START");
93 0 : get(m_pEd_TimeEnd, "ED_TIME_BASED_END");
94 :
95 0 : m_pFT_Caption->Show(!bHideDescription);
96 :
97 0 : this->SetText( m_pFTTitle->GetText());// OH:remove later with dialog
98 :
99 : // set defaults as long as DetectArguments does not work
100 0 : m_pRB_Columns->Check();
101 0 : m_pCB_FirstColumnAsLabel->Check();
102 0 : m_pCB_FirstRowAsLabel->Check();
103 :
104 : // BM: Note, that the range selection is not available, if there is no view.
105 : // This happens for charts having their own embedded spreadsheet. If you
106 : // force to get the range selection here, this would mean when entering this
107 : // page the calc view would be created in this case. So, I enable the
108 : // button here, and in the worst case nothing happens when it is pressed.
109 : // Not nice, but I see no better solution for the moment.
110 0 : m_pIB_Range->SetClickHdl( LINK( this, RangeChooserTabPage, ChooseRangeHdl ));
111 :
112 : // #i75179# enable setting the background to a different color
113 0 : m_pED_Range->SetStyle( m_pED_Range->GetStyle() | WB_FORCECTRLBACKGROUND );
114 :
115 0 : m_pED_Range->SetUpdateDataHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ));
116 0 : m_pED_Range->SetModifyHdl( LINK( this, RangeChooserTabPage, ControlEditedHdl ));
117 0 : m_pRB_Rows->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
118 0 : m_pCB_FirstRowAsLabel->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
119 0 : m_pCB_FirstColumnAsLabel->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
120 0 : m_pCB_TimeBased->SetToggleHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
121 0 : m_pEd_TimeStart->SetModifyHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
122 0 : m_pEd_TimeEnd->SetModifyHdl( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
123 0 : }
124 :
125 0 : RangeChooserTabPage::~RangeChooserTabPage()
126 : {
127 0 : }
128 :
129 0 : void RangeChooserTabPage::ActivatePage()
130 : {
131 0 : OWizardPage::ActivatePage();
132 0 : initControlsFromModel();
133 0 : }
134 :
135 0 : void RangeChooserTabPage::initControlsFromModel()
136 : {
137 0 : m_nChangingControlCalls++;
138 :
139 0 : if(m_pTemplateProvider)
140 0 : m_xCurrentChartTypeTemplate = m_pTemplateProvider->getCurrentTemplate();
141 :
142 0 : bool bUseColumns = ! m_pRB_Rows->IsChecked();
143 0 : bool bFirstCellAsLabel = bUseColumns ? m_pCB_FirstRowAsLabel->IsChecked() : m_pCB_FirstColumnAsLabel->IsChecked();
144 0 : bool bHasCategories = bUseColumns ? m_pCB_FirstColumnAsLabel->IsChecked() : m_pCB_FirstRowAsLabel->IsChecked();
145 :
146 0 : bool bIsValid = m_rDialogModel.allArgumentsForRectRangeDetected();
147 0 : if( bIsValid )
148 0 : m_rDialogModel.detectArguments(m_aLastValidRangeString, bUseColumns, bFirstCellAsLabel, bHasCategories );
149 : else
150 0 : m_aLastValidRangeString = "";
151 :
152 0 : m_pED_Range->SetText( m_aLastValidRangeString );
153 :
154 0 : m_pRB_Rows->Check( !bUseColumns );
155 0 : m_pRB_Columns->Check( bUseColumns );
156 :
157 0 : m_pCB_FirstRowAsLabel->Check( m_pRB_Rows->IsChecked()?bHasCategories:bFirstCellAsLabel );
158 0 : m_pCB_FirstColumnAsLabel->Check( m_pRB_Columns->IsChecked()?bHasCategories:bFirstCellAsLabel );
159 :
160 0 : isValid();
161 :
162 0 : m_nChangingControlCalls--;
163 0 : }
164 :
165 0 : void RangeChooserTabPage::DeactivatePage()
166 : {
167 0 : commitPage();
168 0 : svt::OWizardPage::DeactivatePage();
169 0 : }
170 :
171 0 : void RangeChooserTabPage::commitPage()
172 : {
173 0 : commitPage(::svt::WizardTypes::eFinish);
174 0 : }
175 :
176 0 : bool RangeChooserTabPage::commitPage( ::svt::WizardTypes::CommitPageReason /*eReason*/ )
177 : {
178 : //ranges may have been edited in the meanwhile (dirty is true in that case here)
179 0 : if( isValid() )
180 : {
181 0 : changeDialogModelAccordingToControls();
182 0 : return true; // return false if this page should not be left
183 : }
184 : else
185 0 : return false;
186 : }
187 :
188 0 : void RangeChooserTabPage::changeDialogModelAccordingToControls()
189 : {
190 0 : if(m_nChangingControlCalls>0)
191 0 : return;
192 :
193 0 : if( !m_xCurrentChartTypeTemplate.is() )
194 : {
195 0 : if(m_pTemplateProvider)
196 0 : m_xCurrentChartTypeTemplate.set( m_pTemplateProvider->getCurrentTemplate());
197 0 : if( !m_xCurrentChartTypeTemplate.is())
198 : {
199 : OSL_FAIL( "Need a template to change data source" );
200 0 : return;
201 : }
202 : }
203 :
204 0 : if( m_bIsDirty )
205 : {
206 0 : bool bFirstCellAsLabel = ( m_pCB_FirstColumnAsLabel->IsChecked() && !m_pRB_Columns->IsChecked() )
207 0 : || ( m_pCB_FirstRowAsLabel->IsChecked() && !m_pRB_Rows->IsChecked() );
208 0 : bool bHasCategories = ( m_pCB_FirstColumnAsLabel->IsChecked() && m_pRB_Columns->IsChecked() )
209 0 : || ( m_pCB_FirstRowAsLabel->IsChecked() && m_pRB_Rows->IsChecked() );
210 0 : bool bTimeBased = m_pCB_TimeBased->IsChecked();
211 :
212 : Sequence< beans::PropertyValue > aArguments(
213 : DataSourceHelper::createArguments(
214 0 : m_pRB_Columns->IsChecked(), bFirstCellAsLabel, bHasCategories ) );
215 :
216 0 : if(bTimeBased)
217 : {
218 0 : aArguments.realloc( aArguments.getLength() + 1 );
219 0 : aArguments[aArguments.getLength() - 1] =
220 : beans::PropertyValue( "TimeBased", -1, uno::makeAny(bTimeBased),
221 0 : beans::PropertyState_DIRECT_VALUE );
222 : }
223 :
224 : // only if range is valid
225 0 : if( m_aLastValidRangeString.equals(m_pED_Range->GetText()))
226 : {
227 0 : m_rDialogModel.setTemplate( m_xCurrentChartTypeTemplate );
228 0 : aArguments.realloc( aArguments.getLength() + 1 );
229 0 : aArguments[aArguments.getLength() - 1] =
230 : beans::PropertyValue( "CellRangeRepresentation" , -1,
231 : uno::makeAny( m_aLastValidRangeString ),
232 0 : beans::PropertyState_DIRECT_VALUE );
233 0 : m_rDialogModel.setData( aArguments );
234 0 : m_bIsDirty = false;
235 :
236 0 : if(bTimeBased)
237 : {
238 0 : sal_Int32 nStart = m_pEd_TimeStart->GetText().toInt32();
239 0 : sal_Int32 nEnd = m_pEd_TimeEnd->GetText().toInt32();
240 0 : m_rDialogModel.setTimeBasedRange(true, nStart, nEnd);
241 : }
242 0 : }
243 :
244 : //@todo warn user that the selected range is not valid
245 : //@todo better: disable OK-Button if range is invalid
246 : }
247 : }
248 :
249 0 : bool RangeChooserTabPage::isValid()
250 : {
251 0 : OUString aRange( m_pED_Range->GetText());
252 0 : bool bFirstCellAsLabel = ( m_pCB_FirstColumnAsLabel->IsChecked() && !m_pRB_Columns->IsChecked() )
253 0 : || ( m_pCB_FirstRowAsLabel->IsChecked() && !m_pRB_Rows->IsChecked() );
254 0 : bool bHasCategories = ( m_pCB_FirstColumnAsLabel->IsChecked() && m_pRB_Columns->IsChecked() )
255 0 : || ( m_pCB_FirstRowAsLabel->IsChecked() && m_pRB_Rows->IsChecked() );
256 0 : bool bIsValid = ( aRange.isEmpty() ) ||
257 : m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
258 : DataSourceHelper::createArguments(
259 0 : aRange, Sequence< sal_Int32 >(), m_pRB_Columns->IsChecked(), bFirstCellAsLabel, bHasCategories ));
260 :
261 0 : if( bIsValid )
262 : {
263 0 : m_pED_Range->SetControlForeground();
264 0 : m_pED_Range->SetControlBackground();
265 0 : if( m_pTabPageNotifiable )
266 0 : m_pTabPageNotifiable->setValidPage( this );
267 0 : m_aLastValidRangeString = aRange;
268 : }
269 : else
270 : {
271 0 : m_pED_Range->SetControlBackground( RANGE_SELECTION_INVALID_RANGE_BACKGROUND_COLOR );
272 0 : m_pED_Range->SetControlForeground( RANGE_SELECTION_INVALID_RANGE_FOREGROUND_COLOR );
273 0 : if( m_pTabPageNotifiable )
274 0 : m_pTabPageNotifiable->setInvalidPage( this );
275 : }
276 :
277 : // enable/disable controls
278 : // #i79531# if the range is valid but an action of one of these buttons
279 : // would render it invalid, the button should be disabled
280 0 : if( bIsValid )
281 : {
282 0 : bool bDataInColumns = m_pRB_Columns->IsChecked();
283 : bool bIsSwappedRangeValid = m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
284 : DataSourceHelper::createArguments(
285 0 : aRange, Sequence< sal_Int32 >(), ! bDataInColumns, bHasCategories, bFirstCellAsLabel ));
286 0 : m_pRB_Rows->Enable( bIsSwappedRangeValid );
287 0 : m_pRB_Columns->Enable( bIsSwappedRangeValid );
288 :
289 : m_pCB_FirstRowAsLabel->Enable(
290 : m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
291 : DataSourceHelper::createArguments(
292 0 : aRange, Sequence< sal_Int32 >(), m_pRB_Columns->IsChecked(),
293 0 : bDataInColumns ? ! bFirstCellAsLabel : bFirstCellAsLabel,
294 0 : bDataInColumns ? bHasCategories : ! bHasCategories )));
295 : m_pCB_FirstColumnAsLabel->Enable(
296 : m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
297 : DataSourceHelper::createArguments(
298 0 : aRange, Sequence< sal_Int32 >(), m_pRB_Columns->IsChecked(),
299 0 : bDataInColumns ? bFirstCellAsLabel : ! bFirstCellAsLabel,
300 0 : bDataInColumns ? ! bHasCategories : bHasCategories )));
301 : }
302 : else
303 : {
304 0 : m_pRB_Rows->Enable( bIsValid );
305 0 : m_pRB_Columns->Enable( bIsValid );
306 0 : m_pCB_FirstRowAsLabel->Enable( bIsValid );
307 0 : m_pCB_FirstColumnAsLabel->Enable( bIsValid );
308 : }
309 :
310 0 : bool bShowIB = m_rDialogModel.getRangeSelectionHelper()->hasRangeSelection();
311 0 : lcl_ShowChooserButton( *m_pIB_Range, bShowIB );
312 :
313 0 : return bIsValid;
314 : }
315 :
316 0 : IMPL_LINK_NOARG(RangeChooserTabPage, ControlEditedHdl)
317 : {
318 0 : setDirty();
319 0 : isValid();
320 0 : return 0;
321 : }
322 :
323 0 : IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedHdl)
324 : {
325 0 : setDirty();
326 0 : if( isValid())
327 0 : changeDialogModelAccordingToControls();
328 0 : return 0;
329 : }
330 :
331 0 : IMPL_LINK_NOARG(RangeChooserTabPage, ChooseRangeHdl)
332 : {
333 0 : OUString aRange = m_pED_Range->GetText();
334 : // using assignment for broken gcc 3.3
335 0 : OUString aTitle = m_pFTTitle->GetText();
336 :
337 0 : lcl_enableRangeChoosing( true, m_pParentDialog );
338 0 : m_rDialogModel.getRangeSelectionHelper()->chooseRange( aRange, aTitle, *this );
339 :
340 0 : return 0;
341 : }
342 :
343 0 : void RangeChooserTabPage::listeningFinished( const OUString & rNewRange )
344 : {
345 : //user has selected a new range
346 :
347 0 : OUString aRange( rNewRange );
348 :
349 0 : m_rDialogModel.startControllerLockTimer();
350 :
351 : // stop listening
352 0 : m_rDialogModel.getRangeSelectionHelper()->stopRangeListening();
353 :
354 : //update dialog state
355 0 : ToTop();
356 0 : GrabFocus();
357 0 : m_pED_Range->SetText( aRange );
358 0 : m_pED_Range->GrabFocus();
359 :
360 0 : setDirty();
361 0 : if( isValid())
362 0 : changeDialogModelAccordingToControls();
363 :
364 0 : lcl_enableRangeChoosing( false, m_pParentDialog );
365 0 : }
366 0 : void RangeChooserTabPage::disposingRangeSelection()
367 : {
368 0 : m_rDialogModel.getRangeSelectionHelper()->stopRangeListening( false );
369 0 : }
370 :
371 0 : void RangeChooserTabPage::setDirty()
372 : {
373 0 : if( m_nChangingControlCalls == 0 )
374 0 : m_bIsDirty = true;
375 0 : }
376 :
377 102 : } //namespace chart
378 :
379 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|