1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sortparam.hxx>
22 #include <address.hxx>
23 #include <queryparam.hxx>
24 #include <subtotalparam.hxx>
26 #include <osl/diagnose.h>
30 ScSortParam::ScSortParam()
35 ScSortParam::ScSortParam( const ScSortParam
& r
) :
36 nCol1(r
.nCol1
),nRow1(r
.nRow1
),nCol2(r
.nCol2
),nRow2(r
.nRow2
),
37 nSourceTab(r
.nSourceTab
),
38 aDataAreaExtras(r
.aDataAreaExtras
),
39 nUserIndex(r
.nUserIndex
),
40 bHasHeader(r
.bHasHeader
),bByRow(r
.bByRow
),bCaseSens(r
.bCaseSens
),
41 bNaturalSort(r
.bNaturalSort
),
44 nDestTab(r
.nDestTab
),nDestCol(r
.nDestCol
),nDestRow(r
.nDestRow
),
45 maKeyState( r
.maKeyState
),
46 aCollatorLocale( r
.aCollatorLocale
), aCollatorAlgorithm( r
.aCollatorAlgorithm
),
47 nCompatHeader( r
.nCompatHeader
)
51 ScSortParam::~ScSortParam() {}
53 void ScSortParam::Clear()
55 ScSortKeyState aKeyState
;
57 nCol1
=nCol2
=nDestCol
= 0;
58 nRow1
=nRow2
=nDestRow
= 0;
60 aDataAreaExtras
= ScDataAreaExtras();
61 aDataAreaExtras
.mbCellDrawObjects
= true;
62 aDataAreaExtras
.mbCellFormats
= true;
66 bHasHeader
=bCaseSens
=bUserDef
=bNaturalSort
= false;
67 bByRow
= bInplace
= true;
68 aCollatorLocale
= css::lang::Locale();
69 aCollatorAlgorithm
.clear();
71 aKeyState
.bDoSort
= false;
73 aKeyState
.bAscending
= true;
74 aKeyState
.aColorSortMode
= ScColorSortMode::None
;
76 // Initialize to default size
77 maKeyState
.assign( DEFSORT
, aKeyState
);
80 ScSortParam
& ScSortParam::operator=( const ScSortParam
& r
)
86 nSourceTab
= r
.nSourceTab
;
87 aDataAreaExtras
= r
.aDataAreaExtras
;
88 nUserIndex
= r
.nUserIndex
;
89 bHasHeader
= r
.bHasHeader
;
91 bCaseSens
= r
.bCaseSens
;
92 bNaturalSort
= r
.bNaturalSort
;
93 bUserDef
= r
.bUserDef
;
94 bInplace
= r
.bInplace
;
95 nDestTab
= r
.nDestTab
;
96 nDestCol
= r
.nDestCol
;
97 nDestRow
= r
.nDestRow
;
98 maKeyState
= r
.maKeyState
;
99 aCollatorLocale
= r
.aCollatorLocale
;
100 aCollatorAlgorithm
= r
.aCollatorAlgorithm
;
101 nCompatHeader
= r
.nCompatHeader
;
106 bool ScSortParam::operator==( const ScSortParam
& rOther
) const
109 // Number of Sorts the same?
110 sal_uInt16 nLast
= 0;
111 sal_uInt16 nOtherLast
= 0;
112 sal_uInt16 nSortSize
= GetSortKeyCount();
114 if ( !maKeyState
.empty() )
116 while ( maKeyState
[nLast
++].bDoSort
&& nLast
< nSortSize
) ;
120 if ( !rOther
.maKeyState
.empty() )
122 while ( rOther
.maKeyState
[nOtherLast
++].bDoSort
&& nOtherLast
< nSortSize
) ;
126 if ( (nLast
== nOtherLast
)
127 && (nCol1
== rOther
.nCol1
)
128 && (nRow1
== rOther
.nRow1
)
129 && (nCol2
== rOther
.nCol2
)
130 && (nRow2
== rOther
.nRow2
)
131 && (nSourceTab
== rOther
.nSourceTab
)
132 && (aDataAreaExtras
== rOther
.aDataAreaExtras
)
133 && (bHasHeader
== rOther
.bHasHeader
)
134 && (bByRow
== rOther
.bByRow
)
135 && (bCaseSens
== rOther
.bCaseSens
)
136 && (bNaturalSort
== rOther
.bNaturalSort
)
137 && (bUserDef
== rOther
.bUserDef
)
138 && (nUserIndex
== rOther
.nUserIndex
)
139 && (bInplace
== rOther
.bInplace
)
140 && (nDestTab
== rOther
.nDestTab
)
141 && (nDestCol
== rOther
.nDestCol
)
142 && (nDestRow
== rOther
.nDestRow
)
143 && (aCollatorLocale
.Language
== rOther
.aCollatorLocale
.Language
)
144 && (aCollatorLocale
.Country
== rOther
.aCollatorLocale
.Country
)
145 && (aCollatorLocale
.Variant
== rOther
.aCollatorLocale
.Variant
)
146 && (aCollatorAlgorithm
== rOther
.aCollatorAlgorithm
)
147 && ( !maKeyState
.empty() || !rOther
.maKeyState
.empty() )
151 for ( sal_uInt16 i
=0; i
<=nLast
&& bEqual
; i
++ )
152 bEqual
= ( maKeyState
[i
].nField
== rOther
.maKeyState
[i
].nField
) &&
153 ( maKeyState
[i
].bAscending
== rOther
.maKeyState
[i
].bAscending
);
155 if ( maKeyState
.empty() && rOther
.maKeyState
.empty() )
161 ScSortParam::ScSortParam( const ScSubTotalParam
& rSub
, const ScSortParam
& rOld
) :
162 nCol1(rSub
.nCol1
),nRow1(rSub
.nRow1
),nCol2(rSub
.nCol2
),nRow2(rSub
.nRow2
),
164 aDataAreaExtras(rOld
.aDataAreaExtras
),
165 nUserIndex(rSub
.nUserIndex
),
166 bHasHeader(true),bByRow(true),bCaseSens(rSub
.bCaseSens
),bNaturalSort(rOld
.bNaturalSort
),
167 bUserDef(rSub
.bUserDef
),
169 nDestTab(0),nDestCol(0),nDestRow(0),
170 aCollatorLocale( rOld
.aCollatorLocale
), aCollatorAlgorithm( rOld
.aCollatorAlgorithm
),
171 nCompatHeader( rOld
.nCompatHeader
)
173 aDataAreaExtras
.mbCellFormats
= rSub
.bIncludePattern
;
174 aDataAreaExtras
.resetArea();
176 // first the groups from the partial results
178 for (const auto& group
: rSub
.aGroups
)
183 key
.nField
= group
.nField
;
184 key
.bAscending
= rSub
.bAscending
;
185 key
.aColorSortMode
= ScColorSortMode::None
;
186 maKeyState
.push_back(key
);
189 // then the old settings
190 for (sal_uInt16 i
= 0; i
< rOld
.GetSortKeyCount(); i
++)
191 if (rOld
.maKeyState
[i
].bDoSort
)
193 SCCOLROW nThisField
= rOld
.maKeyState
[i
].nField
;
194 bool bDouble
= false;
195 for (sal_uInt16 j
= 0; j
< GetSortKeyCount(); j
++)
196 if ( maKeyState
[j
].nField
== nThisField
)
198 if (!bDouble
) // do not enter a field twice
202 key
.nField
= nThisField
;
203 key
.bAscending
= rOld
.maKeyState
[i
].bAscending
;
204 key
.aColorSortMode
= ScColorSortMode::None
;
205 maKeyState
.push_back(key
);
210 ScSortParam::ScSortParam( const ScQueryParam
& rParam
, SCCOL nCol
) :
211 nCol1(nCol
),nRow1(rParam
.nRow1
),nCol2(nCol
),nRow2(rParam
.nRow2
),
212 nSourceTab(rParam
.nTab
),
214 bHasHeader(rParam
.bHasHeader
),bByRow(true),bCaseSens(rParam
.bCaseSens
),
216 //TODO: what about Locale and Algorithm?
219 nDestTab(0),nDestCol(0),nDestRow(0), nCompatHeader(2)
221 aDataAreaExtras
.mbCellDrawObjects
= true;
223 ScSortKeyState aKeyState
;
224 aKeyState
.bDoSort
= true;
225 aKeyState
.nField
= nCol
;
226 aKeyState
.bAscending
= true;
227 aKeyState
.aColorSortMode
= ScColorSortMode::None
;
229 maKeyState
.push_back( aKeyState
);
232 aKeyState
.bDoSort
= false;
233 aKeyState
.nField
= 0;
235 for (sal_uInt16 i
=1; i
<GetSortKeyCount(); i
++)
236 maKeyState
.push_back( aKeyState
);
239 void ScSortParam::MoveToDest()
243 SCCOL nDifX
= nDestCol
- nCol1
;
244 SCROW nDifY
= nDestRow
- nRow1
;
246 nCol1
= sal::static_int_cast
<SCCOL
>( nCol1
+ nDifX
);
247 nRow1
= sal::static_int_cast
<SCROW
>( nRow1
+ nDifY
);
248 nCol2
= sal::static_int_cast
<SCCOL
>( nCol2
+ nDifX
);
249 nRow2
= sal::static_int_cast
<SCROW
>( nRow2
+ nDifY
);
250 for (sal_uInt16 i
=0; i
<GetSortKeyCount(); i
++)
252 maKeyState
[i
].nField
+= nDifX
;
254 maKeyState
[i
].nField
+= nDifY
;
260 OSL_FAIL("MoveToDest, bInplace == TRUE");
272 bool operator() ( const ReorderIndex
& r1
, const ReorderIndex
& r2
) const
274 return r1
.mnPos2
< r2
.mnPos2
;
281 ReorderIndex( SCCOLROW nPos1
, SCCOLROW nPos2
) : mnPos1(nPos1
), mnPos2(nPos2
) {}
286 void ReorderParam::reverse()
290 nStart
= maSortRange
.aStart
.Row();
292 nStart
= maSortRange
.aStart
.Col();
294 size_t n
= maOrderIndices
.size();
295 std::vector
<ReorderIndex
> aBucket
;
297 for (size_t i
= 0; i
< n
; ++i
)
299 SCCOLROW nPos1
= i
+ nStart
;
300 SCCOLROW nPos2
= maOrderIndices
[i
];
301 aBucket
.emplace_back(nPos1
, nPos2
);
304 std::sort(aBucket
.begin(), aBucket
.end(), ReorderIndex::LessByPos2());
305 std::vector
<SCCOLROW
> aNew
;
307 for (size_t i
= 0; i
< n
; ++i
)
308 aNew
.push_back(aBucket
[i
].mnPos1
);
310 maOrderIndices
.swap(aNew
);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */