Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/view/dbfunc3.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 <dbfunc.hxx>
21
#include <scitems.hxx>
22
#include <vcl/svapp.hxx>
23
#include <vcl/weld/MessageDialog.hxx>
24
#include <vcl/weld/weld.hxx>
25
#include <svl/numformat.hxx>
26
#include <svl/zforlist.hxx>
27
#include <sfx2/app.hxx>
28
#include <unotools/collatorwrapper.hxx>
29
#include <com/sun/star/beans/XPropertySet.hpp>
30
#include <com/sun/star/container/XNameAccess.hpp>
31
#include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
32
#include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
33
#include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
34
#include <com/sun/star/sheet/DataPilotFieldSortMode.hpp>
35
#include <com/sun/star/sheet/DataPilotTableHeaderData.hpp>
36
#include <com/sun/star/sheet/MemberResultFlags.hpp>
37
#include <com/sun/star/sheet/XDimensionsSupplier.hpp>
38
#include <com/sun/star/sheet/XDrillDownDataSupplier.hpp>
39
40
#include <global.hxx>
41
#include <scresid.hxx>
42
#include <globstr.hrc>
43
#include <undotab.hxx>
44
#include <undodat.hxx>
45
#include <dbdata.hxx>
46
#include <rangenam.hxx>
47
#include <docsh.hxx>
48
#include <olinetab.hxx>
49
#include <olinefun.hxx>
50
#include <dpobject.hxx>
51
#include <dpsave.hxx>
52
#include <dpdimsave.hxx>
53
#include <dbdocfun.hxx>
54
#include <dpoutput.hxx>
55
#include <editable.hxx>
56
#include <patattr.hxx>
57
#include <unonames.hxx>
58
#include <userlist.hxx>
59
#include <queryentry.hxx>
60
#include <markdata.hxx>
61
#include <tabvwsh.hxx>
62
#include <generalfunction.hxx>
63
#include <sortparam.hxx>
64
65
#include <comphelper/lok.hxx>
66
#include <osl/diagnose.h>
67
68
#include <memory>
69
#include <string_view>
70
#include <unordered_set>
71
#include <unordered_map>
72
#include <vector>
73
#include <algorithm>
74
75
using namespace com::sun::star;
76
using ::com::sun::star::uno::Any;
77
using ::com::sun::star::uno::Sequence;
78
using ::com::sun::star::uno::Reference;
79
using ::com::sun::star::uno::UNO_QUERY;
80
using ::com::sun::star::beans::XPropertySet;
81
using ::com::sun::star::container::XNameAccess;
82
using ::com::sun::star::sheet::XDimensionsSupplier;
83
84
//          outliner
85
86
// create outline grouping
87
88
void ScDBFunc::MakeOutline( bool bColumns, bool bRecord )
89
0
{
90
0
    ScRange aRange;
91
0
    if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
92
0
    {
93
0
        ScDocShell* pDocSh = GetViewData().GetDocShell();
94
0
        ScOutlineDocFunc aFunc(*pDocSh);
95
0
        aFunc.MakeOutline( aRange, bColumns, bRecord, false );
96
97
0
        ScTabViewShell::notifyAllViewsHeaderInvalidation(GetViewData().GetViewShell(), bColumns ? COLUMN_HEADER : ROW_HEADER, GetViewData().GetTabNumber());
98
0
        ScTabViewShell::notifyAllViewsSheetGeomInvalidation(GetViewData().GetViewShell(),
99
0
                                                            bColumns, !bColumns, false /* bSizes*/,
100
0
                                                            false /* bHidden */, false /* bFiltered */,
101
0
                                                            true /* bGroups */, GetViewData().GetTabNumber());
102
0
    }
103
0
    else
104
0
        ErrorMessage(STR_NOMULTISELECT);
105
0
}
106
107
// delete outline grouping
108
109
void ScDBFunc::RemoveOutline( bool bColumns, bool bRecord )
110
0
{
111
0
    ScRange aRange;
112
0
    if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
113
0
    {
114
0
        ScDocShell* pDocSh = GetViewData().GetDocShell();
115
0
        ScOutlineDocFunc aFunc(*pDocSh);
116
0
        aFunc.RemoveOutline( aRange, bColumns, bRecord, false );
117
118
0
        ScTabViewShell::notifyAllViewsHeaderInvalidation(GetViewData().GetViewShell(), bColumns ? COLUMN_HEADER : ROW_HEADER, GetViewData().GetTabNumber());
119
0
        ScTabViewShell::notifyAllViewsSheetGeomInvalidation(GetViewData().GetViewShell(),
120
0
                                                            bColumns, !bColumns, false /* bSizes*/,
121
0
                                                            true /* bHidden */, true /* bFiltered */,
122
0
                                                            true /* bGroups */, GetViewData().GetTabNumber());
123
0
    }
124
0
    else
125
0
        ErrorMessage(STR_NOMULTISELECT);
126
0
}
127
128
//  menu status: delete outlines
129
130
void ScDBFunc::TestRemoveOutline( bool& rCol, bool& rRow )
131
0
{
132
0
    bool bColFound = false;
133
0
    bool bRowFound = false;
134
135
0
    SCCOL nStartCol, nEndCol;
136
0
    SCROW nStartRow, nEndRow;
137
0
    SCTAB nStartTab, nEndTab;
138
0
    if (GetViewData().GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
139
0
    {
140
0
        SCTAB nTab = nStartTab;
141
0
        ScDocument& rDoc = GetViewData().GetDocument();
142
0
        ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
143
0
        if (pTable)
144
0
        {
145
0
            ScOutlineEntry* pEntry;
146
0
            SCCOLROW nStart;
147
0
            SCCOLROW nEnd;
148
0
            bool bColMarked = ( nStartRow == 0 && nEndRow == rDoc.MaxRow() );
149
0
            bool bRowMarked = ( nStartCol == 0 && nEndCol == rDoc.MaxCol() );
150
151
            // columns
152
153
0
            if ( !bRowMarked || bColMarked )        // not when entire rows are marked
154
0
            {
155
0
                ScOutlineArray& rArray = pTable->GetColArray();
156
0
                ScSubOutlineIterator aColIter( &rArray );
157
0
                while (!bColFound)
158
0
                {
159
0
                    pEntry=aColIter.GetNext();
160
0
                    if (!pEntry)
161
0
                        break;
162
0
                    nStart = pEntry->GetStart();
163
0
                    nEnd   = pEntry->GetEnd();
164
0
                    if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
165
0
                        bColFound = true;
166
0
                }
167
0
            }
168
169
            // rows
170
171
0
            if ( !bColMarked || bRowMarked )        // not when entire columns are marked
172
0
            {
173
0
                ScOutlineArray& rArray = pTable->GetRowArray();
174
0
                ScSubOutlineIterator aRowIter( &rArray );
175
0
                while (!bRowFound)
176
0
                {
177
0
                    pEntry=aRowIter.GetNext();
178
0
                    if (!pEntry)
179
0
                        break;
180
0
                    nStart = pEntry->GetStart();
181
0
                    nEnd   = pEntry->GetEnd();
182
0
                    if ( nStartRow<=nEnd && nEndRow>=nStart )
183
0
                        bRowFound = true;
184
0
                }
185
0
            }
186
0
        }
187
0
    }
188
189
0
    rCol = bColFound;
190
0
    rRow = bRowFound;
191
0
}
192
193
void ScDBFunc::RemoveAllOutlines( bool bRecord )
194
0
{
195
0
    SCTAB nTab = GetViewData().CurrentTabForData();
196
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
197
0
    ScOutlineDocFunc aFunc(*pDocSh);
198
199
0
    bool bOk = aFunc.RemoveAllOutlines( nTab, bRecord );
200
201
0
    if (bOk)
202
0
    {
203
0
        ScTabViewShell::notifyAllViewsSheetGeomInvalidation(GetViewData().GetViewShell(),
204
0
                                                            true /* bColumns */, true /* bRows */, false /* bSizes*/,
205
0
                                                            true /* bHidden */, true /* bFiltered */,
206
0
                                                            true /* bGroups */, GetViewData().GetTabNumber());
207
0
        UpdateScrollBars(BOTH_HEADERS);
208
0
    }
209
0
}
210
211
// auto outlines
212
213
void ScDBFunc::AutoOutline( )
214
0
{
215
0
    ScDocument& rDoc = GetViewData().GetDocument();
216
0
    SCTAB nTab = GetViewData().CurrentTabForData();
217
0
    ScRange aRange( 0,0,nTab, rDoc.MaxCol(),rDoc.MaxRow(),nTab );     // the complete sheet, if nothing is marked
218
0
    ScMarkData& rMark = GetViewData().GetMarkData();
219
0
    if ( rMark.IsMarked() || rMark.IsMultiMarked() )
220
0
    {
221
0
        rMark.MarkToMulti();
222
0
        aRange = rMark.GetMultiMarkArea();
223
0
    }
224
225
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
226
0
    ScOutlineDocFunc aFunc(*pDocSh);
227
0
    aFunc.AutoOutline( aRange, true );
228
0
}
229
230
// select outline level
231
232
void ScDBFunc::SelectLevel( bool bColumns, sal_uInt16 nLevel, bool bRecord )
233
0
{
234
0
    SCTAB nTab = GetViewData().CurrentTabForData();
235
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
236
0
    ScOutlineDocFunc aFunc(*pDocSh);
237
238
0
    bool bOk = aFunc.SelectLevel( nTab, bColumns, nLevel, bRecord, true/*bPaint*/ );
239
240
0
    if (bOk)
241
0
    {
242
0
        ScTabViewShell::notifyAllViewsSheetGeomInvalidation(GetViewData().GetViewShell(),
243
0
                                                            bColumns, !bColumns, false /* bSizes*/,
244
0
                                                            true /* bHidden */, true /* bFiltered */,
245
0
                                                            true /* bGroups */, GetViewData().GetTabNumber());
246
0
        UpdateScrollBars(bColumns ? COLUMN_HEADER : ROW_HEADER);
247
0
    }
248
0
}
249
250
// show individual outline groups
251
252
void ScDBFunc::SetOutlineState( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bHidden)
253
0
{
254
0
    const sal_uInt16 nHeadEntry = static_cast< sal_uInt16 >( -1 );
255
0
    if ( nEntry ==  nHeadEntry)
256
0
        SelectLevel( bColumns, sal::static_int_cast<sal_uInt16>(nLevel) );
257
0
    else
258
0
    {
259
0
        if ( !bHidden )
260
0
            ShowOutline( bColumns, sal::static_int_cast<sal_uInt16>(nLevel), sal::static_int_cast<sal_uInt16>(nEntry) );
261
0
        else
262
0
            HideOutline( bColumns, sal::static_int_cast<sal_uInt16>(nLevel), sal::static_int_cast<sal_uInt16>(nEntry) );
263
0
    }
264
0
}
265
266
void ScDBFunc::ShowOutline( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bRecord, bool bPaint )
267
0
{
268
0
    SCTAB nTab = GetViewData().CurrentTabForData();
269
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
270
0
    ScOutlineDocFunc aFunc(*pDocSh);
271
272
0
    aFunc.ShowOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint );
273
274
0
    if ( bPaint )
275
0
    {
276
0
        ScTabViewShell::notifyAllViewsSheetGeomInvalidation(GetViewData().GetViewShell(),
277
0
                                                            bColumns, !bColumns, false /* bSizes*/,
278
0
                                                            true /* bHidden */, true /* bFiltered */,
279
0
                                                            true /* bGroups */, GetViewData().GetTabNumber());
280
0
        UpdateScrollBars(bColumns ? COLUMN_HEADER : ROW_HEADER);
281
0
    }
282
0
}
283
284
// hide individual outline groups
285
286
void ScDBFunc::HideOutline( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bRecord, bool bPaint )
287
0
{
288
0
    SCTAB nTab = GetViewData().CurrentTabForData();
289
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
290
0
    ScOutlineDocFunc aFunc(*pDocSh);
291
292
0
    bool bOk = aFunc.HideOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint );
293
294
0
    if ( bOk && bPaint )
295
0
    {
296
0
        ScTabViewShell::notifyAllViewsSheetGeomInvalidation(GetViewData().GetViewShell(),
297
0
                                                            bColumns, !bColumns, false /* bSizes*/,
298
0
                                                            true /* bHidden */, true /* bFiltered */,
299
0
                                                            true /* bGroups */, GetViewData().GetTabNumber());
300
0
        UpdateScrollBars(bColumns ? COLUMN_HEADER : ROW_HEADER);
301
0
    }
302
0
}
303
304
// menu status: show/hide marked range
305
306
bool ScDBFunc::OutlinePossible(bool bHide)
307
0
{
308
0
    bool bEnable = false;
309
310
0
    SCCOL nStartCol;
311
0
    SCROW nStartRow;
312
0
    SCTAB nStartTab;
313
0
    SCCOL nEndCol;
314
0
    SCROW nEndRow;
315
0
    SCTAB nEndTab;
316
317
0
    if (GetViewData().GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
318
0
    {
319
0
        ScDocument& rDoc = GetViewData().GetDocument();
320
0
        SCTAB nTab = GetViewData().CurrentTabForData();
321
0
        ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
322
0
        if (pTable)
323
0
        {
324
0
            SCCOLROW nStart;
325
0
            SCCOLROW nEnd;
326
327
            // columns
328
329
0
            ScOutlineArray& rColArray = pTable->GetColArray();
330
0
            ScSubOutlineIterator aColIter( &rColArray );
331
0
            while (!bEnable)
332
0
            {
333
0
                ScOutlineEntry* pEntry = aColIter.GetNext();
334
0
                if (!pEntry)
335
0
                    break;
336
0
                nStart = pEntry->GetStart();
337
0
                nEnd   = pEntry->GetEnd();
338
0
                if ( bHide )
339
0
                {
340
0
                    if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
341
0
                        if (!pEntry->IsHidden())
342
0
                            bEnable = true;
343
0
                }
344
0
                else
345
0
                {
346
0
                    if ( nStart>=nStartCol && nEnd<=nEndCol )
347
0
                        if (pEntry->IsHidden())
348
0
                            bEnable = true;
349
0
                }
350
0
            }
351
352
            // rows
353
354
0
            ScOutlineArray& rRowArray = pTable->GetRowArray();
355
0
            ScSubOutlineIterator aRowIter( &rRowArray );
356
0
            for (;;)
357
0
            {
358
0
                ScOutlineEntry* pEntry = aRowIter.GetNext();
359
0
                if (!pEntry)
360
0
                    break;
361
0
                nStart = pEntry->GetStart();
362
0
                nEnd   = pEntry->GetEnd();
363
0
                if ( bHide )
364
0
                {
365
0
                    if ( nStartRow<=nEnd && nEndRow>=nStart )
366
0
                        if (!pEntry->IsHidden())
367
0
                            bEnable = true;
368
0
                }
369
0
                else
370
0
                {
371
0
                    if ( nStart>=nStartRow && nEnd<=nEndRow )
372
0
                        if (pEntry->IsHidden())
373
0
                            bEnable = true;
374
0
                }
375
0
            }
376
0
        }
377
0
    }
378
379
0
    return bEnable;
380
0
}
381
382
// show marked range
383
384
void ScDBFunc::ShowMarkedOutlines( bool bRecord )
385
0
{
386
0
    ScRange aRange;
387
0
    if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
388
0
    {
389
0
        ScDocShell* pDocSh = GetViewData().GetDocShell();
390
0
        ScOutlineDocFunc aFunc(*pDocSh);
391
0
        bool bDone = aFunc.ShowMarkedOutlines( aRange, bRecord );
392
0
        if (bDone)
393
0
        {
394
0
            ScTabViewShell::notifyAllViewsSheetGeomInvalidation(
395
0
                GetViewData().GetViewShell(), true, true,
396
0
                false /* bSizes*/, true /* bHidden */, true /* bFiltered */,
397
0
                true /* bGroups */, GetViewData().GetTabNumber());
398
0
            UpdateScrollBars();
399
0
        }
400
0
    }
401
0
    else
402
0
        ErrorMessage(STR_NOMULTISELECT);
403
0
}
404
405
// hide marked range
406
407
void ScDBFunc::HideMarkedOutlines( bool bRecord )
408
0
{
409
0
    ScRange aRange;
410
0
    if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
411
0
    {
412
0
        ScDocShell* pDocSh = GetViewData().GetDocShell();
413
0
        ScOutlineDocFunc aFunc(*pDocSh);
414
0
        bool bDone = aFunc.HideMarkedOutlines( aRange, bRecord );
415
0
        if (bDone)
416
0
        {
417
0
            ScTabViewShell::notifyAllViewsSheetGeomInvalidation(
418
0
                GetViewData().GetViewShell(), true, true,
419
0
                false /* bSizes*/, true /* bHidden */, true /* bFiltered */,
420
0
                true /* bGroups */, GetViewData().GetTabNumber());
421
0
            UpdateScrollBars();
422
0
        }
423
0
    }
424
0
    else
425
0
        ErrorMessage(STR_NOMULTISELECT);
426
0
}
427
428
// sub totals
429
430
void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, bool bRecord,
431
                            const ScSortParam* pForceNewSort )
432
0
{
433
0
    bool bDo = !rParam.bRemoveOnly;                         // sal_False = only delete
434
435
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
436
0
    ScDocument& rDoc = pDocSh->GetDocument();
437
0
    ScMarkData& rMark = GetViewData().GetMarkData();
438
0
    SCTAB nTab = GetViewData().CurrentTabForData();
439
0
    if (bRecord && !rDoc.IsUndoEnabled())
440
0
        bRecord = false;
441
442
0
    ScDBData* pDBData = rDoc.GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
443
0
                                                rParam.nCol2, rParam.nRow2 );
444
0
    if (!pDBData)
445
0
    {
446
0
        OSL_FAIL( "SubTotals: no DBData" );
447
0
        return;
448
0
    }
449
450
0
    ScEditableTester aTester = ScEditableTester::CreateAndTestBlock(rDoc, nTab, 0, rParam.nRow1 + 1, rDoc.MaxCol(), rDoc.MaxRow());
451
0
    if (!aTester.IsEditable())
452
0
    {
453
0
        ErrorMessage(aTester.GetMessageId());
454
0
        return;
455
0
    }
456
457
0
    if (rDoc.HasAttrib( rParam.nCol1, rParam.nRow1+1, nTab,
458
0
                         rParam.nCol2, rParam.nRow2, nTab, HasAttrFlags::Merged | HasAttrFlags::Overlapped ))
459
0
    {
460
0
        ErrorMessage(STR_MSSG_INSERTCELLS_0);   // do not insert into merged
461
0
        return;
462
0
    }
463
464
0
    weld::WaitObject aWait(GetViewData().GetDialogParent());
465
0
    bool bOk = true;
466
0
    if (rParam.bReplace)
467
0
    {
468
0
        if (rDoc.TestRemoveSubTotals( nTab, rParam ))
469
0
        {
470
0
            std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetViewData().GetDialogParent(),
471
0
                                                      VclMessageType::Question, VclButtonsType::YesNo,
472
0
                                                      ScResId(STR_MSSG_DOSUBTOTALS_1))); // "delete data?"
473
0
            xBox->set_title(ScResId(STR_MSSG_DOSUBTOTALS_0)); // "StarCalc"
474
0
            xBox->set_default_response(RET_YES);
475
0
            bOk = xBox->run() == RET_YES;
476
0
        }
477
0
    }
478
479
0
    if (!bOk)
480
0
        return;
481
482
0
    ScDocShellModificator aModificator( *pDocSh );
483
484
0
    ScSubTotalParam aNewParam( rParam );        // change end of range
485
0
    ScDocumentUniquePtr pUndoDoc;
486
0
    std::unique_ptr<ScOutlineTable> pUndoTab;
487
0
    std::unique_ptr<ScRangeName> pUndoRange;
488
0
    std::unique_ptr<ScDBCollection> pUndoDB;
489
490
0
    if (bRecord)                                        // record old data
491
0
    {
492
0
        bool bOldFilter = bDo && rParam.bDoSort;
493
0
        SCTAB nTabCount = rDoc.GetTableCount();
494
0
        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
495
0
        ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
496
0
        if (pTable)
497
0
        {
498
0
            pUndoTab.reset(new ScOutlineTable( *pTable ));
499
500
0
            SCCOLROW nOutStartCol;                          // row/column status
501
0
            SCCOLROW nOutStartRow;
502
0
            SCCOLROW nOutEndCol;
503
0
            SCCOLROW nOutEndRow;
504
0
            pTable->GetColArray().GetRange( nOutStartCol, nOutEndCol );
505
0
            pTable->GetRowArray().GetRange( nOutStartRow, nOutEndRow );
506
507
0
            pUndoDoc->InitUndo( rDoc, nTab, nTab, true, true );
508
0
            rDoc.CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), rDoc.MaxRow(), nTab, InsertDeleteFlags::NONE, false, *pUndoDoc );
509
0
            rDoc.CopyToDocument( 0, nOutStartRow, nTab, rDoc.MaxCol(), nOutEndRow, nTab, InsertDeleteFlags::NONE, false, *pUndoDoc );
510
0
        }
511
0
        else
512
0
            pUndoDoc->InitUndo( rDoc, nTab, nTab, false, bOldFilter );
513
514
        // record data range - including filter results
515
0
        rDoc.CopyToDocument( 0,rParam.nRow1+1,nTab, rDoc.MaxCol(),rParam.nRow2,nTab,
516
0
                                InsertDeleteFlags::ALL, false, *pUndoDoc );
517
518
        // all formulas for reference
519
0
        rDoc.CopyToDocument( 0,0,0, rDoc.MaxCol(),rDoc.MaxRow(),nTabCount-1,
520
0
                                    InsertDeleteFlags::FORMULA, false, *pUndoDoc );
521
522
        // database and other ranges
523
0
        ScRangeName* pDocRange = rDoc.GetRangeName();
524
0
        if (!pDocRange->empty())
525
0
            pUndoRange.reset(new ScRangeName( *pDocRange ));
526
0
        ScDBCollection* pDocDB = rDoc.GetDBCollection();
527
0
        if (!pDocDB->empty())
528
0
            pUndoDB.reset(new ScDBCollection( *pDocDB ));
529
0
    }
530
531
0
    ScOutlineTable* pOut = rDoc.GetOutlineTable( nTab );
532
0
    if (pOut)
533
0
    {
534
        // Remove all existing outlines in the specified range.
535
0
        ScOutlineArray& rRowArray = pOut->GetRowArray();
536
0
        sal_uInt16 nDepth = rRowArray.GetDepth();
537
0
        for (sal_uInt16 i = 0; i < nDepth; ++i)
538
0
        {
539
0
            bool bSize;
540
0
            rRowArray.Remove(aNewParam.nRow1, aNewParam.nRow2, bSize);
541
0
        }
542
0
    }
543
544
0
    if (rParam.bReplace)
545
0
        rDoc.RemoveSubTotals( nTab, aNewParam );
546
0
    bool bSuccess = true;
547
0
    if (bDo)
548
0
    {
549
        // Sort
550
0
        if ( rParam.bDoSort || pForceNewSort )
551
0
        {
552
0
            pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
553
554
            // set subtotal fields before sorting
555
            // (duplicate values are dropped, so that they can be called again)
556
557
0
            ScSortParam aOldSort;
558
0
            pDBData->GetSortParam( aOldSort );
559
0
            ScSortParam aSortParam( aNewParam, pForceNewSort ? *pForceNewSort : aOldSort );
560
0
            Sort( aSortParam, false, false );
561
0
        }
562
563
0
        bSuccess = rDoc.DoSubTotals( nTab, aNewParam );
564
0
    }
565
0
    ScRange aDirtyRange( aNewParam.nCol1, aNewParam.nRow1, nTab,
566
0
        aNewParam.nCol2, aNewParam.nRow2, nTab );
567
0
    rDoc.SetDirty( aDirtyRange, true );
568
569
0
    if (bRecord)
570
0
    {
571
0
        pDocSh->GetUndoManager()->AddUndoAction(
572
0
            std::make_unique<ScUndoSubTotals>( *pDocSh, nTab,
573
0
                                    rParam, aNewParam.nRow2,
574
0
                                    std::move(pUndoDoc), std::move(pUndoTab), // pUndoDBData,
575
0
                                    std::move(pUndoRange), std::move(pUndoDB) ) );
576
0
    }
577
578
0
    if (!bSuccess)
579
0
    {
580
        // "Can not insert any rows"
581
0
        ErrorMessage(STR_MSSG_DOSUBTOTALS_2);
582
0
    }
583
584
                                                // store
585
0
    pDBData->SetSubTotalParam( aNewParam );
586
0
    pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
587
0
    rDoc.CompileDBFormula();
588
589
0
    const ScRange aMarkRange( aNewParam.nCol1, aNewParam.nRow1, nTab, aNewParam.nCol2, aNewParam.nRow2, nTab);
590
0
    DoneBlockMode();
591
0
    InitOwnBlockMode( aMarkRange );
592
0
    rMark.SetMarkArea( aMarkRange );
593
0
    MarkDataChanged();
594
595
0
    pDocSh->PostPaint(ScRange(0, 0, nTab, rDoc.MaxCol(), rDoc.MaxRow(), nTab),
596
0
                      PaintPartFlags::Grid | PaintPartFlags::Left | PaintPartFlags::Top | PaintPartFlags::Size);
597
598
0
    aModificator.SetDocumentModified();
599
600
0
    SelectionChanged();
601
0
}
602
603
void ScDBFunc::DoTableSubTotals( const ScDBData& rNewData, const ScSubTotalParam& rParam, bool bRecord )
604
0
{
605
0
    bool bDo = !rParam.bRemoveOnly; // sal_False = only delete
606
607
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
608
0
    ScDocument& rDoc = pDocSh->GetDocument();
609
0
    SCTAB nTab = GetViewData().GetTabNumber();
610
0
    if (bRecord && !rDoc.IsUndoEnabled())
611
0
        bRecord = false;
612
613
0
    ScDBData* pDBData
614
0
        = rDoc.GetDBAtArea(nTab, rParam.nCol1, rParam.nRow1, rParam.nCol2, rParam.nRow2);
615
0
    if (!pDBData)
616
0
    {
617
0
        OSL_FAIL("SubTotals: no DBData");
618
0
        return;
619
0
    }
620
621
0
    ScEditableTester aTester = ScEditableTester::CreateAndTestBlock(rDoc, nTab, 0, rParam.nRow1 + 1, rDoc.MaxCol(), rDoc.MaxRow());
622
0
    if (!aTester.IsEditable())
623
0
    {
624
0
        ErrorMessage(aTester.GetMessageId());
625
0
        return;
626
0
    }
627
628
0
    if (rDoc.HasAttrib(rParam.nCol1, rParam.nRow1 + 1, nTab, rParam.nCol2, rParam.nRow2, nTab,
629
0
                       HasAttrFlags::Merged | HasAttrFlags::Overlapped))
630
0
    {
631
0
        ErrorMessage(STR_MSSG_INSERTCELLS_0); // do not insert into merged
632
0
        return;
633
0
    }
634
635
0
    weld::WaitObject aWait(GetViewData().GetDialogParent());
636
0
    ScDocShellModificator aModificator(*pDocSh);
637
638
    // ScSubTotalParam aNewParam(rParam);
639
0
    ScSubTotalParam aNewParam;
640
0
    rNewData.GetSubTotalParam(aNewParam); // change end of range
641
0
    ScDocumentUniquePtr pUndoDoc;
642
0
    std::unique_ptr<ScDBCollection> pUndoDB;
643
644
0
    if (bRecord) // record old data
645
0
    {
646
0
        bool bOldFilter = bDo && rParam.bDoSort;
647
0
        SCTAB nTabCount = rDoc.GetTableCount();
648
0
        pUndoDoc.reset(new ScDocument(SCDOCMODE_UNDO));
649
0
        pUndoDoc->InitUndo(rDoc, nTab, nTab, false, bOldFilter);
650
651
        // record data range - including filter results
652
0
        rDoc.CopyToDocument(rParam.nCol1, rParam.nRow1 + 1, nTab, rParam.nCol2, rParam.nRow2, nTab,
653
0
                            InsertDeleteFlags::ALL, false, *pUndoDoc);
654
655
        // all formulas for reference
656
0
        rDoc.CopyToDocument(0, 0, 0, rDoc.MaxCol(), rDoc.MaxRow(), nTabCount - 1,
657
0
                            InsertDeleteFlags::FORMULA, false, *pUndoDoc);
658
659
        // database ranges
660
0
        ScDBCollection* pDocDB = rDoc.GetDBCollection();
661
0
        if (!pDocDB->empty())
662
0
            pUndoDB.reset(new ScDBCollection(*pDocDB));
663
0
    }
664
665
0
    if (rParam.bReplace)
666
0
        rDoc.RemoveTableSubTotals(nTab, aNewParam, rParam);
667
0
    bool bSuccess = true;
668
0
    if (bDo)
669
0
    {
670
0
        bSuccess = rDoc.DoTableSubTotals(nTab, aNewParam);
671
0
    }
672
0
    ScRange aDirtyRange(aNewParam.nCol1, aNewParam.nRow1, nTab, aNewParam.nCol2, aNewParam.nRow2,
673
0
                        nTab);
674
0
    rDoc.SetDirty(aDirtyRange, true);
675
676
    // Need to store with the new values
677
0
    *pDBData = rNewData;
678
0
    if (bRecord)
679
0
    {
680
0
        ScDBCollection* pDocDB = rDoc.GetDBCollection();
681
0
        pDocSh->GetUndoManager()->AddUndoAction(std::make_unique<ScUndoTableTotals>(
682
0
            *pDocSh, nTab, rParam, aNewParam.nRow2, std::move(pUndoDoc),
683
0
            std::move(pUndoDB), std::make_unique<ScDBCollection>(*pDocDB)));
684
0
    }
685
686
0
    if (!bSuccess)
687
0
    {
688
        // "Can not insert any rows"
689
0
        ErrorMessage(STR_MSSG_DOSUBTOTALS_2);
690
0
    }
691
692
    // store
693
0
    pDBData->SetSubTotalParam(aNewParam);
694
0
    pDBData->SetArea(nTab, aNewParam.nCol1, aNewParam.nRow1, aNewParam.nCol2, aNewParam.nRow2);
695
0
    rDoc.CompileDBFormula();
696
697
0
    pDocSh->PostPaint(ScRange(0, 0, nTab, rDoc.MaxCol(), rDoc.MaxRow(), nTab),
698
0
                     PaintPartFlags::Grid | PaintPartFlags::Left | PaintPartFlags::Top
699
0
                         | PaintPartFlags::Size);
700
701
0
    aModificator.SetDocumentModified();
702
703
0
    SelectionChanged();
704
0
}
705
706
void ScDBFunc::DeleteCalcTable()
707
0
{
708
0
    ScDocShell* pDocSh = GetViewData().GetDocShell();
709
0
    ScDocument& rDoc = pDocSh->GetDocument();
710
0
    ScDBData* pDBObj = rDoc.GetTableDBAtCursor(GetViewData().GetCurX(), GetViewData().GetCurY(),
711
0
                                          GetViewData().CurrentTabForData(), ScDBDataPortion::AREA);
712
0
    if (pDBObj)
713
0
    {
714
0
        ScDBDocFunc aFunc(*pDocSh);
715
0
        aFunc.DeleteDBTable(pDBObj, true, false);
716
0
        CursorPosChanged(); // shells may be switched
717
0
    }
718
0
    else
719
0
        ErrorMessage(STR_TABLE_NOTFOUND);
720
0
}
721
722
// consolidate
723
724
void ScDBFunc::Consolidate( const ScConsolidateParam& rParam )
725
0
{
726
0
    ScDocShell* pDocShell = GetViewData().GetDocShell();
727
0
    pDocShell->DoConsolidate( rParam );
728
0
    SetTabNo( rParam.nTab, true );
729
0
}
730
731
// pivot
732
733
static OUString lcl_MakePivotTabName( std::u16string_view rPrefix, SCTAB nNumber )
734
0
{
735
0
    OUString aName = rPrefix + OUString::number( nNumber );
736
0
    return aName;
737
0
}
738
739
bool ScDBFunc::MakePivotTable(
740
    const ScDPSaveData& rData, const ScRange& rDest, bool bNewTable,
741
    const ScDPObject& rSource )
742
0
{
743
    //  error message if no fields are set
744
    //  this must be removed when drag&drop of fields from a toolbox is available
745
746
0
    if ( rData.IsEmpty() )
747
0
    {
748
0
        ErrorMessage(STR_PIVOT_NODATA);
749
0
        return false;
750
0
    }
751
752
0
    ScDocShell* pDocSh  = GetViewData().GetDocShell();
753
0
    ScDocument& rDoc    = GetViewData().GetDocument();
754
0
    bool bUndo = rDoc.IsUndoEnabled();
755
756
0
    ScRange aDestRange = rDest;
757
0
    if ( bNewTable )
758
0
    {
759
0
        SCTAB nSrcTab = GetViewData().CurrentTabForData();
760
761
0
        OUString aName( ScResId(STR_PIVOT_TABLE) );
762
0
        OUString aStr;
763
764
0
        rDoc.GetName( nSrcTab, aStr );
765
0
        aName += "_" + aStr + "_";
766
767
0
        SCTAB nNewTab = nSrcTab+1;
768
769
0
        SCTAB i=1;
770
0
        while ( !rDoc.InsertTab( nNewTab, lcl_MakePivotTabName( aName, i ) ) && i <= MAXTAB )
771
0
            i++;
772
773
0
        bool bAppend = ( nNewTab+1 == rDoc.GetTableCount() );
774
0
        if (bUndo)
775
0
        {
776
0
            pDocSh->GetUndoManager()->AddUndoAction(
777
0
                        std::make_unique<ScUndoInsertTab>( *pDocSh, nNewTab, bAppend, lcl_MakePivotTabName( aName, i ) ));
778
0
        }
779
780
0
        GetViewData().InsertTab( nNewTab );
781
0
        SetTabNo(nNewTab, true);
782
783
0
        aDestRange = ScRange( 0, 0, nNewTab );
784
0
    }
785
786
0
    ScDPObject* pDPObj = rDoc.GetDPAtCursor(
787
0
                            aDestRange.aStart.Col(), aDestRange.aStart.Row(), aDestRange.aStart.Tab() );
788
789
0
    ScDPObject aObj( rSource );
790
0
    aObj.SetOutRange( aDestRange );
791
0
    if ( pDPObj && (!rData.GetExistingDimensionData() || !rData.GetExistingDimCalcData()) )
792
0
    {
793
0
        ScDPSaveData aNewData( rData );
794
0
        const ScDPSaveData* pOldData = pDPObj->GetSaveData();
795
0
        if ( pOldData )
796
0
        {
797
            // copy dimension data from old object - lost in the dialog
798
            //! change the dialog to keep the dimension data
799
0
            if (!rData.GetExistingDimensionData())
800
0
            {
801
0
                const ScDPDimensionSaveData* pDimSave = pOldData->GetExistingDimensionData();
802
0
                aNewData.SetDimensionData(pDimSave);
803
0
            }
804
            // copy calculation dimension data from old object - stored in a different dialog
805
0
            if (!rData.GetExistingDimCalcData())
806
0
            {
807
0
                const ScDPDimCalcSaveData* pDimCalcData = pOldData->GetExistingDimCalcData();
808
0
                aNewData.SetDimCalcData(pDimCalcData);
809
0
            }
810
0
        }
811
0
        aObj.SetSaveData( aNewData );
812
0
    }
813
0
    else
814
0
        aObj.SetSaveData( rData );
815
816
0
    bool bAllowMove = (pDPObj != nullptr);   // allow re-positioning when editing existing table
817
818
0
    ScDBDocFunc aFunc( *pDocSh );
819
0
    bool bSuccess = aFunc.DataPilotUpdate(pDPObj, &aObj, true, false, bAllowMove);
820
821
0
    CursorPosChanged();     // shells may be switched
822
823
0
    if ( bNewTable )
824
0
    {
825
0
        pDocSh->PostPaintExtras();
826
0
        SfxGetpApp()->Broadcast( SfxHint( SfxHintId::ScTablesChanged ) );
827
0
    }
828
829
0
    return bSuccess;
830
0
}
831
832
void ScDBFunc::DeletePivotTable()
833
0
{
834
0
    ScDocShell* pDocSh    = GetViewData().GetDocShell();
835
0
    ScDocument& rDoc      = pDocSh->GetDocument();
836
0
    ScDPObject* pDPObj    = rDoc.GetDPAtCursor( GetViewData().GetCurX(),
837
0
                                                  GetViewData().GetCurY(),
838
0
                                                  GetViewData().CurrentTabForData() );
839
0
    if ( pDPObj )
840
0
    {
841
0
        ScDBDocFunc aFunc( *pDocSh );
842
0
        aFunc.RemovePivotTable(*pDPObj, true, false);
843
0
        CursorPosChanged();     // shells may be switched
844
0
    }
845
0
    else
846
0
        ErrorMessage(STR_PIVOT_NOTFOUND);
847
0
}
848
849
void ScDBFunc::RecalcPivotTable()
850
0
{
851
0
    ScDocShell* pDocSh  = GetViewData().GetDocShell();
852
0
    ScDocument& rDoc    = GetViewData().GetDocument();
853
854
0
    ScDPObject* pDPObj  = rDoc.GetDPAtCursor( GetViewData().GetCurX(),
855
0
                                              GetViewData().GetCurY(),
856
0
                                              GetViewData().CurrentTabForData() );
857
0
    if (pDPObj)
858
0
    {
859
        // Remove existing data cache for the data that this datapilot uses,
860
        // to force re-build data cache.
861
0
        ScDBDocFunc aFunc(*pDocSh);
862
0
        aFunc.RefreshPivotTables(pDPObj, false);
863
864
0
        CursorPosChanged();     // shells may be switched
865
0
    }
866
0
    else
867
0
        ErrorMessage(STR_PIVOT_NOTFOUND);
868
0
}
869
870
void ScDBFunc::GetSelectedMemberList(ScDPUniqueStringSet& rEntries, tools::Long& rDimension)
871
0
{
872
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
873
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
874
0
    if ( !pDPObj )
875
0
        return;
876
877
0
    tools::Long nStartDimension = -1;
878
0
    tools::Long nStartHierarchy = -1;
879
0
    tools::Long nStartLevel     = -1;
880
881
0
    ScRangeListRef xRanges;
882
0
    GetViewData().GetMultiArea( xRanges );         // incl. cursor if nothing is selected
883
0
    size_t nRangeCount = xRanges->size();
884
0
    bool bContinue = true;
885
886
0
    for (size_t nRangePos=0; nRangePos < nRangeCount && bContinue; nRangePos++)
887
0
    {
888
0
        ScRange const & rRange = (*xRanges)[nRangePos];
889
0
        SCCOL nStartCol = rRange.aStart.Col();
890
0
        SCROW nStartRow = rRange.aStart.Row();
891
0
        SCCOL nEndCol = rRange.aEnd.Col();
892
0
        SCROW nEndRow = rRange.aEnd.Row();
893
0
        SCTAB nTab = rRange.aStart.Tab();
894
895
0
        for (SCROW nRow=nStartRow; nRow<=nEndRow && bContinue; nRow++)
896
0
            for (SCCOL nCol=nStartCol; nCol<=nEndCol && bContinue; nCol++)
897
0
            {
898
0
                sheet::DataPilotTableHeaderData aData;
899
0
                pDPObj->GetHeaderPositionData(ScAddress(nCol, nRow, nTab), aData);
900
0
                if ( aData.Dimension < 0 )
901
0
                    bContinue = false;              // not part of any dimension
902
0
                else
903
0
                {
904
0
                    if ( nStartDimension < 0 )      // first member?
905
0
                    {
906
0
                        nStartDimension = aData.Dimension;
907
0
                        nStartHierarchy = aData.Hierarchy;
908
0
                        nStartLevel     = aData.Level;
909
0
                    }
910
0
                    if ( aData.Dimension != nStartDimension ||
911
0
                         aData.Hierarchy != nStartHierarchy ||
912
0
                         aData.Level     != nStartLevel )
913
0
                    {
914
0
                        bContinue = false;          // cannot mix dimensions
915
0
                    }
916
0
                }
917
0
                if ( bContinue )
918
0
                {
919
                    // accept any part of a member description, also subtotals,
920
                    // but don't stop if empty parts are contained
921
0
                    if ( aData.Flags & sheet::MemberResultFlags::HASMEMBER )
922
0
                        rEntries.insert(aData.MemberName);
923
0
                }
924
0
            }
925
0
    }
926
927
0
    rDimension = nStartDimension;   // dimension from which the found members came
928
0
    if (!bContinue)
929
0
        rEntries.clear();         // remove all if not valid
930
0
}
931
932
bool ScDBFunc::HasSelectionForDateGroup( ScDPNumGroupInfo& rOldInfo, sal_Int32& rParts )
933
0
{
934
    // determine if the date group dialog has to be shown for the current selection
935
936
0
    bool bFound = false;
937
938
0
    SCCOL nCurX = GetViewData().GetCurX();
939
0
    SCROW nCurY = GetViewData().GetCurY();
940
0
    SCTAB nTab = GetViewData().CurrentTabForData();
941
0
    ScDocument& rDoc = GetViewData().GetDocument();
942
943
0
    ScDPObject* pDPObj = rDoc.GetDPAtCursor( nCurX, nCurY, nTab );
944
0
    if ( pDPObj )
945
0
    {
946
0
        ScDPUniqueStringSet aEntries;
947
0
        tools::Long nSelectDimension = -1;
948
0
        GetSelectedMemberList( aEntries, nSelectDimension );
949
950
0
        if (!aEntries.empty())
951
0
        {
952
0
            bool bIsDataLayout;
953
0
            OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
954
0
            OUString aBaseDimName( aDimName );
955
956
0
            bool bInGroupDim = false;
957
0
            bool bFoundParts = false;
958
959
0
            ScDPDimensionSaveData* pDimData =
960
0
                const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
961
0
            if ( pDimData )
962
0
            {
963
0
                const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
964
0
                const ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDim( aDimName );
965
0
                if ( pNumGroupDim )
966
0
                {
967
                    //  existing num group dimension
968
969
0
                    if ( pNumGroupDim->GetDatePart() != 0 )
970
0
                    {
971
                        //  dimension has date info -> edit settings of this dimension
972
                        //  (parts are collected below)
973
974
0
                        rOldInfo = pNumGroupDim->GetDateInfo();
975
0
                        bFound = true;
976
0
                    }
977
0
                    else if ( pNumGroupDim->GetInfo().mbDateValues )
978
0
                    {
979
                        //  Numerical grouping with DateValues flag is used for grouping
980
                        //  of days with a "Number of days" value.
981
982
0
                        rOldInfo = pNumGroupDim->GetInfo();
983
0
                        rParts = css::sheet::DataPilotFieldGroupBy::DAYS;               // not found in CollectDateParts
984
0
                        bFoundParts = true;
985
0
                        bFound = true;
986
0
                    }
987
0
                    bInGroupDim = true;
988
0
                }
989
0
                else if ( pGroupDim )
990
0
                {
991
                    //  existing additional group dimension
992
993
0
                    if ( pGroupDim->GetDatePart() != 0 )
994
0
                    {
995
                        //  dimension has date info -> edit settings of this dimension
996
                        //  (parts are collected below)
997
998
0
                        rOldInfo = pGroupDim->GetDateInfo();
999
0
                        aBaseDimName = pGroupDim->GetSourceDimName();
1000
0
                        bFound = true;
1001
0
                    }
1002
0
                    bInGroupDim = true;
1003
0
                }
1004
0
            }
1005
0
            if ( bFound && !bFoundParts )
1006
0
            {
1007
                // collect date parts from all group dimensions
1008
0
                rParts = pDimData->CollectDateParts( aBaseDimName );
1009
0
            }
1010
0
            if ( !bFound && !bInGroupDim )
1011
0
            {
1012
                // create new date group dimensions if the selection is a single cell
1013
                // in a normal dimension with date content
1014
1015
0
                ScRange aSelRange;
1016
0
                if ( (GetViewData().GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
1017
0
                        aSelRange.aStart == aSelRange.aEnd )
1018
0
                {
1019
0
                    SCCOL nSelCol = aSelRange.aStart.Col();
1020
0
                    SCROW nSelRow = aSelRange.aStart.Row();
1021
0
                    SCTAB nSelTab = aSelRange.aStart.Tab();
1022
0
                    if ( rDoc.HasValueData( nSelCol, nSelRow, nSelTab ) )
1023
0
                    {
1024
0
                        sal_uLong nIndex = rDoc.GetAttr(
1025
0
                                        nSelCol, nSelRow, nSelTab, ATTR_VALUE_FORMAT).GetValue();
1026
0
                        SvNumFormatType nType = rDoc.GetFormatTable()->GetType(nIndex);
1027
0
                        if ( nType == SvNumFormatType::DATE || nType == SvNumFormatType::TIME || nType == SvNumFormatType::DATETIME )
1028
0
                        {
1029
0
                            bFound = true;
1030
                            // use currently selected value for automatic limits
1031
0
                            if( rOldInfo.mbAutoStart )
1032
0
                                rOldInfo.mfStart = rDoc.GetValue( aSelRange.aStart );
1033
0
                            if( rOldInfo.mbAutoEnd )
1034
0
                                rOldInfo.mfEnd = rDoc.GetValue( aSelRange.aStart );
1035
0
                        }
1036
0
                    }
1037
0
                }
1038
0
            }
1039
0
        }
1040
0
    }
1041
1042
0
    return bFound;
1043
0
}
1044
1045
bool ScDBFunc::HasSelectionForNumGroup( ScDPNumGroupInfo& rOldInfo )
1046
0
{
1047
    // determine if the numeric group dialog has to be shown for the current selection
1048
1049
0
    bool bFound = false;
1050
1051
0
    SCCOL nCurX = GetViewData().GetCurX();
1052
0
    SCROW nCurY = GetViewData().GetCurY();
1053
0
    SCTAB nTab = GetViewData().CurrentTabForData();
1054
0
    ScDocument& rDoc = GetViewData().GetDocument();
1055
1056
0
    ScDPObject* pDPObj = rDoc.GetDPAtCursor( nCurX, nCurY, nTab );
1057
0
    if ( pDPObj )
1058
0
    {
1059
0
        ScDPUniqueStringSet aEntries;
1060
0
        tools::Long nSelectDimension = -1;
1061
0
        GetSelectedMemberList( aEntries, nSelectDimension );
1062
1063
0
        if (!aEntries.empty())
1064
0
        {
1065
0
            bool bIsDataLayout;
1066
0
            OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1067
1068
0
            bool bInGroupDim = false;
1069
1070
0
            ScDPDimensionSaveData* pDimData =
1071
0
                const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
1072
0
            if ( pDimData )
1073
0
            {
1074
0
                const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
1075
0
                if ( pNumGroupDim )
1076
0
                {
1077
                    //  existing num group dimension
1078
                    //  -> edit settings of this dimension
1079
1080
0
                    rOldInfo = pNumGroupDim->GetInfo();
1081
0
                    bFound = true;
1082
0
                }
1083
0
                else if ( pDimData->GetNamedGroupDim( aDimName ) )
1084
0
                    bInGroupDim = true;                                    // in a group dimension
1085
0
            }
1086
0
            if ( !bFound && !bInGroupDim )
1087
0
            {
1088
                // create a new num group dimension if the selection is a single cell
1089
                // in a normal dimension with numeric content
1090
1091
0
                ScRange aSelRange;
1092
0
                if ( (GetViewData().GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
1093
0
                        aSelRange.aStart == aSelRange.aEnd )
1094
0
                {
1095
0
                    if ( rDoc.HasValueData( aSelRange.aStart.Col(), aSelRange.aStart.Row(),
1096
0
                                             aSelRange.aStart.Tab() ) )
1097
0
                    {
1098
0
                        bFound = true;
1099
                        // use currently selected value for automatic limits
1100
0
                        if( rOldInfo.mbAutoStart )
1101
0
                            rOldInfo.mfStart = rDoc.GetValue( aSelRange.aStart );
1102
0
                        if( rOldInfo.mbAutoEnd )
1103
0
                            rOldInfo.mfEnd = rDoc.GetValue( aSelRange.aStart );
1104
0
                    }
1105
0
                }
1106
0
            }
1107
0
        }
1108
0
    }
1109
1110
0
    return bFound;
1111
0
}
1112
1113
void ScDBFunc::DateGroupDataPilot( const ScDPNumGroupInfo& rInfo, sal_Int32 nParts )
1114
0
{
1115
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
1116
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
1117
0
    if (!pDPObj)
1118
0
        return;
1119
1120
0
    ScDPUniqueStringSet aEntries;
1121
0
    tools::Long nSelectDimension = -1;
1122
0
    GetSelectedMemberList( aEntries, nSelectDimension );
1123
1124
0
    if (aEntries.empty())
1125
0
        return;
1126
1127
0
    std::vector<OUString> aDeletedNames;
1128
0
    bool bIsDataLayout;
1129
0
    OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1130
1131
0
    ScDPSaveData aData( *pDPObj->GetSaveData() );
1132
0
    ScDPDimensionSaveData* pDimData = aData.GetDimensionData();     // created if not there
1133
1134
    // find the source dimension name.
1135
0
    OUString aBaseDimName = aDimName;
1136
0
    if( const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName ) )
1137
0
        aBaseDimName = pBaseGroupDim->GetSourceDimName();
1138
1139
    // Remove all group dimensions associated with this source dimension. For
1140
    // date grouping, we need to remove all existing groups for the affected
1141
    // source dimension and build new one(s) from scratch.  Keep the deleted
1142
    // names so that they can be reused during re-construction.
1143
0
    aData.RemoveAllGroupDimensions(aBaseDimName, &aDeletedNames);
1144
1145
0
    if ( nParts )
1146
0
    {
1147
        // create date group dimensions
1148
1149
0
        bool bFirst = true;
1150
0
        sal_Int32 nMask = 1;
1151
0
        for (sal_uInt16 nBit=0; nBit<32; nBit++)
1152
0
        {
1153
0
            if ( nParts & nMask )
1154
0
            {
1155
0
                if ( bFirst )
1156
0
                {
1157
                    // innermost part: create NumGroupDimension (replacing original values)
1158
                    // Dimension name is left unchanged
1159
1160
0
                    if ( (nParts == sheet::DataPilotFieldGroupBy::DAYS) && (rInfo.mfStep >= 1.0) )
1161
0
                    {
1162
                        // only days, and a step value specified: use numerical grouping
1163
                        // with DateValues flag, not date grouping
1164
1165
0
                        ScDPNumGroupInfo aNumInfo( rInfo );
1166
0
                        aNumInfo.mbDateValues = true;
1167
1168
0
                        ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, aNumInfo );
1169
0
                        pDimData->AddNumGroupDimension( aNumGroupDim );
1170
0
                    }
1171
0
                    else
1172
0
                    {
1173
0
                        ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, rInfo, nMask );
1174
0
                        pDimData->AddNumGroupDimension( aNumGroupDim );
1175
0
                    }
1176
1177
0
                    bFirst = false;
1178
0
                }
1179
0
                else
1180
0
                {
1181
                    // additional parts: create GroupDimension (shown as additional dimensions)
1182
0
                    OUString aGroupDimName =
1183
0
                        pDimData->CreateDateGroupDimName(nMask, *pDPObj, true, &aDeletedNames);
1184
0
                    ScDPSaveGroupDimension aGroupDim( aBaseDimName, aGroupDimName );
1185
0
                    aGroupDim.SetDateInfo( rInfo, nMask );
1186
0
                    pDimData->AddGroupDimension( aGroupDim );
1187
1188
                    // set orientation
1189
0
                    ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
1190
0
                    if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
1191
0
                    {
1192
0
                        ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aBaseDimName );
1193
0
                        pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
1194
0
                        aData.SetPosition( pSaveDimension, 0 ); //! before (immediate) base
1195
0
                    }
1196
0
                }
1197
0
            }
1198
0
            nMask *= 2;
1199
0
        }
1200
0
    }
1201
1202
    // apply changes
1203
0
    ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1204
0
    pDPObj->SetSaveData( aData );
1205
0
    aFunc.RefreshPivotTableGroups(pDPObj);
1206
1207
    // unmark cell selection
1208
0
    Unmark();
1209
0
}
1210
1211
void ScDBFunc::NumGroupDataPilot( const ScDPNumGroupInfo& rInfo )
1212
0
{
1213
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
1214
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
1215
0
    if (!pDPObj)
1216
0
        return;
1217
1218
0
    ScDPUniqueStringSet aEntries;
1219
0
    tools::Long nSelectDimension = -1;
1220
0
    GetSelectedMemberList( aEntries, nSelectDimension );
1221
1222
0
    if (aEntries.empty())
1223
0
        return;
1224
1225
0
    bool bIsDataLayout;
1226
0
    OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1227
1228
0
    ScDPSaveData aData( *pDPObj->GetSaveData() );
1229
0
    ScDPDimensionSaveData* pDimData = aData.GetDimensionData();     // created if not there
1230
1231
0
    ScDPSaveNumGroupDimension* pExisting = pDimData->GetNumGroupDimAcc( aDimName );
1232
0
    if ( pExisting )
1233
0
    {
1234
        // modify existing group dimension
1235
0
        pExisting->SetGroupInfo( rInfo );
1236
0
    }
1237
0
    else
1238
0
    {
1239
        // create new group dimension
1240
0
        ScDPSaveNumGroupDimension aNumGroupDim( aDimName, rInfo );
1241
0
        pDimData->AddNumGroupDimension( aNumGroupDim );
1242
0
    }
1243
1244
    // apply changes
1245
0
    ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1246
0
    pDPObj->SetSaveData( aData );
1247
0
    aFunc.RefreshPivotTableGroups(pDPObj);
1248
1249
    // unmark cell selection
1250
0
    Unmark();
1251
0
}
1252
1253
void ScDBFunc::GroupDataPilot()
1254
0
{
1255
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
1256
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
1257
0
    if (!pDPObj)
1258
0
        return;
1259
1260
0
    ScDPUniqueStringSet aEntries;
1261
0
    tools::Long nSelectDimension = -1;
1262
0
    GetSelectedMemberList( aEntries, nSelectDimension );
1263
1264
0
    if (aEntries.empty())
1265
0
        return;
1266
1267
0
    bool bIsDataLayout;
1268
0
    OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1269
1270
0
    ScDPSaveData aData( *pDPObj->GetSaveData() );
1271
0
    ScDPDimensionSaveData* pDimData = aData.GetDimensionData();     // created if not there
1272
1273
    // find original base
1274
0
    OUString aBaseDimName = aDimName;
1275
0
    const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName );
1276
0
    if ( pBaseGroupDim )
1277
0
    {
1278
        // any entry's SourceDimName is the original base
1279
0
        aBaseDimName = pBaseGroupDim->GetSourceDimName();
1280
0
    }
1281
1282
    // find existing group dimension
1283
    // (using the selected dim, can be intermediate group dim)
1284
0
    ScDPSaveGroupDimension* pGroupDimension = pDimData->GetGroupDimAccForBase( aDimName );
1285
1286
    // remove the selected items from their groups
1287
    // (empty groups are removed, too)
1288
0
    if ( pGroupDimension )
1289
0
    {
1290
0
        for (const OUString& aEntryName : aEntries)
1291
0
        {
1292
0
            if ( pBaseGroupDim )
1293
0
            {
1294
                // for each selected (intermediate) group, remove all its items
1295
                // (same logic as for adding, below)
1296
0
                const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
1297
0
                if ( pBaseGroup )
1298
0
                    pBaseGroup->RemoveElementsFromGroups( *pGroupDimension );   // remove all elements
1299
0
                else
1300
0
                    pGroupDimension->RemoveFromGroups( aEntryName );
1301
0
            }
1302
0
            else
1303
0
                pGroupDimension->RemoveFromGroups( aEntryName );
1304
0
        }
1305
0
    }
1306
1307
0
    std::unique_ptr<ScDPSaveGroupDimension> pNewGroupDim;
1308
0
    if ( !pGroupDimension )
1309
0
    {
1310
        // create a new group dimension
1311
0
        OUString aGroupDimName =
1312
0
            pDimData->CreateGroupDimName(aBaseDimName, *pDPObj, false, nullptr);
1313
0
        pNewGroupDim.reset(new ScDPSaveGroupDimension( aBaseDimName, aGroupDimName ));
1314
1315
0
        pGroupDimension = pNewGroupDim.get();     // make changes to the new dim if none existed
1316
1317
0
        if ( pBaseGroupDim )
1318
0
        {
1319
            // If it's a higher-order group dimension, pre-allocate groups for all
1320
            // non-selected original groups, so the individual base members aren't
1321
            // used for automatic groups (this would make the original groups hard
1322
            // to find).
1323
            //! Also do this when removing groups?
1324
            //! Handle this case dynamically with automatic groups?
1325
1326
0
            tools::Long nGroupCount = pBaseGroupDim->GetGroupCount();
1327
0
            for ( tools::Long nGroup = 0; nGroup < nGroupCount; nGroup++ )
1328
0
            {
1329
0
                const ScDPSaveGroupItem& rBaseGroup = pBaseGroupDim->GetGroupByIndex( nGroup );
1330
1331
0
                if (!aEntries.count(rBaseGroup.GetGroupName()))
1332
0
                {
1333
                    // add an additional group for each item that is not in the selection
1334
0
                    ScDPSaveGroupItem aGroup( rBaseGroup.GetGroupName() );
1335
0
                    aGroup.AddElementsFromGroup( rBaseGroup );
1336
0
                    pGroupDimension->AddGroupItem( aGroup );
1337
0
                }
1338
0
            }
1339
0
        }
1340
0
    }
1341
0
    OUString aGroupDimName = pGroupDimension->GetGroupDimName();
1342
1343
0
    ScDPSaveGroupItem aGroup(pGroupDimension->CreateGroupName(ScResId(STR_PIVOT_GROUP)));
1344
0
    for (const OUString& aEntryName : aEntries)
1345
0
    {
1346
0
        if ( pBaseGroupDim )
1347
0
        {
1348
            // for each selected (intermediate) group, add all its items
1349
0
            const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
1350
0
            if ( pBaseGroup )
1351
0
                aGroup.AddElementsFromGroup( *pBaseGroup );
1352
0
            else
1353
0
                aGroup.AddElement( aEntryName );    // no group found -> automatic group, add the item itself
1354
0
        }
1355
0
        else
1356
0
            aGroup.AddElement( aEntryName );        // no group dimension, add all items directly
1357
0
    }
1358
1359
0
    pGroupDimension->AddGroupItem( aGroup );
1360
1361
0
    if ( pNewGroupDim )
1362
0
    {
1363
0
        pDimData->AddGroupDimension( *pNewGroupDim );
1364
0
        pNewGroupDim.reset();        // AddGroupDimension copies the object
1365
        // don't access pGroupDimension after here
1366
0
    }
1367
0
    pGroupDimension = nullptr;
1368
1369
    // set orientation
1370
0
    ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
1371
0
    if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
1372
0
    {
1373
0
        ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aDimName );
1374
0
        pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
1375
0
        aData.SetPosition( pSaveDimension, 0 ); //! before (immediate) base
1376
0
    }
1377
1378
    // apply changes
1379
0
    ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1380
0
    pDPObj->SetSaveData( aData );
1381
0
    aFunc.RefreshPivotTableGroups(pDPObj);
1382
1383
    // unmark cell selection
1384
0
    Unmark();
1385
0
}
1386
1387
void ScDBFunc::UngroupDataPilot()
1388
0
{
1389
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
1390
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
1391
0
    if (!pDPObj)
1392
0
        return;
1393
1394
0
    ScDPUniqueStringSet aEntries;
1395
0
    tools::Long nSelectDimension = -1;
1396
0
    GetSelectedMemberList( aEntries, nSelectDimension );
1397
1398
0
    if (aEntries.empty())
1399
0
        return;
1400
1401
0
    bool bIsDataLayout;
1402
0
    OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1403
1404
0
    ScDPSaveData aData( *pDPObj->GetSaveData() );
1405
0
    if (!aData.GetExistingDimensionData())
1406
        // There is nothing to ungroup.
1407
0
        return;
1408
1409
0
    ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1410
1411
0
    ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
1412
0
    const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
1413
0
    if ( ( pGroupDim && pGroupDim->GetDatePart() != 0 ) ||
1414
0
         ( pNumGroupDim && pNumGroupDim->GetDatePart() != 0 ) )
1415
0
    {
1416
        // Date grouping: need to remove all affected group dimensions.
1417
        // This is done using DateGroupDataPilot with nParts=0.
1418
1419
0
        DateGroupDataPilot( ScDPNumGroupInfo(), 0 );
1420
0
        return;
1421
0
    }
1422
1423
0
    if ( pGroupDim )
1424
0
    {
1425
0
        for (const auto& rEntry : aEntries)
1426
0
            pGroupDim->RemoveGroup(rEntry);
1427
1428
        // remove group dimension if empty
1429
0
        bool bEmptyDim = pGroupDim->IsEmpty();
1430
0
        if ( !bEmptyDim )
1431
0
        {
1432
            // If all remaining groups in the dimension aren't shown, remove
1433
            // the dimension too, as if it was completely empty.
1434
0
            ScDPUniqueStringSet aVisibleEntries;
1435
0
            pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
1436
0
            bEmptyDim = pGroupDim->HasOnlyHidden( aVisibleEntries );
1437
0
        }
1438
0
        if ( bEmptyDim )
1439
0
        {
1440
0
            pDimData->RemoveGroupDimension( aDimName );     // pGroupDim is deleted
1441
1442
            // also remove SaveData settings for the dimension that no longer exists
1443
0
            aData.RemoveDimensionByName( aDimName );
1444
0
        }
1445
0
    }
1446
0
    else if ( pNumGroupDim )
1447
0
    {
1448
        // remove the numerical grouping
1449
0
        pDimData->RemoveNumGroupDimension( aDimName );
1450
        // SaveData settings can remain unchanged - the same dimension still exists
1451
0
    }
1452
1453
    // apply changes
1454
0
    ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1455
0
    pDPObj->SetSaveData( aData );
1456
0
    aFunc.RefreshPivotTableGroups(pDPObj);
1457
1458
    // unmark cell selection
1459
0
    Unmark();
1460
0
}
1461
1462
static OUString lcl_replaceMemberNameInSubtotal(const OUString& rSubtotal, std::u16string_view rMemberName)
1463
0
{
1464
0
    sal_Int32 n = rSubtotal.getLength();
1465
0
    const sal_Unicode* p = rSubtotal.getStr();
1466
0
    OUStringBuffer aBuf, aWordBuf;
1467
0
    for (sal_Int32 i = 0; i < n; ++i)
1468
0
    {
1469
0
        sal_Unicode c = p[i];
1470
0
        if (c == ' ')
1471
0
        {
1472
0
            OUString aWord = aWordBuf.makeStringAndClear();
1473
0
            if (aWord == rMemberName)
1474
0
                aBuf.append('?');
1475
0
            else
1476
0
                aBuf.append(aWord);
1477
0
            aBuf.append(c);
1478
0
        }
1479
0
        else if (c == '\\')
1480
0
        {
1481
            // Escape a backslash character.
1482
0
            aWordBuf.append(OUStringChar(c) + OUStringChar(c));
1483
0
        }
1484
0
        else if (c == '?')
1485
0
        {
1486
            // A literal '?' must be escaped with a backslash ('\');
1487
0
            aWordBuf.append("\\" + OUStringChar(c));
1488
0
        }
1489
0
        else
1490
0
            aWordBuf.append(c);
1491
0
    }
1492
1493
0
    if (!aWordBuf.isEmpty())
1494
0
    {
1495
0
        OUString aWord = aWordBuf.makeStringAndClear();
1496
0
        if (aWord == rMemberName)
1497
0
            aBuf.append('?');
1498
0
        else
1499
0
            aBuf.append(aWord);
1500
0
    }
1501
1502
0
    return aBuf.makeStringAndClear();
1503
0
}
1504
1505
void ScDBFunc::DataPilotInput( const ScAddress& rPos, const OUString& rString )
1506
0
{
1507
0
    using namespace ::com::sun::star::sheet;
1508
1509
0
    ScDocument& rDoc = GetViewData().GetDocument();
1510
0
    ScDPObject* pDPObj = rDoc.GetDPAtCursor( rPos.Col(), rPos.Row(), rPos.Tab() );
1511
0
    if (!pDPObj)
1512
0
        return;
1513
1514
0
    OUString aOldText = rDoc.GetString(rPos.Col(), rPos.Row(), rPos.Tab());
1515
1516
0
    if ( aOldText == rString )
1517
0
    {
1518
        // nothing to do: silently exit
1519
0
        return;
1520
0
    }
1521
1522
0
    TranslateId pErrorId;
1523
1524
0
    pDPObj->BuildAllDimensionMembers();
1525
0
    ScDPSaveData aData( *pDPObj->GetSaveData() );
1526
0
    bool bChange = false;
1527
0
    bool bNeedReloadGroups = false;
1528
1529
0
    DataPilotFieldOrientation nOrient = DataPilotFieldOrientation_HIDDEN;
1530
0
    tools::Long nField = pDPObj->GetHeaderDim( rPos, nOrient );
1531
0
    if ( nField >= 0 )
1532
0
    {
1533
        // changing a field title
1534
0
        if ( aData.GetExistingDimensionData() )
1535
0
        {
1536
            // only group dimensions can be renamed
1537
1538
0
            ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1539
0
            ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aOldText );
1540
0
            if ( pGroupDim )
1541
0
            {
1542
                // valid name: not empty, no existing dimension (group or other)
1543
0
                if (!rString.isEmpty() && !pDPObj->IsDimNameInUse(rString))
1544
0
                {
1545
0
                    pGroupDim->Rename( rString );
1546
1547
                    // also rename in SaveData to preserve the field settings
1548
0
                    ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aOldText );
1549
0
                    pSaveDim->SetName( rString );
1550
1551
0
                    bChange = true;
1552
0
                }
1553
0
                else
1554
0
                    pErrorId = STR_INVALIDNAME;
1555
0
            }
1556
0
        }
1557
0
        else if (nOrient == DataPilotFieldOrientation_COLUMN || nOrient == DataPilotFieldOrientation_ROW)
1558
0
        {
1559
0
            bool bDataLayout = false;
1560
0
            OUString aDimName = pDPObj->GetDimName(nField, bDataLayout);
1561
0
            ScDPSaveDimension* pDim = bDataLayout ? aData.GetDataLayoutDimension() : aData.GetDimensionByName(aDimName);
1562
0
            if (pDim)
1563
0
            {
1564
0
                if (!rString.isEmpty())
1565
0
                {
1566
0
                    if (rString.equalsIgnoreAsciiCase(aDimName))
1567
0
                    {
1568
0
                        pDim->RemoveLayoutName();
1569
0
                        bChange = true;
1570
0
                    }
1571
0
                    else if (!pDPObj->IsDimNameInUse(rString))
1572
0
                    {
1573
0
                        pDim->SetLayoutName(rString);
1574
0
                        bChange = true;
1575
0
                    }
1576
0
                    else
1577
0
                        pErrorId = STR_INVALIDNAME;
1578
0
                }
1579
0
                else
1580
0
                    pErrorId = STR_INVALIDNAME;
1581
0
            }
1582
0
        }
1583
0
    }
1584
0
    else if (pDPObj->IsDataDescriptionCell(rPos))
1585
0
    {
1586
        // There is only one data dimension.
1587
0
        ScDPSaveDimension* pDim = aData.GetFirstDimension(sheet::DataPilotFieldOrientation_DATA);
1588
0
        if (pDim)
1589
0
        {
1590
0
            if (!rString.isEmpty())
1591
0
            {
1592
0
                if (pDim->GetName().equalsIgnoreAsciiCase(rString))
1593
0
                {
1594
0
                    pDim->RemoveLayoutName();
1595
0
                    bChange = true;
1596
0
                }
1597
0
                else if (!pDPObj->IsDimNameInUse(rString))
1598
0
                {
1599
0
                    pDim->SetLayoutName(rString);
1600
0
                    bChange = true;
1601
0
                }
1602
0
                else
1603
0
                    pErrorId = STR_INVALIDNAME;
1604
0
            }
1605
0
            else
1606
0
                pErrorId = STR_INVALIDNAME;
1607
0
        }
1608
0
    }
1609
0
    else
1610
0
    {
1611
        // This is not a field header.
1612
0
        sheet::DataPilotTableHeaderData aPosData;
1613
0
        pDPObj->GetHeaderPositionData(rPos, aPosData);
1614
1615
0
        if ((aPosData.Flags & MemberResultFlags::HASMEMBER) && !aOldText.isEmpty())
1616
0
        {
1617
0
            if ( aData.GetExistingDimensionData() && !(aPosData.Flags & MemberResultFlags::SUBTOTAL))
1618
0
            {
1619
0
                bool bIsDataLayout;
1620
0
                OUString aDimName = pDPObj->GetDimName( aPosData.Dimension, bIsDataLayout );
1621
1622
0
                ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1623
0
                ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
1624
0
                if ( pGroupDim )
1625
0
                {
1626
                    // valid name: not empty, no existing group in this dimension
1627
                    //! ignore case?
1628
0
                    if (!rString.isEmpty() && !pGroupDim->GetNamedGroup(rString))
1629
0
                    {
1630
0
                        ScDPSaveGroupItem* pGroup = pGroupDim->GetNamedGroupAcc( aOldText );
1631
0
                        if ( pGroup )
1632
0
                            pGroup->Rename( rString );     // rename the existing group
1633
0
                        else
1634
0
                        {
1635
                            // create a new group to replace the automatic group
1636
0
                            ScDPSaveGroupItem aGroup( rString );
1637
0
                            aGroup.AddElement( aOldText );
1638
0
                            pGroupDim->AddGroupItem( aGroup );
1639
0
                        }
1640
1641
                        // in both cases also adjust savedata, to preserve member settings (show details)
1642
0
                        ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aDimName );
1643
0
                        ScDPSaveMember* pSaveMember = pSaveDim->GetExistingMemberByName( aOldText );
1644
0
                        if ( pSaveMember )
1645
0
                            pSaveMember->SetName( rString );
1646
1647
0
                        bChange = true;
1648
0
                        bNeedReloadGroups = true;
1649
0
                    }
1650
0
                    else
1651
0
                        pErrorId = STR_INVALIDNAME;
1652
0
                }
1653
0
            }
1654
0
            else if (aPosData.Flags & MemberResultFlags::GRANDTOTAL)
1655
0
            {
1656
0
                aData.SetGrandTotalName(rString);
1657
0
                bChange = true;
1658
0
            }
1659
0
            else if (aPosData.Dimension >= 0 && !aPosData.MemberName.isEmpty())
1660
0
            {
1661
0
                bool bDataLayout = false;
1662
0
                OUString aDimName = pDPObj->GetDimName(static_cast<tools::Long>(aPosData.Dimension), bDataLayout);
1663
0
                if (bDataLayout)
1664
0
                {
1665
                    // data dimension
1666
0
                    do
1667
0
                    {
1668
0
                        if (aPosData.Flags & MemberResultFlags::SUBTOTAL)
1669
0
                            break;
1670
1671
0
                        ScDPSaveDimension* pDim = aData.GetDimensionByName(aPosData.MemberName);
1672
0
                        if (!pDim)
1673
0
                            break;
1674
1675
0
                        if (rString.isEmpty())
1676
0
                        {
1677
0
                            pErrorId = STR_INVALIDNAME;
1678
0
                            break;
1679
0
                        }
1680
1681
0
                        if (aPosData.MemberName.equalsIgnoreAsciiCase(rString))
1682
0
                        {
1683
0
                            pDim->RemoveLayoutName();
1684
0
                            bChange = true;
1685
0
                        }
1686
0
                        else if (!pDPObj->IsDimNameInUse(rString))
1687
0
                        {
1688
0
                            pDim->SetLayoutName(rString);
1689
0
                            bChange = true;
1690
0
                        }
1691
0
                        else
1692
0
                            pErrorId = STR_INVALIDNAME;
1693
0
                    }
1694
0
                    while (false);
1695
0
                }
1696
0
                else
1697
0
                {
1698
                    // field member
1699
0
                    do
1700
0
                    {
1701
0
                        ScDPSaveDimension* pDim = aData.GetDimensionByName(aDimName);
1702
0
                        if (!pDim)
1703
0
                            break;
1704
1705
0
                        ScDPSaveMember* pMem = pDim->GetExistingMemberByName(aPosData.MemberName);
1706
0
                        if (!pMem)
1707
0
                            break;
1708
1709
0
                        if (aPosData.Flags & MemberResultFlags::SUBTOTAL)
1710
0
                        {
1711
                            // Change subtotal only when the table has one data dimension.
1712
0
                            if (aData.GetDataDimensionCount() > 1)
1713
0
                                break;
1714
1715
                            // display name for subtotal is allowed only if the subtotal type is 'Automatic'.
1716
0
                            if (pDim->GetSubTotalsCount() != 1)
1717
0
                                break;
1718
1719
0
                            if (pDim->GetSubTotalFunc(0) != ScGeneralFunction::AUTO)
1720
0
                                break;
1721
1722
0
                            const std::optional<OUString> & pLayoutName = pMem->GetLayoutName();
1723
0
                            OUString aMemberName;
1724
0
                            if (pLayoutName)
1725
0
                                aMemberName = *pLayoutName;
1726
0
                            else
1727
0
                                aMemberName = aPosData.MemberName;
1728
1729
0
                            OUString aNew = lcl_replaceMemberNameInSubtotal(rString, aMemberName);
1730
0
                            pDim->SetSubtotalName(aNew);
1731
0
                            bChange = true;
1732
0
                        }
1733
0
                        else
1734
0
                        {
1735
                            // Check to make sure the member name isn't
1736
                            // already used.
1737
0
                            if (!rString.isEmpty())
1738
0
                            {
1739
0
                                if (rString.equalsIgnoreAsciiCase(pMem->GetName()))
1740
0
                                {
1741
0
                                    pMem->RemoveLayoutName();
1742
0
                                    bChange = true;
1743
0
                                }
1744
0
                                else if (!pDim->IsMemberNameInUse(rString))
1745
0
                                {
1746
0
                                    pMem->SetLayoutName(rString);
1747
0
                                    bChange = true;
1748
0
                                }
1749
0
                                else
1750
0
                                    pErrorId = STR_INVALIDNAME;
1751
0
                            }
1752
0
                            else
1753
0
                                pErrorId = STR_INVALIDNAME;
1754
0
                        }
1755
0
                    }
1756
0
                    while (false);
1757
0
                }
1758
0
            }
1759
0
        }
1760
0
    }
1761
1762
0
    if ( bChange )
1763
0
    {
1764
        // apply changes
1765
0
        ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1766
0
        pDPObj->SetSaveData( aData );
1767
0
        if (bNeedReloadGroups)
1768
0
        {
1769
0
            ScDPCollection* pDPs = rDoc.GetDPCollection();
1770
0
            if (pDPs)
1771
0
            {
1772
0
                o3tl::sorted_vector<ScDPObject*> aRefs;
1773
                // tdf#111305: Reload groups in cache after modifications.
1774
0
                pDPs->ReloadGroupsInCache(pDPObj, aRefs);
1775
0
            } // pDPs
1776
0
        } // bNeedReloadGroups
1777
0
        aFunc.UpdatePivotTable(*pDPObj, true, false);
1778
0
    }
1779
0
    else
1780
0
    {
1781
0
        if (!pErrorId)
1782
0
            pErrorId = STR_ERR_DATAPILOT_INPUT;
1783
0
        ErrorMessage(pErrorId);
1784
0
    }
1785
0
}
1786
1787
static void lcl_MoveToEnd( ScDPSaveDimension& rDim, const OUString& rItemName )
1788
0
{
1789
0
    std::unique_ptr<ScDPSaveMember> pNewMember;
1790
0
    const ScDPSaveMember* pOldMember = rDim.GetExistingMemberByName( rItemName );
1791
0
    if ( pOldMember )
1792
0
        pNewMember.reset(new ScDPSaveMember( *pOldMember ));
1793
0
    else
1794
0
        pNewMember.reset(new ScDPSaveMember( rItemName ));
1795
0
    rDim.AddMember( std::move(pNewMember) );
1796
    // AddMember takes ownership of the new pointer,
1797
    // puts it to the end of the list even if it was in the list before.
1798
0
}
1799
1800
namespace {
1801
1802
struct ScOUStringCollate
1803
{
1804
    CollatorWrapper* mpCollator;
1805
1806
0
    explicit ScOUStringCollate(CollatorWrapper* pColl) : mpCollator(pColl) {}
1807
1808
    bool operator()(const OUString& rStr1, const OUString& rStr2) const
1809
0
    {
1810
0
        return ( mpCollator->compareString(rStr1, rStr2) < 0 );
1811
0
    }
1812
};
1813
1814
}
1815
1816
void ScDBFunc::DataPilotSort(ScDPObject* pDPObj, tools::Long nDimIndex, bool bAscending, const sal_uInt16* pUserListId)
1817
0
{
1818
0
    if (!pDPObj)
1819
0
        return;
1820
1821
    // We need to run this to get all members later.
1822
0
    if ( pUserListId )
1823
0
        pDPObj->BuildAllDimensionMembers();
1824
1825
0
    if (nDimIndex < 0)
1826
        // Invalid dimension index.  Bail out.
1827
0
        return;
1828
1829
0
    ScDPSaveData* pSaveData = pDPObj->GetSaveData();
1830
0
    if (!pSaveData)
1831
0
        return;
1832
1833
0
    ScDPSaveData aNewSaveData(*pSaveData);
1834
0
    bool bDataLayout;
1835
0
    OUString aDimName = pDPObj->GetDimName(nDimIndex, bDataLayout);
1836
0
    ScDPSaveDimension* pSaveDim = aNewSaveData.GetDimensionByName(aDimName);
1837
0
    if (!pSaveDim)
1838
0
        return;
1839
1840
    // manual evaluation of sort order is only needed if a user list id is given
1841
0
    if ( pUserListId )
1842
0
    {
1843
0
        typedef ScDPSaveDimension::MemberList MemList;
1844
0
        const MemList& rDimMembers = pSaveDim->GetMembers();
1845
0
        std::vector<OUString> aMembers;
1846
0
        std::unordered_set<OUString> aMemberSet;
1847
0
        size_t nMemberCount = 0;
1848
0
        for (ScDPSaveMember* pMem : rDimMembers)
1849
0
        {
1850
0
            aMembers.push_back(pMem->GetName());
1851
0
            aMemberSet.insert(pMem->GetName());
1852
0
            ++nMemberCount;
1853
0
        }
1854
1855
        // Sort the member list in ascending order.
1856
0
        ScOUStringCollate aCollate( &ScGlobal::GetCollator() );
1857
0
        std::stable_sort(aMembers.begin(), aMembers.end(), aCollate);
1858
1859
        // Collect and rank those custom sort strings that also exist in the member name list.
1860
1861
0
        typedef std::unordered_map<OUString, sal_uInt16> UserSortMap;
1862
0
        UserSortMap aSubStrs;
1863
0
        sal_uInt16 nSubCount = 0;
1864
0
        ScUserList& rUserList = ScGlobal::GetUserList();
1865
0
        size_t nUserListSize = rUserList.size();
1866
0
        if (!nUserListSize || *pUserListId >= static_cast<sal_uInt16>(nUserListSize))
1867
0
            return;
1868
1869
0
        const ScUserListData& rData = rUserList[*pUserListId];
1870
0
        sal_uInt16 n = rData.GetSubCount();
1871
0
        for (sal_uInt16 i = 0; i < n; ++i)
1872
0
        {
1873
0
            OUString aSub = rData.GetSubStr(i);
1874
0
            if (!aMemberSet.count(aSub))
1875
                // This string doesn't exist in the member name set.  Don't add this.
1876
0
                continue;
1877
1878
0
            aSubStrs.emplace(aSub, nSubCount++);
1879
0
        }
1880
1881
        // Rank all members.
1882
1883
0
        std::vector<OUString> aRankedNames(nMemberCount);
1884
0
        sal_uInt16 nCurStrId = 0;
1885
0
        for (auto const& aMemberName : aMembers)
1886
0
        {
1887
0
            sal_uInt16 nRank = 0;
1888
0
            UserSortMap::const_iterator itrSub = aSubStrs.find(aMemberName);
1889
0
            if (itrSub == aSubStrs.end())
1890
0
                nRank = nSubCount + nCurStrId++;
1891
0
            else
1892
0
                nRank = itrSub->second;
1893
1894
0
            if (!bAscending)
1895
0
                nRank = static_cast< sal_uInt16 >( nMemberCount - nRank - 1 );
1896
1897
0
            aRankedNames[nRank] = aMemberName;
1898
0
        }
1899
1900
        // Re-order ScDPSaveMember instances with the new ranks.
1901
0
        for (auto const& aRankedName : aRankedNames)
1902
0
        {
1903
0
            const ScDPSaveMember* pOldMem = pSaveDim->GetExistingMemberByName(aRankedName);
1904
0
            if (!pOldMem)
1905
                // All members are supposed to be present.
1906
0
                continue;
1907
1908
0
            pSaveDim->AddMember(std::unique_ptr<ScDPSaveMember>(new ScDPSaveMember(*pOldMem)));
1909
0
        }
1910
1911
        // Set the sorting mode to manual for now.  We may introduce a new sorting
1912
        // mode later on.
1913
1914
0
        sheet::DataPilotFieldSortInfo aSortInfo;
1915
0
        aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
1916
0
        pSaveDim->SetSortInfo(&aSortInfo);
1917
0
    }
1918
0
    else
1919
0
    {
1920
        // without user list id, just apply sorting mode
1921
1922
0
        sheet::DataPilotFieldSortInfo aSortInfo;
1923
0
        aSortInfo.Mode = sheet::DataPilotFieldSortMode::NAME;
1924
0
        aSortInfo.IsAscending = bAscending;
1925
0
        pSaveDim->SetSortInfo(&aSortInfo);
1926
0
    }
1927
1928
    // Update the datapilot with the newly sorted field members.
1929
1930
0
    std::unique_ptr<ScDPObject> pNewObj(new ScDPObject(*pDPObj));
1931
0
    pNewObj->SetSaveData(aNewSaveData);
1932
0
    ScDBDocFunc aFunc(*GetViewData().GetDocShell());
1933
1934
0
    aFunc.DataPilotUpdate(pDPObj, pNewObj.get(), true, false);
1935
0
}
1936
1937
bool ScDBFunc::DataPilotMove( const ScRange& rSource, const ScAddress& rDest )
1938
0
{
1939
0
    bool bRet = false;
1940
0
    ScDocument& rDoc = GetViewData().GetDocument();
1941
0
    ScDPObject* pDPObj = rDoc.GetDPAtCursor( rSource.aStart.Col(), rSource.aStart.Row(), rSource.aStart.Tab() );
1942
0
    if ( pDPObj && pDPObj == rDoc.GetDPAtCursor( rDest.Col(), rDest.Row(), rDest.Tab() ) )
1943
0
    {
1944
0
        sheet::DataPilotTableHeaderData aDestData;
1945
0
        pDPObj->GetHeaderPositionData( rDest, aDestData );
1946
0
        bool bValid = ( aDestData.Dimension >= 0 );        // dropping onto a field
1947
1948
        // look through the source range
1949
0
        std::unordered_set< OUString > aMembersSet;   // for lookup
1950
0
        std::vector< OUString > aMembersVector;  // members in original order, for inserting
1951
0
        aMembersVector.reserve( std::max( static_cast<SCSIZE>( rSource.aEnd.Col() - rSource.aStart.Col() + 1 ),
1952
0
                                          static_cast<SCSIZE>( rSource.aEnd.Row() - rSource.aStart.Row() + 1 ) ) );
1953
0
        for (SCROW nRow = rSource.aStart.Row(); bValid && nRow <= rSource.aEnd.Row(); ++nRow )
1954
0
            for (SCCOL nCol = rSource.aStart.Col(); bValid && nCol <= rSource.aEnd.Col(); ++nCol )
1955
0
            {
1956
0
                sheet::DataPilotTableHeaderData aSourceData;
1957
0
                pDPObj->GetHeaderPositionData( ScAddress( nCol, nRow, rSource.aStart.Tab() ), aSourceData );
1958
0
                if ( aSourceData.Dimension == aDestData.Dimension && !aSourceData.MemberName.isEmpty() )
1959
0
                {
1960
0
                    if ( aMembersSet.insert( aSourceData.MemberName ).second )
1961
0
                    {
1962
0
                        aMembersVector.push_back( aSourceData.MemberName );
1963
0
                    }
1964
                    // duplicates are ignored
1965
0
                }
1966
0
                else
1967
0
                    bValid = false;     // empty (subtotal) or different field
1968
0
            }
1969
1970
0
        if ( bValid )
1971
0
        {
1972
0
            bool bIsDataLayout;
1973
0
            OUString aDimName = pDPObj->GetDimName( aDestData.Dimension, bIsDataLayout );
1974
0
            if ( !bIsDataLayout )
1975
0
            {
1976
0
                ScDPSaveData aData( *pDPObj->GetSaveData() );
1977
0
                ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
1978
1979
                // get all member names in source order
1980
0
                uno::Sequence<OUString> aMemberNames;
1981
0
                pDPObj->GetMemberNames( aDestData.Dimension, aMemberNames );
1982
1983
0
                bool bInserted = false;
1984
1985
0
                for (const OUString& aMemberStr : aMemberNames)
1986
0
                {
1987
0
                    if ( !bInserted && aMemberStr == aDestData.MemberName )
1988
0
                    {
1989
                        // insert dragged items before this item
1990
0
                        for ( const auto& rMember : aMembersVector )
1991
0
                            lcl_MoveToEnd( *pDim, rMember );
1992
0
                        bInserted = true;
1993
0
                    }
1994
1995
0
                    if ( aMembersSet.find( aMemberStr ) == aMembersSet.end() )  // skip dragged items
1996
0
                        lcl_MoveToEnd( *pDim, aMemberStr );
1997
0
                }
1998
                // insert dragged item at end if dest wasn't found (for example, empty)
1999
0
                if ( !bInserted )
2000
0
                    for ( const auto& rMember : aMembersVector )
2001
0
                        lcl_MoveToEnd( *pDim, rMember );
2002
2003
                // Items that were in SaveData, but not in the source, end up at the start of the list.
2004
2005
                // set flag for manual sorting
2006
0
                sheet::DataPilotFieldSortInfo aSortInfo;
2007
0
                aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
2008
0
                pDim->SetSortInfo( &aSortInfo );
2009
2010
                // apply changes
2011
0
                ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
2012
0
                std::unique_ptr<ScDPObject> pNewObj(new ScDPObject( *pDPObj ));
2013
0
                pNewObj->SetSaveData( aData );
2014
0
                aFunc.DataPilotUpdate( pDPObj, pNewObj.get(), true, false );      //! bApi for drag&drop?
2015
0
                pNewObj.reset();
2016
2017
0
                Unmark();       // entry was moved - no use in leaving the old cell selected
2018
2019
0
                bRet = true;
2020
0
            }
2021
0
        }
2022
0
    }
2023
2024
0
    return bRet;
2025
0
}
2026
2027
bool ScDBFunc::HasSelectionForDrillDown( css::sheet::DataPilotFieldOrientation& rOrientation )
2028
0
{
2029
0
    bool bRet = false;
2030
2031
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
2032
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
2033
0
    if ( pDPObj )
2034
0
    {
2035
0
        ScDPUniqueStringSet aEntries;
2036
0
        tools::Long nSelectDimension = -1;
2037
0
        GetSelectedMemberList( aEntries, nSelectDimension );
2038
2039
0
        if (!aEntries.empty())
2040
0
        {
2041
0
            bool bIsDataLayout;
2042
0
            OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
2043
0
            if ( !bIsDataLayout )
2044
0
            {
2045
0
                ScDPSaveData* pSaveData = pDPObj->GetSaveData();
2046
0
                ScDPSaveDimension* pDim = pSaveData->GetExistingDimensionByName( aDimName );
2047
0
                if ( pDim )
2048
0
                {
2049
0
                    css::sheet::DataPilotFieldOrientation nDimOrient = pDim->GetOrientation();
2050
0
                    ScDPSaveDimension* pInner = pSaveData->GetInnermostDimension( nDimOrient );
2051
0
                    if ( pDim == pInner )
2052
0
                    {
2053
0
                        rOrientation = nDimOrient;
2054
0
                        bRet = true;
2055
0
                    }
2056
0
                }
2057
0
            }
2058
0
        }
2059
0
    }
2060
2061
0
    return bRet;
2062
0
}
2063
2064
void ScDBFunc::SetDataPilotDetails(bool bShow, const OUString* pNewDimensionName)
2065
0
{
2066
0
    ScDPObject* pDPObj = GetViewData().GetDocument().GetDPAtCursor( GetViewData().GetCurX(),
2067
0
                                        GetViewData().GetCurY(), GetViewData().CurrentTabForData() );
2068
0
    if ( !pDPObj )
2069
0
        return;
2070
2071
0
    ScDPUniqueStringSet aEntries;
2072
0
    tools::Long nSelectDimension = -1;
2073
0
    GetSelectedMemberList( aEntries, nSelectDimension );
2074
2075
0
    if (aEntries.empty())
2076
0
        return;
2077
2078
0
    bool bIsDataLayout;
2079
0
    OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
2080
0
    if ( bIsDataLayout )
2081
0
        return;
2082
2083
0
    ScDPSaveData aData( *pDPObj->GetSaveData() );
2084
0
    ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
2085
2086
0
    if ( bShow && pNewDimensionName )
2087
0
    {
2088
        //  add the new dimension with the same orientation, at the end
2089
2090
0
        ScDPSaveDimension* pNewDim = aData.GetDimensionByName( *pNewDimensionName );
2091
0
        ScDPSaveDimension* pDuplicated = nullptr;
2092
0
        if ( pNewDim->GetOrientation() == sheet::DataPilotFieldOrientation_DATA )
2093
0
        {
2094
            // Need to duplicate the dimension, create column/row in addition to data:
2095
            // The duplicated dimension inherits the existing settings, pNewDim is modified below.
2096
0
            pDuplicated = aData.DuplicateDimension( *pNewDimensionName );
2097
0
        }
2098
2099
0
        css::sheet::DataPilotFieldOrientation nOrientation = pDim->GetOrientation();
2100
0
        pNewDim->SetOrientation( nOrientation );
2101
2102
0
        tools::Long nPosition = LONG_MAX;
2103
0
        aData.SetPosition( pNewDim, nPosition );
2104
2105
0
        ScDPSaveDimension* pDataLayout = aData.GetDataLayoutDimension();
2106
0
        if ( pDataLayout->GetOrientation() == nOrientation &&
2107
0
             aData.GetDataDimensionCount() <= 1 )
2108
0
        {
2109
            // If there is only one data dimension, the data layout dimension
2110
            // must still be the last one in its orientation.
2111
0
            aData.SetPosition( pDataLayout, nPosition );
2112
0
        }
2113
2114
0
        if ( pDuplicated )
2115
0
        {
2116
            // The duplicated (data) dimension needs to be behind the original dimension
2117
0
            aData.SetPosition( pDuplicated, nPosition );
2118
0
        }
2119
2120
        //  Hide details for all visible members (selected are changed below).
2121
        //! Use all members from source level instead (including non-visible)?
2122
2123
0
        ScDPUniqueStringSet aVisibleEntries;
2124
0
        pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
2125
2126
0
        for (const OUString& aVisName : aVisibleEntries)
2127
0
        {
2128
0
            ScDPSaveMember* pMember = pDim->GetMemberByName( aVisName );
2129
0
            pMember->SetShowDetails( false );
2130
0
        }
2131
0
    }
2132
2133
0
    for (const auto& rEntry : aEntries)
2134
0
    {
2135
0
        ScDPSaveMember* pMember = pDim->GetMemberByName(rEntry);
2136
0
        pMember->SetShowDetails( bShow );
2137
0
    }
2138
2139
    // apply changes
2140
0
    ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
2141
0
    std::unique_ptr<ScDPObject> pNewObj(new ScDPObject( *pDPObj ));
2142
0
    pNewObj->SetSaveData( aData );
2143
0
    aFunc.DataPilotUpdate( pDPObj, pNewObj.get(), true, false );
2144
0
    pNewObj.reset();
2145
2146
    // unmark cell selection
2147
0
    Unmark();
2148
0
}
2149
2150
void ScDBFunc::ShowDataPilotSourceData( ScDPObject& rDPObj, const Sequence<sheet::DataPilotFieldFilter>& rFilters )
2151
0
{
2152
0
    ScDocument& rDoc = GetViewData().GetDocument();
2153
0
    if (rDoc.GetDocumentShell()->IsReadOnly())
2154
0
    {
2155
0
        ErrorMessage(STR_READONLYERR);
2156
0
        return;
2157
0
    }
2158
2159
0
    Reference<sheet::XDimensionsSupplier> xDimSupplier = rDPObj.GetSource();
2160
0
    Reference<container::XNameAccess> xDims = xDimSupplier->getDimensions();
2161
0
    Reference<sheet::XDrillDownDataSupplier> xDDSupplier(xDimSupplier, UNO_QUERY);
2162
0
    if (!xDDSupplier.is())
2163
0
        return;
2164
2165
0
    Sequence< Sequence<Any> > aTabData = xDDSupplier->getDrillDownData(rFilters);
2166
0
    sal_Int32 nRowSize = aTabData.getLength();
2167
0
    if (nRowSize <= 1)
2168
        // There is no data to show.  Bail out.
2169
0
        return;
2170
2171
0
    SCCOL nColSize = aTabData[0].getLength();
2172
2173
0
    SCTAB nNewTab = GetViewData().CurrentTabForData();
2174
2175
0
    ScDocumentUniquePtr pInsDoc(new ScDocument(SCDOCMODE_CLIP));
2176
0
    pInsDoc->ResetClip( &rDoc, nNewTab );
2177
0
    for (SCROW nRow = 0; nRow < nRowSize; ++nRow)
2178
0
    {
2179
0
        for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
2180
0
        {
2181
0
            const Any& rAny = aTabData[nRow][nCol];
2182
0
            OUString aStr;
2183
0
            double fVal;
2184
0
            if (rAny >>= aStr)
2185
0
            {
2186
0
                pInsDoc->SetString(ScAddress(nCol,nRow,nNewTab), aStr);
2187
0
            }
2188
0
            else if (rAny >>= fVal)
2189
0
                pInsDoc->SetValue(nCol, nRow, nNewTab, fVal);
2190
0
        }
2191
0
    }
2192
2193
    // set number format (important for dates)
2194
0
    for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
2195
0
    {
2196
0
        OUString aStr;
2197
0
        if (!(aTabData[0][nCol] >>= aStr))
2198
0
            continue;
2199
2200
0
        Reference<XPropertySet> xPropSet(xDims->getByName(aStr), UNO_QUERY);
2201
0
        if (!xPropSet.is())
2202
0
            continue;
2203
2204
0
        Any any = xPropSet->getPropertyValue( SC_UNO_DP_NUMBERFO );
2205
0
        sal_Int32 nNumFmt = 0;
2206
0
        if (!(any >>= nNumFmt))
2207
0
            continue;
2208
2209
0
        ScPatternAttr aPattern(pInsDoc->getCellAttributeHelper());
2210
0
        aPattern.ItemSetPut(SfxUInt32Item(ATTR_VALUE_FORMAT, static_cast<sal_uInt32>(nNumFmt)));
2211
0
        pInsDoc->ApplyPatternAreaTab(nCol, 1, nCol, nRowSize-1, nNewTab, aPattern);
2212
0
    }
2213
2214
0
    SCCOL nEndCol = 0;
2215
0
    SCROW nEndRow = 0;
2216
0
    pInsDoc->GetCellArea( nNewTab, nEndCol, nEndRow );
2217
0
    pInsDoc->SetClipArea( ScRange( 0, 0, nNewTab, nEndCol, nEndRow, nNewTab ) );
2218
2219
0
    SfxUndoManager* pMgr = GetViewData().GetDocShell()->GetUndoManager();
2220
0
    OUString aUndo = ScResId( STR_UNDO_DOOUTLINE );
2221
0
    pMgr->EnterListAction( aUndo, aUndo, 0, GetViewData().GetViewShell()->GetViewShellId() );
2222
2223
0
    OUString aNewTabName;
2224
0
    rDoc.CreateValidTabName(aNewTabName);
2225
0
    if ( InsertTable(aNewTabName, nNewTab) )
2226
0
        PasteFromClip( InsertDeleteFlags::ALL, pInsDoc.get() );
2227
2228
0
    pMgr->LeaveListAction();
2229
0
}
2230
2231
// repeat data base operations (sorting, filtering, subtotals)
2232
2233
void ScDBFunc::RepeatDB( bool bRecord )
2234
0
{
2235
0
    SCCOL nCurX = GetViewData().GetCurX();
2236
0
    SCROW nCurY = GetViewData().GetCurY();
2237
0
    SCTAB nTab = GetViewData().CurrentTabForData();
2238
0
    ScDocument& rDoc = GetViewData().GetDocument();
2239
0
    ScDBData* pDBData = GetDBData();
2240
0
    if (bRecord && !rDoc.IsUndoEnabled())
2241
0
        bRecord = false;
2242
2243
0
    ScQueryParam aQueryParam;
2244
0
    pDBData->GetQueryParam( aQueryParam );
2245
0
    bool bQuery = aQueryParam.GetEntry(0).bDoQuery;
2246
2247
0
    ScSortParam aSortParam;
2248
0
    pDBData->GetSortParam( aSortParam );
2249
0
    bool bSort = aSortParam.maKeyState[0].bDoSort;
2250
2251
0
    ScSubTotalParam aSubTotalParam;
2252
0
    pDBData->GetSubTotalParam( aSubTotalParam );
2253
0
    bool bSubTotal = aSubTotalParam.aGroups[0].bActive && !aSubTotalParam.bRemoveOnly;
2254
2255
0
    if ( bQuery || bSort || bSubTotal )
2256
0
    {
2257
0
        bool bQuerySize = false;
2258
0
        ScRange aOldQuery;
2259
0
        ScRange aNewQuery;
2260
0
        if (bQuery && !aQueryParam.bInplace)
2261
0
        {
2262
0
            ScDBData* pDest = rDoc.GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
2263
0
                                                  aQueryParam.nDestTab, ScDBDataPortion::TOP_LEFT );
2264
0
            if (pDest && pDest->IsDoSize())
2265
0
            {
2266
0
                pDest->GetArea( aOldQuery );
2267
0
                bQuerySize = true;
2268
0
            }
2269
0
        }
2270
2271
0
        SCTAB nDummy;
2272
0
        SCCOL nStartCol;
2273
0
        SCROW nStartRow;
2274
0
        SCCOL nEndCol;
2275
0
        SCROW nEndRow;
2276
0
        pDBData->GetArea( nDummy, nStartCol, nStartRow, nEndCol, nEndRow );
2277
2278
        //! undo only needed data ?
2279
2280
0
        ScDocumentUniquePtr pUndoDoc;
2281
0
        std::unique_ptr<ScOutlineTable> pUndoTab;
2282
0
        std::unique_ptr<ScRangeName> pUndoRange;
2283
0
        std::unique_ptr<ScDBCollection> pUndoDB;
2284
2285
0
        if (bRecord)
2286
0
        {
2287
0
            SCTAB nTabCount = rDoc.GetTableCount();
2288
0
            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
2289
0
            ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
2290
0
            if (pTable)
2291
0
            {
2292
0
                pUndoTab.reset(new ScOutlineTable( *pTable ));
2293
2294
0
                SCCOLROW nOutStartCol;                          // row/column status
2295
0
                SCCOLROW nOutStartRow;
2296
0
                SCCOLROW nOutEndCol;
2297
0
                SCCOLROW nOutEndRow;
2298
0
                pTable->GetColArray().GetRange( nOutStartCol, nOutEndCol );
2299
0
                pTable->GetRowArray().GetRange( nOutStartRow, nOutEndRow );
2300
2301
0
                pUndoDoc->InitUndo( rDoc, nTab, nTab, true, true );
2302
0
                rDoc.CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), rDoc.MaxRow(), nTab, InsertDeleteFlags::NONE, false, *pUndoDoc );
2303
0
                rDoc.CopyToDocument( 0, nOutStartRow, nTab, rDoc.MaxCol(), nOutEndRow, nTab, InsertDeleteFlags::NONE, false, *pUndoDoc );
2304
0
            }
2305
0
            else
2306
0
                pUndoDoc->InitUndo( rDoc, nTab, nTab, false, true );
2307
2308
            // Record data range - including filter results
2309
0
            rDoc.CopyToDocument( 0,nStartRow,nTab, rDoc.MaxCol(),nEndRow,nTab, InsertDeleteFlags::ALL, false, *pUndoDoc );
2310
2311
            // all formulas for reference
2312
0
            rDoc.CopyToDocument( 0,0,0, rDoc.MaxCol(),rDoc.MaxRow(),nTabCount-1, InsertDeleteFlags::FORMULA, false, *pUndoDoc );
2313
2314
            // data base and other ranges
2315
0
            ScRangeName* pDocRange = rDoc.GetRangeName();
2316
0
            if (!pDocRange->empty())
2317
0
                pUndoRange.reset(new ScRangeName( *pDocRange ));
2318
0
            ScDBCollection* pDocDB = rDoc.GetDBCollection();
2319
0
            if (!pDocDB->empty())
2320
0
                pUndoDB.reset(new ScDBCollection( *pDocDB ));
2321
0
        }
2322
2323
0
        if (bSort && bSubTotal)
2324
0
        {
2325
            // sort without subtotals
2326
2327
0
            aSubTotalParam.bRemoveOnly = true;      // is reset below
2328
0
            DoSubTotals( aSubTotalParam, false );
2329
0
        }
2330
2331
0
        if (bSort)
2332
0
        {
2333
0
            pDBData->GetSortParam( aSortParam );            // range may have changed
2334
0
            Sort( aSortParam, false, false);
2335
0
        }
2336
0
        if (bQuery)
2337
0
        {
2338
0
            pDBData->GetQueryParam( aQueryParam );          // range may have changed
2339
0
            ScRange aAdvSource;
2340
0
            if (pDBData->GetAdvancedQuerySource(aAdvSource))
2341
0
            {
2342
0
                rDoc.CreateQueryParam(aAdvSource, aQueryParam);
2343
0
                Query( aQueryParam, &aAdvSource, false );
2344
0
            }
2345
0
            else
2346
0
                Query( aQueryParam, nullptr, false );
2347
2348
            // if not inplace the sheet may have changed
2349
0
            if ( !aQueryParam.bInplace && aQueryParam.nDestTab != nTab )
2350
0
                SetTabNo( nTab );
2351
0
        }
2352
0
        if (bSubTotal)
2353
0
        {
2354
0
            pDBData->GetSubTotalParam( aSubTotalParam );    // range may have changed
2355
0
            aSubTotalParam.bRemoveOnly = false;
2356
0
            DoSubTotals( aSubTotalParam, false );
2357
0
        }
2358
2359
0
        if (bRecord)
2360
0
        {
2361
0
            SCTAB nDummyTab;
2362
0
            SCCOL nDummyCol;
2363
0
            SCROW nDummyRow, nNewEndRow;
2364
0
            pDBData->GetArea( nDummyTab, nDummyCol,nDummyRow, nDummyCol,nNewEndRow );
2365
2366
0
            const ScRange* pOld = nullptr;
2367
0
            const ScRange* pNew = nullptr;
2368
0
            if (bQuerySize)
2369
0
            {
2370
0
                ScDBData* pDest = rDoc.GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
2371
0
                                                      aQueryParam.nDestTab, ScDBDataPortion::TOP_LEFT );
2372
0
                if (pDest)
2373
0
                {
2374
0
                    pDest->GetArea( aNewQuery );
2375
0
                    pOld = &aOldQuery;
2376
0
                    pNew = &aNewQuery;
2377
0
                }
2378
0
            }
2379
2380
0
            GetViewData().GetDocShell()->GetUndoManager()->AddUndoAction(
2381
0
                std::make_unique<ScUndoRepeatDB>( *GetViewData().GetDocShell(), nTab,
2382
0
                                        nStartCol, nStartRow, nEndCol, nEndRow,
2383
0
                                        nNewEndRow,
2384
0
                                        nCurX, nCurY,
2385
0
                                        std::move(pUndoDoc), std::move(pUndoTab),
2386
0
                                        std::move(pUndoRange), std::move(pUndoDB),
2387
0
                                        pOld, pNew ) );
2388
0
        }
2389
2390
0
        GetViewData().GetDocShell()->PostPaint(
2391
0
            ScRange(0, 0, nTab, rDoc.MaxCol(), rDoc.MaxRow(), nTab),
2392
0
            PaintPartFlags::Grid | PaintPartFlags::Left | PaintPartFlags::Top | PaintPartFlags::Size);
2393
0
    }
2394
0
    else        // "no not execute any operations"
2395
0
        ErrorMessage(STR_MSSG_REPEATDB_0);
2396
0
}
2397
2398
void ScDBFunc::OnLOKShowHideColRow(bool bColumns, SCCOLROW nStart)
2399
0
{
2400
0
    if (!comphelper::LibreOfficeKit::isActive())
2401
0
        return;
2402
2403
0
    SCTAB nCurrentTabIndex = GetViewData().CurrentTabForData();
2404
0
    SfxViewShell* pThisViewShell = GetViewData().GetViewShell();
2405
0
    SfxViewShell* pViewShell = SfxViewShell::GetFirst();
2406
0
    while (pViewShell)
2407
0
    {
2408
0
        ScTabViewShell* pTabViewShell = dynamic_cast<ScTabViewShell*>(pViewShell);
2409
0
        if (pTabViewShell && pTabViewShell->GetDocId() == pThisViewShell->GetDocId())
2410
0
        {
2411
0
            if (bColumns)
2412
0
            {
2413
0
                if (ScPositionHelper* pPosHelper = pTabViewShell->GetViewData().GetLOKWidthHelper(nCurrentTabIndex))
2414
0
                    pPosHelper->invalidateByIndex(nStart);
2415
0
            }
2416
0
            else
2417
0
            {
2418
0
                if (ScPositionHelper* pPosHelper = pTabViewShell->GetViewData().GetLOKHeightHelper(nCurrentTabIndex))
2419
0
                    pPosHelper->invalidateByIndex(nStart);
2420
0
            }
2421
2422
0
            if (pTabViewShell->getPart() == nCurrentTabIndex)
2423
0
            {
2424
0
                pTabViewShell->ShowCursor();
2425
0
                pTabViewShell->MarkDataChanged();
2426
0
            }
2427
0
        }
2428
0
        pViewShell = SfxViewShell::GetNext(*pViewShell);
2429
0
    }
2430
0
}
2431
2432
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */