Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / data / sortparam.cxx
blobd416f32ed0dbb62b54ba44c880a32193b63570a9
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"
27 //------------------------------------------------------------------------
29 ScSortParam::ScSortParam()
31 Clear();
34 //------------------------------------------------------------------------
36 ScSortParam::ScSortParam( const ScSortParam& r ) :
37 nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),nUserIndex(r.nUserIndex),
38 bHasHeader(r.bHasHeader),bByRow(r.bByRow),bCaseSens(r.bCaseSens),
39 bNaturalSort(r.bNaturalSort),bUserDef(r.bUserDef),
40 bIncludePattern(r.bIncludePattern),bInplace(r.bInplace),
41 nDestTab(r.nDestTab),nDestCol(r.nDestCol),nDestRow(r.nDestRow),
42 maKeyState( r.maKeyState ),
43 aCollatorLocale( r.aCollatorLocale ), aCollatorAlgorithm( r.aCollatorAlgorithm ),
44 nCompatHeader( r.nCompatHeader )
48 ScSortParam::~ScSortParam() {}
50 //------------------------------------------------------------------------
52 void ScSortParam::Clear()
54 ScSortKeyState aKeyState;
56 nCol1=nCol2=nDestCol = 0;
57 nRow1=nRow2=nDestRow = 0;
58 nCompatHeader = 2;
59 nDestTab = 0;
60 nUserIndex = 0;
61 bHasHeader=bCaseSens=bUserDef=bNaturalSort = false;
62 bByRow=bIncludePattern=bInplace = true;
63 aCollatorLocale = ::com::sun::star::lang::Locale();
64 aCollatorAlgorithm = OUString();
66 aKeyState.bDoSort = false;
67 aKeyState.nField = 0;
68 aKeyState.bAscending = true;
70 // Initialize to default size
71 maKeyState.assign( DEFSORT, aKeyState );
74 //------------------------------------------------------------------------
76 ScSortParam& ScSortParam::operator=( const ScSortParam& r )
78 nCol1 = r.nCol1;
79 nRow1 = r.nRow1;
80 nCol2 = r.nCol2;
81 nRow2 = r.nRow2;
82 nUserIndex = r.nUserIndex;
83 bHasHeader = r.bHasHeader;
84 bByRow = r.bByRow;
85 bCaseSens = r.bCaseSens;
86 bNaturalSort = r.bNaturalSort;
87 bUserDef = r.bUserDef;
88 bIncludePattern = r.bIncludePattern;
89 bInplace = r.bInplace;
90 nDestTab = r.nDestTab;
91 nDestCol = r.nDestCol;
92 nDestRow = r.nDestRow;
93 maKeyState = r.maKeyState;
94 aCollatorLocale = r.aCollatorLocale;
95 aCollatorAlgorithm = r.aCollatorAlgorithm;
96 nCompatHeader = r.nCompatHeader;
98 return *this;
101 //------------------------------------------------------------------------
103 bool ScSortParam::operator==( const ScSortParam& rOther ) const
105 bool bEqual = false;
106 // Anzahl der Sorts gleich?
107 sal_uInt16 nLast = 0;
108 sal_uInt16 nOtherLast = 0;
109 sal_uInt16 nSortSize = GetSortKeyCount();
111 if ( !maKeyState.empty() )
113 while ( maKeyState[nLast++].bDoSort && nLast < nSortSize ) ;
114 nLast--;
117 if ( !rOther.maKeyState.empty() )
119 while ( rOther.maKeyState[nOtherLast++].bDoSort && nOtherLast < nSortSize ) ;
120 nOtherLast--;
123 if ( (nLast == nOtherLast)
124 && (nCol1 == rOther.nCol1)
125 && (nRow1 == rOther.nRow1)
126 && (nCol2 == rOther.nCol2)
127 && (nRow2 == rOther.nRow2)
128 && (bHasHeader == rOther.bHasHeader)
129 && (bByRow == rOther.bByRow)
130 && (bCaseSens == rOther.bCaseSens)
131 && (bNaturalSort == rOther.bNaturalSort)
132 && (bUserDef == rOther.bUserDef)
133 && (nUserIndex == rOther.nUserIndex)
134 && (bIncludePattern == rOther.bIncludePattern)
135 && (bInplace == rOther.bInplace)
136 && (nDestTab == rOther.nDestTab)
137 && (nDestCol == rOther.nDestCol)
138 && (nDestRow == rOther.nDestRow)
139 && (aCollatorLocale.Language == rOther.aCollatorLocale.Language)
140 && (aCollatorLocale.Country == rOther.aCollatorLocale.Country)
141 && (aCollatorLocale.Variant == rOther.aCollatorLocale.Variant)
142 && (aCollatorAlgorithm == rOther.aCollatorAlgorithm)
143 && ( !maKeyState.empty() || !rOther.maKeyState.empty() )
146 bEqual = true;
147 for ( sal_uInt16 i=0; i<=nLast && bEqual; i++ )
148 bEqual = ( maKeyState[i].nField == rOther.maKeyState[i].nField ) &&
149 ( maKeyState[i].bAscending == rOther.maKeyState[i].bAscending );
151 if ( maKeyState.empty() && rOther.maKeyState.empty() )
152 bEqual = true;
154 return bEqual;
157 //------------------------------------------------------------------------
159 ScSortParam::ScSortParam( const ScSubTotalParam& rSub, const ScSortParam& rOld ) :
160 nCol1(rSub.nCol1),nRow1(rSub.nRow1),nCol2(rSub.nCol2),nRow2(rSub.nRow2),nUserIndex(rSub.nUserIndex),
161 bHasHeader(true),bByRow(true),bCaseSens(rSub.bCaseSens),bNaturalSort(rOld.bNaturalSort),
162 bUserDef(rSub.bUserDef),bIncludePattern(rSub.bIncludePattern),
163 bInplace(true),
164 nDestTab(0),nDestCol(0),nDestRow(0),
165 aCollatorLocale( rOld.aCollatorLocale ), aCollatorAlgorithm( rOld.aCollatorAlgorithm ),
166 nCompatHeader( rOld.nCompatHeader )
168 sal_uInt16 i;
170 // zuerst die Gruppen aus den Teilergebnissen
171 if (rSub.bDoSort)
172 for (i=0; i<MAXSUBTOTAL; i++)
173 if (rSub.bGroupActive[i])
175 ScSortKeyState key;
176 key.bDoSort = true;
177 key.nField = rSub.nField[i];
178 key.bAscending = rSub.bAscending;
179 maKeyState.push_back(key);
182 // dann dahinter die alten Einstellungen
183 for (i=0; i < rOld.GetSortKeyCount(); i++)
184 if (rOld.maKeyState[i].bDoSort)
186 SCCOLROW nThisField = rOld.maKeyState[i].nField;
187 bool bDouble = false;
188 for (sal_uInt16 j = 0; j < GetSortKeyCount(); j++)
189 if ( maKeyState[j].nField == nThisField )
190 bDouble = true;
191 if (!bDouble) // ein Feld nicht zweimal eintragen
193 ScSortKeyState key;
194 key.bDoSort = true;
195 key.nField = nThisField;
196 key.bAscending = rOld.maKeyState[i].bAscending;
197 maKeyState.push_back(key);
202 //------------------------------------------------------------------------
204 ScSortParam::ScSortParam( const ScQueryParam& rParam, SCCOL nCol ) :
205 nCol1(nCol),nRow1(rParam.nRow1),nCol2(nCol),nRow2(rParam.nRow2),nUserIndex(0),
206 bHasHeader(rParam.bHasHeader),bByRow(true),bCaseSens(rParam.bCaseSens),
207 bNaturalSort(false),
208 //! TODO: what about Locale and Algorithm?
209 bUserDef(false),bIncludePattern(false),
210 bInplace(true),
211 nDestTab(0),nDestCol(0),nDestRow(0), nCompatHeader(2)
213 ScSortKeyState aKeyState;
214 aKeyState.bDoSort = true;
215 aKeyState.nField = nCol;
216 aKeyState.bAscending = true;
218 maKeyState.push_back( aKeyState );
220 // Set the rest
221 aKeyState.bDoSort = false;
222 aKeyState.nField = 0;
224 for (sal_uInt16 i=1; i<GetSortKeyCount(); i++)
225 maKeyState.push_back( aKeyState );
228 //------------------------------------------------------------------------
230 void ScSortParam::MoveToDest()
232 if (!bInplace)
234 SCsCOL nDifX = ((SCsCOL) nDestCol) - ((SCsCOL) nCol1);
235 SCsROW nDifY = ((SCsROW) nDestRow) - ((SCsROW) nRow1);
237 nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
238 nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
239 nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
240 nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
241 for (sal_uInt16 i=0; i<GetSortKeyCount(); i++)
242 if (bByRow)
243 maKeyState[i].nField += nDifX;
244 else
245 maKeyState[i].nField += nDifY;
247 bInplace = true;
249 else
251 OSL_FAIL("MoveToDest, bInplace == TRUE");
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */