summaryrefslogtreecommitdiffstats
path: root/chart2/source/controller/dialogs/res_ErrorBar.cxx
blob: 12fabc659dff4ecd0dbf2085174c2f05e06aa547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <res_ErrorBar.hxx>
#include <bitmaps.hlst>
#include <RangeSelectionHelper.hxx>
#include <helpids.h>
#include <chartview/ChartSfxItemIds.hxx>
#include <vcl/weld.hxx>
#include <ChartModel.hxx>

#include <rtl/math.hxx>
#include <tools/diagnose_ex.h>
#include <osl/diagnose.h>
#include <svl/stritem.hxx>

#define CHART_LB_FUNCTION_STD_ERROR     0
#define CHART_LB_FUNCTION_STD_DEV       1
#define CHART_LB_FUNCTION_VARIANCE      2
#define CHART_LB_FUNCTION_ERROR_MARGIN  3

using namespace ::com::sun::star;

namespace
{
void lcl_enableRangeChoosing(bool bEnable, weld::DialogController* pController)
{
    weld::Window* pWeldDialog = pController->getDialog();
    pWeldDialog->set_modal(!bEnable);
    pWeldDialog->set_visible(!bEnable);
}

sal_uInt16 lcl_getLbEntryPosByErrorKind( SvxChartKindError eErrorKind )
{
    sal_uInt16 nResult = 0;
    switch( eErrorKind )
    {
        // for these cases select the default in the list box
        case SvxChartKindError::NONE:
        case SvxChartKindError::Percent:
        case SvxChartKindError::Const:
        case SvxChartKindError::Range:
            nResult = CHART_LB_FUNCTION_STD_DEV;
            break;
        case SvxChartKindError::Variant:
            nResult = CHART_LB_FUNCTION_VARIANCE;
            break;
        case SvxChartKindError::Sigma:
            nResult = CHART_LB_FUNCTION_STD_DEV;
            break;
        case SvxChartKindError::BigError:
            nResult = CHART_LB_FUNCTION_ERROR_MARGIN;
            break;
        case SvxChartKindError::StdError:
            nResult = CHART_LB_FUNCTION_STD_ERROR;
            break;
    }
    return nResult;
}
} // anonymous namespace

namespace chart
{

ErrorBarResources::ErrorBarResources(weld::Builder* pParent, weld::DialogController* pController,
                                     const SfxItemSet& rInAttrs, bool bNoneAvailable,
                                     tErrorBarType eType /* = ERROR_BAR_Y */ )
    : m_eErrorKind( SvxChartKindError::NONE )
    , m_eIndicate( SvxChartIndicate::Both )
    , m_bErrorKindUnique( true )
    , m_bIndicatorUnique( true )
    , m_bRangePosUnique( true )
    , m_bRangeNegUnique( true )
    , m_eErrorBarType( eType )
    , m_nConstDecimalDigits( 1 )
    , m_nConstSpinSize( 1 )
    , m_fPlusValue(0.0)
    , m_fMinusValue(0.0)
    , m_pController(pController)
    , m_pCurrentRangeChoosingField( nullptr )
    , m_bHasInternalDataProvider( true )
    , m_bEnableDataTableDialog( true )
    , m_xRbNone(pParent->weld_radio_button("RB_NONE"))
    , m_xRbConst(pParent->weld_radio_button("RB_CONST"))
    , m_xRbPercent(pParent->weld_radio_button("RB_PERCENT"))
    , m_xRbFunction(pParent->weld_radio_button("RB_FUNCTION"))
    , m_xRbRange(pParent->weld_radio_button("RB_RANGE"))
    , m_xLbFunction(pParent->weld_combo_box("LB_FUNCTION"))
    , m_xFlParameters(pParent->weld_frame("framePARAMETERS"))
    , m_xBxPositive(pParent->weld_widget("boxPOSITIVE"))
    , m_xMfPositive(pParent->weld_metric_spin_button("MF_POSITIVE", FieldUnit::NONE))
    , m_xEdRangePositive(pParent->weld_entry("ED_RANGE_POSITIVE"))
    , m_xIbRangePositive(pParent->weld_button("IB_RANGE_POSITIVE"))
    , m_xBxNegative(pParent->weld_widget("boxNEGATIVE"))
    , m_xMfNegative(pParent->weld_metric_spin_button("MF_NEGATIVE", FieldUnit::NONE))
    , m_xEdRangeNegative(pParent->weld_entry("ED_RANGE_NEGATIVE"))
    , m_xIbRangeNegative(pParent->weld_button("IB_RANGE_NEGATIVE"))
    , m_xCbSyncPosNeg(pParent->weld_check_button("CB_SYN_POS_NEG"))
    , m_xRbBoth(pParent->weld_radio_button("RB_BOTH"))
    , m_xRbPositive(pParent->weld_radio_button("RB_POSITIVE"))
    , m_xRbNegative(pParent->weld_radio_button("RB_NEGATIVE"))
    , m_xFiBoth(pParent->weld_image("FI_BOTH"))
    , m_xFiPositive(pParent->weld_image("FI_POSITIVE"))
    , m_xFiNegative(pParent->weld_image("FI_NEGATIVE"))
    , m_xUIStringPos(pParent->weld_label("STR_DATA_SELECT_RANGE_FOR_POSITIVE_ERRORBARS"))
    , m_xUIStringNeg(pParent->weld_label("STR_DATA_SELECT_RANGE_FOR_NEGATIVE_ERRORBARS"))
    , m_xUIStringRbRange(pParent->weld_label("STR_CONTROLTEXT_ERROR_BARS_FROM_DATA"))
{
    if( bNoneAvailable )
        m_xRbNone->connect_toggled(LINK(this, ErrorBarResources, CategoryChosen));
    else
        m_xRbNone->hide();

    m_xRbConst->connect_toggled( LINK( this, ErrorBarResources, CategoryChosen ));
    m_xRbPercent->connect_toggled( LINK( this, ErrorBarResources, CategoryChosen ));
    m_xRbFunction->connect_toggled( LINK( this, ErrorBarResources, CategoryChosen ));
    m_xRbRange->connect_toggled( LINK( this, ErrorBarResources, CategoryChosen ));
    m_xLbFunction->connect_changed( LINK( this, ErrorBarResources, CategoryChosen2 ));

    m_xCbSyncPosNeg->set_active( false );
    m_xCbSyncPosNeg->connect_toggled( LINK( this, ErrorBarResources, SynchronizePosAndNeg ));

    m_xMfPositive->connect_value_changed( LINK( this, ErrorBarResources, PosValueChanged ));
    m_xEdRangePositive->connect_changed( LINK( this, ErrorBarResources, RangeChanged ));
    m_xEdRangeNegative->connect_changed( LINK( this, ErrorBarResources, RangeChanged ));

    m_xRbPositive->connect_toggled( LINK( this, ErrorBarResources, IndicatorChanged ));
    m_xRbNegative->connect_toggled( LINK( this, ErrorBarResources, IndicatorChanged ));
    m_xRbBoth->connect_toggled( LINK( this, ErrorBarResources, IndicatorChanged ));

    m_xIbRangePositive->connect_clicked( LINK( this, ErrorBarResources, ChooseRange ));
    m_xIbRangeNegative->connect_clicked( LINK( this, ErrorBarResources, ChooseRange ));

    FillValueSets();
    Reset( rInAttrs );
}

ErrorBarResources::~ErrorBarResources()
{
}

void ErrorBarResources::SetErrorBarType( tErrorBarType eNewType )
{
    if( m_eErrorBarType != eNewType )
    {
        m_eErrorBarType = eNewType;
        FillValueSets();
    }
}

void ErrorBarResources::SetChartDocumentForRangeChoosing(
    const rtl::Reference<::chart::ChartModel> & xChartDocument )
{
    if( xChartDocument.is())
    {
        m_bHasInternalDataProvider = xChartDocument->hasInternalDataProvider();
        uno::Reference< beans::XPropertySet > xProps( static_cast<cppu::OWeakObject*>(xChartDocument.get()), uno::UNO_QUERY );
        if ( xProps.is() )
        {
            try
            {
                xProps->getPropertyValue("EnableDataTableDialog") >>= m_bEnableDataTableDialog;
            }
            catch( const uno::Exception& )
            {
                TOOLS_WARN_EXCEPTION("chart2", "" );
            }
        }
    }
    m_apRangeSelectionHelper.reset( new RangeSelectionHelper( xChartDocument ));

    // has internal data provider => rename "cell range" to "from data"
    OSL_ASSERT(m_apRangeSelectionHelper);
    if( m_bHasInternalDataProvider )
    {
        m_xRbRange->set_label(m_xUIStringRbRange->get_label());
        m_xRbRange->set_help_id(HID_SCH_ERROR_BARS_FROM_DATA);
    }

    if( m_xRbRange->get_active())
    {
        isRangeFieldContentValid( *m_xEdRangePositive );
        isRangeFieldContentValid( *m_xEdRangeNegative );
    }
}

void ErrorBarResources::SetAxisMinorStepWidthForErrorBarDecimals( double fMinorStepWidth )
{
    if( fMinorStepWidth < 0 )
        fMinorStepWidth = -fMinorStepWidth;

    sal_Int32 nExponent = static_cast< sal_Int32 >( ::rtl::math::approxFloor( log10( fMinorStepWidth )));
    if( nExponent <= 0 )
    {
        // one digit precision more
        m_nConstDecimalDigits = static_cast< sal_uInt16 >( (-nExponent) + 1 );
        m_nConstSpinSize = 10;
    }
    else
    {
        m_nConstDecimalDigits = 0;
        m_nConstSpinSize = static_cast< sal_Int64 >( pow( 10.0, static_cast<int>(nExponent) ));
    }
}

void ErrorBarResources::UpdateControlStates()
{
    // function
    bool bIsFunction = m_xRbFunction->get_active();
    m_xLbFunction->set_sensitive( bIsFunction );

    // range buttons
    m_xRbRange->set_sensitive( !m_bHasInternalDataProvider || m_bEnableDataTableDialog );
    bool bShowRange = m_xRbRange->get_active();
    bool bCanChooseRange =
        ( bShowRange &&
          m_apRangeSelectionHelper &&
          m_apRangeSelectionHelper->hasRangeSelection());

    m_xMfPositive->set_visible( ! bShowRange );
    m_xMfNegative->set_visible( ! bShowRange );

    // use range but without range chooser => hide controls
    m_xEdRangePositive->set_visible( bShowRange && ! m_bHasInternalDataProvider );
    m_xIbRangePositive->set_visible( bCanChooseRange );
    m_xEdRangeNegative->set_visible( bShowRange && ! m_bHasInternalDataProvider );
    m_xIbRangeNegative->set_visible( bCanChooseRange );

    bool bShowPosNegAndSync = ! (bShowRange && m_bHasInternalDataProvider);
    m_xFlParameters->set_visible( bShowPosNegAndSync );

    // unit for metric fields
    bool bIsErrorMargin(
        ( m_xRbFunction->get_active()) &&
        ( m_xLbFunction->get_active() == CHART_LB_FUNCTION_ERROR_MARGIN ));
    bool bIsPercentage( m_xRbPercent->get_active() || bIsErrorMargin );
    FieldUnit eFieldUnit = FieldUnit::NONE;

    if( bIsPercentage )
    {
        eFieldUnit = FieldUnit::PERCENT;
        m_xMfPositive->set_digits( 1 );
        m_xMfPositive->set_increments(10, 100, FieldUnit::NONE);
        m_xMfNegative->set_digits( 1 );
        m_xMfNegative->set_increments(10, 100, FieldUnit::NONE);
    }
    else
    {
        m_xMfPositive->set_digits( m_nConstDecimalDigits );
        m_xMfPositive->set_increments(m_nConstSpinSize, m_nConstSpinSize * 10, FieldUnit::NONE);
        m_xMfNegative->set_digits( m_nConstDecimalDigits );
        m_xMfNegative->set_increments(m_nConstSpinSize, m_nConstSpinSize * 10, FieldUnit::NONE);
    }

    sal_Int32 nPlusValue = static_cast< sal_Int32 >( m_fPlusValue * pow(10.0,m_xMfPositive->get_digits()) );
    sal_Int32 nMinusValue = static_cast< sal_Int32 >( m_fMinusValue * pow(10.0,m_xMfNegative->get_digits()) );

    m_xMfPositive->set_value(nPlusValue, FieldUnit::NONE);
    m_xMfNegative->set_value(nMinusValue, FieldUnit::NONE);

    m_xMfPositive->set_unit(eFieldUnit);
    m_xMfNegative->set_unit(eFieldUnit);

    // positive and negative value fields
    bool bPosEnabled = ( m_xRbPositive->get_active() || m_xRbBoth->get_active());
    bool bNegEnabled = ( m_xRbNegative->get_active() || m_xRbBoth->get_active());
    if( !( bPosEnabled || bNegEnabled ))
    {
        // all three controls are not checked -> ambiguous state
        bPosEnabled = true;
        bNegEnabled = true;
    }

    // functions with only one parameter
    bool bOneParameterCategory =
        bIsErrorMargin || m_xRbPercent->get_active();
    if( bOneParameterCategory )
    {
        m_xCbSyncPosNeg->set_active(true);
    }

    if( m_xCbSyncPosNeg->get_active())
    {
        bPosEnabled = true;
        bNegEnabled = false;
    }

    // all functions except error margin take no arguments
    if( m_xRbFunction->get_active() &&  ( m_xLbFunction->get_active() != CHART_LB_FUNCTION_ERROR_MARGIN ))
    {
        bPosEnabled = false;
        bNegEnabled = false;
    }

    // enable/disable pos/neg fields
    m_xBxPositive->set_sensitive( bPosEnabled );
    m_xBxNegative->set_sensitive( bNegEnabled );
    if( bShowRange )
    {
        m_xEdRangePositive->set_sensitive( bPosEnabled );
        m_xIbRangePositive->set_sensitive( bPosEnabled );
        m_xEdRangeNegative->set_sensitive( bNegEnabled );
        m_xIbRangeNegative->set_sensitive( bNegEnabled );
    }
    else
    {
        m_xMfPositive->set_sensitive( bPosEnabled );
        m_xMfNegative->set_sensitive( bNegEnabled );
    }

    m_xCbSyncPosNeg->set_sensitive( !bOneParameterCategory &&  ( bPosEnabled || bNegEnabled ));

    // mark invalid entries in the range fields
    if( bShowRange && ! m_bHasInternalDataProvider )
    {
        isRangeFieldContentValid( *m_xEdRangePositive );
        isRangeFieldContentValid( *m_xEdRangeNegative );
    }
}

IMPL_LINK_NOARG( ErrorBarResources, CategoryChosen2, weld::ComboBox&, void )
{
   CategoryChosen(*m_xRbConst);
}

IMPL_LINK_NOARG( ErrorBarResources, CategoryChosen, weld::Toggleable&, void )
{
    m_bErrorKindUnique = true;
    SvxChartKindError eOldError = m_eErrorKind;

    if( m_xRbNone->get_active())
        m_eErrorKind = SvxChartKindError::NONE;
    else if( m_xRbConst->get_active())
        m_eErrorKind = SvxChartKindError::Const;
    else if( m_xRbPercent->get_active())
        m_eErrorKind = SvxChartKindError::Percent;
    else if( m_xRbRange->get_active())
        m_eErrorKind = SvxChartKindError::Range;
    else if( m_xRbFunction->get_active())
    {
        switch( m_xLbFunction->get_active())
        {
            case CHART_LB_FUNCTION_STD_ERROR:
                m_eErrorKind = SvxChartKindError::StdError; break;
            case CHART_LB_FUNCTION_STD_DEV:
                m_eErrorKind = SvxChartKindError::Sigma; break;
            case CHART_LB_FUNCTION_VARIANCE:
                m_eErrorKind = SvxChartKindError::Variant; break;
            case CHART_LB_FUNCTION_ERROR_MARGIN:
                m_eErrorKind = SvxChartKindError::BigError; break;
            default:
                m_bErrorKindUnique = false;
        }
    }
    else
    {
        OSL_FAIL( "Unknown category chosen" );
        m_bErrorKindUnique = false;
    }

    // changed to range
    if( m_eErrorKind == SvxChartKindError::Range &&
        eOldError != SvxChartKindError::Range )
    {
        m_xCbSyncPosNeg->set_active(
            (!m_xEdRangePositive->get_text().isEmpty()) &&
            m_xEdRangePositive->get_text() == m_xEdRangeNegative->get_text());
    }
    // changed from range
    else if( m_eErrorKind != SvxChartKindError::Range &&
        eOldError == SvxChartKindError::Range )
    {
        m_xCbSyncPosNeg->set_active( m_xMfPositive->get_value(FieldUnit::NONE) == m_xMfNegative->get_value(FieldUnit::NONE));
    }

    UpdateControlStates();
}

IMPL_LINK_NOARG(ErrorBarResources, SynchronizePosAndNeg, weld::Toggleable&, void)
{
    UpdateControlStates();
    PosValueChanged( *m_xMfPositive );
}

IMPL_LINK_NOARG(ErrorBarResources, PosValueChanged, weld::MetricSpinButton&, void)
{
    if( m_xCbSyncPosNeg->get_active())
    {
        if( m_xRbRange->get_active())
        {
            m_xEdRangeNegative->set_text( m_xEdRangePositive->get_text());
            m_bRangeNegUnique = m_bRangePosUnique;
        }
        else
            m_xMfNegative->set_value(m_xMfPositive->get_value(FieldUnit::NONE), FieldUnit::NONE);
    }
}

IMPL_LINK_NOARG(ErrorBarResources, IndicatorChanged, weld::Toggleable&, void)
{
    m_bIndicatorUnique = true;
    if( m_xRbBoth->get_active())
        m_eIndicate = SvxChartIndicate::Both;
    else if( m_xRbPositive->get_active())
        m_eIndicate = SvxChartIndicate::Up;
    else if( m_xRbNegative->get_active())
        m_eIndicate = SvxChartIndicate::Down;
    else
        m_bIndicatorUnique = false;

    UpdateControlStates();
}

IMPL_LINK(ErrorBarResources, ChooseRange, weld::Button&, rButton, void)
{
    OSL_ASSERT(m_apRangeSelectionHelper);
    if (!m_apRangeSelectionHelper)
        return;
    OSL_ASSERT( m_pCurrentRangeChoosingField == nullptr );

    OUString aUIString;

    if (&rButton == m_xIbRangePositive.get())
    {
        m_pCurrentRangeChoosingField = m_xEdRangePositive.get();
        aUIString = m_xUIStringPos->get_label();
    }
    else
    {
        m_pCurrentRangeChoosingField = m_xEdRangeNegative.get();
        aUIString = m_xUIStringNeg->get_label();
    }

    lcl_enableRangeChoosing(true, m_pController);
    m_apRangeSelectionHelper->chooseRange(
        m_pCurrentRangeChoosingField->get_text(),
        aUIString, *this );
}

IMPL_LINK( ErrorBarResources, RangeChanged, weld::Entry&, rEdit, void )
{
    if( &rEdit == m_xEdRangePositive.get() )
    {
        m_bRangePosUnique = true;
        PosValueChanged( *m_xMfPositive );
    }
    else
    {
        m_bRangeNegUnique = true;
    }

    isRangeFieldContentValid( rEdit );
}

void ErrorBarResources::Reset(const SfxItemSet& rInAttrs)
{
    const SfxPoolItem *pPoolItem = nullptr;

    // category
    m_eErrorKind = SvxChartKindError::NONE;
    SfxItemState aState = rInAttrs.GetItemState( SCHATTR_STAT_KIND_ERROR, true, &pPoolItem );
    m_bErrorKindUnique = ( aState != SfxItemState::DONTCARE );

    if( aState == SfxItemState::SET )
        m_eErrorKind = static_cast<const SvxChartKindErrorItem*>(pPoolItem)->GetValue();

    m_xLbFunction->set_active( lcl_getLbEntryPosByErrorKind( m_eErrorKind ));

    if( m_bErrorKindUnique )
    {
        switch( m_eErrorKind )
        {
            case SvxChartKindError::NONE:
                m_xRbNone->set_active(true);
                break;
            case SvxChartKindError::Percent:
                m_xRbPercent->set_active(true);
                break;
            case SvxChartKindError::Const:
                m_xRbConst->set_active(true);
                break;
            case SvxChartKindError::StdError:
            case SvxChartKindError::Variant:
            case SvxChartKindError::Sigma:
            case SvxChartKindError::BigError:
                m_xRbFunction->set_active(true);
                break;
            case SvxChartKindError::Range:
                m_xRbRange->set_active(true);
                break;
        }
    }
    else
    {
        m_xRbNone->set_active( false );
        m_xRbConst->set_active( false );
        m_xRbPercent->set_active( false );
        m_xRbFunction->set_active( false );
    }

    // parameters
    if( const SvxDoubleItem* pDoubleItem = rInAttrs.GetItemIfSet( SCHATTR_STAT_CONSTPLUS ) )
    {
        m_fPlusValue = pDoubleItem->GetValue();
    }

    if( const SvxDoubleItem* pStatItem = rInAttrs.GetItemIfSet( SCHATTR_STAT_CONSTMINUS ) )
    {
        m_fMinusValue = pStatItem->GetValue();

        if( m_eErrorKind != SvxChartKindError::Range &&
            m_fPlusValue == m_fMinusValue )
            m_xCbSyncPosNeg->set_active(true);
    }

    // indicator
    aState = rInAttrs.GetItemState( SCHATTR_STAT_INDICATE, true, &pPoolItem );
    m_bIndicatorUnique = ( aState != SfxItemState::DONTCARE );
    if( aState == SfxItemState::SET)
        m_eIndicate = static_cast<const SvxChartIndicateItem *>(pPoolItem)->GetValue();

    if( m_bIndicatorUnique )
    {
        switch( m_eIndicate )
        {
            case SvxChartIndicate::NONE :
                // no longer used, use both as default
                m_eIndicate = SvxChartIndicate::Both;
                [[fallthrough]]; // to BOTH
            case SvxChartIndicate::Both :
                m_xRbBoth->set_active(true); break;
            case SvxChartIndicate::Up :
                m_xRbPositive->set_active(true); break;
            case SvxChartIndicate::Down :
                m_xRbNegative->set_active(true); break;
        }
    }
    else
    {
        m_xRbBoth->set_active( false );
        m_xRbPositive->set_active( false );
        m_xRbNegative->set_active( false );
    }

    // ranges
    aState = rInAttrs.GetItemState( SCHATTR_STAT_RANGE_POS, true, &pPoolItem );
    m_bRangePosUnique = ( aState != SfxItemState::DONTCARE );
    if( aState == SfxItemState::SET )
    {
        OUString sRangePositive = static_cast< const SfxStringItem * >( pPoolItem )->GetValue();
        m_xEdRangePositive->set_text( sRangePositive );
    }

    aState = rInAttrs.GetItemState( SCHATTR_STAT_RANGE_NEG, true, &pPoolItem );
    m_bRangeNegUnique = ( aState != SfxItemState::DONTCARE );
    if( aState == SfxItemState::SET )
    {
        OUString sRangeNegative = static_cast< const SfxStringItem * >( pPoolItem )->GetValue();
        m_xEdRangeNegative->set_text( sRangeNegative );
        if( m_eErrorKind == SvxChartKindError::Range &&
            !sRangeNegative.isEmpty() &&
            sRangeNegative == m_xEdRangePositive->get_text() )
            m_xCbSyncPosNeg->set_active(true);
    }

    UpdateControlStates();
}

void ErrorBarResources::FillItemSet(SfxItemSet& rOutAttrs) const
{
    if( m_bErrorKindUnique )
        rOutAttrs.Put( SvxChartKindErrorItem( m_eErrorKind, SCHATTR_STAT_KIND_ERROR ));
    if( m_bIndicatorUnique )
        rOutAttrs.Put( SvxChartIndicateItem( m_eIndicate, SCHATTR_STAT_INDICATE ));

    if( m_bErrorKindUnique )
    {
        if( m_eErrorKind == SvxChartKindError::Range )
        {
            OUString aPosRange;
            OUString aNegRange;
            if( m_bHasInternalDataProvider )
            {
                // the strings aPosRange/aNegRange have to be set to a non-empty
                // arbitrary string to generate error-bar sequences
                aPosRange = "x";
                aNegRange = aPosRange;
            }
            else
            {
                aPosRange = m_xEdRangePositive->get_text();
                if( m_xCbSyncPosNeg->get_active())
                    aNegRange = aPosRange;
                else
                    aNegRange = m_xEdRangeNegative->get_text();
            }

            if( m_bRangePosUnique )
                rOutAttrs.Put( SfxStringItem( SCHATTR_STAT_RANGE_POS, aPosRange ));
            if( m_bRangeNegUnique )
                rOutAttrs.Put( SfxStringItem( SCHATTR_STAT_RANGE_NEG, aNegRange ));
        }
        else if( m_eErrorKind == SvxChartKindError::Const ||
                 m_eErrorKind == SvxChartKindError::Percent ||
                 m_eErrorKind == SvxChartKindError::BigError )
        {
            double fPosValue = static_cast< double >( m_xMfPositive->get_value(FieldUnit::NONE)) /
                pow( 10.0, m_xMfPositive->get_digits());
            double fNegValue = 0.0;

            if( m_xCbSyncPosNeg->get_active())
                fNegValue = fPosValue;
            else
                fNegValue = static_cast< double >( m_xMfNegative->get_value(FieldUnit::NONE)) /
                    pow( 10.0, m_xMfNegative->get_digits());

            rOutAttrs.Put( SvxDoubleItem( fPosValue, SCHATTR_STAT_CONSTPLUS ));
            rOutAttrs.Put( SvxDoubleItem( fNegValue, SCHATTR_STAT_CONSTMINUS ));
        }
    }

    rOutAttrs.Put( SfxBoolItem( SCHATTR_STAT_ERRORBAR_TYPE , m_eErrorBarType == ERROR_BAR_Y ));
}

void ErrorBarResources::FillValueSets()
{
    if( m_eErrorBarType == ERROR_BAR_Y )
    {
        m_xFiNegative->set_from_icon_name(BMP_INDICATE_DOWN);
        m_xFiPositive->set_from_icon_name(BMP_INDICATE_UP);
        m_xFiBoth->set_from_icon_name(BMP_INDICATE_BOTH_VERTI);
    }
    else if( m_eErrorBarType == ERROR_BAR_X )
    {
        m_xFiNegative->set_from_icon_name(BMP_INDICATE_LEFT);
        m_xFiPositive->set_from_icon_name(BMP_INDICATE_RIGHT);
        m_xFiBoth->set_from_icon_name(BMP_INDICATE_BOTH_HORI);
    }
}

void ErrorBarResources::listeningFinished(
    const OUString & rNewRange )
{
    OSL_ASSERT(m_apRangeSelectionHelper);
    if (!m_apRangeSelectionHelper)
        return;

    // rNewRange becomes invalid after removing the listener
    OUString aRange( rNewRange );

    // stop listening
    m_apRangeSelectionHelper->stopRangeListening();

    // change edit field
//     if( m_pParentWindow )
//     {
//         m_pParentWindow->ToTop();
//         m_pParentWindow->grab_focus();
//     }

    if( m_pCurrentRangeChoosingField )
    {
        m_pCurrentRangeChoosingField->set_text( aRange );
        m_pCurrentRangeChoosingField->grab_focus();
        PosValueChanged( *m_xMfPositive );
    }

    m_pCurrentRangeChoosingField = nullptr;

    UpdateControlStates();
    lcl_enableRangeChoosing(false, m_pController);
}

void ErrorBarResources::disposingRangeSelection()
{
    OSL_ASSERT(m_apRangeSelectionHelper);
    if (m_apRangeSelectionHelper)
        m_apRangeSelectionHelper->stopRangeListening( false );
}

void ErrorBarResources::isRangeFieldContentValid(weld::Entry& rEdit)
{
    OUString aRange( rEdit.get_text());
    bool bIsValid = ( aRange.isEmpty() ) ||
        ( m_apRangeSelectionHelper &&
          m_apRangeSelectionHelper->verifyCellRange( aRange ));

    if( bIsValid || !rEdit.get_sensitive())
    {
        rEdit.set_message_type(weld::EntryMessageType::Normal);
    }
    else
    {
        rEdit.set_message_type(weld::EntryMessageType::Error);
    }
}

} //namespace chart

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */