tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / core / data / sortparam.cxx
blob868cf38a8df819c096324a716236074fbbfeedc2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <global.hxx>
22 #include <address.hxx>
23 #include <queryparam.hxx>
24 #include <subtotalparam.hxx>
26 #include <osl/diagnose.h>
28 #include <algorithm>
30 ScSortParam::ScSortParam()
32 Clear();
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),
42 bUserDef(r.bUserDef),
43 bInplace(r.bInplace),
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;
59 nSourceTab = 0;
60 aDataAreaExtras = ScDataAreaExtras();
61 aDataAreaExtras.mbCellDrawObjects = true;
62 aDataAreaExtras.mbCellFormats = true;
63 nCompatHeader = 2;
64 nDestTab = 0;
65 nUserIndex = 0;
66 bHasHeader=bCaseSens=bUserDef=bNaturalSort = false;
67 bByRow = bInplace = true;
68 aCollatorLocale = css::lang::Locale();
69 aCollatorAlgorithm.clear();
71 aKeyState.bDoSort = false;
72 aKeyState.nField = 0;
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 )
82 nCol1 = r.nCol1;
83 nRow1 = r.nRow1;
84 nCol2 = r.nCol2;
85 nRow2 = r.nRow2;
86 nSourceTab = r.nSourceTab;
87 aDataAreaExtras = r.aDataAreaExtras;
88 nUserIndex = r.nUserIndex;
89 bHasHeader = r.bHasHeader;
90 bByRow = r.bByRow;
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;
103 return *this;
106 bool ScSortParam::operator==( const ScSortParam& rOther ) const
108 bool bEqual = false;
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 ) ;
117 nLast--;
120 if ( !rOther.maKeyState.empty() )
122 while ( rOther.maKeyState[nOtherLast++].bDoSort && nOtherLast < nSortSize ) ;
123 nOtherLast--;
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() )
150 bEqual = true;
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() )
156 bEqual = true;
158 return bEqual;
161 ScSortParam::ScSortParam( const ScSubTotalParam& rSub, const ScSortParam& rOld ) :
162 nCol1(rSub.nCol1),nRow1(rSub.nRow1),nCol2(rSub.nCol2),nRow2(rSub.nRow2),
163 nSourceTab(0),
164 aDataAreaExtras(rOld.aDataAreaExtras),
165 nUserIndex(rSub.nUserIndex),
166 bHasHeader(true),bByRow(true),bCaseSens(rSub.bCaseSens),bNaturalSort(rOld.bNaturalSort),
167 bUserDef(rSub.bUserDef),
168 bInplace(true),
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
177 if (rSub.bDoSort)
178 for (const auto& group : rSub.aGroups)
179 if (group.bActive)
181 ScSortKeyState key;
182 key.bDoSort = true;
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 )
197 bDouble = true;
198 if (!bDouble) // do not enter a field twice
200 ScSortKeyState key;
201 key.bDoSort = true;
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),
213 nUserIndex(0),
214 bHasHeader(rParam.bHasHeader),bByRow(true),bCaseSens(rParam.bCaseSens),
215 bNaturalSort(false),
216 //TODO: what about Locale and Algorithm?
217 bUserDef(false),
218 bInplace(true),
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 );
231 // Set the rest
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()
241 if (!bInplace)
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++)
251 if (bByRow)
252 maKeyState[i].nField += nDifX;
253 else
254 maKeyState[i].nField += nDifY;
256 bInplace = true;
258 else
260 OSL_FAIL("MoveToDest, bInplace == TRUE");
264 namespace sc {
266 namespace {
268 struct ReorderIndex
270 struct LessByPos2
272 bool operator() ( const ReorderIndex& r1, const ReorderIndex& r2 ) const
274 return r1.mnPos2 < r2.mnPos2;
278 SCCOLROW mnPos1;
279 SCCOLROW mnPos2;
281 ReorderIndex( SCCOLROW nPos1, SCCOLROW nPos2 ) : mnPos1(nPos1), mnPos2(nPos2) {}
286 void ReorderParam::reverse()
288 SCCOLROW nStart;
289 if (mbByRow)
290 nStart = maSortRange.aStart.Row();
291 else
292 nStart = maSortRange.aStart.Col();
294 size_t n = maOrderIndices.size();
295 std::vector<ReorderIndex> aBucket;
296 aBucket.reserve(n);
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;
306 aNew.reserve(n);
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: */