summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/dbgui/PivotLayoutDialog.cxx
blob: 08dcaf200ab9396e5ebb6358adfda68fc1aa593c (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
717
/* -*- 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:
 */

#include <PivotLayoutTreeList.hxx>
#include <PivotLayoutDialog.hxx>
#include <reffact.hxx>

#include <rangeutl.hxx>
#include <uiitems.hxx>
#include <dputil.hxx>
#include <dbdocfun.hxx>
#include <dpsave.hxx>
#include <dpshttab.hxx>

#include <memory>
#include <vector>

#include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
#include <com/sun/star/sheet/DataPilotFieldSortMode.hpp>

using namespace css::uno;
using namespace css::sheet;

ScItemValue::ScItemValue(OUString const & aName, SCCOL nColumn, PivotFunc nFunctionMask) :
    maName(aName),
    maFunctionData(nColumn, nFunctionMask),
    mpOriginalItemValue(this)
{}

ScItemValue::ScItemValue(const ScItemValue* pInputItemValue) :
    maName(pInputItemValue->maName),
    maFunctionData(pInputItemValue->maFunctionData),
    mpOriginalItemValue(this)
{}

ScItemValue::~ScItemValue()
{}

namespace
{

ScRange lclGetRangeForNamedRange(OUString const & aName, const ScDocument* pDocument)
{
    ScRange aInvalidRange(ScAddress::INITIALIZE_INVALID);
    ScRangeName* pRangeName = pDocument->GetRangeName();
    if (pRangeName == nullptr)
        return aInvalidRange;

    const ScRangeData* pData = pRangeName->findByUpperName(aName.toAsciiUpperCase());
    if (pData == nullptr)
        return aInvalidRange;

    ScRange aRange;
    if (pData->IsReference(aRange))
        return aRange;

    return aInvalidRange;
}

}

ScPivotLayoutDialog::ScPivotLayoutDialog(
                            SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow, weld::Window* pParent,
                            ScViewData* pViewData, const ScDPObject* pPivotTableObject, bool bNewPivotTable)
    : ScAnyRefDlgController(pSfxBindings, pChildWindow, pParent, "modules/scalc/ui/pivottablelayoutdialog.ui", "PivotTableLayout")
    , maPivotTableObject(*pPivotTableObject)
    , mpPreviouslyFocusedListBox(nullptr)
    , mpViewData(pViewData)
    , mrDocument(*pViewData->GetDocument())
    , mbNewPivotTable(bNewPivotTable)
    , maAddressDetails(mrDocument.GetAddressConvention(), 0, 0)
    , mbDialogLostFocus(false)
    , mpActiveEdit(nullptr)
    , mxListBoxField(new ScPivotLayoutTreeListLabel(m_xBuilder->weld_tree_view("listbox-fields")))
    , mxListBoxPage(new ScPivotLayoutTreeList(m_xBuilder->weld_tree_view("listbox-page")))
    , mxListBoxColumn(new ScPivotLayoutTreeList(m_xBuilder->weld_tree_view("listbox-column")))
    , mxListBoxRow(new ScPivotLayoutTreeList(m_xBuilder->weld_tree_view("listbox-row")))
    , mxListBoxData(new ScPivotLayoutTreeListData(m_xBuilder->weld_tree_view("listbox-data")))
    , mxCheckIgnoreEmptyRows(m_xBuilder->weld_check_button("check-ignore-empty-rows"))
    , mxCheckTotalColumns(m_xBuilder->weld_check_button("check-total-columns"))
    , mxCheckAddFilter(m_xBuilder->weld_check_button("check-add-filter"))
    , mxCheckIdentifyCategories(m_xBuilder->weld_check_button("check-identify-categories"))
    , mxCheckTotalRows(m_xBuilder->weld_check_button("check-total-rows"))
    , mxCheckDrillToDetail(m_xBuilder->weld_check_button("check-drill-to-details"))
    , mxSourceRadioNamedRange(m_xBuilder->weld_radio_button("source-radio-named-range"))
    , mxSourceRadioSelection(m_xBuilder->weld_radio_button("source-radio-selection"))
    , mxSourceListBox(m_xBuilder->weld_combo_box("source-list"))
    , mxSourceEdit(new formula::RefEdit(m_xBuilder->weld_entry("source-edit")))
    , mxSourceButton(new formula::RefButton(m_xBuilder->weld_button("source-button")))
    , mxDestinationRadioNewSheet(m_xBuilder->weld_radio_button("destination-radio-new-sheet"))
    , mxDestinationRadioNamedRange(m_xBuilder->weld_radio_button("destination-radio-named-range"))
    , mxDestinationRadioSelection(m_xBuilder->weld_radio_button("destination-radio-selection"))
    , mxDestinationListBox(m_xBuilder->weld_combo_box("destination-list"))
    , mxDestinationEdit(new formula::RefEdit(m_xBuilder->weld_entry("destination-edit")))
    , mxDestinationButton(new formula::RefButton(m_xBuilder->weld_button("destination-button")))
    , mxBtnOK(m_xBuilder->weld_button("ok"))
    , mxBtnCancel(m_xBuilder->weld_button("cancel"))
    , mxSourceFrame(m_xBuilder->weld_frame("frame2"))
    , mxSourceLabel(mxSourceFrame->weld_label_widget())
    , mxDestFrame(m_xBuilder->weld_frame("frame1"))
    , mxDestLabel(mxDestFrame->weld_label_widget())
    , mxOptions(m_xBuilder->weld_expander("options"))
    , mxMore(m_xBuilder->weld_expander("more"))
{
    // Source UI
    Link<weld::ToggleButton&,void> aLink2 = LINK(this, ScPivotLayoutDialog, ToggleSource);
    mxSourceRadioNamedRange->connect_toggled(aLink2);
    mxSourceRadioSelection->connect_toggled(aLink2);

    mxSourceEdit->SetReferences(this, mxSourceLabel.get());
    mxSourceButton->SetReferences(this, mxSourceEdit.get());

    Link<formula::RefEdit&,void> aEditLink = LINK(this, ScPivotLayoutDialog, GetEditFocusHandler);
    mxDestinationEdit->SetGetFocusHdl(aEditLink);
    mxSourceEdit->SetGetFocusHdl(aEditLink);

    aEditLink = LINK(this, ScPivotLayoutDialog, LoseEditFocusHandler);
    mxDestinationEdit->SetLoseFocusHdl(aEditLink);
    mxSourceEdit->SetLoseFocusHdl(aEditLink);

    mxSourceEdit->SetModifyHdl(LINK(this, ScPivotLayoutDialog, SourceEditModified));
    mxSourceListBox->connect_changed(LINK(this, ScPivotLayoutDialog, SourceListSelected));

    // Destination UI
    aLink2 = LINK(this, ScPivotLayoutDialog, ToggleDestination);
    mxDestinationRadioNewSheet->connect_toggled(aLink2);
    mxDestinationRadioNamedRange->connect_toggled(aLink2);
    mxDestinationRadioSelection->connect_toggled(aLink2);

    mxDestinationEdit->SetReferences(this, mxDestLabel.get());
    mxDestinationButton->SetReferences(this, mxDestinationEdit.get());

    Link<formula::RefButton&,void> aButtonLink = LINK(this, ScPivotLayoutDialog, GetButtonFocusHandler);
    mxSourceButton->SetGetFocusHdl(aButtonLink);
    mxDestinationButton->SetGetFocusHdl(aButtonLink);

    aButtonLink = LINK(this, ScPivotLayoutDialog, LoseButtonFocusHandler);
    mxSourceButton->SetLoseFocusHdl(aButtonLink);
    mxDestinationButton->SetLoseFocusHdl(aButtonLink);

    // Buttons
    mxBtnCancel->connect_clicked(LINK(this, ScPivotLayoutDialog, CancelClicked));
    mxBtnOK->connect_clicked(LINK(this, ScPivotLayoutDialog, OKClicked));

    // Initialize Data
    maPivotTableObject.FillOldParam(maPivotParameters);
    maPivotTableObject.FillLabelData(maPivotParameters);

    mxListBoxField->Setup (this);
    mxListBoxPage->Setup  (this, ScPivotLayoutTreeList::PAGE_LIST);
    mxListBoxColumn->Setup(this, ScPivotLayoutTreeList::COLUMN_LIST);
    mxListBoxRow->Setup   (this, ScPivotLayoutTreeList::ROW_LIST);
    mxListBoxData->Setup  (this);

    FillValuesToListBoxes();

    // Initialize Options
    const ScDPSaveData* pSaveData = maPivotTableObject.GetSaveData();
    if (pSaveData == nullptr)
    {
        mxCheckAddFilter->set_active(false);
        mxCheckDrillToDetail->set_active(false);
    }
    else
    {
        mxCheckAddFilter->set_active(pSaveData->GetFilterButton());
        mxCheckDrillToDetail->set_active(pSaveData->GetDrillDown());
    }

    mxCheckIgnoreEmptyRows->set_active(maPivotParameters.bIgnoreEmptyRows);
    mxCheckIdentifyCategories->set_active(maPivotParameters.bDetectCategories);
    mxCheckTotalColumns->set_active(maPivotParameters.bMakeTotalCol);
    mxCheckTotalRows->set_active(maPivotParameters.bMakeTotalRow);

    SetupSource();
    SetupDestination();
}

ScPivotLayoutDialog::~ScPivotLayoutDialog()
{
}

void ScPivotLayoutDialog::SetupSource()
{
    mxSourceListBox->clear();

    ScRange aSourceRange;
    OUString sSourceNamedRangeName;

    if (maPivotTableObject.GetSheetDesc())
    {
        const ScSheetSourceDesc* pSheetSourceDesc = maPivotTableObject.GetSheetDesc();
        aSourceRange = pSheetSourceDesc->GetSourceRange();

        if(!aSourceRange.IsValid())
        {
            // Source is probably a DB Range
            mxSourceRadioNamedRange->set_sensitive(false);
            mxSourceRadioSelection->set_sensitive(false);
            ToggleSource();
            return;
        }
        else
        {
            OUString aSourceRangeName = aSourceRange.Format(mrDocument, ScRefFlags::RANGE_ABS_3D, maAddressDetails);
            mxSourceEdit->SetText(aSourceRangeName);
        }
    }
    else
    {
        mxSourceRadioNamedRange->set_sensitive(false);
        mxSourceRadioSelection->set_sensitive(false);
        ToggleSource();
        return;
    }

    // Setup Named Ranges
    bool bIsNamedRange = false;

    ScAreaNameIterator aIterator(&mrDocument);
    OUString aEachName;
    ScRange aEachRange;

    while (aIterator.Next(aEachName, aEachRange))
    {
        if (!aIterator.WasDBName())
        {
            mxSourceListBox->append_text(aEachName);
            if (aEachRange == aSourceRange)
            {
                sSourceNamedRangeName = aEachName;
                bIsNamedRange = true;
            }
        }
    }

    bool bSourceBoxHasEntries = mxSourceListBox->get_count() > 0;

    if (bIsNamedRange)
    {
        mxSourceListBox->set_active_text(sSourceNamedRangeName);
        mxSourceRadioNamedRange->set_active(true);
    }
    else
    {
        // If entries - select first entry
        mxSourceListBox->set_active(bSourceBoxHasEntries ? 0 : -1);
        mxSourceRadioSelection->set_active(true);
    }

    // If no entries disable the radio button.
    if (!bSourceBoxHasEntries)
        mxSourceRadioNamedRange->set_sensitive(false);

    ToggleSource();
}

void ScPivotLayoutDialog::SetupDestination()
{
    mxDestinationListBox->clear();

    // Fill up named ranges
    ScAreaNameIterator aIterator(&mrDocument);
    OUString aName;
    ScRange aRange;

    while (aIterator.Next(aName, aRange))
    {
        if (!aIterator.WasDBName())
        {
            mxDestinationListBox->append_text(aName);
        }
    }

    // If entries - select first entry, otherwise disable the radio button.
    if (mxDestinationListBox->get_count() > 0)
        mxDestinationListBox->set_active(0);
    else
        mxDestinationRadioNamedRange->set_sensitive(false);

    //
    if (mbNewPivotTable)
    {
        mxDestinationRadioNewSheet->set_active(true);
    }
    else
    {
        if (maPivotParameters.nTab != MAXTAB + 1)
        {
            ScAddress aAddress(maPivotParameters.nCol, maPivotParameters.nRow, maPivotParameters.nTab);
            OUString aAddressString = aAddress.Format(ScRefFlags::ADDR_ABS_3D, &mrDocument, maAddressDetails);
            mxDestinationEdit->SetText(aAddressString);
            mxDestinationRadioSelection->set_active(true);
        }
    }

    ToggleDestination();
}

void ScPivotLayoutDialog::FillValuesToListBoxes()
{
    mxListBoxField->FillLabelFields(maPivotParameters.maLabelArray);
    mxListBoxData->FillDataField(maPivotParameters.maDataFields);
    mxListBoxColumn->FillFields(maPivotParameters.maColFields);
    mxListBoxRow->FillFields(maPivotParameters.maRowFields);
    mxListBoxPage->FillFields(maPivotParameters.maPageFields);
}

void ScPivotLayoutDialog::SetActive()
{
    if (mbDialogLostFocus)
    {
        mbDialogLostFocus = false;
        if(mpActiveEdit != nullptr)
        {
            mpActiveEdit->GrabFocus();
            if (mpActiveEdit == mxSourceEdit.get())
                UpdateSourceRange();
        }
    }
    else
    {
        m_xDialog->grab_focus();
    }

    RefInputDone();
}

void ScPivotLayoutDialog::SetReference(const ScRange& rReferenceRange, ScDocument& rDocument)
{
    if (!mbDialogLostFocus)
        return;

    if (mpActiveEdit == nullptr)
        return;

    if (rReferenceRange.aStart != rReferenceRange.aEnd)
        RefInputStart(mpActiveEdit);

    OUString aReferenceString = rReferenceRange.Format(rDocument, ScRefFlags::RANGE_ABS_3D, maAddressDetails);

    if (mpActiveEdit == mxSourceEdit.get())
    {
        mxSourceEdit->SetRefString(aReferenceString);
    }
    else if (mpActiveEdit == mxDestinationEdit.get())
    {
        mxDestinationEdit->SetRefString(aReferenceString);
    }
}

bool ScPivotLayoutDialog::IsRefInputMode() const
{
    return mbDialogLostFocus;
}

void ScPivotLayoutDialog::ItemInserted(const ScItemValue* pItemValue, ScPivotLayoutTreeList::SvPivotTreeListType eType)
{
    if (pItemValue == nullptr)
        return;

    switch (eType)
    {
        case ScPivotLayoutTreeList::ROW_LIST:
        case ScPivotLayoutTreeList::COLUMN_LIST:
        case ScPivotLayoutTreeList::PAGE_LIST:
        {
            mxListBoxRow->RemoveEntryForItem(pItemValue);
            mxListBoxColumn->RemoveEntryForItem(pItemValue);
            mxListBoxPage->RemoveEntryForItem(pItemValue);
        }
        break;
        case ScPivotLayoutTreeList::LABEL_LIST:
        {
            mxListBoxRow->RemoveEntryForItem(pItemValue);
            mxListBoxColumn->RemoveEntryForItem(pItemValue);
            mxListBoxPage->RemoveEntryForItem(pItemValue);
            mxListBoxData->RemoveEntryForItem(pItemValue);
        }
        break;
        default:
            break;
    }
}

void ScPivotLayoutDialog::UpdateSourceRange()
{
    if (!maPivotTableObject.GetSheetDesc())
       return;

    ScSheetSourceDesc aSourceSheet = *maPivotTableObject.GetSheetDesc();

    if (mxSourceRadioNamedRange->get_active())
    {
        OUString aEntryString = mxSourceListBox->get_active_text();
        ScRange aSourceRange = lclGetRangeForNamedRange(aEntryString, &mrDocument);
        if (!aSourceRange.IsValid() || aSourceSheet.GetSourceRange() == aSourceRange)
            return;
        aSourceSheet.SetRangeName(aEntryString);
    }
    else if (mxSourceRadioSelection->get_active())
    {
        OUString aSourceString = mxSourceEdit->GetText();
        ScRange aSourceRange;
        ScRefFlags nResult = aSourceRange.Parse(aSourceString, &mrDocument, maAddressDetails);

        bool bIsValid = (nResult & ScRefFlags::VALID) == ScRefFlags::VALID; // aSourceString is valid

        mxSourceEdit->SetRefValid(true);

        if (bIsValid)
        {
            ScRefAddress aStart;
            ScRefAddress aEnd;

            ConvertDoubleRef(&mrDocument, aSourceString, 1, aStart, aEnd, maAddressDetails);
            aSourceRange.aStart = aStart.GetAddress();
            aSourceRange.aEnd = aEnd.GetAddress();
        }
        else
        {
            aSourceRange = lclGetRangeForNamedRange(aSourceString, &mrDocument);
        }

        if (!aSourceRange.IsValid())
        {
            mxSourceEdit->SetRefValid(false);
            return;
        }

        if (aSourceSheet.GetSourceRange() == aSourceRange)
                return;

        aSourceSheet.SetSourceRange(aSourceRange);
        if (aSourceSheet.CheckSourceRange() != nullptr)
        {
            mxSourceEdit->SetRefValid(false);
            return;
        }
    }
    else
    {
        return;
    }

    maPivotTableObject.SetSheetDesc(aSourceSheet);
    maPivotTableObject.FillOldParam(maPivotParameters);
    maPivotTableObject.FillLabelData(maPivotParameters);

    FillValuesToListBoxes();
}

void ScPivotLayoutDialog::ApplyChanges()
{
    ScDPSaveData aSaveData;
    ApplySaveData(aSaveData);
    ApplyLabelData(aSaveData);

    ScDPObject *pOldDPObj = mrDocument.GetDPAtCursor( maPivotParameters.nCol, maPivotParameters.nRow, maPivotParameters.nTab);
    ScRange aDestinationRange;
    bool bToNewSheet = false;

    if (!GetDestination(aDestinationRange, bToNewSheet))
        return;

    SetDispatcherLock(false);
    SwitchToDocument();

    sal_uInt16 nWhichPivot = SC_MOD()->GetPool().GetWhich(SID_PIVOT_TABLE);
    ScPivotItem aPivotItem(nWhichPivot, &aSaveData, &aDestinationRange, bToNewSheet);
    mpViewData->GetViewShell()->SetDialogDPObject(std::make_unique<ScDPObject>(maPivotTableObject));


    SfxDispatcher* pDispatcher = GetBindings().GetDispatcher();
    SfxCallMode const nCallMode = SfxCallMode::SLOT | SfxCallMode::RECORD;
    const SfxPoolItem* pResult = pDispatcher->ExecuteList(SID_PIVOT_TABLE,
            nCallMode, { &aPivotItem });

    if (pResult != nullptr)
    {
        // existing pivot table might have moved to a new range or a new sheet
        if ( pOldDPObj != nullptr  )
        {
            const ScRange& rOldRange = pOldDPObj->GetOutRange();

            ScDPObject *pDPObj = nullptr;
            // FIXME: if the new range overlaps with the old one, the table actually doesn't move
            // and shouldn't therefore be deleted
            if ( ( ( rOldRange != aDestinationRange ) && !rOldRange.In( aDestinationRange ) )
                 || bToNewSheet )
            {
                pDPObj = mrDocument.GetDPAtCursor( maPivotParameters.nCol, maPivotParameters.nRow, maPivotParameters.nTab);
            }
            if (pDPObj)
            {
                ScDBDocFunc aFunc( *(mpViewData->GetDocShell() ));
                aFunc.RemovePivotTable( *pDPObj, true, false);
                mpViewData->GetView()->CursorPosChanged();
            }
        }
        return;
    }

    SetDispatcherLock(true);
}

void ScPivotLayoutDialog::ApplySaveData(ScDPSaveData& rSaveData)
{
    rSaveData.SetIgnoreEmptyRows(mxCheckIgnoreEmptyRows->get_active());
    rSaveData.SetRepeatIfEmpty(mxCheckIdentifyCategories->get_active());
    rSaveData.SetColumnGrand(mxCheckTotalColumns->get_active());
    rSaveData.SetRowGrand(mxCheckTotalRows->get_active());
    rSaveData.SetFilterButton(mxCheckAddFilter->get_active());
    rSaveData.SetDrillDown(mxCheckDrillToDetail->get_active());

    Reference<XDimensionsSupplier> xSource = maPivotTableObject.GetSource();

    ScPivotFieldVector aPageFieldVector;
    mxListBoxPage->PushEntriesToPivotFieldVector(aPageFieldVector);
    ScDPObject::ConvertOrientation(rSaveData, aPageFieldVector, DataPilotFieldOrientation_PAGE,
                                   xSource, maPivotParameters.maLabelArray);

    ScPivotFieldVector aColFieldVector;
    mxListBoxColumn->PushEntriesToPivotFieldVector(aColFieldVector);
    ScDPObject::ConvertOrientation(rSaveData, aColFieldVector, DataPilotFieldOrientation_COLUMN,
                                   xSource, maPivotParameters.maLabelArray);

    ScPivotFieldVector aRowFieldVector;
    mxListBoxRow->PushEntriesToPivotFieldVector(aRowFieldVector);
    ScDPObject::ConvertOrientation(rSaveData, aRowFieldVector, DataPilotFieldOrientation_ROW,
                                   xSource, maPivotParameters.maLabelArray);

    ScPivotFieldVector aDataFieldVector;
    mxListBoxData->PushEntriesToPivotFieldVector(aDataFieldVector);
    ScDPObject::ConvertOrientation(rSaveData, aDataFieldVector, DataPilotFieldOrientation_DATA,
                                   xSource, maPivotParameters.maLabelArray,
                                   &aColFieldVector, &aRowFieldVector, &aPageFieldVector);
}

void ScPivotLayoutDialog::ApplyLabelData(const ScDPSaveData& rSaveData)
{
    ScDPLabelDataVector& rLabelDataVector = GetLabelDataVector();

    for (std::unique_ptr<ScDPLabelData> const & pLabelData : rLabelDataVector)
    {
        OUString aUnoName = ScDPUtil::createDuplicateDimensionName(pLabelData->maName, pLabelData->mnDupCount);
        ScDPSaveDimension* pSaveDimensions = rSaveData.GetExistingDimensionByName(aUnoName);

        if (pSaveDimensions == nullptr)
            continue;

        pSaveDimensions->SetUsedHierarchy(pLabelData->mnUsedHier);
        pSaveDimensions->SetShowEmpty(pLabelData->mbShowAll);
        pSaveDimensions->SetRepeatItemLabels(pLabelData->mbRepeatItemLabels);
        pSaveDimensions->SetSortInfo(&pLabelData->maSortInfo);
        pSaveDimensions->SetLayoutInfo(&pLabelData->maLayoutInfo);
        pSaveDimensions->SetAutoShowInfo(&pLabelData->maShowInfo);

        bool bManualSort = (pLabelData->maSortInfo.Mode == DataPilotFieldSortMode::MANUAL);

        for (ScDPLabelData::Member const & rLabelMember : pLabelData->maMembers)
        {
            ScDPSaveMember* pMember = pSaveDimensions->GetMemberByName(rLabelMember.maName);

            if (bManualSort || !rLabelMember.mbVisible || !rLabelMember.mbShowDetails)
            {
                pMember->SetIsVisible(rLabelMember.mbVisible);
                pMember->SetShowDetails(rLabelMember.mbShowDetails);
            }
        }
    }
}

bool ScPivotLayoutDialog::GetDestination(ScRange& aDestinationRange, bool& bToNewSheet)
{
    bToNewSheet = false;

    if (mxDestinationRadioNamedRange->get_active())
    {
        OUString aName = mxDestinationListBox->get_active_text();
        aDestinationRange = lclGetRangeForNamedRange(aName, &mrDocument);
        if (!aDestinationRange.IsValid())
            return false;
    }
    else if (mxDestinationRadioSelection->get_active())
    {
        ScAddress aAddress;
        aAddress.Parse(mxDestinationEdit->GetText(), &mrDocument, maAddressDetails);
        aDestinationRange = ScRange(aAddress);
    }
    else
    {
        bToNewSheet = true;
        aDestinationRange = ScRange(maPivotParameters.nCol, maPivotParameters.nRow, maPivotParameters.nTab);
    }
    return true;
}

ScItemValue* ScPivotLayoutDialog::GetItem(SCCOL nColumn)
{
    return mxListBoxField->GetItem(nColumn);
}

bool ScPivotLayoutDialog::IsDataElement(SCCOL nColumn)
{
    return mxListBoxField->IsDataElement(nColumn);
}

ScDPLabelData& ScPivotLayoutDialog::GetLabelData(SCCOL nColumn)
{
    return *maPivotParameters.maLabelArray[nColumn];
}

void ScPivotLayoutDialog::PushDataFieldNames(std::vector<ScDPName>& rDataFieldNames)
{
    mxListBoxData->PushDataFieldNames(rDataFieldNames);
}

void ScPivotLayoutDialog::Close()
{
    DoClose( ScPivotLayoutWrapper::GetChildWindowId() );
}

IMPL_LINK_NOARG( ScPivotLayoutDialog, OKClicked, weld::Button&, void )
{
    /* tdf#137726 hide so its not a candidate to be parent of any error
       messages that may appear because this dialog is going to disappear on
       response(RET_OK) and the error dialog is not run in its own event loop
       but instead async */
    m_xDialog->hide();

    ApplyChanges();
    m_xDialog->response(RET_OK);
}

IMPL_LINK_NOARG( ScPivotLayoutDialog, CancelClicked, weld::Button&, void )
{
    m_xDialog->response(RET_CANCEL);
}

IMPL_LINK(ScPivotLayoutDialog, GetEditFocusHandler, formula::RefEdit&, rCtrl, void)
{
    mpActiveEdit = &rCtrl;
    mpActiveEdit->SelectAll();
}

IMPL_LINK(ScPivotLayoutDialog, GetButtonFocusHandler, formula::RefButton&, rCtrl, void)
{
    mpActiveEdit = nullptr;

    if (&rCtrl == mxSourceButton.get())
        mpActiveEdit = mxSourceEdit.get();
    else if (&rCtrl == mxDestinationButton.get())
        mpActiveEdit = mxDestinationEdit.get();

    if (mpActiveEdit)
        mpActiveEdit->SelectAll();
}

IMPL_LINK_NOARG(ScPivotLayoutDialog, LoseEditFocusHandler, formula::RefEdit&, void)
{
    mbDialogLostFocus = !m_xDialog->has_toplevel_focus();
}

IMPL_LINK_NOARG(ScPivotLayoutDialog, LoseButtonFocusHandler, formula::RefButton&, void)
{
    mbDialogLostFocus = !m_xDialog->has_toplevel_focus();
}

IMPL_LINK_NOARG(ScPivotLayoutDialog, SourceListSelected, weld::ComboBox&, void)
{
    UpdateSourceRange();
}

IMPL_LINK_NOARG(ScPivotLayoutDialog, SourceEditModified, formula::RefEdit&, void)
{
    UpdateSourceRange();
}

IMPL_LINK_NOARG(ScPivotLayoutDialog, ToggleSource, weld::ToggleButton&, void)
{
    ToggleSource();
}

void ScPivotLayoutDialog::ToggleSource()
{
    bool bNamedRange = mxSourceRadioNamedRange->get_active();
    bool bSelection = mxSourceRadioSelection->get_active();
    mxSourceListBox->set_sensitive(bNamedRange);
    mxSourceButton->GetWidget()->set_sensitive(bSelection);
    mxSourceEdit->GetWidget()->set_sensitive(bSelection);
    UpdateSourceRange();
}

IMPL_LINK_NOARG(ScPivotLayoutDialog, ToggleDestination, weld::ToggleButton&, void)
{
    ToggleDestination();
}

void ScPivotLayoutDialog::ToggleDestination()
{
    bool bNamedRange = mxDestinationRadioNamedRange->get_active();
    bool bSelection = mxDestinationRadioSelection->get_active();
    mxDestinationListBox->set_sensitive(bNamedRange);
    mxDestinationButton->GetWidget()->set_sensitive(bSelection);
    mxDestinationEdit->GetWidget()->set_sensitive(bSelection);
}

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