Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/chart2/source/controller/dialogs/tp_RangeChooser.cxx
Line
Count
Source
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 <DataSourceHelper.hxx>
22
#include <ChartTypeTemplateProvider.hxx>
23
#include <ChartTypeTemplate.hxx>
24
#include "DialogModel.hxx"
25
#include <RangeSelectionHelper.hxx>
26
#include <TabPageNotifiable.hxx>
27
#include <com/sun/star/beans/PropertyState.hpp>
28
#include <com/sun/star/beans/PropertyValue.hpp>
29
#include <officecfg/Office/Common.hxx>
30
#include <osl/diagnose.h>
31
#include <vcl/weld/Builder.hxx>
32
33
namespace
34
{
35
    void lcl_ShowChooserButton(
36
        weld::Button& rChooserButton,
37
        bool bShow)
38
0
    {
39
0
        if( rChooserButton.get_visible() != bShow )
40
0
        {
41
0
            rChooserButton.set_visible( bShow );
42
0
        }
43
0
    }
44
45
} // anonymous namespace
46
47
namespace chart
48
{
49
using namespace ::com::sun::star;
50
using namespace ::com::sun::star::chart2;
51
52
using ::com::sun::star::uno::Sequence;
53
54
RangeChooserTabPage::RangeChooserTabPage(weld::Container* pPage, weld::DialogController* pController, DialogModel & rDialogModel,
55
                                         ChartTypeTemplateProvider* pTemplateProvider,
56
                                         bool bHideDescription /* = false */)
57
0
    : OWizardPage(pPage, pController, u"modules/schart/ui/tp_RangeChooser.ui"_ustr, u"tp_RangeChooser"_ustr)
58
0
    , m_nChangingControlCalls(0)
59
0
    , m_bIsDirty(false)
60
0
    , m_pTemplateProvider(pTemplateProvider)
61
0
    , m_rDialogModel( rDialogModel )
62
0
    , m_pTabPageNotifiable(dynamic_cast<TabPageNotifiable*>(pController))
63
0
    , m_xFT_Caption(m_xBuilder->weld_label(u"FT_CAPTION_FOR_WIZARD"_ustr))
64
0
    , m_xED_Range(m_xBuilder->weld_entry(u"ED_RANGE"_ustr))
65
0
    , m_xIB_Range(m_xBuilder->weld_button(u"IB_RANGE"_ustr))
66
0
    , m_xRB_Rows(m_xBuilder->weld_radio_button(u"RB_DATAROWS"_ustr))
67
0
    , m_xRB_Columns(m_xBuilder->weld_radio_button(u"RB_DATACOLS"_ustr))
68
0
    , m_xCB_FirstRowAsLabel(m_xBuilder->weld_check_button(u"CB_FIRST_ROW_ASLABELS"_ustr))
69
0
    , m_xCB_FirstColumnAsLabel(m_xBuilder->weld_check_button(u"CB_FIRST_COLUMN_ASLABELS"_ustr))
70
0
    , m_xFTTitle(m_xBuilder->weld_label(u"STR_PAGE_DATA_RANGE"_ustr))
71
0
    , m_xFL_TimeBased(m_xBuilder->weld_widget(u"separator1"_ustr))
72
0
    , m_xCB_TimeBased(m_xBuilder->weld_check_button(u"CB_TIME_BASED"_ustr))
73
0
    , m_xFT_TimeStart(m_xBuilder->weld_label(u"label1"_ustr))
74
0
    , m_xEd_TimeStart(m_xBuilder->weld_entry(u"ED_TIME_BASED_START"_ustr))
75
0
    , m_xFT_TimeEnd(m_xBuilder->weld_label(u"label2"_ustr))
76
0
    , m_xEd_TimeEnd(m_xBuilder->weld_entry(u"ED_TIME_BASED_END"_ustr))
77
0
{
78
0
    m_xFT_Caption->set_visible(!bHideDescription);
79
80
0
    SetPageTitle(m_xFTTitle->get_label());// OH:remove later with dialog
81
82
    // set defaults as long as DetectArguments does not work
83
0
    m_xRB_Columns->set_active(true);
84
0
    m_xCB_FirstColumnAsLabel->set_active(true);
85
0
    m_xCB_FirstRowAsLabel->set_active(true);
86
87
    // BM: Note, that the range selection is not available, if there is no view.
88
    // This happens for charts having their own embedded spreadsheet.  If you
89
    // force to get the range selection here, this would mean when entering this
90
    // page the calc view would be created in this case.  So, I enable the
91
    // button here, and in the worst case nothing happens when it is pressed.
92
    // Not nice, but I see no better solution for the moment.
93
0
    m_xIB_Range->connect_clicked( LINK( this, RangeChooserTabPage, ChooseRangeHdl ));
94
95
0
    m_xED_Range->connect_changed( LINK( this, RangeChooserTabPage, ControlEditedHdl ));
96
0
    m_xRB_Rows->connect_toggled( LINK( this, RangeChooserTabPage, ControlChangedRadioHdl ) );
97
0
    m_xCB_FirstRowAsLabel->connect_toggled( LINK( this, RangeChooserTabPage, ControlChangedCheckBoxHdl ) );
98
0
    m_xCB_FirstColumnAsLabel->connect_toggled( LINK( this, RangeChooserTabPage, ControlChangedCheckBoxHdl ) );
99
0
    m_xCB_TimeBased->connect_toggled( LINK( this, RangeChooserTabPage, ControlChangedCheckBoxHdl ) );
100
0
    m_xEd_TimeStart->connect_changed( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
101
0
    m_xEd_TimeEnd->connect_changed( LINK( this, RangeChooserTabPage, ControlChangedHdl ) );
102
103
0
    if ( !officecfg::Office::Common::Misc::ExperimentalMode::get() )
104
0
    {
105
0
        m_xFL_TimeBased->hide();
106
0
        m_xCB_TimeBased->hide();
107
0
        m_xFT_TimeStart->hide();
108
0
        m_xEd_TimeStart->hide();
109
0
        m_xFT_TimeEnd->hide();
110
0
        m_xEd_TimeEnd->hide();
111
0
    }
112
0
}
113
114
RangeChooserTabPage::~RangeChooserTabPage()
115
0
{
116
0
}
117
118
void RangeChooserTabPage::Activate()
119
0
{
120
0
    OWizardPage::Activate();
121
0
    initControlsFromModel();
122
0
    m_xED_Range->grab_focus();
123
0
}
124
125
void RangeChooserTabPage::initControlsFromModel()
126
0
{
127
0
    m_nChangingControlCalls++;
128
129
0
    if(m_pTemplateProvider)
130
0
        m_xCurrentChartTypeTemplate = m_pTemplateProvider->getCurrentTemplate();
131
132
0
    bool bUseColumns = !m_xRB_Rows->get_active();
133
0
    bool bFirstCellAsLabel = bUseColumns ? m_xCB_FirstRowAsLabel->get_active() : m_xCB_FirstColumnAsLabel->get_active();
134
0
    bool bHasCategories = bUseColumns ? m_xCB_FirstColumnAsLabel->get_active() : m_xCB_FirstRowAsLabel->get_active();
135
136
0
    bool bIsValid = m_rDialogModel.allArgumentsForRectRangeDetected();
137
0
    if( bIsValid )
138
0
        m_rDialogModel.detectArguments(m_aLastValidRangeString, bUseColumns, bFirstCellAsLabel, bHasCategories );
139
0
    else
140
0
        m_aLastValidRangeString.clear();
141
142
0
    m_xED_Range->set_text( m_aLastValidRangeString );
143
144
0
    m_xRB_Rows->set_active( !bUseColumns );
145
0
    m_xRB_Columns->set_active(  bUseColumns );
146
147
0
    m_xCB_FirstRowAsLabel->set_active( m_xRB_Rows->get_active()?bHasCategories:bFirstCellAsLabel  );
148
0
    m_xCB_FirstColumnAsLabel->set_active( m_xRB_Columns->get_active()?bHasCategories:bFirstCellAsLabel  );
149
150
0
    isValid();
151
152
0
    m_nChangingControlCalls--;
153
0
}
154
155
void RangeChooserTabPage::Deactivate()
156
0
{
157
0
    commitPage();
158
0
    vcl::OWizardPage::Deactivate();
159
0
}
160
161
void RangeChooserTabPage::commitPage()
162
0
{
163
0
    commitPage(::vcl::WizardTypes::eFinish);
164
0
}
165
166
bool RangeChooserTabPage::commitPage( ::vcl::WizardTypes::CommitPageReason /*eReason*/ )
167
0
{
168
    //ranges may have been edited in the meanwhile (dirty is true in that case here)
169
0
    if( isValid() )
170
0
    {
171
0
        changeDialogModelAccordingToControls();
172
0
        return true; // return false if this page should not be left
173
0
    }
174
0
    else
175
0
        return false;
176
0
}
177
178
void RangeChooserTabPage::changeDialogModelAccordingToControls()
179
0
{
180
0
    if(m_nChangingControlCalls>0)
181
0
        return;
182
183
0
    if( !m_xCurrentChartTypeTemplate.is() )
184
0
    {
185
0
        if(m_pTemplateProvider)
186
0
            m_xCurrentChartTypeTemplate = m_pTemplateProvider->getCurrentTemplate();
187
0
        if( !m_xCurrentChartTypeTemplate.is())
188
0
        {
189
0
            OSL_FAIL( "Need a template to change data source" );
190
0
            return;
191
0
        }
192
0
    }
193
194
0
    if( !m_bIsDirty )
195
0
        return;
196
197
0
    bool bFirstCellAsLabel = ( m_xCB_FirstColumnAsLabel->get_active() && !m_xRB_Columns->get_active() )
198
0
        || ( m_xCB_FirstRowAsLabel->get_active()    && !m_xRB_Rows->get_active() );
199
0
    bool bHasCategories = ( m_xCB_FirstColumnAsLabel->get_active() && m_xRB_Columns->get_active() )
200
0
        || ( m_xCB_FirstRowAsLabel->get_active()    && m_xRB_Rows->get_active() );
201
0
    bool bTimeBased = m_xCB_TimeBased->get_active();
202
203
0
    Sequence< beans::PropertyValue > aArguments(
204
0
        DataSourceHelper::createArguments(
205
0
            m_xRB_Columns->get_active(), bFirstCellAsLabel, bHasCategories ) );
206
207
0
    if(bTimeBased)
208
0
    {
209
0
        aArguments.realloc( aArguments.getLength() + 1 );
210
0
        aArguments.getArray()[aArguments.getLength() - 1] =
211
0
            beans::PropertyValue( u"TimeBased"_ustr, -1, uno::Any(bTimeBased),
212
0
                    beans::PropertyState_DIRECT_VALUE );
213
0
    }
214
215
    // only if range is valid
216
0
    if( m_aLastValidRangeString != m_xED_Range->get_text())
217
0
        return;
218
219
0
    m_rDialogModel.setTemplate( m_xCurrentChartTypeTemplate );
220
0
    aArguments.realloc( aArguments.getLength() + 1 );
221
0
    aArguments.getArray()[aArguments.getLength() - 1] =
222
0
        beans::PropertyValue( u"CellRangeRepresentation"_ustr , -1,
223
0
                              uno::Any( m_aLastValidRangeString ),
224
0
                              beans::PropertyState_DIRECT_VALUE );
225
0
    m_rDialogModel.setData( aArguments );
226
0
    m_bIsDirty = false;
227
228
0
    if(bTimeBased)
229
0
    {
230
0
        sal_Int32 nStart = m_xEd_TimeStart->get_text().toInt32();
231
0
        sal_Int32 nEnd = m_xEd_TimeEnd->get_text().toInt32();
232
0
        m_rDialogModel.setTimeBasedRange(true, nStart, nEnd);
233
0
    }
234
235
    //@todo warn user that the selected range is not valid
236
    //@todo better: disable OK-Button if range is invalid
237
0
}
238
239
bool RangeChooserTabPage::isValid()
240
0
{
241
0
    OUString aRange( m_xED_Range->get_text());
242
0
    bool bFirstCellAsLabel = ( m_xCB_FirstColumnAsLabel->get_active() && !m_xRB_Columns->get_active() )
243
0
        || ( m_xCB_FirstRowAsLabel->get_active()    && !m_xRB_Rows->get_active() );
244
0
    bool bHasCategories = ( m_xCB_FirstColumnAsLabel->get_active() && m_xRB_Columns->get_active() )
245
0
        || ( m_xCB_FirstRowAsLabel->get_active()    && m_xRB_Rows->get_active() );
246
0
    bool bIsValid = ( aRange.isEmpty() ) ||
247
0
        m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
248
0
            DataSourceHelper::createArguments(
249
0
                aRange, Sequence< sal_Int32 >(), m_xRB_Columns->get_active(), bFirstCellAsLabel, bHasCategories ));
250
251
0
    if( bIsValid )
252
0
    {
253
0
        m_xED_Range->set_message_type(weld::EntryMessageType::Normal);
254
0
        if( m_pTabPageNotifiable )
255
0
            m_pTabPageNotifiable->setValidPage( this );
256
0
        m_aLastValidRangeString = aRange;
257
0
    }
258
0
    else
259
0
    {
260
0
        m_xED_Range->set_message_type(weld::EntryMessageType::Error);
261
0
        if( m_pTabPageNotifiable )
262
0
            m_pTabPageNotifiable->setInvalidPage( this );
263
0
    }
264
265
    // enable/disable controls
266
    // #i79531# if the range is valid but an action of one of these buttons
267
    // would render it invalid, the button should be disabled
268
0
    if( bIsValid )
269
0
    {
270
0
        bool bDataInColumns = m_xRB_Columns->get_active();
271
0
        bool bIsSwappedRangeValid = m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
272
0
            DataSourceHelper::createArguments(
273
0
                aRange, Sequence< sal_Int32 >(), ! bDataInColumns, bHasCategories, bFirstCellAsLabel ));
274
0
        m_xRB_Rows->set_sensitive( bIsSwappedRangeValid );
275
0
        m_xRB_Columns->set_sensitive( bIsSwappedRangeValid );
276
277
0
        m_xCB_FirstRowAsLabel->set_sensitive(
278
0
            m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
279
0
                DataSourceHelper::createArguments(
280
0
                    aRange, Sequence< sal_Int32 >(), m_xRB_Columns->get_active(),
281
0
                    bDataInColumns ? ! bFirstCellAsLabel : bFirstCellAsLabel,
282
0
                    bDataInColumns ? bHasCategories : ! bHasCategories )));
283
0
        m_xCB_FirstColumnAsLabel->set_sensitive(
284
0
            m_rDialogModel.getRangeSelectionHelper()->verifyArguments(
285
0
                DataSourceHelper::createArguments(
286
0
                    aRange, Sequence< sal_Int32 >(), m_xRB_Columns->get_active(),
287
0
                    bDataInColumns ? bFirstCellAsLabel : ! bFirstCellAsLabel,
288
0
                    bDataInColumns ? ! bHasCategories : bHasCategories )));
289
0
    }
290
0
    else
291
0
    {
292
0
        m_xRB_Rows->set_sensitive( bIsValid );
293
0
        m_xRB_Columns->set_sensitive( bIsValid );
294
0
        m_xCB_FirstRowAsLabel->set_sensitive( bIsValid );
295
0
        m_xCB_FirstColumnAsLabel->set_sensitive( bIsValid );
296
0
    }
297
298
0
    bool bShowIB = m_rDialogModel.getRangeSelectionHelper()->hasRangeSelection();
299
0
    lcl_ShowChooserButton( *m_xIB_Range, bShowIB );
300
301
0
    return bIsValid;
302
0
}
303
304
IMPL_LINK_NOARG(RangeChooserTabPage, ControlEditedHdl, weld::Entry&, void)
305
0
{
306
0
    setDirty();
307
0
    isValid();
308
0
}
309
310
IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedRadioHdl, weld::Toggleable&, void)
311
0
{
312
0
    ControlChangedHdl(*m_xED_Range);
313
0
}
314
315
IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedCheckBoxHdl, weld::Toggleable&, void)
316
0
{
317
0
    ControlChangedHdl(*m_xED_Range);
318
0
}
319
320
IMPL_LINK_NOARG(RangeChooserTabPage, ControlChangedHdl, weld::Entry&, void)
321
0
{
322
0
    setDirty();
323
0
    if( isValid())
324
0
        changeDialogModelAccordingToControls();
325
0
}
326
327
IMPL_LINK_NOARG(RangeChooserTabPage, ChooseRangeHdl, weld::Button&, void)
328
0
{
329
0
    OUString aRange = m_xED_Range->get_text();
330
0
    OUString aTitle = m_xFTTitle->get_label();
331
332
0
    enableRangeChoosing(true, m_pDialogController);
333
0
    m_rDialogModel.getRangeSelectionHelper()->chooseRange( aRange, aTitle, *this );
334
0
}
335
336
void RangeChooserTabPage::listeningFinished( const OUString & rNewRange )
337
0
{
338
    //user has selected a new range
339
340
    // rNewRange becomes invalid after removing the listener
341
0
    OUString aRange( rNewRange );
342
343
0
    m_rDialogModel.startControllerLockTimer();
344
345
    // stop listening
346
0
    m_rDialogModel.getRangeSelectionHelper()->stopRangeListening();
347
348
    //update dialog state
349
0
    m_xED_Range->set_text(aRange);
350
0
    m_xED_Range->grab_focus();
351
352
0
    setDirty();
353
0
    if( isValid())
354
0
        changeDialogModelAccordingToControls();
355
356
0
    enableRangeChoosing(false, m_pDialogController);
357
0
}
358
359
void RangeChooserTabPage::disposingRangeSelection()
360
0
{
361
0
    m_rDialogModel.getRangeSelectionHelper()->stopRangeListening( false );
362
0
}
363
364
void RangeChooserTabPage::setDirty()
365
0
{
366
0
    if( m_nChangingControlCalls == 0 )
367
0
        m_bIsDirty = true;
368
0
}
369
370
} //namespace chart
371
372
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */