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