Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / view / dbfunc3.cxx
blob76d6cc2b6b7b988fe1aad3bb39186d6ee1ec80a3
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 "dbfunc.hxx"
21 #include "scitems.hxx"
22 #include <sfx2/bindings.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/msgbox.hxx>
25 #include <vcl/waitobj.hxx>
26 #include <svl/zforlist.hxx>
27 #include <sfx2/app.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
31 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
32 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
33 #include <com/sun/star/sheet/DataPilotFieldSortMode.hpp>
34 #include <com/sun/star/sheet/DataPilotTableHeaderData.hpp>
35 #include <com/sun/star/sheet/GeneralFunction.hpp>
36 #include <com/sun/star/sheet/MemberResultFlags.hpp>
37 #include <com/sun/star/sheet/XDimensionsSupplier.hpp>
38 #include <com/sun/star/sheet/XDrillDownDataSupplier.hpp>
40 #include "global.hxx"
41 #include "globstr.hrc"
42 #include "sc.hrc"
43 #include "undotab.hxx"
44 #include "undodat.hxx"
45 #include "dbdata.hxx"
46 #include "rangenam.hxx"
47 #include "rangeutl.hxx"
48 #include "docsh.hxx"
49 #include "olinetab.hxx"
50 #include "consoli.hxx"
51 #include "olinefun.hxx"
52 #include "dpobject.hxx"
53 #include "dpsave.hxx"
54 #include "dpdimsave.hxx"
55 #include "dbdocfun.hxx"
56 #include "dpoutput.hxx"
57 #include "dptabsrc.hxx"
58 #include "dpshttab.hxx"
59 #include "dpsdbtab.hxx"
60 #include "editable.hxx"
61 #include "docpool.hxx"
62 #include "patattr.hxx"
63 #include "unonames.hxx"
64 #include "formulacell.hxx"
65 #include "userlist.hxx"
66 #include "queryentry.hxx"
67 #include "markdata.hxx"
68 #include "stringutil.hxx"
70 #include <boost/scoped_ptr.hpp>
71 #include <boost/unordered_set.hpp>
72 #include <boost/unordered_map.hpp>
73 #include <list>
74 #include <vector>
76 using namespace com::sun::star;
77 using ::com::sun::star::uno::Any;
78 using ::com::sun::star::uno::Sequence;
79 using ::com::sun::star::uno::Reference;
80 using ::com::sun::star::uno::UNO_QUERY;
81 using ::com::sun::star::beans::XPropertySet;
82 using ::com::sun::star::container::XNameAccess;
83 using ::com::sun::star::sheet::XDimensionsSupplier;
84 using ::std::list;
85 using ::std::vector;
87 // STATIC DATA -----------------------------------------------------------
91 // Outliner
94 // Outline-Gruppierung erzeugen
96 void ScDBFunc::MakeOutline( bool bColumns, bool bRecord )
98 ScRange aRange;
99 if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
101 ScDocShell* pDocSh = GetViewData()->GetDocShell();
102 ScOutlineDocFunc aFunc(*pDocSh);
103 aFunc.MakeOutline( aRange, bColumns, bRecord, false );
105 else
106 ErrorMessage(STR_NOMULTISELECT);
109 // Outline-Gruppierung loeschen
111 void ScDBFunc::RemoveOutline( bool bColumns, bool bRecord )
113 ScRange aRange;
114 if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
116 ScDocShell* pDocSh = GetViewData()->GetDocShell();
117 ScOutlineDocFunc aFunc(*pDocSh);
118 aFunc.RemoveOutline( aRange, bColumns, bRecord, false );
120 else
121 ErrorMessage(STR_NOMULTISELECT);
124 // Menue-Status: Outlines loeschen
126 void ScDBFunc::TestRemoveOutline( bool& rCol, bool& rRow )
128 bool bColFound = false;
129 bool bRowFound = false;
131 SCCOL nStartCol, nEndCol;
132 SCROW nStartRow, nEndRow;
133 SCTAB nStartTab, nEndTab;
134 if (GetViewData()->GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
136 SCTAB nTab = nStartTab;
137 ScDocument* pDoc = GetViewData()->GetDocument();
138 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
139 if (pTable)
141 ScOutlineArray* pArray;
142 ScOutlineEntry* pEntry;
143 SCCOLROW nStart;
144 SCCOLROW nEnd;
145 bool bColMarked = ( nStartRow == 0 && nEndRow == MAXROW );
146 bool bRowMarked = ( nStartCol == 0 && nEndCol == MAXCOL );
148 // Spalten
150 if ( !bRowMarked || bColMarked ) // nicht wenn ganze Zeilen markiert
152 pArray = pTable->GetColArray();
153 ScSubOutlineIterator aColIter( pArray );
154 while ((pEntry=aColIter.GetNext()) != NULL && !bColFound)
156 nStart = pEntry->GetStart();
157 nEnd = pEntry->GetEnd();
158 if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
159 bColFound = true;
163 // Zeilen
165 if ( !bColMarked || bRowMarked ) // nicht wenn ganze Spalten markiert
167 pArray = pTable->GetRowArray();
168 ScSubOutlineIterator aRowIter( pArray );
169 while ((pEntry=aRowIter.GetNext()) != NULL && !bRowFound)
171 nStart = pEntry->GetStart();
172 nEnd = pEntry->GetEnd();
173 if ( nStartRow<=nEnd && nEndRow>=nStart )
174 bRowFound = true;
180 rCol = bColFound;
181 rRow = bRowFound;
184 void ScDBFunc::RemoveAllOutlines( bool bRecord )
186 SCTAB nTab = GetViewData()->GetTabNo();
187 ScDocShell* pDocSh = GetViewData()->GetDocShell();
188 ScOutlineDocFunc aFunc(*pDocSh);
190 bool bOk = aFunc.RemoveAllOutlines( nTab, bRecord );
192 if (bOk)
193 UpdateScrollBars();
196 // Auto-Outlines
198 void ScDBFunc::AutoOutline( bool bRecord )
200 SCTAB nTab = GetViewData()->GetTabNo();
201 ScRange aRange( 0,0,nTab, MAXCOL,MAXROW,nTab ); // ganze Tabelle, wenn nichts markiert
202 ScMarkData& rMark = GetViewData()->GetMarkData();
203 if ( rMark.IsMarked() || rMark.IsMultiMarked() )
205 rMark.MarkToMulti();
206 rMark.GetMultiMarkArea( aRange );
209 ScDocShell* pDocSh = GetViewData()->GetDocShell();
210 ScOutlineDocFunc aFunc(*pDocSh);
211 aFunc.AutoOutline( aRange, bRecord );
214 // Outline-Ebene auswaehlen
216 void ScDBFunc::SelectLevel( bool bColumns, sal_uInt16 nLevel, bool bRecord, bool bPaint )
218 SCTAB nTab = GetViewData()->GetTabNo();
219 ScDocShell* pDocSh = GetViewData()->GetDocShell();
220 ScOutlineDocFunc aFunc(*pDocSh);
222 bool bOk = aFunc.SelectLevel( nTab, bColumns, nLevel, bRecord, bPaint );
224 if (bOk)
225 UpdateScrollBars();
228 // einzelne Outline-Gruppe einblenden
230 void ScDBFunc::ShowOutline( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bRecord, bool bPaint )
232 SCTAB nTab = GetViewData()->GetTabNo();
233 ScDocShell* pDocSh = GetViewData()->GetDocShell();
234 ScOutlineDocFunc aFunc(*pDocSh);
236 bool bOk = aFunc.ShowOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint );
238 if ( bOk && bPaint )
239 UpdateScrollBars();
242 // einzelne Outline-Gruppe ausblenden
244 void ScDBFunc::HideOutline( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bRecord, bool bPaint )
246 SCTAB nTab = GetViewData()->GetTabNo();
247 ScDocShell* pDocSh = GetViewData()->GetDocShell();
248 ScOutlineDocFunc aFunc(*pDocSh);
250 bool bOk = aFunc.HideOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint );
252 if ( bOk && bPaint )
253 UpdateScrollBars();
256 // Menue-Status: markierten Bereich ein-/ausblenden
258 bool ScDBFunc::OutlinePossible(bool bHide)
260 bool bEnable = false;
262 SCCOL nStartCol;
263 SCROW nStartRow;
264 SCTAB nStartTab;
265 SCCOL nEndCol;
266 SCROW nEndRow;
267 SCTAB nEndTab;
269 if (GetViewData()->GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
271 ScDocument* pDoc = GetViewData()->GetDocument();
272 SCTAB nTab = GetViewData()->GetTabNo();
273 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
274 if (pTable)
276 ScOutlineArray* pArray;
277 ScOutlineEntry* pEntry;
278 SCCOLROW nStart;
279 SCCOLROW nEnd;
281 // Spalten
283 pArray = pTable->GetColArray();
284 ScSubOutlineIterator aColIter( pArray );
285 while ((pEntry=aColIter.GetNext()) != NULL && !bEnable)
287 nStart = pEntry->GetStart();
288 nEnd = pEntry->GetEnd();
289 if ( bHide )
291 if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
292 if (!pEntry->IsHidden())
293 bEnable = true;
295 else
297 if ( nStart>=nStartCol && nEnd<=nEndCol )
298 if (pEntry->IsHidden())
299 bEnable = true;
303 // Zeilen
305 pArray = pTable->GetRowArray();
306 ScSubOutlineIterator aRowIter( pArray );
307 while ((pEntry=aRowIter.GetNext()) != NULL)
309 nStart = pEntry->GetStart();
310 nEnd = pEntry->GetEnd();
311 if ( bHide )
313 if ( nStartRow<=nEnd && nEndRow>=nStart )
314 if (!pEntry->IsHidden())
315 bEnable = true;
317 else
319 if ( nStart>=nStartRow && nEnd<=nEndRow )
320 if (pEntry->IsHidden())
321 bEnable = true;
327 return bEnable;
330 // markierten Bereich einblenden
332 void ScDBFunc::ShowMarkedOutlines( bool bRecord )
334 ScRange aRange;
335 if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
337 ScDocShell* pDocSh = GetViewData()->GetDocShell();
338 ScOutlineDocFunc aFunc(*pDocSh);
339 bool bDone = aFunc.ShowMarkedOutlines( aRange, bRecord );
340 if (bDone)
341 UpdateScrollBars();
343 else
344 ErrorMessage(STR_NOMULTISELECT);
347 // markierten Bereich ausblenden
349 void ScDBFunc::HideMarkedOutlines( bool bRecord )
351 ScRange aRange;
352 if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
354 ScDocShell* pDocSh = GetViewData()->GetDocShell();
355 ScOutlineDocFunc aFunc(*pDocSh);
356 bool bDone = aFunc.HideMarkedOutlines( aRange, bRecord );
357 if (bDone)
358 UpdateScrollBars();
360 else
361 ErrorMessage(STR_NOMULTISELECT);
365 // Teilergebnisse
368 void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, bool bRecord,
369 const ScSortParam* pForceNewSort )
371 bool bDo = !rParam.bRemoveOnly; // sal_False = nur loeschen
373 ScDocShell* pDocSh = GetViewData()->GetDocShell();
374 ScDocument* pDoc = pDocSh->GetDocument();
375 ScMarkData& rMark = GetViewData()->GetMarkData();
376 SCTAB nTab = GetViewData()->GetTabNo();
377 if (bRecord && !pDoc->IsUndoEnabled())
378 bRecord = false;
380 ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
381 rParam.nCol2, rParam.nRow2 );
382 if (!pDBData)
384 OSL_FAIL( "SubTotals: keine DBData" );
385 return;
388 ScEditableTester aTester( pDoc, nTab, 0,rParam.nRow1+1, MAXCOL,MAXROW );
389 if (!aTester.IsEditable())
391 ErrorMessage(aTester.GetMessageId());
392 return;
395 if (pDoc->HasAttrib( rParam.nCol1, rParam.nRow1+1, nTab,
396 rParam.nCol2, rParam.nRow2, nTab, HASATTR_MERGED | HASATTR_OVERLAPPED ))
398 ErrorMessage(STR_MSSG_INSERTCELLS_0); // nicht in zusammengefasste einfuegen
399 return;
402 WaitObject aWait( GetViewData()->GetDialogParent() );
403 bool bOk = true;
404 if (rParam.bReplace)
405 if (pDoc->TestRemoveSubTotals( nTab, rParam ))
407 bOk = ( MessBox( GetViewData()->GetDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
408 // "StarCalc" "Daten loeschen?"
409 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
410 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_1 ) ).Execute()
411 == RET_YES );
414 if (bOk)
416 ScDocShellModificator aModificator( *pDocSh );
418 ScSubTotalParam aNewParam( rParam ); // Bereichsende wird veraendert
419 ScDocument* pUndoDoc = NULL;
420 ScOutlineTable* pUndoTab = NULL;
421 ScRangeName* pUndoRange = NULL;
422 ScDBCollection* pUndoDB = NULL;
424 if (bRecord) // alte Daten sichern
426 bool bOldFilter = bDo && rParam.bDoSort;
427 SCTAB nTabCount = pDoc->GetTableCount();
428 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
429 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
430 if (pTable)
432 pUndoTab = new ScOutlineTable( *pTable );
434 SCCOLROW nOutStartCol; // Zeilen/Spaltenstatus
435 SCCOLROW nOutStartRow;
436 SCCOLROW nOutEndCol;
437 SCCOLROW nOutEndRow;
438 pTable->GetColArray()->GetRange( nOutStartCol, nOutEndCol );
439 pTable->GetRowArray()->GetRange( nOutStartRow, nOutEndRow );
441 pUndoDoc->InitUndo( pDoc, nTab, nTab, true, true );
442 pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, false, pUndoDoc );
443 pDoc->CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, false, pUndoDoc );
445 else
446 pUndoDoc->InitUndo( pDoc, nTab, nTab, false, bOldFilter );
448 // Datenbereich sichern - incl. Filter-Ergebnis
449 pDoc->CopyToDocument( 0,rParam.nRow1+1,nTab, MAXCOL,rParam.nRow2,nTab,
450 IDF_ALL, false, pUndoDoc );
452 // alle Formeln wegen Referenzen
453 pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1,
454 IDF_FORMULA, false, pUndoDoc );
456 // DB- und andere Bereiche
457 ScRangeName* pDocRange = pDoc->GetRangeName();
458 if (!pDocRange->empty())
459 pUndoRange = new ScRangeName( *pDocRange );
460 ScDBCollection* pDocDB = pDoc->GetDBCollection();
461 if (!pDocDB->empty())
462 pUndoDB = new ScDBCollection( *pDocDB );
465 ScOutlineTable* pOut = pDoc->GetOutlineTable( nTab );
466 if (pOut)
468 // Remove all existing outlines in the specified range.
469 ScOutlineArray* pRowArray = pOut->GetRowArray();
470 sal_uInt16 nDepth = pRowArray->GetDepth();
471 for (sal_uInt16 i = 0; i < nDepth; ++i)
473 bool bSize;
474 pRowArray->Remove(aNewParam.nRow1, aNewParam.nRow2, bSize);
478 if (rParam.bReplace)
479 pDoc->RemoveSubTotals( nTab, aNewParam );
480 bool bSuccess = true;
481 if (bDo)
483 // Sortieren
484 if ( rParam.bDoSort || pForceNewSort )
486 pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
488 // Teilergebnis-Felder vor die Sortierung setzen
489 // (doppelte werden weggelassen, kann darum auch wieder aufgerufen werden)
491 ScSortParam aOldSort;
492 pDBData->GetSortParam( aOldSort );
493 ScSortParam aSortParam( aNewParam, pForceNewSort ? *pForceNewSort : aOldSort );
494 Sort( aSortParam, false, false );
497 bSuccess = pDoc->DoSubTotals( nTab, aNewParam );
499 ScRange aDirtyRange( aNewParam.nCol1, aNewParam.nRow1, nTab,
500 aNewParam.nCol2, aNewParam.nRow2, nTab );
501 pDoc->SetDirty( aDirtyRange );
503 if (bRecord)
505 pDocSh->GetUndoManager()->AddUndoAction(
506 new ScUndoSubTotals( pDocSh, nTab,
507 rParam, aNewParam.nRow2,
508 pUndoDoc, pUndoTab, // pUndoDBData,
509 pUndoRange, pUndoDB ) );
512 if (!bSuccess)
514 // "Kann keine Zeilen einfuegen"
515 ErrorMessage(STR_MSSG_DOSUBTOTALS_2);
518 // merken
519 pDBData->SetSubTotalParam( aNewParam );
520 pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
521 pDoc->CompileDBFormula();
523 DoneBlockMode();
524 InitOwnBlockMode();
525 rMark.SetMarkArea( ScRange( aNewParam.nCol1,aNewParam.nRow1,nTab,
526 aNewParam.nCol2,aNewParam.nRow2,nTab ) );
527 MarkDataChanged();
529 pDocSh->PostPaint(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab),
530 PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE);
532 aModificator.SetDocumentModified();
534 SelectionChanged();
539 // Consolidate
542 void ScDBFunc::Consolidate( const ScConsolidateParam& rParam, bool bRecord )
544 ScDocShell* pDocShell = GetViewData()->GetDocShell();
545 pDocShell->DoConsolidate( rParam, bRecord );
546 SetTabNo( rParam.nTab, true );
550 // Pivot
553 static OUString lcl_MakePivotTabName( const OUString& rPrefix, SCTAB nNumber )
555 OUString aName = rPrefix + OUString::number( nNumber );
556 return aName;
559 bool ScDBFunc::MakePivotTable(
560 const ScDPSaveData& rData, const ScRange& rDest, bool bNewTable,
561 const ScDPObject& rSource, bool bApi )
563 // error message if no fields are set
564 // this must be removed when drag&drop of fields from a toolbox is available
566 if ( rData.IsEmpty() && !bApi )
568 ErrorMessage(STR_PIVOT_NODATA);
569 return false;
572 ScDocShell* pDocSh = GetViewData()->GetDocShell();
573 ScDocument* pDoc = GetViewData()->GetDocument();
574 bool bUndo = pDoc->IsUndoEnabled();
576 ScRange aDestRange = rDest;
577 if ( bNewTable )
579 SCTAB nSrcTab = GetViewData()->GetTabNo();
581 OUString aName( ScGlobal::GetRscString(STR_PIVOT_TABLE) );
582 OUString aStr;
584 pDoc->GetName( nSrcTab, aStr );
585 aName += "_";
586 aName += aStr;
587 aName += "_";
589 SCTAB nNewTab = nSrcTab+1;
591 SCTAB i=1;
592 while ( !pDoc->InsertTab( nNewTab, lcl_MakePivotTabName( aName, i ) ) && i <= MAXTAB )
593 i++;
595 bool bAppend = ( nNewTab+1 == pDoc->GetTableCount() );
596 if (bUndo)
598 pDocSh->GetUndoManager()->AddUndoAction(
599 new ScUndoInsertTab( pDocSh, nNewTab, bAppend, lcl_MakePivotTabName( aName, i ) ));
602 GetViewData()->InsertTab( nNewTab );
603 SetTabNo(nNewTab, true);
605 aDestRange = ScRange( 0, 0, nNewTab );
608 ScDPObject* pDPObj = pDoc->GetDPAtCursor(
609 aDestRange.aStart.Col(), aDestRange.aStart.Row(), aDestRange.aStart.Tab() );
611 ScDPObject aObj( rSource );
612 aObj.SetOutRange( aDestRange );
613 if ( pDPObj && !rData.GetExistingDimensionData() )
615 // copy dimension data from old object - lost in the dialog
616 //! change the dialog to keep the dimension data
618 ScDPSaveData aNewData( rData );
619 const ScDPSaveData* pOldData = pDPObj->GetSaveData();
620 if ( pOldData )
622 const ScDPDimensionSaveData* pDimSave = pOldData->GetExistingDimensionData();
623 aNewData.SetDimensionData( pDimSave );
625 aObj.SetSaveData( aNewData );
627 else
628 aObj.SetSaveData( rData );
630 bool bAllowMove = (pDPObj != NULL); // allow re-positioning when editing existing table
632 ScDBDocFunc aFunc( *pDocSh );
633 bool bSuccess = aFunc.DataPilotUpdate(pDPObj, &aObj, true, false, bAllowMove);
635 CursorPosChanged(); // shells may be switched
637 if ( bNewTable )
639 pDocSh->PostPaintExtras();
640 SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
643 return bSuccess;
646 void ScDBFunc::DeletePivotTable()
648 ScDocShell* pDocSh = GetViewData()->GetDocShell();
649 ScDocument* pDoc = pDocSh->GetDocument();
650 ScDPObject* pDPObj = pDoc->GetDPAtCursor( GetViewData()->GetCurX(),
651 GetViewData()->GetCurY(),
652 GetViewData()->GetTabNo() );
653 if ( pDPObj )
655 ScDBDocFunc aFunc( *pDocSh );
656 aFunc.RemovePivotTable(*pDPObj, true, false);
657 CursorPosChanged(); // shells may be switched
659 else
660 ErrorMessage(STR_PIVOT_NOTFOUND);
663 void ScDBFunc::RecalcPivotTable()
665 ScDocShell* pDocSh = GetViewData()->GetDocShell();
666 ScDocument* pDoc = GetViewData()->GetDocument();
668 ScDPObject* pDPObj = pDoc->GetDPAtCursor( GetViewData()->GetCurX(),
669 GetViewData()->GetCurY(),
670 GetViewData()->GetTabNo() );
671 if (pDPObj)
673 // Remove existing data cache for the data that this datapilot uses,
674 // to force re-build data cache.
675 ScDBDocFunc aFunc(*pDocSh);
676 aFunc.RefreshPivotTables(pDPObj, false);
678 CursorPosChanged(); // shells may be switched
680 else
681 ErrorMessage(STR_PIVOT_NOTFOUND);
684 void ScDBFunc::GetSelectedMemberList(ScDPUniqueStringSet& rEntries, long& rDimension)
686 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
687 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
688 if ( !pDPObj )
689 return;
691 long nStartDimension = -1;
692 long nStartHierarchy = -1;
693 long nStartLevel = -1;
695 ScRangeListRef xRanges;
696 GetViewData()->GetMultiArea( xRanges ); // incl. cursor if nothing is selected
697 size_t nRangeCount = xRanges->size();
698 bool bContinue = true;
700 for (size_t nRangePos=0; nRangePos < nRangeCount && bContinue; nRangePos++)
702 ScRange aRange = *(*xRanges)[nRangePos];
703 SCCOL nStartCol = aRange.aStart.Col();
704 SCROW nStartRow = aRange.aStart.Row();
705 SCCOL nEndCol = aRange.aEnd.Col();
706 SCROW nEndRow = aRange.aEnd.Row();
707 SCTAB nTab = aRange.aStart.Tab();
709 for (SCROW nRow=nStartRow; nRow<=nEndRow && bContinue; nRow++)
710 for (SCCOL nCol=nStartCol; nCol<=nEndCol && bContinue; nCol++)
712 sheet::DataPilotTableHeaderData aData;
713 pDPObj->GetHeaderPositionData(ScAddress(nCol, nRow, nTab), aData);
714 if ( aData.Dimension < 0 )
715 bContinue = false; // not part of any dimension
716 else
718 if ( nStartDimension < 0 ) // first member?
720 nStartDimension = aData.Dimension;
721 nStartHierarchy = aData.Hierarchy;
722 nStartLevel = aData.Level;
724 if ( aData.Dimension != nStartDimension ||
725 aData.Hierarchy != nStartHierarchy ||
726 aData.Level != nStartLevel )
728 bContinue = false; // cannot mix dimensions
731 if ( bContinue )
733 // accept any part of a member description, also subtotals,
734 // but don't stop if empty parts are contained
735 if ( aData.Flags & sheet::MemberResultFlags::HASMEMBER )
736 rEntries.insert(aData.MemberName);
741 rDimension = nStartDimension; // dimension from which the found members came
742 if (!bContinue)
743 rEntries.clear(); // remove all if not valid
746 bool ScDBFunc::HasSelectionForDateGroup( ScDPNumGroupInfo& rOldInfo, sal_Int32& rParts )
748 // determine if the date group dialog has to be shown for the current selection
750 bool bFound = false;
752 SCCOL nCurX = GetViewData()->GetCurX();
753 SCROW nCurY = GetViewData()->GetCurY();
754 SCTAB nTab = GetViewData()->GetTabNo();
755 ScDocument* pDoc = GetViewData()->GetDocument();
757 ScDPObject* pDPObj = pDoc->GetDPAtCursor( nCurX, nCurY, nTab );
758 if ( pDPObj )
760 ScDPUniqueStringSet aEntries;
761 long nSelectDimension = -1;
762 GetSelectedMemberList( aEntries, nSelectDimension );
764 if (!aEntries.empty())
766 bool bIsDataLayout;
767 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
768 OUString aBaseDimName( aDimName );
770 bool bInGroupDim = false;
771 bool bFoundParts = false;
773 ScDPDimensionSaveData* pDimData =
774 const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
775 if ( pDimData )
777 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
778 const ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDim( aDimName );
779 if ( pNumGroupDim )
781 // existing num group dimension
783 if ( pNumGroupDim->GetDatePart() != 0 )
785 // dimension has date info -> edit settings of this dimension
786 // (parts are collected below)
788 rOldInfo = pNumGroupDim->GetDateInfo();
789 bFound = true;
791 else if ( pNumGroupDim->GetInfo().mbDateValues )
793 // Numerical grouping with DateValues flag is used for grouping
794 // of days with a "Number of days" value.
796 rOldInfo = pNumGroupDim->GetInfo();
797 rParts = com::sun::star::sheet::DataPilotFieldGroupBy::DAYS; // not found in CollectDateParts
798 bFoundParts = true;
799 bFound = true;
801 bInGroupDim = true;
803 else if ( pGroupDim )
805 // existing additional group dimension
807 if ( pGroupDim->GetDatePart() != 0 )
809 // dimension has date info -> edit settings of this dimension
810 // (parts are collected below)
812 rOldInfo = pGroupDim->GetDateInfo();
813 aBaseDimName = pGroupDim->GetSourceDimName();
814 bFound = true;
816 bInGroupDim = true;
819 if ( bFound && !bFoundParts )
821 // collect date parts from all group dimensions
822 rParts = pDimData->CollectDateParts( aBaseDimName );
824 if ( !bFound && !bInGroupDim )
826 // create new date group dimensions if the selection is a single cell
827 // in a normal dimension with date content
829 ScRange aSelRange;
830 if ( (GetViewData()->GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
831 aSelRange.aStart == aSelRange.aEnd )
833 SCCOL nSelCol = aSelRange.aStart.Col();
834 SCROW nSelRow = aSelRange.aStart.Row();
835 SCTAB nSelTab = aSelRange.aStart.Tab();
836 if ( pDoc->HasValueData( nSelCol, nSelRow, nSelTab ) )
838 sal_uLong nIndex = static_cast<const SfxUInt32Item*>(pDoc->GetAttr(
839 nSelCol, nSelRow, nSelTab, ATTR_VALUE_FORMAT))->GetValue();
840 short nType = pDoc->GetFormatTable()->GetType(nIndex);
841 if ( nType == NUMBERFORMAT_DATE || nType == NUMBERFORMAT_TIME || nType == NUMBERFORMAT_DATETIME )
843 bFound = true;
844 // use currently selected value for automatic limits
845 if( rOldInfo.mbAutoStart )
846 rOldInfo.mfStart = pDoc->GetValue( aSelRange.aStart );
847 if( rOldInfo.mbAutoEnd )
848 rOldInfo.mfEnd = pDoc->GetValue( aSelRange.aStart );
856 return bFound;
859 bool ScDBFunc::HasSelectionForNumGroup( ScDPNumGroupInfo& rOldInfo )
861 // determine if the numeric group dialog has to be shown for the current selection
863 bool bFound = false;
865 SCCOL nCurX = GetViewData()->GetCurX();
866 SCROW nCurY = GetViewData()->GetCurY();
867 SCTAB nTab = GetViewData()->GetTabNo();
868 ScDocument* pDoc = GetViewData()->GetDocument();
870 ScDPObject* pDPObj = pDoc->GetDPAtCursor( nCurX, nCurY, nTab );
871 if ( pDPObj )
873 ScDPUniqueStringSet aEntries;
874 long nSelectDimension = -1;
875 GetSelectedMemberList( aEntries, nSelectDimension );
877 if (!aEntries.empty())
879 bool bIsDataLayout;
880 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
882 bool bInGroupDim = false;
884 ScDPDimensionSaveData* pDimData =
885 const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
886 if ( pDimData )
888 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
889 if ( pNumGroupDim )
891 // existing num group dimension
892 // -> edit settings of this dimension
894 rOldInfo = pNumGroupDim->GetInfo();
895 bFound = true;
897 else if ( pDimData->GetNamedGroupDim( aDimName ) )
898 bInGroupDim = true; // in a group dimension
900 if ( !bFound && !bInGroupDim )
902 // create a new num group dimension if the selection is a single cell
903 // in a normal dimension with numeric content
905 ScRange aSelRange;
906 if ( (GetViewData()->GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
907 aSelRange.aStart == aSelRange.aEnd )
909 if ( pDoc->HasValueData( aSelRange.aStart.Col(), aSelRange.aStart.Row(),
910 aSelRange.aStart.Tab() ) )
912 bFound = true;
913 // use currently selected value for automatic limits
914 if( rOldInfo.mbAutoStart )
915 rOldInfo.mfStart = pDoc->GetValue( aSelRange.aStart );
916 if( rOldInfo.mbAutoEnd )
917 rOldInfo.mfEnd = pDoc->GetValue( aSelRange.aStart );
924 return bFound;
927 void ScDBFunc::DateGroupDataPilot( const ScDPNumGroupInfo& rInfo, sal_Int32 nParts )
929 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
930 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
931 if (!pDPObj)
932 return;
934 ScDPUniqueStringSet aEntries;
935 long nSelectDimension = -1;
936 GetSelectedMemberList( aEntries, nSelectDimension );
938 if (aEntries.empty())
939 return;
941 std::vector<OUString> aDeletedNames;
942 bool bIsDataLayout;
943 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
945 ScDPSaveData aData( *pDPObj->GetSaveData() );
946 ScDPDimensionSaveData* pDimData = aData.GetDimensionData(); // created if not there
948 // find the source dimension name.
949 OUString aBaseDimName = aDimName;
950 if( const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName ) )
951 aBaseDimName = pBaseGroupDim->GetSourceDimName();
953 // Remove all group dimensions associated with this source dimension. For
954 // date grouping, we need to remove all existing groups for the affected
955 // source dimension and build new one(s) from scratch. Keep the deleted
956 // names so that they can be reused during re-construction.
957 aData.RemoveAllGroupDimensions(aBaseDimName, &aDeletedNames);
959 if ( nParts )
961 // create date group dimensions
963 ScDPNumGroupInfo aEmpty;
964 bool bFirst = true;
965 sal_Int32 nMask = 1;
966 for (sal_uInt16 nBit=0; nBit<32; nBit++)
968 if ( nParts & nMask )
970 if ( bFirst )
972 // innermost part: create NumGroupDimension (replacing original values)
973 // Dimension name is left unchanged
975 if ( (nParts == sheet::DataPilotFieldGroupBy::DAYS) && (rInfo.mfStep >= 1.0) )
977 // only days, and a step value specified: use numerical grouping
978 // with DateValues flag, not date grouping
980 ScDPNumGroupInfo aNumInfo( rInfo );
981 aNumInfo.mbDateValues = true;
983 ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, aNumInfo );
984 pDimData->AddNumGroupDimension( aNumGroupDim );
986 else
988 ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, rInfo, nMask );
989 pDimData->AddNumGroupDimension( aNumGroupDim );
992 bFirst = false;
994 else
996 // additional parts: create GroupDimension (shown as additional dimensions)
997 OUString aGroupDimName =
998 pDimData->CreateDateGroupDimName(nMask, *pDPObj, true, &aDeletedNames);
999 ScDPSaveGroupDimension aGroupDim( aBaseDimName, aGroupDimName );
1000 aGroupDim.SetDateInfo( rInfo, nMask );
1001 pDimData->AddGroupDimension( aGroupDim );
1003 // set orientation
1004 ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
1005 if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
1007 ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aBaseDimName );
1008 pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
1009 long nPosition = 0; //! before (immediate) base
1010 aData.SetPosition( pSaveDimension, nPosition );
1014 nMask *= 2;
1018 // apply changes
1019 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1020 pDPObj->SetSaveData( aData );
1021 aFunc.RefreshPivotTableGroups(pDPObj);
1023 // unmark cell selection
1024 Unmark();
1027 void ScDBFunc::NumGroupDataPilot( const ScDPNumGroupInfo& rInfo )
1029 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1030 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1031 if (!pDPObj)
1032 return;
1034 ScDPUniqueStringSet aEntries;
1035 long nSelectDimension = -1;
1036 GetSelectedMemberList( aEntries, nSelectDimension );
1038 if (aEntries.empty())
1039 return;
1041 bool bIsDataLayout;
1042 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1044 ScDPSaveData aData( *pDPObj->GetSaveData() );
1045 ScDPDimensionSaveData* pDimData = aData.GetDimensionData(); // created if not there
1047 ScDPSaveNumGroupDimension* pExisting = pDimData->GetNumGroupDimAcc( aDimName );
1048 if ( pExisting )
1050 // modify existing group dimension
1051 pExisting->SetGroupInfo( rInfo );
1053 else
1055 // create new group dimension
1056 ScDPSaveNumGroupDimension aNumGroupDim( aDimName, rInfo );
1057 pDimData->AddNumGroupDimension( aNumGroupDim );
1060 // apply changes
1061 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1062 pDPObj->SetSaveData( aData );
1063 aFunc.RefreshPivotTableGroups(pDPObj);
1065 // unmark cell selection
1066 Unmark();
1069 void ScDBFunc::GroupDataPilot()
1071 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1072 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1073 if (!pDPObj)
1074 return;
1076 ScDPUniqueStringSet aEntries;
1077 long nSelectDimension = -1;
1078 GetSelectedMemberList( aEntries, nSelectDimension );
1080 if (aEntries.empty())
1081 return;
1083 bool bIsDataLayout;
1084 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1086 ScDPSaveData aData( *pDPObj->GetSaveData() );
1087 ScDPDimensionSaveData* pDimData = aData.GetDimensionData(); // created if not there
1089 // find original base
1090 OUString aBaseDimName = aDimName;
1091 const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName );
1092 if ( pBaseGroupDim )
1094 // any entry's SourceDimName is the original base
1095 aBaseDimName = pBaseGroupDim->GetSourceDimName();
1098 // find existing group dimension
1099 // (using the selected dim, can be intermediate group dim)
1100 ScDPSaveGroupDimension* pGroupDimension = pDimData->GetGroupDimAccForBase( aDimName );
1102 // remove the selected items from their groups
1103 // (empty groups are removed, too)
1104 if ( pGroupDimension )
1106 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1107 for (; it != itEnd; ++it)
1109 const OUString& aEntryName = *it;
1110 if ( pBaseGroupDim )
1112 // for each selected (intermediate) group, remove all its items
1113 // (same logic as for adding, below)
1114 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
1115 if ( pBaseGroup )
1116 pBaseGroup->RemoveElementsFromGroups( *pGroupDimension ); // remove all elements
1117 else
1118 pGroupDimension->RemoveFromGroups( aEntryName );
1120 else
1121 pGroupDimension->RemoveFromGroups( aEntryName );
1125 ScDPSaveGroupDimension* pNewGroupDim = NULL;
1126 if ( !pGroupDimension )
1128 // create a new group dimension
1129 OUString aGroupDimName =
1130 pDimData->CreateGroupDimName(aBaseDimName, *pDPObj, false, NULL);
1131 pNewGroupDim = new ScDPSaveGroupDimension( aBaseDimName, aGroupDimName );
1133 pGroupDimension = pNewGroupDim; // make changes to the new dim if none existed
1135 if ( pBaseGroupDim )
1137 // If it's a higher-order group dimension, pre-allocate groups for all
1138 // non-selected original groups, so the individual base members aren't
1139 // used for automatic groups (this would make the original groups hard
1140 // to find).
1141 //! Also do this when removing groups?
1142 //! Handle this case dynamically with automatic groups?
1144 long nGroupCount = pBaseGroupDim->GetGroupCount();
1145 for ( long nGroup = 0; nGroup < nGroupCount; nGroup++ )
1147 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetGroupByIndex( nGroup );
1149 if (!aEntries.count(pBaseGroup->GetGroupName()))
1151 // add an additional group for each item that is not in the selection
1152 ScDPSaveGroupItem aGroup( pBaseGroup->GetGroupName() );
1153 aGroup.AddElementsFromGroup( *pBaseGroup );
1154 pGroupDimension->AddGroupItem( aGroup );
1159 OUString aGroupDimName = pGroupDimension->GetGroupDimName();
1161 OUString aGroupName = pGroupDimension->CreateGroupName(ScGlobal::GetRscString(STR_PIVOT_GROUP));
1162 ScDPSaveGroupItem aGroup( aGroupName );
1163 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1164 for (; it != itEnd; ++it)
1166 const OUString& aEntryName = *it;
1167 if ( pBaseGroupDim )
1169 // for each selected (intermediate) group, add all its items
1170 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
1171 if ( pBaseGroup )
1172 aGroup.AddElementsFromGroup( *pBaseGroup );
1173 else
1174 aGroup.AddElement( aEntryName ); // no group found -> automatic group, add the item itself
1176 else
1177 aGroup.AddElement( aEntryName ); // no group dimension, add all items directly
1180 pGroupDimension->AddGroupItem( aGroup );
1182 if ( pNewGroupDim )
1184 pDimData->AddGroupDimension( *pNewGroupDim );
1185 delete pNewGroupDim; // AddGroupDimension copies the object
1186 // don't access pGroupDimension after here
1188 pGroupDimension = pNewGroupDim = NULL;
1190 // set orientation
1191 ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
1192 if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
1194 ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aDimName );
1195 pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
1196 long nPosition = 0; //! before (immediate) base
1197 aData.SetPosition( pSaveDimension, nPosition );
1200 // apply changes
1201 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1202 pDPObj->SetSaveData( aData );
1203 aFunc.RefreshPivotTableGroups(pDPObj);
1205 // unmark cell selection
1206 Unmark();
1209 void ScDBFunc::UngroupDataPilot()
1211 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1212 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1213 if (!pDPObj)
1214 return;
1216 ScDPUniqueStringSet aEntries;
1217 long nSelectDimension = -1;
1218 GetSelectedMemberList( aEntries, nSelectDimension );
1220 if (aEntries.empty())
1221 return;
1223 bool bIsDataLayout;
1224 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1226 ScDPSaveData aData( *pDPObj->GetSaveData() );
1227 if (!aData.GetExistingDimensionData())
1228 // There is nothing to ungroup.
1229 return;
1231 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1233 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
1234 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
1235 if ( ( pGroupDim && pGroupDim->GetDatePart() != 0 ) ||
1236 ( pNumGroupDim && pNumGroupDim->GetDatePart() != 0 ) )
1238 // Date grouping: need to remove all affected group dimensions.
1239 // This is done using DateGroupDataPilot with nParts=0.
1241 DateGroupDataPilot( ScDPNumGroupInfo(), 0 );
1242 return;
1245 if ( pGroupDim )
1247 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1248 for (; it != itEnd; ++it)
1249 pGroupDim->RemoveGroup(*it);
1251 // remove group dimension if empty
1252 bool bEmptyDim = pGroupDim->IsEmpty();
1253 if ( !bEmptyDim )
1255 // If all remaining groups in the dimension aren't shown, remove
1256 // the dimension too, as if it was completely empty.
1257 ScDPUniqueStringSet aVisibleEntries;
1258 pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
1259 bEmptyDim = pGroupDim->HasOnlyHidden( aVisibleEntries );
1261 if ( bEmptyDim )
1263 pDimData->RemoveGroupDimension( aDimName ); // pGroupDim is deleted
1265 // also remove SaveData settings for the dimension that no longer exists
1266 aData.RemoveDimensionByName( aDimName );
1269 else if ( pNumGroupDim )
1271 // remove the numerical grouping
1272 pDimData->RemoveNumGroupDimension( aDimName );
1273 // SaveData settings can remain unchanged - the same dimension still exists
1276 // apply changes
1277 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1278 pDPObj->SetSaveData( aData );
1279 aFunc.RefreshPivotTableGroups(pDPObj);
1281 // unmark cell selection
1282 Unmark();
1285 static OUString lcl_replaceMemberNameInSubtotal(const OUString& rSubtotal, const OUString& rMemberName)
1287 sal_Int32 n = rSubtotal.getLength();
1288 const sal_Unicode* p = rSubtotal.getStr();
1289 OUStringBuffer aBuf, aWordBuf;
1290 for (sal_Int32 i = 0; i < n; ++i)
1292 sal_Unicode c = p[i];
1293 if (c == ' ')
1295 OUString aWord = aWordBuf.makeStringAndClear();
1296 if (aWord.equals(rMemberName))
1297 aBuf.append('?');
1298 else
1299 aBuf.append(aWord);
1300 aBuf.append(c);
1302 else if (c == '\\')
1304 // Escape a backslash character.
1305 aWordBuf.append(c);
1306 aWordBuf.append(c);
1308 else if (c == '?')
1310 // A literal '?' must be escaped with a backslash ('\');
1311 aWordBuf.append('\\');
1312 aWordBuf.append(c);
1314 else
1315 aWordBuf.append(c);
1318 if (!aWordBuf.isEmpty())
1320 OUString aWord = aWordBuf.makeStringAndClear();
1321 if (aWord.equals(rMemberName))
1322 aBuf.append('?');
1323 else
1324 aBuf.append(aWord);
1327 return aBuf.makeStringAndClear();
1330 void ScDBFunc::DataPilotInput( const ScAddress& rPos, const OUString& rString )
1332 using namespace ::com::sun::star::sheet;
1334 ScDocument* pDoc = GetViewData()->GetDocument();
1335 ScDPObject* pDPObj = pDoc->GetDPAtCursor( rPos.Col(), rPos.Row(), rPos.Tab() );
1336 if (!pDPObj)
1337 return;
1339 OUString aOldText = pDoc->GetString(rPos.Col(), rPos.Row(), rPos.Tab());
1341 if ( aOldText == rString )
1343 // nothing to do: silently exit
1344 return;
1347 sal_uInt16 nErrorId = 0;
1349 pDPObj->BuildAllDimensionMembers();
1350 ScDPSaveData aData( *pDPObj->GetSaveData() );
1351 bool bChange = false;
1353 sal_uInt16 nOrient = DataPilotFieldOrientation_HIDDEN;
1354 long nField = pDPObj->GetHeaderDim( rPos, nOrient );
1355 if ( nField >= 0 )
1357 // changing a field title
1358 if ( aData.GetExistingDimensionData() )
1360 // only group dimensions can be renamed
1362 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1363 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aOldText );
1364 if ( pGroupDim )
1366 // valid name: not empty, no existing dimension (group or other)
1367 if (!rString.isEmpty() && !pDPObj->IsDimNameInUse(rString))
1369 pGroupDim->Rename( rString );
1371 // also rename in SaveData to preserve the field settings
1372 ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aOldText );
1373 pSaveDim->SetName( rString );
1375 bChange = true;
1377 else
1378 nErrorId = STR_INVALIDNAME;
1381 else if (nOrient == DataPilotFieldOrientation_COLUMN || nOrient == DataPilotFieldOrientation_ROW)
1383 bool bDataLayout = false;
1384 OUString aDimName = pDPObj->GetDimName(nField, bDataLayout);
1385 ScDPSaveDimension* pDim = bDataLayout ? aData.GetDataLayoutDimension() : aData.GetDimensionByName(aDimName);
1386 if (pDim)
1388 if (!rString.isEmpty())
1390 if (rString.equalsIgnoreAsciiCase(aDimName))
1392 pDim->RemoveLayoutName();
1393 bChange = true;
1395 else if (!pDPObj->IsDimNameInUse(rString))
1397 pDim->SetLayoutName(rString);
1398 bChange = true;
1400 else
1401 nErrorId = STR_INVALIDNAME;
1403 else
1404 nErrorId = STR_INVALIDNAME;
1408 else if (pDPObj->IsDataDescriptionCell(rPos))
1410 // There is only one data dimension.
1411 ScDPSaveDimension* pDim = aData.GetFirstDimension(sheet::DataPilotFieldOrientation_DATA);
1412 if (pDim)
1414 if (!rString.isEmpty())
1416 if (pDim->GetName().equalsIgnoreAsciiCase(rString))
1418 pDim->RemoveLayoutName();
1419 bChange = true;
1421 else if (!pDPObj->IsDimNameInUse(rString))
1423 pDim->SetLayoutName(rString);
1424 bChange = true;
1426 else
1427 nErrorId = STR_INVALIDNAME;
1429 else
1430 nErrorId = STR_INVALIDNAME;
1433 else
1435 // This is not a field header.
1436 sheet::DataPilotTableHeaderData aPosData;
1437 pDPObj->GetHeaderPositionData(rPos, aPosData);
1439 if ((aPosData.Flags & MemberResultFlags::HASMEMBER) && !aOldText.isEmpty())
1441 if ( aData.GetExistingDimensionData() && !(aPosData.Flags & MemberResultFlags::SUBTOTAL))
1443 bool bIsDataLayout;
1444 OUString aDimName = pDPObj->GetDimName( aPosData.Dimension, bIsDataLayout );
1446 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1447 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
1448 if ( pGroupDim )
1450 // valid name: not empty, no existing group in this dimension
1451 //! ignore case?
1452 if (!rString.isEmpty() && !pGroupDim->GetNamedGroup(rString))
1454 ScDPSaveGroupItem* pGroup = pGroupDim->GetNamedGroupAcc( aOldText );
1455 if ( pGroup )
1456 pGroup->Rename( rString ); // rename the existing group
1457 else
1459 // create a new group to replace the automatic group
1460 ScDPSaveGroupItem aGroup( rString );
1461 aGroup.AddElement( aOldText );
1462 pGroupDim->AddGroupItem( aGroup );
1465 // in both cases also adjust savedata, to preserve member settings (show details)
1466 ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aDimName );
1467 ScDPSaveMember* pSaveMember = pSaveDim->GetExistingMemberByName( aOldText );
1468 if ( pSaveMember )
1469 pSaveMember->SetName( rString );
1471 bChange = true;
1473 else
1474 nErrorId = STR_INVALIDNAME;
1477 else if ((aPosData.Flags & MemberResultFlags::GRANDTOTAL))
1479 aData.SetGrandTotalName(rString);
1480 bChange = true;
1482 else if (aPosData.Dimension >= 0 && !aPosData.MemberName.isEmpty())
1484 bool bDataLayout = false;
1485 OUString aDimName = pDPObj->GetDimName(static_cast<long>(aPosData.Dimension), bDataLayout);
1486 if (bDataLayout)
1488 // data dimension
1491 if ((aPosData.Flags & MemberResultFlags::SUBTOTAL))
1492 break;
1494 ScDPSaveDimension* pDim = aData.GetDimensionByName(aPosData.MemberName);
1495 if (!pDim)
1496 break;
1498 if (rString.isEmpty())
1500 nErrorId = STR_INVALIDNAME;
1501 break;
1504 if (aPosData.MemberName.equalsIgnoreAsciiCase(rString))
1506 pDim->RemoveLayoutName();
1507 bChange = true;
1509 else if (!pDPObj->IsDimNameInUse(rString))
1511 pDim->SetLayoutName(rString);
1512 bChange = true;
1514 else
1515 nErrorId = STR_INVALIDNAME;
1517 while (false);
1519 else
1521 // field member
1524 ScDPSaveDimension* pDim = aData.GetDimensionByName(aDimName);
1525 if (!pDim)
1526 break;
1528 ScDPSaveMember* pMem = pDim->GetExistingMemberByName(aPosData.MemberName);
1529 if (!pMem)
1530 break;
1532 if ((aPosData.Flags & MemberResultFlags::SUBTOTAL))
1534 // Change subtotal only when the table has one data dimension.
1535 if (aData.GetDataDimensionCount() > 1)
1536 break;
1538 // display name for subtotal is allowed only if the subtotal type is 'Automatic'.
1539 if (pDim->GetSubTotalsCount() != 1)
1540 break;
1542 if (pDim->GetSubTotalFunc(0) != sheet::GeneralFunction_AUTO)
1543 break;
1545 const OUString* pLayoutName = pMem->GetLayoutName();
1546 OUString aMemberName;
1547 if (pLayoutName)
1548 aMemberName = *pLayoutName;
1549 else
1550 aMemberName = aPosData.MemberName;
1552 OUString aNew = lcl_replaceMemberNameInSubtotal(rString, aMemberName);
1553 pDim->SetSubtotalName(aNew);
1554 bChange = true;
1556 else
1558 // Check to make sure the member name isn't
1559 // already used.
1560 if (!rString.isEmpty())
1562 if (rString.equalsIgnoreAsciiCase(pMem->GetName()))
1564 pMem->RemoveLayoutName();
1565 bChange = true;
1567 else if (!pDim->IsMemberNameInUse(rString))
1569 pMem->SetLayoutName(rString);
1570 bChange = true;
1572 else
1573 nErrorId = STR_INVALIDNAME;
1575 else
1576 nErrorId = STR_INVALIDNAME;
1579 while (false);
1585 if ( bChange )
1587 // apply changes
1588 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1589 pDPObj->SetSaveData( aData );
1590 aFunc.UpdatePivotTable(*pDPObj, true, false);
1592 else
1594 if ( !nErrorId )
1595 nErrorId = STR_ERR_DATAPILOT_INPUT;
1596 ErrorMessage( nErrorId );
1600 static void lcl_MoveToEnd( ScDPSaveDimension& rDim, const OUString& rItemName )
1602 ScDPSaveMember* pNewMember = NULL;
1603 const ScDPSaveMember* pOldMember = rDim.GetExistingMemberByName( rItemName );
1604 if ( pOldMember )
1605 pNewMember = new ScDPSaveMember( *pOldMember );
1606 else
1607 pNewMember = new ScDPSaveMember( rItemName );
1608 rDim.AddMember( pNewMember );
1609 // AddMember takes ownership of the new pointer,
1610 // puts it to the end of the list even if it was in the list before.
1613 struct ScOUStringCollate
1615 CollatorWrapper* mpCollator;
1617 ScOUStringCollate(CollatorWrapper* pColl) : mpCollator(pColl) {}
1619 bool operator()(const OUString& rStr1, const OUString& rStr2) const
1621 return ( mpCollator->compareString(rStr1, rStr2) < 0 );
1625 bool ScDBFunc::DataPilotSort( const ScAddress& rPos, bool bAscending, sal_uInt16* pUserListId )
1627 ScDocument* pDoc = GetViewData()->GetDocument();
1628 ScDPObject* pDPObj = pDoc->GetDPAtCursor(rPos.Col(), rPos.Row(), rPos.Tab());
1629 if (!pDPObj)
1630 return false;
1632 // We need to run this to get all members later.
1633 if ( pUserListId )
1634 pDPObj->BuildAllDimensionMembers();
1636 sal_uInt16 nOrientation;
1637 long nDimIndex = pDPObj->GetHeaderDim(rPos, nOrientation);
1638 if (nDimIndex < 0)
1639 // Invalid dimension index. Bail out.
1640 return false;
1642 ScDPSaveData* pSaveData = pDPObj->GetSaveData();
1643 if (!pSaveData)
1644 return false;
1646 ScDPSaveData aNewSaveData(*pSaveData);
1647 bool bDataLayout;
1648 OUString aDimName = pDPObj->GetDimName(nDimIndex, bDataLayout);
1649 ScDPSaveDimension* pSaveDim = aNewSaveData.GetDimensionByName(aDimName);
1650 if (!pSaveDim)
1651 return false;
1653 // manual evaluation of sort order is only needed if a user list id is given
1654 if ( pUserListId )
1656 typedef ScDPSaveDimension::MemberList MemList;
1657 const MemList& rDimMembers = pSaveDim->GetMembers();
1658 list<OUString> aMembers;
1659 boost::unordered_set<OUString, OUStringHash> aMemberSet;
1660 size_t nMemberCount = 0;
1661 for (MemList::const_iterator itr = rDimMembers.begin(), itrEnd = rDimMembers.end();
1662 itr != itrEnd; ++itr)
1664 ScDPSaveMember* pMem = *itr;
1665 aMembers.push_back(pMem->GetName());
1666 aMemberSet.insert(pMem->GetName());
1667 ++nMemberCount;
1670 // Sort the member list in ascending order.
1671 ScOUStringCollate aCollate( ScGlobal::GetCollator() );
1672 aMembers.sort(aCollate);
1674 // Collect and rank those custom sort strings that also exist in the member name list.
1676 typedef boost::unordered_map<OUString, sal_uInt16, OUStringHash> UserSortMap;
1677 UserSortMap aSubStrs;
1678 sal_uInt16 nSubCount = 0;
1679 if (pUserListId)
1681 ScUserList* pUserList = ScGlobal::GetUserList();
1682 if (!pUserList)
1683 return false;
1686 size_t n = pUserList->size();
1687 if (!n || *pUserListId >= static_cast<sal_uInt16>(n))
1688 return false;
1691 const ScUserListData* pData = (*pUserList)[*pUserListId];
1692 if (pData)
1694 sal_uInt16 n = pData->GetSubCount();
1695 for (sal_uInt16 i = 0; i < n; ++i)
1697 OUString aSub = pData->GetSubStr(i);
1698 if (!aMemberSet.count(aSub))
1699 // This string doesn't exist in the member name set. Don't add this.
1700 continue;
1702 aSubStrs.insert(UserSortMap::value_type(aSub, nSubCount++));
1707 // Rank all members.
1709 vector<OUString> aRankedNames(nMemberCount);
1710 sal_uInt16 nCurStrId = 0;
1711 for (list<OUString>::const_iterator itr = aMembers.begin(), itrEnd = aMembers.end();
1712 itr != itrEnd; ++itr)
1714 OUString aName = *itr;
1715 sal_uInt16 nRank = 0;
1716 UserSortMap::const_iterator itrSub = aSubStrs.find(aName);
1717 if (itrSub == aSubStrs.end())
1718 nRank = nSubCount + nCurStrId++;
1719 else
1720 nRank = itrSub->second;
1722 if (!bAscending)
1723 nRank = static_cast< sal_uInt16 >( nMemberCount - nRank - 1 );
1725 aRankedNames[nRank] = aName;
1728 // Re-order ScDPSaveMember instances with the new ranks.
1730 for (vector<OUString>::const_iterator itr = aRankedNames.begin(), itrEnd = aRankedNames.end();
1731 itr != itrEnd; ++itr)
1733 const ScDPSaveMember* pOldMem = pSaveDim->GetExistingMemberByName(*itr);
1734 if (!pOldMem)
1735 // All members are supposed to be present.
1736 continue;
1738 ScDPSaveMember* pNewMem = new ScDPSaveMember(*pOldMem);
1739 pSaveDim->AddMember(pNewMem);
1742 // Set the sorting mode to manual for now. We may introduce a new sorting
1743 // mode later on.
1745 sheet::DataPilotFieldSortInfo aSortInfo;
1746 aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
1747 pSaveDim->SetSortInfo(&aSortInfo);
1749 else
1751 // without user list id, just apply sorting mode
1753 sheet::DataPilotFieldSortInfo aSortInfo;
1754 aSortInfo.Mode = sheet::DataPilotFieldSortMode::NAME;
1755 aSortInfo.IsAscending = bAscending;
1756 pSaveDim->SetSortInfo(&aSortInfo);
1759 // Update the datapilot with the newly sorted field members.
1761 boost::scoped_ptr<ScDPObject> pNewObj(new ScDPObject(*pDPObj));
1762 pNewObj->SetSaveData(aNewSaveData);
1763 ScDBDocFunc aFunc(*GetViewData()->GetDocShell());
1765 return aFunc.DataPilotUpdate(pDPObj, pNewObj.get(), true, false);
1768 bool ScDBFunc::DataPilotMove( const ScRange& rSource, const ScAddress& rDest )
1770 bool bRet = false;
1771 ScDocument* pDoc = GetViewData()->GetDocument();
1772 ScDPObject* pDPObj = pDoc->GetDPAtCursor( rSource.aStart.Col(), rSource.aStart.Row(), rSource.aStart.Tab() );
1773 if ( pDPObj && pDPObj == pDoc->GetDPAtCursor( rDest.Col(), rDest.Row(), rDest.Tab() ) )
1775 sheet::DataPilotTableHeaderData aDestData;
1776 pDPObj->GetHeaderPositionData( rDest, aDestData );
1777 bool bValid = ( aDestData.Dimension >= 0 ); // dropping onto a field
1779 // look through the source range
1780 boost::unordered_set< OUString, OUStringHash, std::equal_to<OUString> > aMembersSet; // for lookup
1781 std::vector< OUString > aMembersVector; // members in original order, for inserting
1782 aMembersVector.reserve( std::max( static_cast<SCSIZE>( rSource.aEnd.Col() - rSource.aStart.Col() + 1 ),
1783 static_cast<SCSIZE>( rSource.aEnd.Row() - rSource.aStart.Row() + 1 ) ) );
1784 for (SCROW nRow = rSource.aStart.Row(); bValid && nRow <= rSource.aEnd.Row(); ++nRow )
1785 for (SCCOL nCol = rSource.aStart.Col(); bValid && nCol <= rSource.aEnd.Col(); ++nCol )
1787 sheet::DataPilotTableHeaderData aSourceData;
1788 pDPObj->GetHeaderPositionData( ScAddress( nCol, nRow, rSource.aStart.Tab() ), aSourceData );
1789 if ( aSourceData.Dimension == aDestData.Dimension && !aSourceData.MemberName.isEmpty() )
1791 if ( aMembersSet.find( aSourceData.MemberName ) == aMembersSet.end() )
1793 aMembersSet.insert( aSourceData.MemberName );
1794 aMembersVector.push_back( aSourceData.MemberName );
1796 // duplicates are ignored
1798 else
1799 bValid = false; // empty (subtotal) or different field
1802 if ( bValid )
1804 bool bIsDataLayout;
1805 OUString aDimName = pDPObj->GetDimName( aDestData.Dimension, bIsDataLayout );
1806 if ( !bIsDataLayout )
1808 ScDPSaveData aData( *pDPObj->GetSaveData() );
1809 ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
1811 // get all member names in source order
1812 uno::Sequence<OUString> aMemberNames;
1813 pDPObj->GetMemberNames( aDestData.Dimension, aMemberNames );
1815 bool bInserted = false;
1817 sal_Int32 nMemberCount = aMemberNames.getLength();
1818 for (sal_Int32 nMemberPos=0; nMemberPos<nMemberCount; ++nMemberPos)
1820 OUString aMemberStr( aMemberNames[nMemberPos] );
1822 if ( !bInserted && aMemberNames[nMemberPos] == aDestData.MemberName )
1824 // insert dragged items before this item
1825 for ( std::vector<OUString>::const_iterator aIter = aMembersVector.begin();
1826 aIter != aMembersVector.end(); ++aIter )
1827 lcl_MoveToEnd( *pDim, *aIter );
1828 bInserted = true;
1831 if ( aMembersSet.find( aMemberStr ) == aMembersSet.end() ) // skip dragged items
1832 lcl_MoveToEnd( *pDim, aMemberStr );
1834 // insert dragged item at end if dest wasn't found (for example, empty)
1835 if ( !bInserted )
1836 for ( std::vector<OUString>::const_iterator aIter = aMembersVector.begin();
1837 aIter != aMembersVector.end(); ++aIter )
1838 lcl_MoveToEnd( *pDim, *aIter );
1840 // Items that were in SaveData, but not in the source, end up at the start of the list.
1842 // set flag for manual sorting
1843 sheet::DataPilotFieldSortInfo aSortInfo;
1844 aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
1845 pDim->SetSortInfo( &aSortInfo );
1847 // apply changes
1848 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1849 boost::scoped_ptr<ScDPObject> pNewObj(new ScDPObject( *pDPObj ));
1850 pNewObj->SetSaveData( aData );
1851 aFunc.DataPilotUpdate( pDPObj, pNewObj.get(), true, false ); //! bApi for drag&drop?
1852 pNewObj.reset();
1854 Unmark(); // entry was moved - no use in leaving the old cell selected
1856 bRet = true;
1861 return bRet;
1864 bool ScDBFunc::HasSelectionForDrillDown( sal_uInt16& rOrientation )
1866 bool bRet = false;
1868 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1869 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1870 if ( pDPObj )
1872 ScDPUniqueStringSet aEntries;
1873 long nSelectDimension = -1;
1874 GetSelectedMemberList( aEntries, nSelectDimension );
1876 if (!aEntries.empty())
1878 bool bIsDataLayout;
1879 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1880 if ( !bIsDataLayout )
1882 ScDPSaveData* pSaveData = pDPObj->GetSaveData();
1883 ScDPSaveDimension* pDim = pSaveData->GetExistingDimensionByName( aDimName );
1884 if ( pDim )
1886 sal_uInt16 nDimOrient = pDim->GetOrientation();
1887 ScDPSaveDimension* pInner = pSaveData->GetInnermostDimension( nDimOrient );
1888 if ( pDim == pInner )
1890 rOrientation = nDimOrient;
1891 bRet = true;
1898 return bRet;
1901 void ScDBFunc::SetDataPilotDetails(bool bShow, const OUString* pNewDimensionName)
1903 ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1904 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1905 if ( pDPObj )
1907 ScDPUniqueStringSet aEntries;
1908 long nSelectDimension = -1;
1909 GetSelectedMemberList( aEntries, nSelectDimension );
1911 if (!aEntries.empty())
1913 bool bIsDataLayout;
1914 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1915 if ( !bIsDataLayout )
1917 ScDPSaveData aData( *pDPObj->GetSaveData() );
1918 ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
1920 if ( bShow && pNewDimensionName )
1922 // add the new dimension with the same orientation, at the end
1924 ScDPSaveDimension* pNewDim = aData.GetDimensionByName( *pNewDimensionName );
1925 ScDPSaveDimension* pDuplicated = NULL;
1926 if ( pNewDim->GetOrientation() == sheet::DataPilotFieldOrientation_DATA )
1928 // Need to duplicate the dimension, create column/row in addition to data:
1929 // The duplicated dimension inherits the existing settings, pNewDim is modified below.
1930 pDuplicated = aData.DuplicateDimension( *pNewDimensionName );
1933 sal_uInt16 nOrientation = pDim->GetOrientation();
1934 pNewDim->SetOrientation( nOrientation );
1936 long nPosition = LONG_MAX;
1937 aData.SetPosition( pNewDim, nPosition );
1939 ScDPSaveDimension* pDataLayout = aData.GetDataLayoutDimension();
1940 if ( pDataLayout->GetOrientation() == nOrientation &&
1941 aData.GetDataDimensionCount() <= 1 )
1943 // If there is only one data dimension, the data layout dimension
1944 // must still be the last one in its orientation.
1945 aData.SetPosition( pDataLayout, nPosition );
1948 if ( pDuplicated )
1950 // The duplicated (data) dimension needs to be behind the original dimension
1951 aData.SetPosition( pDuplicated, nPosition );
1954 // Hide details for all visible members (selected are changed below).
1955 //! Use all members from source level instead (including non-visible)?
1957 ScDPUniqueStringSet aVisibleEntries;
1958 pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
1960 ScDPUniqueStringSet::const_iterator it = aVisibleEntries.begin(), itEnd = aVisibleEntries.end();
1961 for (; it != itEnd; ++it)
1963 const OUString& aVisName = *it;
1964 ScDPSaveMember* pMember = pDim->GetMemberByName( aVisName );
1965 pMember->SetShowDetails( false );
1969 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1970 for (; it != itEnd; ++it)
1972 ScDPSaveMember* pMember = pDim->GetMemberByName(*it);
1973 pMember->SetShowDetails( bShow );
1976 // apply changes
1977 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
1978 boost::scoped_ptr<ScDPObject> pNewObj(new ScDPObject( *pDPObj ));
1979 pNewObj->SetSaveData( aData );
1980 aFunc.DataPilotUpdate( pDPObj, pNewObj.get(), true, false );
1981 pNewObj.reset();
1983 // unmark cell selection
1984 Unmark();
1990 void ScDBFunc::ShowDataPilotSourceData( ScDPObject& rDPObj, const Sequence<sheet::DataPilotFieldFilter>& rFilters )
1992 ScDocument* pDoc = GetViewData()->GetDocument();
1993 if (pDoc->GetDocumentShell()->IsReadOnly())
1995 ErrorMessage(STR_READONLYERR);
1996 return;
1999 Reference<sheet::XDimensionsSupplier> xDimSupplier = rDPObj.GetSource();
2000 Reference<container::XNameAccess> xDims = xDimSupplier->getDimensions();
2001 Reference<sheet::XDrillDownDataSupplier> xDDSupplier(xDimSupplier, UNO_QUERY);
2002 if (!xDDSupplier.is())
2003 return;
2005 Sequence< Sequence<Any> > aTabData = xDDSupplier->getDrillDownData(rFilters);
2006 sal_Int32 nRowSize = aTabData.getLength();
2007 if (nRowSize <= 1)
2008 // There is no data to show. Bail out.
2009 return;
2011 sal_Int32 nColSize = aTabData[0].getLength();
2013 SCTAB nNewTab = GetViewData()->GetTabNo();
2015 boost::scoped_ptr<ScDocument> pInsDoc(new ScDocument(SCDOCMODE_CLIP));
2016 pInsDoc->ResetClip( pDoc, nNewTab );
2017 for (SCROW nRow = 0; nRow < nRowSize; ++nRow)
2019 for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
2021 const Any& rAny = aTabData[nRow][nCol];
2022 OUString aStr;
2023 double fVal;
2024 if (rAny >>= aStr)
2026 ScSetStringParam aParam;
2027 aParam.setTextInput();
2028 pInsDoc->SetString(ScAddress(nCol,nRow,nNewTab), aStr);
2030 else if (rAny >>= fVal)
2031 pInsDoc->SetValue(nCol, nRow, nNewTab, fVal);
2035 // set number format (important for dates)
2036 for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
2038 OUString aStr;
2039 if (!(aTabData[0][nCol] >>= aStr))
2040 continue;
2042 Reference<XPropertySet> xPropSet(xDims->getByName(aStr), UNO_QUERY);
2043 if (!xPropSet.is())
2044 continue;
2046 Any any = xPropSet->getPropertyValue( OUString(SC_UNO_DP_NUMBERFO) );
2047 sal_Int32 nNumFmt = 0;
2048 if (!(any >>= nNumFmt))
2049 continue;
2051 ScPatternAttr aPattern( pInsDoc->GetPool() );
2052 aPattern.GetItemSet().Put( SfxUInt32Item(ATTR_VALUE_FORMAT, static_cast<sal_uInt32>(nNumFmt)) );
2053 pInsDoc->ApplyPatternAreaTab(nCol, 1, nCol, nRowSize-1, nNewTab, aPattern);
2056 SCCOL nEndCol = 0;
2057 SCROW nEndRow = 0;
2058 pInsDoc->GetCellArea( nNewTab, nEndCol, nEndRow );
2059 pInsDoc->SetClipArea( ScRange( 0, 0, nNewTab, nEndCol, nEndRow, nNewTab ) );
2061 ::svl::IUndoManager* pMgr = GetViewData()->GetDocShell()->GetUndoManager();
2062 OUString aUndo = ScGlobal::GetRscString( STR_UNDO_DOOUTLINE );
2063 pMgr->EnterListAction( aUndo, aUndo );
2065 OUString aNewTabName;
2066 pDoc->CreateValidTabName(aNewTabName);
2067 if ( InsertTable(aNewTabName, nNewTab) )
2068 PasteFromClip( IDF_ALL, pInsDoc.get() );
2070 pMgr->LeaveListAction();
2074 // DB-Operationen (Sortieren, Filtern, Teilergebnisse) wiederholen
2077 void ScDBFunc::RepeatDB( bool bRecord )
2079 SCCOL nCurX = GetViewData()->GetCurX();
2080 SCROW nCurY = GetViewData()->GetCurY();
2081 SCTAB nTab = GetViewData()->GetTabNo();
2082 ScDocument* pDoc = GetViewData()->GetDocument();
2083 ScDBData* pDBData = GetDBData();
2084 if (bRecord && !pDoc->IsUndoEnabled())
2085 bRecord = false;
2087 ScQueryParam aQueryParam;
2088 pDBData->GetQueryParam( aQueryParam );
2089 bool bQuery = aQueryParam.GetEntry(0).bDoQuery;
2091 ScSortParam aSortParam;
2092 pDBData->GetSortParam( aSortParam );
2093 bool bSort = aSortParam.maKeyState[0].bDoSort;
2095 ScSubTotalParam aSubTotalParam;
2096 pDBData->GetSubTotalParam( aSubTotalParam );
2097 bool bSubTotal = aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly;
2099 if ( bQuery || bSort || bSubTotal )
2101 bool bQuerySize = false;
2102 ScRange aOldQuery;
2103 ScRange aNewQuery;
2104 if (bQuery && !aQueryParam.bInplace)
2106 ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
2107 aQueryParam.nDestTab, true );
2108 if (pDest && pDest->IsDoSize())
2110 pDest->GetArea( aOldQuery );
2111 bQuerySize = true;
2115 SCTAB nDummy;
2116 SCCOL nStartCol;
2117 SCROW nStartRow;
2118 SCCOL nEndCol;
2119 SCROW nEndRow;
2120 pDBData->GetArea( nDummy, nStartCol, nStartRow, nEndCol, nEndRow );
2122 //! Undo nur benoetigte Daten ?
2124 ScDocument* pUndoDoc = NULL;
2125 ScOutlineTable* pUndoTab = NULL;
2126 ScRangeName* pUndoRange = NULL;
2127 ScDBCollection* pUndoDB = NULL;
2129 if (bRecord)
2131 SCTAB nTabCount = pDoc->GetTableCount();
2132 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
2133 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
2134 if (pTable)
2136 pUndoTab = new ScOutlineTable( *pTable );
2138 SCCOLROW nOutStartCol; // Zeilen/Spaltenstatus
2139 SCCOLROW nOutStartRow;
2140 SCCOLROW nOutEndCol;
2141 SCCOLROW nOutEndRow;
2142 pTable->GetColArray()->GetRange( nOutStartCol, nOutEndCol );
2143 pTable->GetRowArray()->GetRange( nOutStartRow, nOutEndRow );
2145 pUndoDoc->InitUndo( pDoc, nTab, nTab, true, true );
2146 pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, false, pUndoDoc );
2147 pDoc->CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, false, pUndoDoc );
2149 else
2150 pUndoDoc->InitUndo( pDoc, nTab, nTab, false, true );
2152 // Datenbereich sichern - incl. Filter-Ergebnis
2153 pDoc->CopyToDocument( 0,nStartRow,nTab, MAXCOL,nEndRow,nTab, IDF_ALL, false, pUndoDoc );
2155 // alle Formeln wegen Referenzen
2156 pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1, IDF_FORMULA, false, pUndoDoc );
2158 // DB- und andere Bereiche
2159 ScRangeName* pDocRange = pDoc->GetRangeName();
2160 if (!pDocRange->empty())
2161 pUndoRange = new ScRangeName( *pDocRange );
2162 ScDBCollection* pDocDB = pDoc->GetDBCollection();
2163 if (!pDocDB->empty())
2164 pUndoDB = new ScDBCollection( *pDocDB );
2167 if (bSort && bSubTotal)
2169 // Sortieren ohne SubTotals
2171 aSubTotalParam.bRemoveOnly = true; // wird unten wieder zurueckgesetzt
2172 DoSubTotals( aSubTotalParam, false );
2175 if (bSort)
2177 pDBData->GetSortParam( aSortParam ); // Bereich kann sich geaendert haben
2178 Sort( aSortParam, false, false);
2180 if (bQuery)
2182 pDBData->GetQueryParam( aQueryParam ); // Bereich kann sich geaendert haben
2183 ScRange aAdvSource;
2184 if (pDBData->GetAdvancedQuerySource(aAdvSource))
2186 pDoc->CreateQueryParam(
2187 aAdvSource.aStart.Col(), aAdvSource.aStart.Row(),
2188 aAdvSource.aEnd.Col(), aAdvSource.aEnd.Row(),
2189 aAdvSource.aStart.Tab(), aQueryParam );
2190 Query( aQueryParam, &aAdvSource, false );
2192 else
2193 Query( aQueryParam, NULL, false );
2195 // bei nicht-inplace kann die Tabelle umgestellt worden sein
2196 if ( !aQueryParam.bInplace && aQueryParam.nDestTab != nTab )
2197 SetTabNo( nTab );
2199 if (bSubTotal)
2201 pDBData->GetSubTotalParam( aSubTotalParam ); // Bereich kann sich geaendert haben
2202 aSubTotalParam.bRemoveOnly = false;
2203 DoSubTotals( aSubTotalParam, false );
2206 if (bRecord)
2208 SCTAB nDummyTab;
2209 SCCOL nDummyCol;
2210 SCROW nDummyRow, nNewEndRow;
2211 pDBData->GetArea( nDummyTab, nDummyCol,nDummyRow, nDummyCol,nNewEndRow );
2213 const ScRange* pOld = NULL;
2214 const ScRange* pNew = NULL;
2215 if (bQuerySize)
2217 ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
2218 aQueryParam.nDestTab, true );
2219 if (pDest)
2221 pDest->GetArea( aNewQuery );
2222 pOld = &aOldQuery;
2223 pNew = &aNewQuery;
2227 GetViewData()->GetDocShell()->GetUndoManager()->AddUndoAction(
2228 new ScUndoRepeatDB( GetViewData()->GetDocShell(), nTab,
2229 nStartCol, nStartRow, nEndCol, nEndRow,
2230 nNewEndRow,
2231 nCurX, nCurY,
2232 pUndoDoc, pUndoTab,
2233 pUndoRange, pUndoDB,
2234 pOld, pNew ) );
2237 GetViewData()->GetDocShell()->PostPaint(
2238 ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab),
2239 PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE);
2241 else // "Keine Operationen auszufuehren"
2242 ErrorMessage(STR_MSSG_REPEATDB_0);
2245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */