LibreOffice Module sw (master) 1
docsort.cxx
Go to the documentation of this file.
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 <hintids.hxx>
21#include <osl/diagnose.h>
25#include <docary.hxx>
26#include <fmtanchr.hxx>
27#include <frmfmt.hxx>
28#include <doc.hxx>
29#include <IDocumentUndoRedo.hxx>
32#include <IDocumentState.hxx>
33#include <node.hxx>
34#include <pam.hxx>
35#include <ndtxt.hxx>
36#include <swtable.hxx>
37#include <swundo.hxx>
38#include <sortopt.hxx>
39#include <docsort.hxx>
40#include <UndoSort.hxx>
41#include <UndoRedline.hxx>
42#include <hints.hxx>
43#include <tblsel.hxx>
44#include <cellatr.hxx>
45#include <redline.hxx>
46#include <node2lay.hxx>
47#include <frameformats.hxx>
48#include <svl/numformat.hxx>
49
50#include <set>
51#include <utility>
52
53using namespace ::com::sun::star::lang;
54using namespace ::com::sun::star;
55
57SwDoc* SwSortElement::pDoc = nullptr;
58const FlatFndBox* SwSortElement::pBox = nullptr;
60lang::Locale* SwSortElement::pLocale = nullptr;
61std::optional<OUString> SwSortElement::xLastAlgorithm;
63
64// List of all sorted elements
65
68 FlatFndBox const * pFltBx )
69{
70 OSL_ENSURE( !pDoc && !pOptions && !pBox, "Who forgot to call Finit?" );
71 pDoc = pD;
72 pOptions = new SwSortOptions( rOpt );
73 pBox = pFltBx;
74
76 if ( nLang.anyOf(
80 pLocale = new lang::Locale( LanguageTag::convertToLocale( nLang ) );
81
82 pSortCollator = new CollatorWrapper( ::comphelper::getProcessComponentContext() );
83}
84
86{
87 delete pOptions;
88 pOptions = nullptr;
89 delete pLocale;
90 pLocale = nullptr;
91 xLastAlgorithm.reset();
92 delete pSortCollator;
93 pSortCollator = nullptr;
94 delete pLclData;
95 pLclData = nullptr;
96 pDoc = nullptr;
97 pBox = nullptr;
98}
99
101{
102}
103
104double SwSortElement::StrToDouble( std::u16string_view rStr )
105{
106 if( !pLclData )
108
109 rtl_math_ConversionStatus eStatus;
110 sal_Int32 nEnd;
111 double nRet = pLclData->stringToDouble( rStr, true, &eStatus, &nEnd );
112
113 if( rtl_math_ConversionStatus_Ok != eStatus || nEnd == 0 )
114 nRet = 0.0;
115 return nRet;
116}
117
118int SwSortElement::keycompare(const SwSortElement& rCmp, sal_uInt16 nKey) const
119{
120 int nCmp = 0;
121 // The actual comparison
122 const SwSortElement *pOrig, *pCmp;
123
124 const SwSortKey& rSrtKey = pOptions->aKeys[ nKey ];
125 if( rSrtKey.eSortOrder == SwSortOrder::Ascending )
126 {
127 pOrig = this;
128 pCmp = &rCmp;
129 }
130 else
131 {
132 pOrig = &rCmp;
133 pCmp = this;
134 }
135
136 if( rSrtKey.bIsNumeric )
137 {
138 double n1 = pOrig->GetValue( nKey );
139 double n2 = pCmp->GetValue( nKey );
140
141 nCmp = n1 < n2 ? -1 : n1 == n2 ? 0 : 1;
142 }
143 else
144 {
145 if( !xLastAlgorithm || *xLastAlgorithm != rSrtKey.sSortType )
146 {
147 xLastAlgorithm = rSrtKey.sSortType;
149 *pLocale,
151 }
152
154 pOrig->GetKey( nKey ), pCmp->GetKey( nKey ));
155 }
156 return nCmp;
157}
158
160{
161 // The actual comparison
162 for(size_t nKey = 0; nKey < pOptions->aKeys.size(); ++nKey)
163 {
164 int nCmp = keycompare(rCmp, nKey);
165
166 if (nCmp == 0)
167 continue;
168
169 return nCmp < 0;
170 }
171
172 return false;
173}
174
175double SwSortElement::GetValue( sal_uInt16 nKey ) const
176{
177 return StrToDouble( GetKey( nKey ));
178}
179
182 : nOrg(rPos.GetIndex()), aPos(rPos)
183{
184}
185
186OUString SwSortTextElement::GetKey(sal_uInt16 nId) const
187{
188 SwTextNode* pTextNd = aPos.GetNode().GetTextNode();
189 if( !pTextNd )
190 return OUString();
191
192 // for TextNodes
193 const OUString& rStr = pTextNd->GetText();
194
195 sal_Unicode nDeli = pOptions->cDeli;
196 sal_uInt16 nDCount = pOptions->aKeys[nId].nColumnId, i = 1;
197 sal_Int32 nStart = 0;
198
199 // Find the delimiter
200 while( nStart != -1 && i < nDCount)
201 {
202 nStart = rStr.indexOf( nDeli, nStart );
203 if( -1 != nStart )
204 {
205 nStart++;
206 i++;
207 }
208 }
209
210 // Found next delimiter or end of String
211 // and copy
212 sal_Int32 nEnd = rStr.indexOf( nDeli, nStart+1 );
213 if (nEnd == -1)
214 return rStr.copy( nStart );
215 return rStr.copy( nStart, nEnd-nStart );
216}
217
220 : nRow( nRC )
221{
222}
223
225OUString SwSortBoxElement::GetKey(sal_uInt16 nKey) const
226{
227 const FndBox_* pFndBox;
228 sal_uInt16 nCol = pOptions->aKeys[nKey].nColumnId-1;
229
231 pFndBox = pBox->GetBox(nCol, nRow); // Sort rows
232 else
233 pFndBox = pBox->GetBox(nRow, nCol); // Sort columns
234
235 // Extract the Text
236 OUStringBuffer aRetStr;
237 if( pFndBox )
238 { // Get StartNode and skip it
239 const SwTableBox* pMyBox = pFndBox->GetBox();
240 OSL_ENSURE(pMyBox, "No atomic Box");
241
242 if( pMyBox->GetSttNd() )
243 {
244 // Iterate over all the Box's TextNodes
245 const SwNode *pNd = nullptr, *pEndNd = pMyBox->GetSttNd()->EndOfSectionNode();
246 for( SwNodeOffset nIdx = pMyBox->GetSttIdx() + 1; pNd != pEndNd; ++nIdx )
247 {
248 pNd = pDoc->GetNodes()[ nIdx ];
249 if( pNd->IsTextNode() )
250 aRetStr.append(pNd->GetTextNode()->GetText());
251 }
252 }
253 }
254 return aRetStr.makeStringAndClear();
255}
256
257double SwSortBoxElement::GetValue( sal_uInt16 nKey ) const
258{
259 const FndBox_* pFndBox;
260 sal_uInt16 nCol = pOptions->aKeys[nKey].nColumnId-1;
261
263 pFndBox = pBox->GetBox(nCol, nRow); // Sort rows
264 else
265 pFndBox = pBox->GetBox(nRow, nCol); // Sort columns
266
267 double nVal;
268 if( pFndBox )
269 {
270 const SwFormat *pFormat = pFndBox->GetBox()->GetFrameFormat();
272 nVal = SwSortElement::GetValue( nKey );
273 else
274 nVal = pFormat->GetTableBoxValue().GetValue();
275 }
276 else
277 nVal = 0;
278
279 return nVal;
280}
281
283bool SwDoc::SortText(const SwPaM& rPaM, const SwSortOptions& rOpt)
284{
285 // Check if Frame is in the Text
286 auto [pStart, pEnd] = rPaM.StartEnd(); // SwPosition*
287
288 // Set index to the Selection's start
289 for(sw::SpzFrameFormat* pFormat: *GetSpzFrameFormats())
290 {
291 SwFormatAnchor const*const pAnchor = &pFormat->GetAnchor();
292 SwNode const*const pAnchorNode = pAnchor->GetAnchorNode();
293
294 if (pAnchorNode && (RndStdIds::FLY_AT_PARA == pAnchor->GetAnchorId()) &&
295 pStart->GetNode() <= *pAnchorNode && *pAnchorNode <= pEnd->GetNode() )
296 return false;
297 }
298
299 // Check if only TextNodes are within the Selection
300 {
301 SwNodeOffset nStart = pStart->GetNodeIndex(),
302 nEnd = pEnd->GetNodeIndex();
303 while( nStart <= nEnd )
304 // Iterate over a selected range
305 if( !GetNodes()[ nStart++ ]->IsTextNode() )
306 return false;
307 }
308
309 bool const bUndo = GetIDocumentUndoRedo().DoesUndo();
310 if( bUndo )
311 {
312 GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
313 }
314
315 SwPaM* pRedlPam = nullptr;
316 SwUndoRedlineSort* pRedlUndo = nullptr;
317 SwUndoSort* pUndoSort = nullptr;
318
319 // To-Do - add 'SwExtraRedlineTable' also ?
320 if( getIDocumentRedlineAccess().IsRedlineOn() || (!getIDocumentRedlineAccess().IsIgnoreRedline() && !getIDocumentRedlineAccess().GetRedlineTable().empty() ))
321 {
322 pRedlPam = new SwPaM( pStart->GetNode(), pEnd->GetNode(), SwNodeOffset(-1), SwNodeOffset(1) );
323 SwContentNode* pCNd = pRedlPam->GetMarkContentNode();
324 if( pCNd )
325 pRedlPam->GetMark()->SetContent( pCNd->Len() );
326
328 {
329 if( bUndo )
330 {
331 pRedlUndo = new SwUndoRedlineSort( *pRedlPam,rOpt );
332 GetIDocumentUndoRedo().DoUndo(false);
333 }
334 // First copy the range
335 SwNodeIndex aEndIdx( pEnd->GetNode(), 1 );
336 SwNodeRange aRg( pStart->GetNode(), aEndIdx.GetNode() );
337 GetNodes().Copy_( aRg, aEndIdx.GetNode() );
338
339 // range is new from pEnd->nNode+1 to aEndIdx
340 getIDocumentRedlineAccess().DeleteRedline( *pRedlPam, true, RedlineType::Any );
341
342 pRedlPam->GetMark()->Assign( pEnd->GetNode(), SwNodeOffset(1) );
343
344 pRedlPam->GetPoint()->Assign( aEndIdx.GetNode() );
345 pCNd = pRedlPam->GetPointContentNode();
346 sal_Int32 nCLen = 0;
347 if( !pCNd )
348 {
349 pCNd = GetNodes()[ aEndIdx.GetIndex()-SwNodeOffset(1) ]->GetContentNode();
350 if( pCNd )
351 {
352 nCLen = pCNd->Len();
353 pRedlPam->GetPoint()->Assign( *pCNd );
354 }
355 }
356 if (pCNd)
357 pRedlPam->GetPoint()->SetContent( nCLen );
358
359 if( pRedlUndo )
360 pRedlUndo->SetValues( rPaM );
361 }
362 else
363 {
364 getIDocumentRedlineAccess().DeleteRedline( *pRedlPam, true, RedlineType::Any );
365 delete pRedlPam;
366 pRedlPam = nullptr;
367 }
368 }
369
370 SwNodeIndex aStart(pStart->GetNode());
371 SwSortElement::Init( this, rOpt );
372 std::multiset<SwSortTextElement> aSortSet;
373 while( aStart <= pEnd->GetNode() )
374 {
375 // Iterate over a selected range
376 aSortSet.insert(SwSortTextElement(aStart));
377 ++aStart;
378 }
379
380 // Now comes the tricky part: Move Nodes (and always keep Undo in mind)
381 SwNodeOffset nBeg = pStart->GetNodeIndex();
382 SwNodeRange aRg( aStart, aStart );
383
384 if( bUndo && !pRedlUndo )
385 {
386 pUndoSort = new SwUndoSort(rPaM, rOpt);
387 GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndoSort));
388 }
389
390 GetIDocumentUndoRedo().DoUndo(false);
391
392 SwNodeOffset n(0);
393 for (const auto& rElem : aSortSet)
394 {
395 aStart = nBeg + n;
396 aRg.aStart = rElem.aPos.GetIndex();
397 aRg.aEnd = aRg.aStart.GetIndex() + 1;
398
399 // Move Nodes
402
403 // Insert Move in Undo
404 if(pUndoSort)
405 {
406 pUndoSort->Insert(rElem.nOrg, nBeg + n);
407 }
408 ++n;
409 }
410 // Delete all elements from the SortArray
411 aSortSet.clear();
413
414 if( pRedlPam )
415 {
416 if( pRedlUndo )
417 {
418 pRedlUndo->SetSaveRange( *pRedlPam );
419 // UGLY: temp. enable Undo
420 GetIDocumentUndoRedo().DoUndo(true);
421 GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pRedlUndo) );
422 GetIDocumentUndoRedo().DoUndo(false);
423 }
424
425 // nBeg is start of sorted range
426 SwNodeIndex aSttIdx( GetNodes(), nBeg );
427
428 // the copied range is deleted
429 SwRangeRedline *const pDeleteRedline(
430 new SwRangeRedline( RedlineType::Delete, *pRedlPam ));
431
432 // pRedlPam points to nodes that may be deleted (hidden) by
433 // AppendRedline, so adjust it beforehand to prevent ASSERT
434 pRedlPam->GetPoint()->Assign(aSttIdx);
435
436 getIDocumentRedlineAccess().AppendRedline(pDeleteRedline, true);
437
438 // the sorted range is inserted
439 getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, *pRedlPam ), true);
440
441 if( pRedlUndo )
442 {
443 SwNodeIndex aInsEndIdx( pRedlPam->GetMark()->GetNode(), -1 );
444 SwContentNode *const pContentNode = aInsEndIdx.GetNode().GetContentNode();
445 pRedlPam->GetMark()->Assign( *pContentNode, pContentNode->Len() );
446
447 pRedlUndo->SetValues( *pRedlPam );
448 }
449
450 delete pRedlPam;
451 pRedlPam = nullptr;
452 }
453 GetIDocumentUndoRedo().DoUndo( bUndo );
454 if( bUndo )
455 {
456 GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
457 }
458
459 return true;
460}
461
463bool SwDoc::SortTable(const SwSelBoxes& rBoxes, const SwSortOptions& rOpt)
464{
465 // Via SwDoc for Undo!
466 OSL_ENSURE( !rBoxes.empty(), "no valid Box list" );
467 SwTableNode* pTableNd = const_cast<SwTableNode*>(rBoxes[0]->GetSttNd()->FindTableNode());
468 if( !pTableNd )
469 return false;
470
471 // We begin sorting
472 // Find all Boxes/Lines
473 FndBox_ aFndBox( nullptr, nullptr );
474 {
475 FndPara aPara( rBoxes, &aFndBox );
476 ForEach_FndLineCopyCol( pTableNd->GetTable().GetTabLines(), &aPara );
477 }
478
479 if(aFndBox.GetLines().empty())
480 return false;
481
482 if( !getIDocumentRedlineAccess().IsIgnoreRedline() && !getIDocumentRedlineAccess().GetRedlineTable().empty() )
483 getIDocumentRedlineAccess().DeleteRedline( *pTableNd, true, RedlineType::Any );
484
485 FndLines_t::size_type nStart = 0;
486 if( pTableNd->GetTable().GetRowsToRepeat() > 0 && rOpt.eDirection == SwSortDirection::Rows )
487 {
488 // Uppermost selected Cell
489 FndLines_t& rLines = aFndBox.GetLines();
490
491 while( nStart < rLines.size() )
492 {
493 // Respect Split Merge nesting,
494 // extract the upper most
495 SwTableLine* pLine = rLines[nStart]->GetLine();
496 while ( pLine->GetUpper() )
497 pLine = pLine->GetUpper()->GetUpper();
498
499 if( pTableNd->GetTable().IsHeadline( *pLine ) )
500 nStart++;
501 else
502 break;
503 }
504 // Are all selected in the HeaderLine? -> no Offset
505 if( nStart == rLines.size() )
506 nStart = 0;
507 }
508
510
511 // Table as a flat array structure
512 FlatFndBox aFlatBox(this, aFndBox);
513
514 if(!aFlatBox.IsSymmetric())
515 return false;
516
517 // Delete HTML layout
518 pTableNd->GetTable().SetHTMLTableLayout(std::shared_ptr<SwHTMLTableLayout>());
519
520 // #i37739# A simple 'MakeFrames' after the node sorting
521 // does not work if the table is inside a frame and has no prev/next.
522 SwNode2LayoutSaveUpperFrames aNode2Layout(*pTableNd);
523
524 // Delete the Table's Frames
525 pTableNd->DelFrames();
526 // ? TL_CHART2: ?
527
528 SwUndoSort* pUndoSort = nullptr;
529 if (GetIDocumentUndoRedo().DoesUndo())
530 {
531 pUndoSort = new SwUndoSort( rBoxes[0]->GetSttIdx(),
532 rBoxes.back()->GetSttIdx(),
533 *pTableNd, rOpt, aFlatBox.HasItemSets() );
534 GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndoSort));
535 }
536 ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
537
538 // Insert KeyElements
539 sal_uInt16 nCount = (rOpt.eDirection == SwSortDirection::Rows) ?
540 aFlatBox.GetRows() : aFlatBox.GetCols();
541
542 // Sort SortList by Key
543 SwSortElement::Init( this, rOpt, &aFlatBox );
544 std::multiset<SwSortBoxElement> aSortList;
545
546 // When sorting, do not include the first row if the HeaderLine is repeated
547 for( sal_uInt16 i = o3tl::narrowing<sal_uInt16>(nStart); i < nCount; ++i)
548 {
549 aSortList.insert(SwSortBoxElement(i));
550 }
551
552 // Move after Sorting
553 SwMovedBoxes aMovedList;
554 sal_uInt16 i = 0;
555 for (const auto& rElem : aSortList)
556 {
558 {
559 MoveRow(this, aFlatBox, rElem.nRow, i+nStart, aMovedList, pUndoSort);
560 }
561 else
562 {
563 MoveCol(this, aFlatBox, rElem.nRow, i+nStart, aMovedList, pUndoSort);
564 }
565 ++i;
566 }
567
568 // Restore table frames:
569 // #i37739# A simple 'MakeFrames' after the node sorting
570 // does not work if the table is inside a frame and has no prev/next.
571 const SwNodeOffset nIdx = pTableNd->GetIndex();
572 aNode2Layout.RestoreUpperFrames( GetNodes(), nIdx, nIdx + 1 );
573
574 // TL_CHART2: need to inform chart of probably changed cell names
575 UpdateCharts( pTableNd->GetTable().GetFrameFormat()->GetName() );
576
577 // Delete all Elements in the SortArray
578 aSortList.clear();
580
582 return true;
583}
584
586void MoveRow(SwDoc* pDoc, const FlatFndBox& rBox, sal_uInt16 nS, sal_uInt16 nT,
587 SwMovedBoxes& rMovedList, SwUndoSort* pUD)
588{
589 for( sal_uInt16 i=0; i < rBox.GetCols(); ++i )
590 { // Get old cell position and remember it
591 const FndBox_* pSource = rBox.GetBox(i, nS);
592
593 // new cell position
594 const FndBox_* pTarget = rBox.GetBox(i, nT);
595
596 const SwTableBox* pT = pTarget->GetBox();
597 const SwTableBox* pS = pSource->GetBox();
598
599 bool bMoved = rMovedList.GetPos(pT) != USHRT_MAX;
600
601 // and move it
602 MoveCell(pDoc, pS, pT, bMoved, pUD);
603
604 rMovedList.push_back(pS);
605
606 if( pS != pT )
607 {
608 SwFrameFormat* pTFormat = pT->GetFrameFormat();
609 const SfxItemSet* pSSet = rBox.GetItemSet( i, nS );
610
611 if( pSSet ||
612 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMAT ) ||
613 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMULA ) ||
614 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_VALUE ) )
615 {
616 pTFormat = const_cast<SwTableBox*>(pT)->ClaimFrameFormat();
617 pTFormat->LockModify();
619 pTFormat->ResetFormatAttr( RES_VERT_ORIENT );
620
621 if( pSSet )
622 pTFormat->SetFormatAttr( *pSSet );
623 pTFormat->UnlockModify();
624 }
625 }
626 }
627}
628
630void MoveCol(SwDoc* pDoc, const FlatFndBox& rBox, sal_uInt16 nS, sal_uInt16 nT,
631 SwMovedBoxes& rMovedList, SwUndoSort* pUD)
632{
633 for(sal_uInt16 i=0; i < rBox.GetRows(); ++i)
634 { // Get old cell position and remember it
635 const FndBox_* pSource = rBox.GetBox(nS, i);
636
637 // new cell position
638 const FndBox_* pTarget = rBox.GetBox(nT, i);
639
640 // and move it
641 const SwTableBox* pT = pTarget->GetBox();
642 const SwTableBox* pS = pSource->GetBox();
643
644 // and move it
645 bool bMoved = rMovedList.GetPos(pT) != USHRT_MAX;
646 MoveCell(pDoc, pS, pT, bMoved, pUD);
647
648 rMovedList.push_back(pS);
649
650 if( pS != pT )
651 {
652 SwFrameFormat* pTFormat = pT->GetFrameFormat();
653 const SfxItemSet* pSSet = rBox.GetItemSet( nS, i );
654
655 if( pSSet ||
656 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMAT ) ||
657 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMULA ) ||
658 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_VALUE ) )
659 {
660 pTFormat = const_cast<SwTableBox*>(pT)->ClaimFrameFormat();
661 pTFormat->LockModify();
663 pTFormat->ResetFormatAttr( RES_VERT_ORIENT );
664
665 if( pSSet )
666 pTFormat->SetFormatAttr( *pSSet );
667 pTFormat->UnlockModify();
668 }
669 }
670 }
671}
672
674void MoveCell(SwDoc* pDoc, const SwTableBox* pSource, const SwTableBox* pTar,
675 bool bMovedBefore, SwUndoSort* pUD)
676{
677 OSL_ENSURE(pSource && pTar,"Source or target missing");
678
679 if(pSource == pTar)
680 return;
681
682 if(pUD)
683 pUD->Insert( pSource->GetName(), pTar->GetName() );
684
685 // Set Pam source to the first ContentNode
686 SwNodeRange aRg( *pSource->GetSttNd(), SwNodeOffset(0), *pSource->GetSttNd() );
687 SwNode* pNd = pDoc->GetNodes().GoNext( &aRg.aStart );
688
689 // If the Cell (Source) wasn't moved
690 // -> insert an empty Node and move the rest or the Mark
691 // points to the first ContentNode
692 if( pNd->StartOfSectionNode() == pSource->GetSttNd() )
693 pNd = pDoc->GetNodes().MakeTextNode( aRg.aStart.GetNode(),
694 pDoc->GetDfltTextFormatColl() );
695 aRg.aEnd = *pNd->EndOfSectionNode();
696
697 // If the Target is empty (there is one empty Node)
698 // -> move and delete it
699 SwNodeIndex aTar( *pTar->GetSttNd() );
700 pNd = pDoc->GetNodes().GoNext( &aTar ); // next ContentNode
702
703 bool bDelFirst = false;
704 if( nCount == SwNodeOffset(2) )
705 {
706 OSL_ENSURE( pNd->GetContentNode(), "No ContentNode");
707 bDelFirst = !pNd->GetContentNode()->Len() && bMovedBefore;
708 }
709
710 if(!bDelFirst)
711 { // We already have Content -> old Content Section Down
712 SwNodeRange aRgTar( aTar.GetNode(), SwNodeOffset(0), *pNd->EndOfSectionNode() );
713 pDoc->GetNodes().SectionDown( &aRgTar );
714 }
715
716 // Insert the Source
717 SwNodeIndex aIns( *pTar->GetSttNd()->EndOfSectionNode() );
720
721 // If first Node is empty -> delete it
722 if(bDelFirst)
723 pDoc->GetNodes().Delete( aTar );
724}
725
727FlatFndBox::FlatFndBox(SwDoc* pDocPtr, const FndBox_& rBoxRef) :
728 m_pDoc(pDocPtr),
729 m_nRow(0),
730 m_nCol(0)
731{ // If the array is symmetric
732 m_bSym = CheckLineSymmetry(rBoxRef);
733 if( !m_bSym )
734 return;
735
736 // Determine column/row count
737 m_nCols = GetColCount(rBoxRef);
738 m_nRows = GetRowCount(rBoxRef);
739
740 // Create linear array
741 size_t nCount = static_cast<size_t>(m_nRows) * m_nCols;
742 m_pArr = std::make_unique<FndBox_ const *[]>(nCount);
743 memset(m_pArr.get(), 0, sizeof(const FndBox_*) * nCount);
744
745 FillFlat( rBoxRef );
746}
747
749{
750}
751
754{
755 const FndLines_t &rLines = rBox.GetLines();
756 FndBoxes_t::size_type nBoxes {0};
757
758 for (FndLines_t::size_type i=0; i < rLines.size(); ++i)
759 {
760 const FndLine_* pLn = rLines[i].get();
761 const FndBoxes_t& rBoxes = pLn->GetBoxes();
762
763 // Number of Boxes of all Lines is unequal -> no symmetry
764 if( i && nBoxes != rBoxes.size())
765 return false;
766
767 nBoxes = rBoxes.size();
768 if( !CheckBoxSymmetry( *pLn ) )
769 return false;
770 }
771 return true;
772}
773
776{
777 const FndBoxes_t &rBoxes = rLn.GetBoxes();
778 FndLines_t::size_type nLines {0};
779
780 for (FndBoxes_t::size_type i = 0; i < rBoxes.size(); ++i)
781 {
782 FndBox_ const*const pBox = rBoxes[i].get();
783 const FndLines_t& rLines = pBox->GetLines();
784
785 // Number of Lines of all Boxes is unequal -> no symmetry
786 if( i && nLines != rLines.size() )
787 return false;
788
789 nLines = rLines.size();
790 if( nLines && !CheckLineSymmetry( *pBox ) )
791 return false;
792 }
793 return true;
794}
795
797sal_uInt16 FlatFndBox::GetColCount(const FndBox_& rBox)
798{
799 const FndLines_t& rLines = rBox.GetLines();
800 // Iterate over Lines
801 if( rLines.empty() )
802 return 1;
803
804 sal_uInt16 nSum = 0;
805 for (const auto & pLine : rLines)
806 {
807 // The Boxes of a Line
808 sal_uInt16 nCount = 0;
809 const FndBoxes_t& rBoxes = pLine->GetBoxes();
810 for (const auto &rpB : rBoxes)
811 { // Iterate recursively over the Lines
812 nCount += rpB->GetLines().empty() ? 1 : GetColCount(*rpB);
813 }
814
815 if( nSum < nCount )
816 nSum = nCount;
817 }
818 return nSum;
819}
820
822sal_uInt16 FlatFndBox::GetRowCount(const FndBox_& rBox)
823{
824 const FndLines_t& rLines = rBox.GetLines();
825 if( rLines.empty() )
826 return 1;
827
828 sal_uInt16 nLines = 0;
829 for (const auto & pLine : rLines)
830 { // The Boxes of a Line
831 const FndBoxes_t& rBoxes = pLine->GetBoxes();
832 sal_uInt16 nLn = 1;
833 for (const auto &rpB : rBoxes)
834 {
835 if (!rpB->GetLines().empty())
836 { // Iterate recursively over the Lines
837 nLn = std::max(GetRowCount(*rpB), nLn);
838 }
839 }
840
841 nLines = nLines + nLn;
842 }
843 return nLines;
844}
845
847void FlatFndBox::FillFlat(const FndBox_& rBox, bool bLastBox)
848{
849 bool bModRow = false;
850 const FndLines_t& rLines = rBox.GetLines();
851
852 // Iterate over Lines
853 sal_uInt16 nOldRow = m_nRow;
854 for (const auto & pLine : rLines)
855 {
856 // The Boxes of a Line
857 const FndBoxes_t& rBoxes = pLine->GetBoxes();
858 sal_uInt16 nOldCol = m_nCol;
859 for( FndBoxes_t::size_type j = 0; j < rBoxes.size(); ++j )
860 {
861 // Check the Box if it's an atomic one
862 const FndBox_ *const pBox = rBoxes[j].get();
863
864 if( pBox->GetLines().empty() )
865 {
866 // save it
867 sal_uInt16 nOff = m_nRow * m_nCols + m_nCol;
868 m_pArr[nOff] = pBox;
869
870 // Save the Formula/Format/Value values
871 const SwFrameFormat* pFormat = pBox->GetBox()->GetFrameFormat();
872 if( SfxItemState::SET == pFormat->GetItemState( RES_BOXATR_FORMAT ) ||
873 SfxItemState::SET == pFormat->GetItemState( RES_BOXATR_FORMULA ) ||
874 SfxItemState::SET == pFormat->GetItemState( RES_BOXATR_VALUE ) )
875 {
879 aSet(m_pDoc->GetAttrPool());
880 aSet.Put( pFormat->GetAttrSet() );
881 if( m_vItemSets.empty() )
882 {
883 size_t nCount = static_cast<size_t>(m_nRows) * m_nCols;
884 m_vItemSets.resize(nCount);
885 }
886 m_vItemSets[nOff].emplace(std::move(aSet));
887 }
888
889 bModRow = true;
890 }
891 else
892 {
893 // Iterate recursively over the Lines of a Box
894 FillFlat( *pBox, ( j+1 == rBoxes.size() ) );
895 }
896 m_nCol++;
897 }
898 if(bModRow)
899 m_nRow++;
900 m_nCol = nOldCol;
901 }
902 if(!bLastBox)
903 m_nRow = nOldRow;
904}
905
907const FndBox_* FlatFndBox::GetBox(sal_uInt16 n_Col, sal_uInt16 n_Row) const
908{
909 sal_uInt16 nOff = n_Row * m_nCols + n_Col;
910 const FndBox_* pTmp = m_pArr[nOff];
911
912 OSL_ENSURE(n_Col < m_nCols && n_Row < m_nRows && pTmp, "invalid array access");
913 return pTmp;
914}
915
916const SfxItemSet* FlatFndBox::GetItemSet(sal_uInt16 n_Col, sal_uInt16 n_Row) const
917{
918 OSL_ENSURE( m_vItemSets.empty() || ( n_Col < m_nCols && n_Row < m_nRows), "invalid array access");
919
920 if (m_vItemSets.empty()) {
921 return nullptr;
922 }
923 auto const & el = m_vItemSets[unsigned(n_Row * m_nCols) + n_Col];
924 return el ? &*el : nullptr;
925}
926
927sal_uInt16 SwMovedBoxes::GetPos(const SwTableBox* pTableBox) const
928{
929 std::vector<const SwTableBox*>::const_iterator it = std::find(mBoxes.begin(), mBoxes.end(), pTableBox);
930 return it == mBoxes.end() ? USHRT_MAX : it - mBoxes.begin();
931}
932
933/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt32 GetValue() const
sal_Int32 compareString(const OUString &s1, const OUString &s2) const
void loadCollatorAlgorithm(const OUString &rAlgorithm, const css::lang::Locale &rLocale, sal_Int32 nOption)
FlatFndBox(SwDoc *pDocPtr, const FndBox_ &rBox)
Generate two-dimensional array of FndBoxes.
Definition: docsort.cxx:727
sal_uInt16 GetColCount(const FndBox_ &rBox)
Maximum count of Columns (Boxes)
Definition: docsort.cxx:797
bool IsSymmetric() const
Definition: docsort.hxx:119
sal_uInt16 m_nCols
Definition: docsort.hxx:141
sal_uInt16 m_nRow
Definition: docsort.hxx:142
std::unique_ptr< FndBox_ const *[]> m_pArr
Definition: docsort.hxx:136
SwDoc * m_pDoc
Definition: docsort.hxx:135
bool CheckLineSymmetry(const FndBox_ &rBox)
All Lines of a Box need to have same number of Boxes.
Definition: docsort.cxx:753
const SfxItemSet * GetItemSet(sal_uInt16 nCol, sal_uInt16 nRow) const
Definition: docsort.cxx:916
sal_uInt16 GetRowCount(const FndBox_ &rBox)
Maximum count of Rows (Lines)
Definition: docsort.cxx:822
sal_uInt16 m_nCol
Definition: docsort.hxx:143
bool HasItemSets() const
Definition: docsort.hxx:148
bool m_bSym
Definition: docsort.hxx:145
void FillFlat(const FndBox_ &, bool bLastBox=false)
Create a linear array of atomic FndBoxes.
Definition: docsort.cxx:847
sal_uInt16 GetCols() const
Definition: docsort.hxx:121
sal_uInt16 GetRows() const
Definition: docsort.hxx:120
const FndBox_ * GetBox(sal_uInt16 nCol, sal_uInt16 nRow) const
Access a specific Cell.
Definition: docsort.cxx:907
std::vector< std::optional< SfxItemSet > > m_vItemSets
using optional because SfxItemSet has no default constructor
Definition: docsort.hxx:138
bool CheckBoxSymmetry(const FndLine_ &rLn)
Check Box for symmetry (All Boxes of a Line need to have same number of Lines)
Definition: docsort.cxx:775
sal_uInt16 m_nRows
Definition: docsort.hxx:140
const FndLines_t & GetLines() const
Definition: tblsel.hxx:172
const SwTableBox * GetBox() const
Definition: tblsel.hxx:174
const FndBoxes_t & GetBoxes() const
Definition: tblsel.hxx:202
virtual bool MoveNodeRange(SwNodeRange &, SwNode &, SwMoveFlags)=0
virtual bool DeleteRedline(const SwPaM &rPam, bool bSaveInUndo, RedlineType nDelType)=0
static bool IsShowOriginal(const RedlineFlags eM)
virtual AppendResult AppendRedline(SwRangeRedline *pNewRedl, bool bCallDelete)=0
Append a new redline.
virtual void SetModified()=0
Must be called manually at changes of format.
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
double stringToDouble(std::u16string_view aString, bool bUseGroupSep, rtl_math_ConversionStatus *pStatus, sal_Int32 *pParseEnd) const
bool IsTextFormat(sal_uInt32 nFIndex) const
virtual sal_Int32 Len() const
Definition: node.cxx:1256
Definition: doc.hxx:197
bool SortTable(const SwSelBoxes &rBoxes, const SwSortOptions &)
Sort Table in the Document.
Definition: docsort.cxx:463
void UpdateCharts(const OUString &rName) const
Definition: docchart.cxx:139
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:408
bool SortText(const SwPaM &, const SwSortOptions &)
Sort Text in the Document.
Definition: docsort.cxx:283
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:158
SwNodes & GetNodes()
Definition: doc.hxx:422
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:349
const SwTextFormatColl * GetDfltTextFormatColl() const
Definition: doc.hxx:791
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1337
const sw::FrameFormats< sw::SpzFrameFormat * > * GetSpzFrameFormats() const
Definition: doc.hxx:759
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1429
FlyAnchors.
Definition: fmtanchr.hxx:37
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:67
SwNode * GetAnchorNode() const
Definition: atrfrm.cxx:1614
Base class for various Writer styles.
Definition: format.hxx:47
const SwTableBoxNumFormat & GetTableBoxNumFormat(bool=true) const
TableBox attributes - implemented in cellatr.hxx.
Definition: cellatr.hxx:122
virtual bool ResetFormatAttr(sal_uInt16 nWhich1, sal_uInt16 nWhich2=0)
Definition: format.cxx:618
const OUString & GetName() const
Definition: format.hxx:131
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
Definition: format.cxx:385
const SwTableBoxValue & GetTableBoxValue(bool=true) const
Definition: cellatr.hxx:126
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:136
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:447
Style of a layout element.
Definition: frmfmt.hxx:72
sal_uInt16 GetPos(const SwTableBox *pTableBox) const
Definition: docsort.cxx:927
std::vector< const SwTableBox * > mBoxes
Definition: docsort.hxx:42
void push_back(const SwTableBox *&rpTableBox)
Definition: docsort.hxx:45
void RestoreUpperFrames(SwNodes &rNds, SwNodeOffset nStt, SwNodeOffset nEnd)
Definition: node2lay.cxx:489
Marks a node in the document model.
Definition: ndindex.hxx:31
SwNode & GetNode() const
Definition: ndindex.hxx:123
SwNodeOffset GetIndex() const
Definition: ndindex.hxx:111
SwNodeIndex aStart
Definition: ndindex.hxx:136
SwNodeIndex aEnd
Definition: ndindex.hxx:137
Base class of the Writer document model elements.
Definition: node.hxx:98
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:901
SwNodeOffset GetIndex() const
Definition: node.hxx:312
SwNodeOffset StartOfSectionIndex() const
Definition: node.hxx:687
bool IsTextNode() const
Definition: node.hxx:190
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:153
SwNodeOffset EndOfSectionIndex() const
Definition: node.hxx:691
SwContentNode * GetContentNode()
Definition: node.hxx:666
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:695
SwTextNode * MakeTextNode(SwNode &rWhere, SwTextFormatColl *pColl, bool bNewFrames=true)
Implementations of "Make...Node" are in the given .cxx-files.
Definition: ndtxt.cxx:121
void Delete(const SwNodeIndex &rPos, SwNodeOffset nNodes=SwNodeOffset(1))
Definition: nodes.cxx:1070
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1299
void SectionDown(SwNodeRange *pRange, SwStartNodeType=SwNormalStartNode)
create a start/end section pair
Definition: nodes.cxx:906
void Copy_(const SwNodeRange &rRg, SwNode &rInsPos, bool bNewFrames=true) const
Definition: ndarr.hxx:179
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
const SwPosition * GetMark() const
Definition: pam.hxx:255
std::pair< const SwPosition *, const SwPosition * > StartEnd() const
Because sometimes the cost of the operator<= can add up.
Definition: pam.hxx:269
SwContentNode * GetPointContentNode() const
Definition: pam.hxx:279
SwContentNode * GetMarkContentNode() const
Definition: pam.hxx:280
const SwPosition * GetPoint() const
Definition: pam.hxx:253
double GetValue() const
Definition: cellatr.hxx:112
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:443
SwTableLine * GetUpper()
Definition: swtable.hxx:477
SwNodeOffset GetSttIdx() const
Definition: swtable.cxx:2242
OUString GetName() const
Definition: swtable.cxx:2189
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:481
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:495
SwTableLine is one table row in the document model.
Definition: swtable.hxx:376
SwTableBox * GetUpper()
Definition: swtable.hxx:394
const SwTable & GetTable() const
Definition: node.hxx:542
void DelFrames(SwRootFrame const *pLayout=nullptr)
Method deletes all views of document for the node.
Definition: ndtbl.cxx:2461
void SetHTMLTableLayout(std::shared_ptr< SwHTMLTableLayout > const &r)
Definition: swtable.cxx:2330
SwTableLines & GetTabLines()
Definition: swtable.hxx:206
bool IsHeadline(const SwTableLine &rLine) const
Definition: tabfrm.cxx:6009
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:209
void SwitchFormulasToRelativeRepresentation()
Definition: swtable.hxx:364
sal_uInt16 GetRowsToRepeat() const
Definition: swtable.hxx:201
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
const OUString & GetText() const
Definition: ndtxt.hxx:244
void SetValues(const SwPaM &rPam)
Definition: undobj.cxx:60
void SetSaveRange(const SwPaM &rRange)
Definition: unredln.cxx:413
void Insert(const OUString &rOrgPos, const OUString &rNewPos)
Definition: unsort.cxx:226
const Value & back() const
bool empty() const
int nCount
const char * pS
void MoveCol(SwDoc *pDoc, const FlatFndBox &rBox, sal_uInt16 nS, sal_uInt16 nT, SwMovedBoxes &rMovedList, SwUndoSort *pUD)
Move a column.
Definition: docsort.cxx:630
void MoveRow(SwDoc *pDoc, const FlatFndBox &rBox, sal_uInt16 nS, sal_uInt16 nT, SwMovedBoxes &rMovedList, SwUndoSort *pUD)
Move a row.
Definition: docsort.cxx:586
void MoveCell(SwDoc *pDoc, const SwTableBox *pSource, const SwTableBox *pTar, bool bMovedBefore, SwUndoSort *pUD)
Move a single Cell.
Definition: docsort.cxx:674
constexpr TypedWhichId< SwTableBoxValue > RES_BOXATR_VALUE(158)
constexpr TypedWhichId< SwFormatVertOrient > RES_VERT_ORIENT(108)
constexpr TypedWhichId< SwTableBoxFormula > RES_BOXATR_FORMULA(157)
constexpr TypedWhichId< SwTableBoxNumFormat > RES_BOXATR_FORMAT(RES_BOXATR_BEGIN)
LanguageType GetAppLanguage()
Definition: init.cxx:741
sal_Int64 n
#define LANGUAGE_NONE
#define LANGUAGE_DONTKNOW
int n2
int n1
int i
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
sal_Int16 nId
SwContentNode * GetNode(SwPaM &rPam, bool &rbFirst, SwMoveFnCollection const &fnMove, bool const bInReadOnly, SwRootFrame const *const i_pLayout)
This function returns the next node in direction of search.
Definition: pam.cxx:1043
static LanguageType nLang
Definition: srtdlg.cxx:51
SwNode & GetNode() const
Definition: pam.hxx:81
void Assign(const SwNode &rNd, SwNodeOffset nDelta, sal_Int32 nContentOffset=0)
These all set both nNode and nContent.
Definition: pam.cxx:231
void SetContent(sal_Int32 nContentIndex)
Set content index, only valid to call this if the position points to a SwContentNode subclass.
Definition: pam.cxx:267
SwSortBoxElement(sal_uInt16 nRC)
SortingElement for Tables.
Definition: docsort.cxx:219
sal_uInt16 nRow
Definition: docsort.hxx:104
virtual double GetValue(sal_uInt16 nKey) const override
Definition: docsort.cxx:257
virtual OUString GetKey(sal_uInt16 nKey) const override
Get Key for a cell.
Definition: docsort.cxx:225
static double StrToDouble(std::u16string_view rStr)
Definition: docsort.cxx:104
static void Finit()
Definition: docsort.cxx:85
static SwDoc * pDoc
Definition: docsort.hxx:62
static std::optional< OUString > xLastAlgorithm
Definition: docsort.hxx:66
bool operator<(const SwSortElement &) const
Definition: docsort.cxx:159
static SwSortOptions * pOptions
Definition: docsort.hxx:61
static css::lang::Locale * pLocale
Definition: docsort.hxx:65
static const FlatFndBox * pBox
Definition: docsort.hxx:63
int keycompare(const SwSortElement &rCmp, sal_uInt16 nKey) const
Definition: docsort.cxx:118
static void Init(SwDoc *, const SwSortOptions &rOpt, FlatFndBox const *=nullptr)
Construct a SortElement for the Sort.
Definition: docsort.cxx:67
virtual double GetValue(sal_uInt16 nKey) const
Definition: docsort.cxx:175
static CollatorWrapper * pSortCollator
Definition: docsort.hxx:64
virtual OUString GetKey(sal_uInt16 nKey) const =0
virtual ~SwSortElement()
Definition: docsort.cxx:100
static LocaleDataWrapper * pLclData
Definition: docsort.hxx:67
bool bIsNumeric
Definition: sortopt.hxx:38
OUString sSortType
Definition: sortopt.hxx:35
SwSortOrder eSortOrder
Definition: sortopt.hxx:36
std::vector< SwSortKey > aKeys
Definition: sortopt.hxx:49
sal_Unicode cDeli
Definition: sortopt.hxx:51
LanguageType nLanguage
Definition: sortopt.hxx:52
bool bIgnoreCase
Definition: sortopt.hxx:54
SwSortDirection eDirection
Definition: sortopt.hxx:50
virtual OUString GetKey(sal_uInt16 nKey) const override
Definition: docsort.cxx:186
SwNodeIndex aPos
Definition: docsort.hxx:94
SwSortTextElement(const SwNodeIndex &rPos)
SortingElement for Text.
Definition: docsort.cxx:181
bool anyOf(strong_int v) const
#define SW_COLLATOR_IGNORES
Definition: swtypes.hxx:194
void ForEach_FndLineCopyCol(SwTableLines &rLines, FndPara *pFndPara)
This creates a structure mirroring the SwTable structure that contains all rows and non-leaf boxes (a...
Definition: tblsel.cxx:2090
std::vector< std::unique_ptr< FndLine_ > > FndLines_t
Definition: tblsel.hxx:155
std::vector< std::unique_ptr< FndBox_ > > FndBoxes_t
Definition: tblsel.hxx:152
sal_uInt16 sal_Unicode