1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <dpcache.hxx>
#include <dpdimsave.hxx>
#include <dpgroup.hxx>
#include <dpobject.hxx>
#include <dputil.hxx>
#include <document.hxx>
#include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
#include <svl/numformat.hxx>
#include <osl/diagnose.h>
#include <rtl/math.hxx>
#include <algorithm>
#include <globstr.hrc>
#include <scresid.hxx>
using namespace com::sun::star;
ScDPSaveGroupItem::ScDPSaveGroupItem( const OUString& rName ) :
aGroupName(rName) {}
ScDPSaveGroupItem::~ScDPSaveGroupItem() {}
void ScDPSaveGroupItem::AddElement( const OUString& rName )
{
aElements.push_back(rName);
}
void ScDPSaveGroupItem::AddElementsFromGroup( const ScDPSaveGroupItem& rGroup )
{
// add all elements of the other group (used for nested grouping)
aElements.insert( aElements.end(), rGroup.aElements.begin(), rGroup.aElements.end() );
}
bool ScDPSaveGroupItem::RemoveElement( const OUString& rName )
{
auto it = std::find(aElements.begin(), aElements.end(), rName); //TODO: ignore case
if (it != aElements.end())
{
aElements.erase(it);
return true;
}
return false; // not found
}
bool ScDPSaveGroupItem::IsEmpty() const
{
return aElements.empty();
}
size_t ScDPSaveGroupItem::GetElementCount() const
{
return aElements.size();
}
const OUString* ScDPSaveGroupItem::GetElementByIndex(size_t nIndex) const
{
return (nIndex < aElements.size()) ? &aElements[ nIndex ] : nullptr;
}
void ScDPSaveGroupItem::Rename( const OUString& rNewName )
{
aGroupName = rNewName;
}
void ScDPSaveGroupItem::RemoveElementsFromGroups( ScDPSaveGroupDimension& rDimension ) const
{
// remove this group's elements from their groups in rDimension
// (rDimension must be a different dimension from the one which contains this)
for ( const auto& rElement : aElements )
rDimension.RemoveFromGroups( rElement );
}
void ScDPSaveGroupItem::ConvertElementsToItems(SvNumberFormatter* pFormatter) const
{
maItems.reserve(aElements.size());
for (const auto& rElement : aElements)
{
sal_uInt32 nFormat = 0;
double fValue;
ScDPItemData aData;
if (pFormatter->IsNumberFormat(rElement, nFormat, fValue))
aData.SetValue(fValue);
else
aData.SetString(rElement);
maItems.push_back(aData);
}
}
bool ScDPSaveGroupItem::HasInGroup(const ScDPItemData& rItem) const
{
return std::find(maItems.begin(), maItems.end(), rItem) != maItems.end();
}
void ScDPSaveGroupItem::AddToData(ScDPGroupDimension& rDataDim) const
{
ScDPGroupItem aGroup(aGroupName);
for (const auto& rItem : maItems)
aGroup.AddElement(rItem);
rDataDim.AddItem(aGroup);
}
ScDPSaveGroupDimension::ScDPSaveGroupDimension( const OUString& rSource, const OUString& rName ) :
aSourceDim( rSource ),
aGroupDimName( rName ),
nDatePart( 0 )
{
}
ScDPSaveGroupDimension::ScDPSaveGroupDimension( const OUString& rSource, const OUString& rName, const ScDPNumGroupInfo& rDateInfo, sal_Int32 nPart ) :
aSourceDim( rSource ),
aGroupDimName( rName ),
aDateInfo( rDateInfo ),
nDatePart( nPart )
{
}
void ScDPSaveGroupDimension::SetDateInfo( const ScDPNumGroupInfo& rInfo, sal_Int32 nPart )
{
aDateInfo = rInfo;
nDatePart = nPart;
}
void ScDPSaveGroupDimension::AddGroupItem( const ScDPSaveGroupItem& rItem )
{
aGroups.push_back( rItem );
}
OUString ScDPSaveGroupDimension::CreateGroupName(std::u16string_view rPrefix)
{
// create a name for a new group, using "Group1", "Group2" etc. (translated prefix in rPrefix)
//TODO: look in all dimensions, to avoid clashes with automatic groups (=name of base element)?
//TODO: (only dimensions for the same base)
sal_Int32 nAdd = 1; // first try is "Group1"
const sal_Int32 nMaxAdd = nAdd + aGroups.size(); // limit the loop
while ( nAdd <= nMaxAdd )
{
OUString aGroupName = rPrefix + OUString::number( nAdd );
// look for existing groups
bool bExists = std::any_of(aGroups.begin(), aGroups.end(),
[&aGroupName](const ScDPSaveGroupItem& rGroup) {
return rGroup.GetGroupName() == aGroupName; //TODO: ignore case
});
if ( !bExists )
return aGroupName; // found a new name
++nAdd; // continue with higher number
}
OSL_FAIL("CreateGroupName: no valid name found");
return OUString();
}
const ScDPSaveGroupItem* ScDPSaveGroupDimension::GetNamedGroup( const OUString& rGroupName ) const
{
return const_cast< ScDPSaveGroupDimension* >( this )->GetNamedGroupAcc( rGroupName );
}
ScDPSaveGroupItem* ScDPSaveGroupDimension::GetNamedGroupAcc( const OUString& rGroupName )
{
auto aIter = std::find_if(aGroups.begin(), aGroups.end(),
[&rGroupName](const ScDPSaveGroupItem& rGroup) {
return rGroup.GetGroupName() == rGroupName; //TODO: ignore case
});
if (aIter != aGroups.end())
return &*aIter;
return nullptr; // none found
}
tools::Long ScDPSaveGroupDimension::GetGroupCount() const
{
return aGroups.size();
}
const ScDPSaveGroupItem& ScDPSaveGroupDimension::GetGroupByIndex( tools::Long nIndex ) const
{
return aGroups[nIndex];
}
void ScDPSaveGroupDimension::RemoveFromGroups( const OUString& rItemName )
{
// if the item is in any group, remove it from the group,
// also remove the group if it is empty afterwards
for ( ScDPSaveGroupItemVec::iterator aIter(aGroups.begin()); aIter != aGroups.end(); ++aIter )
if ( aIter->RemoveElement( rItemName ) )
{
if ( aIter->IsEmpty() ) // removed last item from the group?
aGroups.erase( aIter ); // then remove the group
return; // don't have to look further
}
}
void ScDPSaveGroupDimension::RemoveGroup(const OUString& rGroupName)
{
auto aIter = std::find_if(aGroups.begin(), aGroups.end(),
[&rGroupName](const ScDPSaveGroupItem& rGroup) {
return rGroup.GetGroupName() == rGroupName; //TODO: ignore case
});
if (aIter != aGroups.end())
aGroups.erase( aIter );
}
bool ScDPSaveGroupDimension::IsEmpty() const
{
return aGroups.empty();
}
bool ScDPSaveGroupDimension::HasOnlyHidden(const ScDPUniqueStringSet& rVisible)
{
// check if there are only groups that don't appear in the list of visible names
return std::none_of(aGroups.begin(), aGroups.end(),
[&rVisible](const ScDPSaveGroupItem& rGroup) { return rVisible.count(rGroup.GetGroupName()) > 0; });
}
void ScDPSaveGroupDimension::Rename( const OUString& rNewName )
{
aGroupDimName = rNewName;
}
bool ScDPSaveGroupDimension::IsInGroup(const ScDPItemData& rItem) const
{
return std::any_of(aGroups.begin(), aGroups.end(),
[&rItem](const ScDPSaveGroupItem& rGroup) { return rGroup.HasInGroup(rItem); });
}
namespace {
bool isInteger(double fValue)
{
return rtl::math::approxEqual(fValue, rtl::math::approxFloor(fValue));
}
void fillDateGroupDimension(
ScDPCache& rCache, ScDPNumGroupInfo& rDateInfo, tools::Long nSourceDim, tools::Long nGroupDim,
sal_Int32 nDatePart, const SvNumberFormatter* pFormatter)
{
// Auto min/max is only used for "Years" part, but the loop is always
// needed.
double fSourceMin = 0.0;
double fSourceMax = 0.0;
bool bFirst = true;
const ScDPCache::ScDPItemDataVec& rItems = rCache.GetDimMemberValues(nSourceDim);
for (const ScDPItemData& rItem : rItems)
{
if (rItem.GetType() != ScDPItemData::Value)
continue;
double fVal = rItem.GetValue();
if (bFirst)
{
fSourceMin = fSourceMax = fVal;
bFirst = false;
}
else
{
if (fVal < fSourceMin)
fSourceMin = fVal;
if ( fVal > fSourceMax )
fSourceMax = fVal;
}
}
// For the start/end values, use the same date rounding as in
// ScDPNumGroupDimension::GetNumEntries (but not for the list of
// available years).
if (rDateInfo.mbAutoStart)
rDateInfo.mfStart = rtl::math::approxFloor(fSourceMin);
if (rDateInfo.mbAutoEnd)
rDateInfo.mfEnd = rtl::math::approxFloor(fSourceMax) + 1;
//TODO: if not automatic, limit fSourceMin/fSourceMax for list of year values?
tools::Long nStart = 0, nEnd = 0; // end is inclusive
switch (nDatePart)
{
case sheet::DataPilotFieldGroupBy::YEARS:
nStart = ScDPUtil::getDatePartValue(
fSourceMin, nullptr, sheet::DataPilotFieldGroupBy::YEARS, pFormatter);
nEnd = ScDPUtil::getDatePartValue(fSourceMax, nullptr, sheet::DataPilotFieldGroupBy::YEARS, pFormatter);
break;
case sheet::DataPilotFieldGroupBy::QUARTERS: nStart = 1; nEnd = 4; break;
case sheet::DataPilotFieldGroupBy::MONTHS: nStart = 1; nEnd = 12; break;
case sheet::DataPilotFieldGroupBy::DAYS: nStart = 1; nEnd = 366; break;
case sheet::DataPilotFieldGroupBy::HOURS: nStart = 0; nEnd = 23; break;
case sheet::DataPilotFieldGroupBy::MINUTES: nStart = 0; nEnd = 59; break;
case sheet::DataPilotFieldGroupBy::SECONDS: nStart = 0; nEnd = 59; break;
default:
OSL_FAIL("invalid date part");
}
// Now, populate the group items in the cache.
rCache.ResetGroupItems(nGroupDim, rDateInfo, nDatePart);
for (tools::Long nValue = nStart; nValue <= nEnd; ++nValue)
rCache.SetGroupItem(nGroupDim, ScDPItemData(nDatePart, nValue));
// add first/last entry (min/max)
rCache.SetGroupItem(nGroupDim, ScDPItemData(nDatePart, ScDPItemData::DateFirst));
rCache.SetGroupItem(nGroupDim, ScDPItemData(nDatePart, ScDPItemData::DateLast));
}
}
void ScDPSaveGroupDimension::AddToData( ScDPGroupTableData& rData ) const
{
tools::Long nSourceIndex = rData.GetDimensionIndex( aSourceDim );
if ( nSourceIndex < 0 )
return;
ScDPGroupDimension aDim( nSourceIndex, aGroupDimName );
if ( nDatePart )
{
// date grouping
aDim.SetDateDimension();
}
else
{
// normal (manual) grouping
for (const auto& rGroup : aGroups)
rGroup.AddToData(aDim);
}
rData.AddGroupDimension( aDim );
}
void ScDPSaveGroupDimension::AddToCache(ScDPCache& rCache) const
{
tools::Long nSourceDim = rCache.GetDimensionIndex(aSourceDim);
if (nSourceDim < 0)
return;
tools::Long nDim = rCache.AppendGroupField();
SvNumberFormatter* pFormatter = rCache.GetDoc().GetFormatTable();
if (nDatePart)
{
fillDateGroupDimension(rCache, aDateInfo, nSourceDim, nDim, nDatePart, pFormatter);
return;
}
rCache.ResetGroupItems(nDim, aDateInfo, 0);
for (const ScDPSaveGroupItem& rGI : aGroups)
{
rGI.ConvertElementsToItems(pFormatter);
rCache.SetGroupItem(nDim, ScDPItemData(rGI.GetGroupName()));
}
const ScDPCache::ScDPItemDataVec& rItems = rCache.GetDimMemberValues(nSourceDim);
for (const ScDPItemData& rItem : rItems)
{
if (!IsInGroup(rItem))
// Not in any group. Add as its own group.
rCache.SetGroupItem(nDim, rItem);
}
}
ScDPSaveNumGroupDimension::ScDPSaveNumGroupDimension( const OUString& rName, const ScDPNumGroupInfo& rInfo ) :
aDimensionName( rName ),
aGroupInfo( rInfo ),
nDatePart( 0 )
{
}
ScDPSaveNumGroupDimension::ScDPSaveNumGroupDimension( const OUString& rName, const ScDPNumGroupInfo& rDateInfo, sal_Int32 nPart ) :
aDimensionName( rName ),
aDateInfo( rDateInfo ),
nDatePart( nPart )
{
}
void ScDPSaveNumGroupDimension::AddToData( ScDPGroupTableData& rData ) const
{
tools::Long nSource = rData.GetDimensionIndex( aDimensionName );
if ( nSource >= 0 )
{
ScDPNumGroupDimension aDim( aGroupInfo ); // aGroupInfo: value grouping
if ( nDatePart )
aDim.SetDateDimension();
rData.SetNumGroupDimension( nSource, aDim );
}
}
void ScDPSaveNumGroupDimension::AddToCache(ScDPCache& rCache) const
{
tools::Long nDim = rCache.GetDimensionIndex(aDimensionName);
if (nDim < 0)
return;
if (aDateInfo.mbEnable)
{
// Date grouping
SvNumberFormatter* pFormatter = rCache.GetDoc().GetFormatTable();
fillDateGroupDimension(rCache, aDateInfo, nDim, nDim, nDatePart, pFormatter);
}
else if (aGroupInfo.mbEnable)
{
// Number-range grouping
// Look through the source entries for non-integer numbers, minimum
// and maximum.
// non-integer GroupInfo values count, too
aGroupInfo.mbIntegerOnly =
(aGroupInfo.mbAutoStart || isInteger(aGroupInfo.mfStart)) &&
(aGroupInfo.mbAutoEnd || isInteger(aGroupInfo.mfEnd)) &&
isInteger(aGroupInfo.mfStep);
double fSourceMin = 0.0;
double fSourceMax = 0.0;
bool bFirst = true;
const ScDPCache::ScDPItemDataVec& rItems = rCache.GetDimMemberValues(nDim);
for (const ScDPItemData& rItem : rItems)
{
if (rItem.GetType() != ScDPItemData::Value)
continue;
double fValue = rItem.GetValue();
if (bFirst)
{
fSourceMin = fSourceMax = fValue;
bFirst = false;
continue;
}
if (fValue < fSourceMin)
fSourceMin = fValue;
if (fValue > fSourceMax)
fSourceMax = fValue;
if (aGroupInfo.mbIntegerOnly && !isInteger(fValue))
{
// If any non-integer numbers are involved, the group labels
// are shown including their upper limit.
aGroupInfo.mbIntegerOnly = false;
}
}
if (aGroupInfo.mbDateValues)
{
// special handling for dates: always integer, round down limits
aGroupInfo.mbIntegerOnly = true;
fSourceMin = rtl::math::approxFloor(fSourceMin);
fSourceMax = rtl::math::approxFloor(fSourceMax) + 1;
}
if (aGroupInfo.mbAutoStart)
aGroupInfo.mfStart = fSourceMin;
if (aGroupInfo.mbAutoEnd)
aGroupInfo.mfEnd = fSourceMax;
//TODO: limit number of entries?
tools::Long nLoopCount = 0;
double fLoop = aGroupInfo.mfStart;
rCache.ResetGroupItems(nDim, aGroupInfo, 0);
// Use "less than" instead of "less or equal" for the loop - don't
// create a group that consists only of the end value. Instead, the
// end value is then included in the last group (last group is bigger
// than the others). The first group has to be created nonetheless.
// GetNumGroupForValue has corresponding logic.
bool bFirstGroup = true;
while (bFirstGroup || (fLoop < aGroupInfo.mfEnd && !rtl::math::approxEqual(fLoop, aGroupInfo.mfEnd)))
{
ScDPItemData aItem;
aItem.SetRangeStart(fLoop);
rCache.SetGroupItem(nDim, aItem);
++nLoopCount;
fLoop = aGroupInfo.mfStart + nLoopCount * aGroupInfo.mfStep;
bFirstGroup = false;
// ScDPItemData values are compared with approxEqual
}
ScDPItemData aItem;
aItem.SetRangeFirst();
rCache.SetGroupItem(nDim, aItem);
aItem.SetRangeLast();
rCache.SetGroupItem(nDim, aItem);
}
}
void ScDPSaveNumGroupDimension::SetGroupInfo( const ScDPNumGroupInfo& rNew )
{
aGroupInfo = rNew;
}
void ScDPSaveNumGroupDimension::SetDateInfo( const ScDPNumGroupInfo& rInfo, sal_Int32 nPart )
{
aDateInfo = rInfo;
nDatePart = nPart;
}
namespace {
struct ScDPSaveGroupDimNameFunc
{
OUString maDimName;
explicit ScDPSaveGroupDimNameFunc( const OUString& rDimName ) : maDimName( rDimName ) {}
bool operator()( const ScDPSaveGroupDimension& rGroupDim ) const { return rGroupDim.GetGroupDimName() == maDimName; }
};
struct ScDPSaveGroupSourceNameFunc
{
OUString maSrcDimName;
explicit ScDPSaveGroupSourceNameFunc( const OUString& rSrcDimName ) : maSrcDimName( rSrcDimName ) {}
bool operator()( const ScDPSaveGroupDimension& rGroupDim ) const { return rGroupDim.GetSourceDimName() == maSrcDimName; }
};
} // namespace
ScDPDimensionSaveData::ScDPDimensionSaveData()
{
}
bool ScDPDimensionSaveData::operator==( const ScDPDimensionSaveData& ) const
{
return false;
}
void ScDPDimensionSaveData::AddGroupDimension( const ScDPSaveGroupDimension& rGroupDim )
{
OSL_ENSURE( ::std::none_of( maGroupDims.begin(), maGroupDims.end(), ScDPSaveGroupDimNameFunc( rGroupDim.GetGroupDimName() ) ),
"ScDPDimensionSaveData::AddGroupDimension - group dimension exists already" );
// ReplaceGroupDimension() adds new or replaces existing
ReplaceGroupDimension( rGroupDim );
}
void ScDPDimensionSaveData::ReplaceGroupDimension( const ScDPSaveGroupDimension& rGroupDim )
{
ScDPSaveGroupDimVec::iterator aIt = ::std::find_if(
maGroupDims.begin(), maGroupDims.end(), ScDPSaveGroupDimNameFunc( rGroupDim.GetGroupDimName() ) );
if( aIt == maGroupDims.end() )
maGroupDims.push_back( rGroupDim );
else
*aIt = rGroupDim;
}
void ScDPDimensionSaveData::RemoveGroupDimension( const OUString& rGroupDimName )
{
ScDPSaveGroupDimVec::iterator aIt = ::std::find_if(
maGroupDims.begin(), maGroupDims.end(), ScDPSaveGroupDimNameFunc( rGroupDimName ) );
if( aIt != maGroupDims.end() )
maGroupDims.erase( aIt );
}
void ScDPDimensionSaveData::AddNumGroupDimension( const ScDPSaveNumGroupDimension& rGroupDim )
{
OSL_ENSURE( maNumGroupDims.count( rGroupDim.GetDimensionName() ) == 0,
"ScDPDimensionSaveData::AddNumGroupDimension - numeric group dimension exists already" );
// ReplaceNumGroupDimension() adds new or replaces existing
ReplaceNumGroupDimension( rGroupDim );
}
void ScDPDimensionSaveData::ReplaceNumGroupDimension( const ScDPSaveNumGroupDimension& rGroupDim )
{
ScDPSaveNumGroupDimMap::iterator aIt = maNumGroupDims.find( rGroupDim.GetDimensionName() );
if( aIt == maNumGroupDims.end() )
maNumGroupDims.emplace( rGroupDim.GetDimensionName(), rGroupDim );
else
aIt->second = rGroupDim;
}
void ScDPDimensionSaveData::RemoveNumGroupDimension( const OUString& rGroupDimName )
{
maNumGroupDims.erase( rGroupDimName );
}
void ScDPDimensionSaveData::WriteToData( ScDPGroupTableData& rData ) const
{
// rData is assumed to be empty
// AddToData also handles date grouping
for( const auto& rGroupDim : maGroupDims )
rGroupDim.AddToData( rData );
for( const auto& rEntry : maNumGroupDims )
rEntry.second.AddToData( rData );
}
void ScDPDimensionSaveData::WriteToCache(ScDPCache& rCache) const
{
for (const auto& rEntry : maGroupDims)
rEntry.AddToCache(rCache);
for (const auto& rEntry : maNumGroupDims)
rEntry.second.AddToCache(rCache);
}
const ScDPSaveGroupDimension* ScDPDimensionSaveData::GetGroupDimForBase( const OUString& rBaseDimName ) const
{
return const_cast< ScDPDimensionSaveData* >( this )->GetGroupDimAccForBase( rBaseDimName );
}
const ScDPSaveGroupDimension* ScDPDimensionSaveData::GetNamedGroupDim( const OUString& rGroupDimName ) const
{
return const_cast< ScDPDimensionSaveData* >( this )->GetNamedGroupDimAcc( rGroupDimName );
}
const ScDPSaveGroupDimension* ScDPDimensionSaveData::GetFirstNamedGroupDim( const OUString& rBaseDimName ) const
{
return const_cast< ScDPDimensionSaveData* >( this )->GetFirstNamedGroupDimAcc( rBaseDimName );
}
const ScDPSaveGroupDimension* ScDPDimensionSaveData::GetNextNamedGroupDim( const OUString& rGroupDimName ) const
{
return const_cast< ScDPDimensionSaveData* >( this )->GetNextNamedGroupDimAcc( rGroupDimName );
}
const ScDPSaveNumGroupDimension* ScDPDimensionSaveData::GetNumGroupDim( const OUString& rGroupDimName ) const
{
return const_cast< ScDPDimensionSaveData* >( this )->GetNumGroupDimAcc( rGroupDimName );
}
ScDPSaveGroupDimension* ScDPDimensionSaveData::GetGroupDimAccForBase( const OUString& rBaseDimName )
{
ScDPSaveGroupDimension* pGroupDim = GetFirstNamedGroupDimAcc( rBaseDimName );
return pGroupDim ? pGroupDim : GetNextNamedGroupDimAcc( rBaseDimName );
}
ScDPSaveGroupDimension* ScDPDimensionSaveData::GetNamedGroupDimAcc( const OUString& rGroupDimName )
{
ScDPSaveGroupDimVec::iterator aIt = ::std::find_if(
maGroupDims.begin(), maGroupDims.end(), ScDPSaveGroupDimNameFunc( rGroupDimName ) );
return (aIt == maGroupDims.end()) ? nullptr : &*aIt;
}
ScDPSaveGroupDimension* ScDPDimensionSaveData::GetFirstNamedGroupDimAcc( const OUString& rBaseDimName )
{
ScDPSaveGroupDimVec::iterator aIt = ::std::find_if(
maGroupDims.begin(), maGroupDims.end(), ScDPSaveGroupSourceNameFunc( rBaseDimName ) );
return (aIt == maGroupDims.end()) ? nullptr : &*aIt;
}
ScDPSaveGroupDimension* ScDPDimensionSaveData::GetNextNamedGroupDimAcc( const OUString& rGroupDimName )
{
// find the group dimension with the passed name
ScDPSaveGroupDimVec::iterator aIt = ::std::find_if(
maGroupDims.begin(), maGroupDims.end(), ScDPSaveGroupDimNameFunc( rGroupDimName ) );
// find next group dimension based on the same source dimension name
if( aIt != maGroupDims.end() )
aIt = ::std::find_if( aIt + 1, maGroupDims.end(), ScDPSaveGroupSourceNameFunc( aIt->GetSourceDimName() ) );
return (aIt == maGroupDims.end()) ? nullptr : &*aIt;
}
ScDPSaveNumGroupDimension* ScDPDimensionSaveData::GetNumGroupDimAcc( const OUString& rGroupDimName )
{
ScDPSaveNumGroupDimMap::iterator aIt = maNumGroupDims.find( rGroupDimName );
return (aIt == maNumGroupDims.end()) ? nullptr : &aIt->second;
}
bool ScDPDimensionSaveData::HasGroupDimensions() const
{
return !maGroupDims.empty() || !maNumGroupDims.empty();
}
sal_Int32 ScDPDimensionSaveData::CollectDateParts( const OUString& rBaseDimName ) const
{
sal_Int32 nParts = 0;
// start with part of numeric group
if( const ScDPSaveNumGroupDimension* pNumDim = GetNumGroupDim( rBaseDimName ) )
nParts |= pNumDim->GetDatePart();
// collect parts from all matching group dimensions
for( const ScDPSaveGroupDimension* pGroupDim = GetFirstNamedGroupDim( rBaseDimName ); pGroupDim; pGroupDim = GetNextNamedGroupDim( pGroupDim->GetGroupDimName() ) )
nParts |= pGroupDim->GetDatePart();
return nParts;
}
OUString ScDPDimensionSaveData::CreateGroupDimName(
const OUString& rSourceName, const ScDPObject& rObject, bool bAllowSource,
const std::vector<OUString>* pDeletedNames )
{
// create a name for the new dimension by appending a number to the original
// dimension's name
bool bUseSource = bAllowSource; // if set, try the unchanged original name first
sal_Int32 nAdd = 2; // first try is "Name2"
const sal_Int32 nMaxAdd = 1000; // limit the loop
while ( nAdd <= nMaxAdd )
{
OUString aDimName( rSourceName );
if ( !bUseSource )
aDimName += OUString::number(nAdd);
// look for existing group dimensions
bool bExists = std::any_of(maGroupDims.begin(), maGroupDims.end(),
[&aDimName](const ScDPSaveGroupDimension& rDim) {
return rDim.GetGroupDimName() == aDimName; //TODO: ignore case
});
// look for base dimensions that happen to have that name
if ( !bExists && rObject.IsDimNameInUse( aDimName ) )
{
if ( pDeletedNames &&
std::find( pDeletedNames->begin(), pDeletedNames->end(), aDimName ) != pDeletedNames->end() )
{
// allow the name anyway if the name is in pDeletedNames
}
else
bExists = true;
}
if ( !bExists )
return aDimName; // found a new name
if ( bUseSource )
bUseSource = false;
else
++nAdd; // continue with higher number
}
OSL_FAIL("CreateGroupDimName: no valid name found");
return OUString();
}
namespace
{
const TranslateId aDatePartIds[] =
{
STR_DPFIELD_GROUP_BY_SECONDS,
STR_DPFIELD_GROUP_BY_MINUTES,
STR_DPFIELD_GROUP_BY_HOURS,
STR_DPFIELD_GROUP_BY_DAYS,
STR_DPFIELD_GROUP_BY_MONTHS,
STR_DPFIELD_GROUP_BY_QUARTERS,
STR_DPFIELD_GROUP_BY_YEARS
};
}
OUString ScDPDimensionSaveData::CreateDateGroupDimName(
sal_Int32 nDatePart, const ScDPObject& rObject, bool bAllowSource,
const std::vector<OUString>* pDeletedNames )
{
using namespace css::sheet::DataPilotFieldGroupBy;
OUString aPartName;
switch( nDatePart )
{
case SECONDS: aPartName = ScResId(aDatePartIds[0]); break;
case MINUTES: aPartName = ScResId(aDatePartIds[1]); break;
case HOURS: aPartName = ScResId(aDatePartIds[2]); break;
case DAYS: aPartName = ScResId(aDatePartIds[3]); break;
case MONTHS: aPartName = ScResId(aDatePartIds[4]); break;
case QUARTERS: aPartName = ScResId(aDatePartIds[5]); break;
case YEARS: aPartName = ScResId(aDatePartIds[6]); break;
}
OSL_ENSURE(!aPartName.isEmpty(), "ScDPDimensionSaveData::CreateDateGroupDimName - invalid date part");
return CreateGroupDimName( aPartName, rObject, bAllowSource, pDeletedNames );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|