fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / view / dbfunc3.cxx
blobd22ec1031b2d30a61a2f2a4beff994eae5cc6d0a
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 <list>
72 #include <unordered_set>
73 #include <unordered_map>
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 -----------------------------------------------------------
89 // Outliner
91 // Outline-Gruppierung erzeugen
93 void ScDBFunc::MakeOutline( bool bColumns, bool bRecord )
95 ScRange aRange;
96 if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
98 ScDocShell* pDocSh = GetViewData().GetDocShell();
99 ScOutlineDocFunc aFunc(*pDocSh);
100 aFunc.MakeOutline( aRange, bColumns, bRecord, false );
102 else
103 ErrorMessage(STR_NOMULTISELECT);
106 // Outline-Gruppierung loeschen
108 void ScDBFunc::RemoveOutline( bool bColumns, bool bRecord )
110 ScRange aRange;
111 if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
113 ScDocShell* pDocSh = GetViewData().GetDocShell();
114 ScOutlineDocFunc aFunc(*pDocSh);
115 aFunc.RemoveOutline( aRange, bColumns, bRecord, false );
117 else
118 ErrorMessage(STR_NOMULTISELECT);
121 // Menue-Status: Outlines loeschen
123 void ScDBFunc::TestRemoveOutline( bool& rCol, bool& rRow )
125 bool bColFound = false;
126 bool bRowFound = false;
128 SCCOL nStartCol, nEndCol;
129 SCROW nStartRow, nEndRow;
130 SCTAB nStartTab, nEndTab;
131 if (GetViewData().GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
133 SCTAB nTab = nStartTab;
134 ScDocument* pDoc = GetViewData().GetDocument();
135 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
136 if (pTable)
138 ScOutlineEntry* pEntry;
139 SCCOLROW nStart;
140 SCCOLROW nEnd;
141 bool bColMarked = ( nStartRow == 0 && nEndRow == MAXROW );
142 bool bRowMarked = ( nStartCol == 0 && nEndCol == MAXCOL );
144 // Spalten
146 if ( !bRowMarked || bColMarked ) // nicht wenn ganze Zeilen markiert
148 ScOutlineArray& rArray = pTable->GetColArray();
149 ScSubOutlineIterator aColIter( &rArray );
150 while ((pEntry=aColIter.GetNext()) != NULL && !bColFound)
152 nStart = pEntry->GetStart();
153 nEnd = pEntry->GetEnd();
154 if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
155 bColFound = true;
159 // Zeilen
161 if ( !bColMarked || bRowMarked ) // nicht wenn ganze Spalten markiert
163 ScOutlineArray& rArray = pTable->GetRowArray();
164 ScSubOutlineIterator aRowIter( &rArray );
165 while ((pEntry=aRowIter.GetNext()) != NULL && !bRowFound)
167 nStart = pEntry->GetStart();
168 nEnd = pEntry->GetEnd();
169 if ( nStartRow<=nEnd && nEndRow>=nStart )
170 bRowFound = true;
176 rCol = bColFound;
177 rRow = bRowFound;
180 void ScDBFunc::RemoveAllOutlines( bool bRecord )
182 SCTAB nTab = GetViewData().GetTabNo();
183 ScDocShell* pDocSh = GetViewData().GetDocShell();
184 ScOutlineDocFunc aFunc(*pDocSh);
186 bool bOk = aFunc.RemoveAllOutlines( nTab, bRecord );
188 if (bOk)
189 UpdateScrollBars();
192 // Auto-Outlines
194 void ScDBFunc::AutoOutline( bool bRecord )
196 SCTAB nTab = GetViewData().GetTabNo();
197 ScRange aRange( 0,0,nTab, MAXCOL,MAXROW,nTab ); // ganze Tabelle, wenn nichts markiert
198 ScMarkData& rMark = GetViewData().GetMarkData();
199 if ( rMark.IsMarked() || rMark.IsMultiMarked() )
201 rMark.MarkToMulti();
202 rMark.GetMultiMarkArea( aRange );
205 ScDocShell* pDocSh = GetViewData().GetDocShell();
206 ScOutlineDocFunc aFunc(*pDocSh);
207 aFunc.AutoOutline( aRange, bRecord );
210 // Outline-Ebene auswaehlen
212 void ScDBFunc::SelectLevel( bool bColumns, sal_uInt16 nLevel, bool bRecord, bool bPaint )
214 SCTAB nTab = GetViewData().GetTabNo();
215 ScDocShell* pDocSh = GetViewData().GetDocShell();
216 ScOutlineDocFunc aFunc(*pDocSh);
218 bool bOk = aFunc.SelectLevel( nTab, bColumns, nLevel, bRecord, bPaint );
220 if (bOk)
221 UpdateScrollBars();
224 // einzelne Outline-Gruppe einblenden
226 void ScDBFunc::ShowOutline( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bRecord, bool bPaint )
228 SCTAB nTab = GetViewData().GetTabNo();
229 ScDocShell* pDocSh = GetViewData().GetDocShell();
230 ScOutlineDocFunc aFunc(*pDocSh);
232 bool bOk = aFunc.ShowOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint );
234 if ( bOk && bPaint )
235 UpdateScrollBars();
238 // einzelne Outline-Gruppe ausblenden
240 void ScDBFunc::HideOutline( bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, bool bRecord, bool bPaint )
242 SCTAB nTab = GetViewData().GetTabNo();
243 ScDocShell* pDocSh = GetViewData().GetDocShell();
244 ScOutlineDocFunc aFunc(*pDocSh);
246 bool bOk = aFunc.HideOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint );
248 if ( bOk && bPaint )
249 UpdateScrollBars();
252 // Menue-Status: markierten Bereich ein-/ausblenden
254 bool ScDBFunc::OutlinePossible(bool bHide)
256 bool bEnable = false;
258 SCCOL nStartCol;
259 SCROW nStartRow;
260 SCTAB nStartTab;
261 SCCOL nEndCol;
262 SCROW nEndRow;
263 SCTAB nEndTab;
265 if (GetViewData().GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
267 ScDocument* pDoc = GetViewData().GetDocument();
268 SCTAB nTab = GetViewData().GetTabNo();
269 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
270 if (pTable)
272 ScOutlineEntry* pEntry;
273 SCCOLROW nStart;
274 SCCOLROW nEnd;
276 // Spalten
278 ScOutlineArray& rColArray = pTable->GetColArray();
279 ScSubOutlineIterator aColIter( &rColArray );
280 while ((pEntry=aColIter.GetNext()) != NULL && !bEnable)
282 nStart = pEntry->GetStart();
283 nEnd = pEntry->GetEnd();
284 if ( bHide )
286 if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
287 if (!pEntry->IsHidden())
288 bEnable = true;
290 else
292 if ( nStart>=nStartCol && nEnd<=nEndCol )
293 if (pEntry->IsHidden())
294 bEnable = true;
298 // Zeilen
300 ScOutlineArray& rRowArray = pTable->GetRowArray();
301 ScSubOutlineIterator aRowIter( &rRowArray );
302 while ((pEntry=aRowIter.GetNext()) != NULL)
304 nStart = pEntry->GetStart();
305 nEnd = pEntry->GetEnd();
306 if ( bHide )
308 if ( nStartRow<=nEnd && nEndRow>=nStart )
309 if (!pEntry->IsHidden())
310 bEnable = true;
312 else
314 if ( nStart>=nStartRow && nEnd<=nEndRow )
315 if (pEntry->IsHidden())
316 bEnable = true;
322 return bEnable;
325 // markierten Bereich einblenden
327 void ScDBFunc::ShowMarkedOutlines( bool bRecord )
329 ScRange aRange;
330 if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
332 ScDocShell* pDocSh = GetViewData().GetDocShell();
333 ScOutlineDocFunc aFunc(*pDocSh);
334 bool bDone = aFunc.ShowMarkedOutlines( aRange, bRecord );
335 if (bDone)
336 UpdateScrollBars();
338 else
339 ErrorMessage(STR_NOMULTISELECT);
342 // markierten Bereich ausblenden
344 void ScDBFunc::HideMarkedOutlines( bool bRecord )
346 ScRange aRange;
347 if (GetViewData().GetSimpleArea(aRange) == SC_MARK_SIMPLE)
349 ScDocShell* pDocSh = GetViewData().GetDocShell();
350 ScOutlineDocFunc aFunc(*pDocSh);
351 bool bDone = aFunc.HideMarkedOutlines( aRange, bRecord );
352 if (bDone)
353 UpdateScrollBars();
355 else
356 ErrorMessage(STR_NOMULTISELECT);
359 // Teilergebnisse
361 void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, bool bRecord,
362 const ScSortParam* pForceNewSort )
364 bool bDo = !rParam.bRemoveOnly; // sal_False = nur loeschen
366 ScDocShell* pDocSh = GetViewData().GetDocShell();
367 ScDocument& rDoc = pDocSh->GetDocument();
368 ScMarkData& rMark = GetViewData().GetMarkData();
369 SCTAB nTab = GetViewData().GetTabNo();
370 if (bRecord && !rDoc.IsUndoEnabled())
371 bRecord = false;
373 ScDBData* pDBData = rDoc.GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
374 rParam.nCol2, rParam.nRow2 );
375 if (!pDBData)
377 OSL_FAIL( "SubTotals: keine DBData" );
378 return;
381 ScEditableTester aTester( &rDoc, nTab, 0,rParam.nRow1+1, MAXCOL,MAXROW );
382 if (!aTester.IsEditable())
384 ErrorMessage(aTester.GetMessageId());
385 return;
388 if (rDoc.HasAttrib( rParam.nCol1, rParam.nRow1+1, nTab,
389 rParam.nCol2, rParam.nRow2, nTab, HASATTR_MERGED | HASATTR_OVERLAPPED ))
391 ErrorMessage(STR_MSSG_INSERTCELLS_0); // nicht in zusammengefasste einfuegen
392 return;
395 WaitObject aWait( GetViewData().GetDialogParent() );
396 bool bOk = true;
397 if (rParam.bReplace)
398 if (rDoc.TestRemoveSubTotals( nTab, rParam ))
400 bOk = ( ScopedVclPtr<MessBox>::Create( GetViewData().GetDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
401 // "StarCalc" "Daten loeschen?"
402 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
403 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_1 ) )->Execute()
404 == RET_YES );
407 if (bOk)
409 ScDocShellModificator aModificator( *pDocSh );
411 ScSubTotalParam aNewParam( rParam ); // Bereichsende wird veraendert
412 ScDocument* pUndoDoc = NULL;
413 ScOutlineTable* pUndoTab = NULL;
414 ScRangeName* pUndoRange = NULL;
415 ScDBCollection* pUndoDB = NULL;
417 if (bRecord) // alte Daten sichern
419 bool bOldFilter = bDo && rParam.bDoSort;
420 SCTAB nTabCount = rDoc.GetTableCount();
421 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
422 ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
423 if (pTable)
425 pUndoTab = new ScOutlineTable( *pTable );
427 SCCOLROW nOutStartCol; // Zeilen/Spaltenstatus
428 SCCOLROW nOutStartRow;
429 SCCOLROW nOutEndCol;
430 SCCOLROW nOutEndRow;
431 pTable->GetColArray().GetRange( nOutStartCol, nOutEndCol );
432 pTable->GetRowArray().GetRange( nOutStartRow, nOutEndRow );
434 pUndoDoc->InitUndo( &rDoc, nTab, nTab, true, true );
435 rDoc.CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, false, pUndoDoc );
436 rDoc.CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, false, pUndoDoc );
438 else
439 pUndoDoc->InitUndo( &rDoc, nTab, nTab, false, bOldFilter );
441 // Datenbereich sichern - incl. Filter-Ergebnis
442 rDoc.CopyToDocument( 0,rParam.nRow1+1,nTab, MAXCOL,rParam.nRow2,nTab,
443 IDF_ALL, false, pUndoDoc );
445 // alle Formeln wegen Referenzen
446 rDoc.CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1,
447 IDF_FORMULA, false, pUndoDoc );
449 // DB- und andere Bereiche
450 ScRangeName* pDocRange = rDoc.GetRangeName();
451 if (!pDocRange->empty())
452 pUndoRange = new ScRangeName( *pDocRange );
453 ScDBCollection* pDocDB = rDoc.GetDBCollection();
454 if (!pDocDB->empty())
455 pUndoDB = new ScDBCollection( *pDocDB );
458 ScOutlineTable* pOut = rDoc.GetOutlineTable( nTab );
459 if (pOut)
461 // Remove all existing outlines in the specified range.
462 ScOutlineArray& rRowArray = pOut->GetRowArray();
463 sal_uInt16 nDepth = rRowArray.GetDepth();
464 for (sal_uInt16 i = 0; i < nDepth; ++i)
466 bool bSize;
467 rRowArray.Remove(aNewParam.nRow1, aNewParam.nRow2, bSize);
471 if (rParam.bReplace)
472 rDoc.RemoveSubTotals( nTab, aNewParam );
473 bool bSuccess = true;
474 if (bDo)
476 // Sortieren
477 if ( rParam.bDoSort || pForceNewSort )
479 pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
481 // Teilergebnis-Felder vor die Sortierung setzen
482 // (doppelte werden weggelassen, kann darum auch wieder aufgerufen werden)
484 ScSortParam aOldSort;
485 pDBData->GetSortParam( aOldSort );
486 ScSortParam aSortParam( aNewParam, pForceNewSort ? *pForceNewSort : aOldSort );
487 Sort( aSortParam, false, false );
490 bSuccess = rDoc.DoSubTotals( nTab, aNewParam );
492 ScRange aDirtyRange( aNewParam.nCol1, aNewParam.nRow1, nTab,
493 aNewParam.nCol2, aNewParam.nRow2, nTab );
494 rDoc.SetDirty( aDirtyRange, true );
496 if (bRecord)
498 pDocSh->GetUndoManager()->AddUndoAction(
499 new ScUndoSubTotals( pDocSh, nTab,
500 rParam, aNewParam.nRow2,
501 pUndoDoc, pUndoTab, // pUndoDBData,
502 pUndoRange, pUndoDB ) );
505 if (!bSuccess)
507 // "Kann keine Zeilen einfuegen"
508 ErrorMessage(STR_MSSG_DOSUBTOTALS_2);
511 // merken
512 pDBData->SetSubTotalParam( aNewParam );
513 pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
514 rDoc.CompileDBFormula();
516 DoneBlockMode();
517 InitOwnBlockMode();
518 rMark.SetMarkArea( ScRange( aNewParam.nCol1,aNewParam.nRow1,nTab,
519 aNewParam.nCol2,aNewParam.nRow2,nTab ) );
520 MarkDataChanged();
522 pDocSh->PostPaint(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab),
523 PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE);
525 aModificator.SetDocumentModified();
527 SelectionChanged();
531 // Consolidate
533 void ScDBFunc::Consolidate( const ScConsolidateParam& rParam, bool bRecord )
535 ScDocShell* pDocShell = GetViewData().GetDocShell();
536 pDocShell->DoConsolidate( rParam, bRecord );
537 SetTabNo( rParam.nTab, true );
540 // Pivot
542 static OUString lcl_MakePivotTabName( const OUString& rPrefix, SCTAB nNumber )
544 OUString aName = rPrefix + OUString::number( nNumber );
545 return aName;
548 bool ScDBFunc::MakePivotTable(
549 const ScDPSaveData& rData, const ScRange& rDest, bool bNewTable,
550 const ScDPObject& rSource, bool bApi )
552 // error message if no fields are set
553 // this must be removed when drag&drop of fields from a toolbox is available
555 if ( rData.IsEmpty() && !bApi )
557 ErrorMessage(STR_PIVOT_NODATA);
558 return false;
561 ScDocShell* pDocSh = GetViewData().GetDocShell();
562 ScDocument* pDoc = GetViewData().GetDocument();
563 bool bUndo = pDoc->IsUndoEnabled();
565 ScRange aDestRange = rDest;
566 if ( bNewTable )
568 SCTAB nSrcTab = GetViewData().GetTabNo();
570 OUString aName( ScGlobal::GetRscString(STR_PIVOT_TABLE) );
571 OUString aStr;
573 pDoc->GetName( nSrcTab, aStr );
574 aName += "_";
575 aName += aStr;
576 aName += "_";
578 SCTAB nNewTab = nSrcTab+1;
580 SCTAB i=1;
581 while ( !pDoc->InsertTab( nNewTab, lcl_MakePivotTabName( aName, i ) ) && i <= MAXTAB )
582 i++;
584 bool bAppend = ( nNewTab+1 == pDoc->GetTableCount() );
585 if (bUndo)
587 pDocSh->GetUndoManager()->AddUndoAction(
588 new ScUndoInsertTab( pDocSh, nNewTab, bAppend, lcl_MakePivotTabName( aName, i ) ));
591 GetViewData().InsertTab( nNewTab );
592 SetTabNo(nNewTab, true);
594 aDestRange = ScRange( 0, 0, nNewTab );
597 ScDPObject* pDPObj = pDoc->GetDPAtCursor(
598 aDestRange.aStart.Col(), aDestRange.aStart.Row(), aDestRange.aStart.Tab() );
600 ScDPObject aObj( rSource );
601 aObj.SetOutRange( aDestRange );
602 if ( pDPObj && !rData.GetExistingDimensionData() )
604 // copy dimension data from old object - lost in the dialog
605 //! change the dialog to keep the dimension data
607 ScDPSaveData aNewData( rData );
608 const ScDPSaveData* pOldData = pDPObj->GetSaveData();
609 if ( pOldData )
611 const ScDPDimensionSaveData* pDimSave = pOldData->GetExistingDimensionData();
612 aNewData.SetDimensionData( pDimSave );
614 aObj.SetSaveData( aNewData );
616 else
617 aObj.SetSaveData( rData );
619 bool bAllowMove = (pDPObj != NULL); // allow re-positioning when editing existing table
621 ScDBDocFunc aFunc( *pDocSh );
622 bool bSuccess = aFunc.DataPilotUpdate(pDPObj, &aObj, true, false, bAllowMove);
624 CursorPosChanged(); // shells may be switched
626 if ( bNewTable )
628 pDocSh->PostPaintExtras();
629 SfxGetpApp()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
632 return bSuccess;
635 void ScDBFunc::DeletePivotTable()
637 ScDocShell* pDocSh = GetViewData().GetDocShell();
638 ScDocument& rDoc = pDocSh->GetDocument();
639 ScDPObject* pDPObj = rDoc.GetDPAtCursor( GetViewData().GetCurX(),
640 GetViewData().GetCurY(),
641 GetViewData().GetTabNo() );
642 if ( pDPObj )
644 ScDBDocFunc aFunc( *pDocSh );
645 aFunc.RemovePivotTable(*pDPObj, true, false);
646 CursorPosChanged(); // shells may be switched
648 else
649 ErrorMessage(STR_PIVOT_NOTFOUND);
652 void ScDBFunc::RecalcPivotTable()
654 ScDocShell* pDocSh = GetViewData().GetDocShell();
655 ScDocument* pDoc = GetViewData().GetDocument();
657 ScDPObject* pDPObj = pDoc->GetDPAtCursor( GetViewData().GetCurX(),
658 GetViewData().GetCurY(),
659 GetViewData().GetTabNo() );
660 if (pDPObj)
662 // Remove existing data cache for the data that this datapilot uses,
663 // to force re-build data cache.
664 ScDBDocFunc aFunc(*pDocSh);
665 aFunc.RefreshPivotTables(pDPObj, false);
667 CursorPosChanged(); // shells may be switched
669 else
670 ErrorMessage(STR_PIVOT_NOTFOUND);
673 void ScDBFunc::GetSelectedMemberList(ScDPUniqueStringSet& rEntries, long& rDimension)
675 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
676 GetViewData().GetCurY(), GetViewData().GetTabNo() );
677 if ( !pDPObj )
678 return;
680 long nStartDimension = -1;
681 long nStartHierarchy = -1;
682 long nStartLevel = -1;
684 ScRangeListRef xRanges;
685 GetViewData().GetMultiArea( xRanges ); // incl. cursor if nothing is selected
686 size_t nRangeCount = xRanges->size();
687 bool bContinue = true;
689 for (size_t nRangePos=0; nRangePos < nRangeCount && bContinue; nRangePos++)
691 ScRange aRange = *(*xRanges)[nRangePos];
692 SCCOL nStartCol = aRange.aStart.Col();
693 SCROW nStartRow = aRange.aStart.Row();
694 SCCOL nEndCol = aRange.aEnd.Col();
695 SCROW nEndRow = aRange.aEnd.Row();
696 SCTAB nTab = aRange.aStart.Tab();
698 for (SCROW nRow=nStartRow; nRow<=nEndRow && bContinue; nRow++)
699 for (SCCOL nCol=nStartCol; nCol<=nEndCol && bContinue; nCol++)
701 sheet::DataPilotTableHeaderData aData;
702 pDPObj->GetHeaderPositionData(ScAddress(nCol, nRow, nTab), aData);
703 if ( aData.Dimension < 0 )
704 bContinue = false; // not part of any dimension
705 else
707 if ( nStartDimension < 0 ) // first member?
709 nStartDimension = aData.Dimension;
710 nStartHierarchy = aData.Hierarchy;
711 nStartLevel = aData.Level;
713 if ( aData.Dimension != nStartDimension ||
714 aData.Hierarchy != nStartHierarchy ||
715 aData.Level != nStartLevel )
717 bContinue = false; // cannot mix dimensions
720 if ( bContinue )
722 // accept any part of a member description, also subtotals,
723 // but don't stop if empty parts are contained
724 if ( aData.Flags & sheet::MemberResultFlags::HASMEMBER )
725 rEntries.insert(aData.MemberName);
730 rDimension = nStartDimension; // dimension from which the found members came
731 if (!bContinue)
732 rEntries.clear(); // remove all if not valid
735 bool ScDBFunc::HasSelectionForDateGroup( ScDPNumGroupInfo& rOldInfo, sal_Int32& rParts )
737 // determine if the date group dialog has to be shown for the current selection
739 bool bFound = false;
741 SCCOL nCurX = GetViewData().GetCurX();
742 SCROW nCurY = GetViewData().GetCurY();
743 SCTAB nTab = GetViewData().GetTabNo();
744 ScDocument* pDoc = GetViewData().GetDocument();
746 ScDPObject* pDPObj = pDoc->GetDPAtCursor( nCurX, nCurY, nTab );
747 if ( pDPObj )
749 ScDPUniqueStringSet aEntries;
750 long nSelectDimension = -1;
751 GetSelectedMemberList( aEntries, nSelectDimension );
753 if (!aEntries.empty())
755 bool bIsDataLayout;
756 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
757 OUString aBaseDimName( aDimName );
759 bool bInGroupDim = false;
760 bool bFoundParts = false;
762 ScDPDimensionSaveData* pDimData =
763 const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
764 if ( pDimData )
766 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
767 const ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDim( aDimName );
768 if ( pNumGroupDim )
770 // existing num group dimension
772 if ( pNumGroupDim->GetDatePart() != 0 )
774 // dimension has date info -> edit settings of this dimension
775 // (parts are collected below)
777 rOldInfo = pNumGroupDim->GetDateInfo();
778 bFound = true;
780 else if ( pNumGroupDim->GetInfo().mbDateValues )
782 // Numerical grouping with DateValues flag is used for grouping
783 // of days with a "Number of days" value.
785 rOldInfo = pNumGroupDim->GetInfo();
786 rParts = com::sun::star::sheet::DataPilotFieldGroupBy::DAYS; // not found in CollectDateParts
787 bFoundParts = true;
788 bFound = true;
790 bInGroupDim = true;
792 else if ( pGroupDim )
794 // existing additional group dimension
796 if ( pGroupDim->GetDatePart() != 0 )
798 // dimension has date info -> edit settings of this dimension
799 // (parts are collected below)
801 rOldInfo = pGroupDim->GetDateInfo();
802 aBaseDimName = pGroupDim->GetSourceDimName();
803 bFound = true;
805 bInGroupDim = true;
808 if ( bFound && !bFoundParts )
810 // collect date parts from all group dimensions
811 rParts = pDimData->CollectDateParts( aBaseDimName );
813 if ( !bFound && !bInGroupDim )
815 // create new date group dimensions if the selection is a single cell
816 // in a normal dimension with date content
818 ScRange aSelRange;
819 if ( (GetViewData().GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
820 aSelRange.aStart == aSelRange.aEnd )
822 SCCOL nSelCol = aSelRange.aStart.Col();
823 SCROW nSelRow = aSelRange.aStart.Row();
824 SCTAB nSelTab = aSelRange.aStart.Tab();
825 if ( pDoc->HasValueData( nSelCol, nSelRow, nSelTab ) )
827 sal_uLong nIndex = static_cast<const SfxUInt32Item*>(pDoc->GetAttr(
828 nSelCol, nSelRow, nSelTab, ATTR_VALUE_FORMAT))->GetValue();
829 short nType = pDoc->GetFormatTable()->GetType(nIndex);
830 if ( nType == css::util::NumberFormat::DATE || nType == css::util::NumberFormat::TIME || nType == css::util::NumberFormat::DATETIME )
832 bFound = true;
833 // use currently selected value for automatic limits
834 if( rOldInfo.mbAutoStart )
835 rOldInfo.mfStart = pDoc->GetValue( aSelRange.aStart );
836 if( rOldInfo.mbAutoEnd )
837 rOldInfo.mfEnd = pDoc->GetValue( aSelRange.aStart );
845 return bFound;
848 bool ScDBFunc::HasSelectionForNumGroup( ScDPNumGroupInfo& rOldInfo )
850 // determine if the numeric group dialog has to be shown for the current selection
852 bool bFound = false;
854 SCCOL nCurX = GetViewData().GetCurX();
855 SCROW nCurY = GetViewData().GetCurY();
856 SCTAB nTab = GetViewData().GetTabNo();
857 ScDocument* pDoc = GetViewData().GetDocument();
859 ScDPObject* pDPObj = pDoc->GetDPAtCursor( nCurX, nCurY, nTab );
860 if ( pDPObj )
862 ScDPUniqueStringSet aEntries;
863 long nSelectDimension = -1;
864 GetSelectedMemberList( aEntries, nSelectDimension );
866 if (!aEntries.empty())
868 bool bIsDataLayout;
869 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
871 bool bInGroupDim = false;
873 ScDPDimensionSaveData* pDimData =
874 const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
875 if ( pDimData )
877 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
878 if ( pNumGroupDim )
880 // existing num group dimension
881 // -> edit settings of this dimension
883 rOldInfo = pNumGroupDim->GetInfo();
884 bFound = true;
886 else if ( pDimData->GetNamedGroupDim( aDimName ) )
887 bInGroupDim = true; // in a group dimension
889 if ( !bFound && !bInGroupDim )
891 // create a new num group dimension if the selection is a single cell
892 // in a normal dimension with numeric content
894 ScRange aSelRange;
895 if ( (GetViewData().GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
896 aSelRange.aStart == aSelRange.aEnd )
898 if ( pDoc->HasValueData( aSelRange.aStart.Col(), aSelRange.aStart.Row(),
899 aSelRange.aStart.Tab() ) )
901 bFound = true;
902 // use currently selected value for automatic limits
903 if( rOldInfo.mbAutoStart )
904 rOldInfo.mfStart = pDoc->GetValue( aSelRange.aStart );
905 if( rOldInfo.mbAutoEnd )
906 rOldInfo.mfEnd = pDoc->GetValue( aSelRange.aStart );
913 return bFound;
916 void ScDBFunc::DateGroupDataPilot( const ScDPNumGroupInfo& rInfo, sal_Int32 nParts )
918 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
919 GetViewData().GetCurY(), GetViewData().GetTabNo() );
920 if (!pDPObj)
921 return;
923 ScDPUniqueStringSet aEntries;
924 long nSelectDimension = -1;
925 GetSelectedMemberList( aEntries, nSelectDimension );
927 if (aEntries.empty())
928 return;
930 std::vector<OUString> aDeletedNames;
931 bool bIsDataLayout;
932 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
934 ScDPSaveData aData( *pDPObj->GetSaveData() );
935 ScDPDimensionSaveData* pDimData = aData.GetDimensionData(); // created if not there
937 // find the source dimension name.
938 OUString aBaseDimName = aDimName;
939 if( const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName ) )
940 aBaseDimName = pBaseGroupDim->GetSourceDimName();
942 // Remove all group dimensions associated with this source dimension. For
943 // date grouping, we need to remove all existing groups for the affected
944 // source dimension and build new one(s) from scratch. Keep the deleted
945 // names so that they can be reused during re-construction.
946 aData.RemoveAllGroupDimensions(aBaseDimName, &aDeletedNames);
948 if ( nParts )
950 // create date group dimensions
952 ScDPNumGroupInfo aEmpty;
953 bool bFirst = true;
954 sal_Int32 nMask = 1;
955 for (sal_uInt16 nBit=0; nBit<32; nBit++)
957 if ( nParts & nMask )
959 if ( bFirst )
961 // innermost part: create NumGroupDimension (replacing original values)
962 // Dimension name is left unchanged
964 if ( (nParts == sheet::DataPilotFieldGroupBy::DAYS) && (rInfo.mfStep >= 1.0) )
966 // only days, and a step value specified: use numerical grouping
967 // with DateValues flag, not date grouping
969 ScDPNumGroupInfo aNumInfo( rInfo );
970 aNumInfo.mbDateValues = true;
972 ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, aNumInfo );
973 pDimData->AddNumGroupDimension( aNumGroupDim );
975 else
977 ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, rInfo, nMask );
978 pDimData->AddNumGroupDimension( aNumGroupDim );
981 bFirst = false;
983 else
985 // additional parts: create GroupDimension (shown as additional dimensions)
986 OUString aGroupDimName =
987 pDimData->CreateDateGroupDimName(nMask, *pDPObj, true, &aDeletedNames);
988 ScDPSaveGroupDimension aGroupDim( aBaseDimName, aGroupDimName );
989 aGroupDim.SetDateInfo( rInfo, nMask );
990 pDimData->AddGroupDimension( aGroupDim );
992 // set orientation
993 ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
994 if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
996 ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aBaseDimName );
997 pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
998 long nPosition = 0; //! before (immediate) base
999 aData.SetPosition( pSaveDimension, nPosition );
1003 nMask *= 2;
1007 // apply changes
1008 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1009 pDPObj->SetSaveData( aData );
1010 aFunc.RefreshPivotTableGroups(pDPObj);
1012 // unmark cell selection
1013 Unmark();
1016 void ScDBFunc::NumGroupDataPilot( const ScDPNumGroupInfo& rInfo )
1018 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
1019 GetViewData().GetCurY(), GetViewData().GetTabNo() );
1020 if (!pDPObj)
1021 return;
1023 ScDPUniqueStringSet aEntries;
1024 long nSelectDimension = -1;
1025 GetSelectedMemberList( aEntries, nSelectDimension );
1027 if (aEntries.empty())
1028 return;
1030 bool bIsDataLayout;
1031 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1033 ScDPSaveData aData( *pDPObj->GetSaveData() );
1034 ScDPDimensionSaveData* pDimData = aData.GetDimensionData(); // created if not there
1036 ScDPSaveNumGroupDimension* pExisting = pDimData->GetNumGroupDimAcc( aDimName );
1037 if ( pExisting )
1039 // modify existing group dimension
1040 pExisting->SetGroupInfo( rInfo );
1042 else
1044 // create new group dimension
1045 ScDPSaveNumGroupDimension aNumGroupDim( aDimName, rInfo );
1046 pDimData->AddNumGroupDimension( aNumGroupDim );
1049 // apply changes
1050 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1051 pDPObj->SetSaveData( aData );
1052 aFunc.RefreshPivotTableGroups(pDPObj);
1054 // unmark cell selection
1055 Unmark();
1058 void ScDBFunc::GroupDataPilot()
1060 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
1061 GetViewData().GetCurY(), GetViewData().GetTabNo() );
1062 if (!pDPObj)
1063 return;
1065 ScDPUniqueStringSet aEntries;
1066 long nSelectDimension = -1;
1067 GetSelectedMemberList( aEntries, nSelectDimension );
1069 if (aEntries.empty())
1070 return;
1072 bool bIsDataLayout;
1073 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1075 ScDPSaveData aData( *pDPObj->GetSaveData() );
1076 ScDPDimensionSaveData* pDimData = aData.GetDimensionData(); // created if not there
1078 // find original base
1079 OUString aBaseDimName = aDimName;
1080 const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName );
1081 if ( pBaseGroupDim )
1083 // any entry's SourceDimName is the original base
1084 aBaseDimName = pBaseGroupDim->GetSourceDimName();
1087 // find existing group dimension
1088 // (using the selected dim, can be intermediate group dim)
1089 ScDPSaveGroupDimension* pGroupDimension = pDimData->GetGroupDimAccForBase( aDimName );
1091 // remove the selected items from their groups
1092 // (empty groups are removed, too)
1093 if ( pGroupDimension )
1095 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1096 for (; it != itEnd; ++it)
1098 const OUString& aEntryName = *it;
1099 if ( pBaseGroupDim )
1101 // for each selected (intermediate) group, remove all its items
1102 // (same logic as for adding, below)
1103 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
1104 if ( pBaseGroup )
1105 pBaseGroup->RemoveElementsFromGroups( *pGroupDimension ); // remove all elements
1106 else
1107 pGroupDimension->RemoveFromGroups( aEntryName );
1109 else
1110 pGroupDimension->RemoveFromGroups( aEntryName );
1114 ScDPSaveGroupDimension* pNewGroupDim = NULL;
1115 if ( !pGroupDimension )
1117 // create a new group dimension
1118 OUString aGroupDimName =
1119 pDimData->CreateGroupDimName(aBaseDimName, *pDPObj, false, NULL);
1120 pNewGroupDim = new ScDPSaveGroupDimension( aBaseDimName, aGroupDimName );
1122 pGroupDimension = pNewGroupDim; // make changes to the new dim if none existed
1124 if ( pBaseGroupDim )
1126 // If it's a higher-order group dimension, pre-allocate groups for all
1127 // non-selected original groups, so the individual base members aren't
1128 // used for automatic groups (this would make the original groups hard
1129 // to find).
1130 //! Also do this when removing groups?
1131 //! Handle this case dynamically with automatic groups?
1133 long nGroupCount = pBaseGroupDim->GetGroupCount();
1134 for ( long nGroup = 0; nGroup < nGroupCount; nGroup++ )
1136 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetGroupByIndex( nGroup );
1138 if (!aEntries.count(pBaseGroup->GetGroupName()))
1140 // add an additional group for each item that is not in the selection
1141 ScDPSaveGroupItem aGroup( pBaseGroup->GetGroupName() );
1142 aGroup.AddElementsFromGroup( *pBaseGroup );
1143 pGroupDimension->AddGroupItem( aGroup );
1148 OUString aGroupDimName = pGroupDimension->GetGroupDimName();
1150 OUString aGroupName = pGroupDimension->CreateGroupName(ScGlobal::GetRscString(STR_PIVOT_GROUP));
1151 ScDPSaveGroupItem aGroup( aGroupName );
1152 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1153 for (; it != itEnd; ++it)
1155 const OUString& aEntryName = *it;
1156 if ( pBaseGroupDim )
1158 // for each selected (intermediate) group, add all its items
1159 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
1160 if ( pBaseGroup )
1161 aGroup.AddElementsFromGroup( *pBaseGroup );
1162 else
1163 aGroup.AddElement( aEntryName ); // no group found -> automatic group, add the item itself
1165 else
1166 aGroup.AddElement( aEntryName ); // no group dimension, add all items directly
1169 pGroupDimension->AddGroupItem( aGroup );
1171 if ( pNewGroupDim )
1173 pDimData->AddGroupDimension( *pNewGroupDim );
1174 delete pNewGroupDim; // AddGroupDimension copies the object
1175 // don't access pGroupDimension after here
1177 pGroupDimension = pNewGroupDim = NULL;
1179 // set orientation
1180 ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
1181 if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
1183 ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aDimName );
1184 pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
1185 long nPosition = 0; //! before (immediate) base
1186 aData.SetPosition( pSaveDimension, nPosition );
1189 // apply changes
1190 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1191 pDPObj->SetSaveData( aData );
1192 aFunc.RefreshPivotTableGroups(pDPObj);
1194 // unmark cell selection
1195 Unmark();
1198 void ScDBFunc::UngroupDataPilot()
1200 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
1201 GetViewData().GetCurY(), GetViewData().GetTabNo() );
1202 if (!pDPObj)
1203 return;
1205 ScDPUniqueStringSet aEntries;
1206 long nSelectDimension = -1;
1207 GetSelectedMemberList( aEntries, nSelectDimension );
1209 if (aEntries.empty())
1210 return;
1212 bool bIsDataLayout;
1213 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1215 ScDPSaveData aData( *pDPObj->GetSaveData() );
1216 if (!aData.GetExistingDimensionData())
1217 // There is nothing to ungroup.
1218 return;
1220 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1222 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
1223 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
1224 if ( ( pGroupDim && pGroupDim->GetDatePart() != 0 ) ||
1225 ( pNumGroupDim && pNumGroupDim->GetDatePart() != 0 ) )
1227 // Date grouping: need to remove all affected group dimensions.
1228 // This is done using DateGroupDataPilot with nParts=0.
1230 DateGroupDataPilot( ScDPNumGroupInfo(), 0 );
1231 return;
1234 if ( pGroupDim )
1236 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1237 for (; it != itEnd; ++it)
1238 pGroupDim->RemoveGroup(*it);
1240 // remove group dimension if empty
1241 bool bEmptyDim = pGroupDim->IsEmpty();
1242 if ( !bEmptyDim )
1244 // If all remaining groups in the dimension aren't shown, remove
1245 // the dimension too, as if it was completely empty.
1246 ScDPUniqueStringSet aVisibleEntries;
1247 pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
1248 bEmptyDim = pGroupDim->HasOnlyHidden( aVisibleEntries );
1250 if ( bEmptyDim )
1252 pDimData->RemoveGroupDimension( aDimName ); // pGroupDim is deleted
1254 // also remove SaveData settings for the dimension that no longer exists
1255 aData.RemoveDimensionByName( aDimName );
1258 else if ( pNumGroupDim )
1260 // remove the numerical grouping
1261 pDimData->RemoveNumGroupDimension( aDimName );
1262 // SaveData settings can remain unchanged - the same dimension still exists
1265 // apply changes
1266 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1267 pDPObj->SetSaveData( aData );
1268 aFunc.RefreshPivotTableGroups(pDPObj);
1270 // unmark cell selection
1271 Unmark();
1274 static OUString lcl_replaceMemberNameInSubtotal(const OUString& rSubtotal, const OUString& rMemberName)
1276 sal_Int32 n = rSubtotal.getLength();
1277 const sal_Unicode* p = rSubtotal.getStr();
1278 OUStringBuffer aBuf, aWordBuf;
1279 for (sal_Int32 i = 0; i < n; ++i)
1281 sal_Unicode c = p[i];
1282 if (c == ' ')
1284 OUString aWord = aWordBuf.makeStringAndClear();
1285 if (aWord.equals(rMemberName))
1286 aBuf.append('?');
1287 else
1288 aBuf.append(aWord);
1289 aBuf.append(c);
1291 else if (c == '\\')
1293 // Escape a backslash character.
1294 aWordBuf.append(c);
1295 aWordBuf.append(c);
1297 else if (c == '?')
1299 // A literal '?' must be escaped with a backslash ('\');
1300 aWordBuf.append('\\');
1301 aWordBuf.append(c);
1303 else
1304 aWordBuf.append(c);
1307 if (!aWordBuf.isEmpty())
1309 OUString aWord = aWordBuf.makeStringAndClear();
1310 if (aWord.equals(rMemberName))
1311 aBuf.append('?');
1312 else
1313 aBuf.append(aWord);
1316 return aBuf.makeStringAndClear();
1319 void ScDBFunc::DataPilotInput( const ScAddress& rPos, const OUString& rString )
1321 using namespace ::com::sun::star::sheet;
1323 ScDocument* pDoc = GetViewData().GetDocument();
1324 ScDPObject* pDPObj = pDoc->GetDPAtCursor( rPos.Col(), rPos.Row(), rPos.Tab() );
1325 if (!pDPObj)
1326 return;
1328 OUString aOldText = pDoc->GetString(rPos.Col(), rPos.Row(), rPos.Tab());
1330 if ( aOldText == rString )
1332 // nothing to do: silently exit
1333 return;
1336 sal_uInt16 nErrorId = 0;
1338 pDPObj->BuildAllDimensionMembers();
1339 ScDPSaveData aData( *pDPObj->GetSaveData() );
1340 bool bChange = false;
1342 sal_uInt16 nOrient = DataPilotFieldOrientation_HIDDEN;
1343 long nField = pDPObj->GetHeaderDim( rPos, nOrient );
1344 if ( nField >= 0 )
1346 // changing a field title
1347 if ( aData.GetExistingDimensionData() )
1349 // only group dimensions can be renamed
1351 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1352 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aOldText );
1353 if ( pGroupDim )
1355 // valid name: not empty, no existing dimension (group or other)
1356 if (!rString.isEmpty() && !pDPObj->IsDimNameInUse(rString))
1358 pGroupDim->Rename( rString );
1360 // also rename in SaveData to preserve the field settings
1361 ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aOldText );
1362 pSaveDim->SetName( rString );
1364 bChange = true;
1366 else
1367 nErrorId = STR_INVALIDNAME;
1370 else if (nOrient == DataPilotFieldOrientation_COLUMN || nOrient == DataPilotFieldOrientation_ROW)
1372 bool bDataLayout = false;
1373 OUString aDimName = pDPObj->GetDimName(nField, bDataLayout);
1374 ScDPSaveDimension* pDim = bDataLayout ? aData.GetDataLayoutDimension() : aData.GetDimensionByName(aDimName);
1375 if (pDim)
1377 if (!rString.isEmpty())
1379 if (rString.equalsIgnoreAsciiCase(aDimName))
1381 pDim->RemoveLayoutName();
1382 bChange = true;
1384 else if (!pDPObj->IsDimNameInUse(rString))
1386 pDim->SetLayoutName(rString);
1387 bChange = true;
1389 else
1390 nErrorId = STR_INVALIDNAME;
1392 else
1393 nErrorId = STR_INVALIDNAME;
1397 else if (pDPObj->IsDataDescriptionCell(rPos))
1399 // There is only one data dimension.
1400 ScDPSaveDimension* pDim = aData.GetFirstDimension(sheet::DataPilotFieldOrientation_DATA);
1401 if (pDim)
1403 if (!rString.isEmpty())
1405 if (pDim->GetName().equalsIgnoreAsciiCase(rString))
1407 pDim->RemoveLayoutName();
1408 bChange = true;
1410 else if (!pDPObj->IsDimNameInUse(rString))
1412 pDim->SetLayoutName(rString);
1413 bChange = true;
1415 else
1416 nErrorId = STR_INVALIDNAME;
1418 else
1419 nErrorId = STR_INVALIDNAME;
1422 else
1424 // This is not a field header.
1425 sheet::DataPilotTableHeaderData aPosData;
1426 pDPObj->GetHeaderPositionData(rPos, aPosData);
1428 if ((aPosData.Flags & MemberResultFlags::HASMEMBER) && !aOldText.isEmpty())
1430 if ( aData.GetExistingDimensionData() && !(aPosData.Flags & MemberResultFlags::SUBTOTAL))
1432 bool bIsDataLayout;
1433 OUString aDimName = pDPObj->GetDimName( aPosData.Dimension, bIsDataLayout );
1435 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
1436 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
1437 if ( pGroupDim )
1439 // valid name: not empty, no existing group in this dimension
1440 //! ignore case?
1441 if (!rString.isEmpty() && !pGroupDim->GetNamedGroup(rString))
1443 ScDPSaveGroupItem* pGroup = pGroupDim->GetNamedGroupAcc( aOldText );
1444 if ( pGroup )
1445 pGroup->Rename( rString ); // rename the existing group
1446 else
1448 // create a new group to replace the automatic group
1449 ScDPSaveGroupItem aGroup( rString );
1450 aGroup.AddElement( aOldText );
1451 pGroupDim->AddGroupItem( aGroup );
1454 // in both cases also adjust savedata, to preserve member settings (show details)
1455 ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aDimName );
1456 ScDPSaveMember* pSaveMember = pSaveDim->GetExistingMemberByName( aOldText );
1457 if ( pSaveMember )
1458 pSaveMember->SetName( rString );
1460 bChange = true;
1462 else
1463 nErrorId = STR_INVALIDNAME;
1466 else if ((aPosData.Flags & MemberResultFlags::GRANDTOTAL))
1468 aData.SetGrandTotalName(rString);
1469 bChange = true;
1471 else if (aPosData.Dimension >= 0 && !aPosData.MemberName.isEmpty())
1473 bool bDataLayout = false;
1474 OUString aDimName = pDPObj->GetDimName(static_cast<long>(aPosData.Dimension), bDataLayout);
1475 if (bDataLayout)
1477 // data dimension
1480 if ((aPosData.Flags & MemberResultFlags::SUBTOTAL))
1481 break;
1483 ScDPSaveDimension* pDim = aData.GetDimensionByName(aPosData.MemberName);
1484 if (!pDim)
1485 break;
1487 if (rString.isEmpty())
1489 nErrorId = STR_INVALIDNAME;
1490 break;
1493 if (aPosData.MemberName.equalsIgnoreAsciiCase(rString))
1495 pDim->RemoveLayoutName();
1496 bChange = true;
1498 else if (!pDPObj->IsDimNameInUse(rString))
1500 pDim->SetLayoutName(rString);
1501 bChange = true;
1503 else
1504 nErrorId = STR_INVALIDNAME;
1506 while (false);
1508 else
1510 // field member
1513 ScDPSaveDimension* pDim = aData.GetDimensionByName(aDimName);
1514 if (!pDim)
1515 break;
1517 ScDPSaveMember* pMem = pDim->GetExistingMemberByName(aPosData.MemberName);
1518 if (!pMem)
1519 break;
1521 if ((aPosData.Flags & MemberResultFlags::SUBTOTAL))
1523 // Change subtotal only when the table has one data dimension.
1524 if (aData.GetDataDimensionCount() > 1)
1525 break;
1527 // display name for subtotal is allowed only if the subtotal type is 'Automatic'.
1528 if (pDim->GetSubTotalsCount() != 1)
1529 break;
1531 if (pDim->GetSubTotalFunc(0) != sheet::GeneralFunction_AUTO)
1532 break;
1534 const OUString* pLayoutName = pMem->GetLayoutName();
1535 OUString aMemberName;
1536 if (pLayoutName)
1537 aMemberName = *pLayoutName;
1538 else
1539 aMemberName = aPosData.MemberName;
1541 OUString aNew = lcl_replaceMemberNameInSubtotal(rString, aMemberName);
1542 pDim->SetSubtotalName(aNew);
1543 bChange = true;
1545 else
1547 // Check to make sure the member name isn't
1548 // already used.
1549 if (!rString.isEmpty())
1551 if (rString.equalsIgnoreAsciiCase(pMem->GetName()))
1553 pMem->RemoveLayoutName();
1554 bChange = true;
1556 else if (!pDim->IsMemberNameInUse(rString))
1558 pMem->SetLayoutName(rString);
1559 bChange = true;
1561 else
1562 nErrorId = STR_INVALIDNAME;
1564 else
1565 nErrorId = STR_INVALIDNAME;
1568 while (false);
1574 if ( bChange )
1576 // apply changes
1577 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1578 pDPObj->SetSaveData( aData );
1579 aFunc.UpdatePivotTable(*pDPObj, true, false);
1581 else
1583 if ( !nErrorId )
1584 nErrorId = STR_ERR_DATAPILOT_INPUT;
1585 ErrorMessage( nErrorId );
1589 static void lcl_MoveToEnd( ScDPSaveDimension& rDim, const OUString& rItemName )
1591 ScDPSaveMember* pNewMember = NULL;
1592 const ScDPSaveMember* pOldMember = rDim.GetExistingMemberByName( rItemName );
1593 if ( pOldMember )
1594 pNewMember = new ScDPSaveMember( *pOldMember );
1595 else
1596 pNewMember = new ScDPSaveMember( rItemName );
1597 rDim.AddMember( pNewMember );
1598 // AddMember takes ownership of the new pointer,
1599 // puts it to the end of the list even if it was in the list before.
1602 struct ScOUStringCollate
1604 CollatorWrapper* mpCollator;
1606 ScOUStringCollate(CollatorWrapper* pColl) : mpCollator(pColl) {}
1608 bool operator()(const OUString& rStr1, const OUString& rStr2) const
1610 return ( mpCollator->compareString(rStr1, rStr2) < 0 );
1614 bool ScDBFunc::DataPilotSort( const ScAddress& rPos, bool bAscending, sal_uInt16* pUserListId )
1616 ScDocument* pDoc = GetViewData().GetDocument();
1617 ScDPObject* pDPObj = pDoc->GetDPAtCursor(rPos.Col(), rPos.Row(), rPos.Tab());
1618 if (!pDPObj)
1619 return false;
1621 // We need to run this to get all members later.
1622 if ( pUserListId )
1623 pDPObj->BuildAllDimensionMembers();
1625 sal_uInt16 nOrientation;
1626 long nDimIndex = pDPObj->GetHeaderDim(rPos, nOrientation);
1627 if (nDimIndex < 0)
1628 // Invalid dimension index. Bail out.
1629 return false;
1631 ScDPSaveData* pSaveData = pDPObj->GetSaveData();
1632 if (!pSaveData)
1633 return false;
1635 ScDPSaveData aNewSaveData(*pSaveData);
1636 bool bDataLayout;
1637 OUString aDimName = pDPObj->GetDimName(nDimIndex, bDataLayout);
1638 ScDPSaveDimension* pSaveDim = aNewSaveData.GetDimensionByName(aDimName);
1639 if (!pSaveDim)
1640 return false;
1642 // manual evaluation of sort order is only needed if a user list id is given
1643 if ( pUserListId )
1645 typedef ScDPSaveDimension::MemberList MemList;
1646 const MemList& rDimMembers = pSaveDim->GetMembers();
1647 list<OUString> aMembers;
1648 std::unordered_set<OUString, OUStringHash> aMemberSet;
1649 size_t nMemberCount = 0;
1650 for (MemList::const_iterator itr = rDimMembers.begin(), itrEnd = rDimMembers.end();
1651 itr != itrEnd; ++itr)
1653 ScDPSaveMember* pMem = *itr;
1654 aMembers.push_back(pMem->GetName());
1655 aMemberSet.insert(pMem->GetName());
1656 ++nMemberCount;
1659 // Sort the member list in ascending order.
1660 ScOUStringCollate aCollate( ScGlobal::GetCollator() );
1661 aMembers.sort(aCollate);
1663 // Collect and rank those custom sort strings that also exist in the member name list.
1665 typedef std::unordered_map<OUString, sal_uInt16, OUStringHash> UserSortMap;
1666 UserSortMap aSubStrs;
1667 sal_uInt16 nSubCount = 0;
1668 if (pUserListId)
1670 ScUserList* pUserList = ScGlobal::GetUserList();
1671 if (!pUserList)
1672 return false;
1675 size_t n = pUserList->size();
1676 if (!n || *pUserListId >= static_cast<sal_uInt16>(n))
1677 return false;
1680 const ScUserListData* pData = (*pUserList)[*pUserListId];
1681 if (pData)
1683 sal_uInt16 n = pData->GetSubCount();
1684 for (sal_uInt16 i = 0; i < n; ++i)
1686 OUString aSub = pData->GetSubStr(i);
1687 if (!aMemberSet.count(aSub))
1688 // This string doesn't exist in the member name set. Don't add this.
1689 continue;
1691 aSubStrs.insert(UserSortMap::value_type(aSub, nSubCount++));
1696 // Rank all members.
1698 vector<OUString> aRankedNames(nMemberCount);
1699 sal_uInt16 nCurStrId = 0;
1700 for (list<OUString>::const_iterator itr = aMembers.begin(), itrEnd = aMembers.end();
1701 itr != itrEnd; ++itr)
1703 OUString aName = *itr;
1704 sal_uInt16 nRank = 0;
1705 UserSortMap::const_iterator itrSub = aSubStrs.find(aName);
1706 if (itrSub == aSubStrs.end())
1707 nRank = nSubCount + nCurStrId++;
1708 else
1709 nRank = itrSub->second;
1711 if (!bAscending)
1712 nRank = static_cast< sal_uInt16 >( nMemberCount - nRank - 1 );
1714 aRankedNames[nRank] = aName;
1717 // Re-order ScDPSaveMember instances with the new ranks.
1719 for (vector<OUString>::const_iterator itr = aRankedNames.begin(), itrEnd = aRankedNames.end();
1720 itr != itrEnd; ++itr)
1722 const ScDPSaveMember* pOldMem = pSaveDim->GetExistingMemberByName(*itr);
1723 if (!pOldMem)
1724 // All members are supposed to be present.
1725 continue;
1727 ScDPSaveMember* pNewMem = new ScDPSaveMember(*pOldMem);
1728 pSaveDim->AddMember(pNewMem);
1731 // Set the sorting mode to manual for now. We may introduce a new sorting
1732 // mode later on.
1734 sheet::DataPilotFieldSortInfo aSortInfo;
1735 aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
1736 pSaveDim->SetSortInfo(&aSortInfo);
1738 else
1740 // without user list id, just apply sorting mode
1742 sheet::DataPilotFieldSortInfo aSortInfo;
1743 aSortInfo.Mode = sheet::DataPilotFieldSortMode::NAME;
1744 aSortInfo.IsAscending = bAscending;
1745 pSaveDim->SetSortInfo(&aSortInfo);
1748 // Update the datapilot with the newly sorted field members.
1750 boost::scoped_ptr<ScDPObject> pNewObj(new ScDPObject(*pDPObj));
1751 pNewObj->SetSaveData(aNewSaveData);
1752 ScDBDocFunc aFunc(*GetViewData().GetDocShell());
1754 return aFunc.DataPilotUpdate(pDPObj, pNewObj.get(), true, false);
1757 bool ScDBFunc::DataPilotMove( const ScRange& rSource, const ScAddress& rDest )
1759 bool bRet = false;
1760 ScDocument* pDoc = GetViewData().GetDocument();
1761 ScDPObject* pDPObj = pDoc->GetDPAtCursor( rSource.aStart.Col(), rSource.aStart.Row(), rSource.aStart.Tab() );
1762 if ( pDPObj && pDPObj == pDoc->GetDPAtCursor( rDest.Col(), rDest.Row(), rDest.Tab() ) )
1764 sheet::DataPilotTableHeaderData aDestData;
1765 pDPObj->GetHeaderPositionData( rDest, aDestData );
1766 bool bValid = ( aDestData.Dimension >= 0 ); // dropping onto a field
1768 // look through the source range
1769 std::unordered_set< OUString, OUStringHash, std::equal_to<OUString> > aMembersSet; // for lookup
1770 std::vector< OUString > aMembersVector; // members in original order, for inserting
1771 aMembersVector.reserve( std::max( static_cast<SCSIZE>( rSource.aEnd.Col() - rSource.aStart.Col() + 1 ),
1772 static_cast<SCSIZE>( rSource.aEnd.Row() - rSource.aStart.Row() + 1 ) ) );
1773 for (SCROW nRow = rSource.aStart.Row(); bValid && nRow <= rSource.aEnd.Row(); ++nRow )
1774 for (SCCOL nCol = rSource.aStart.Col(); bValid && nCol <= rSource.aEnd.Col(); ++nCol )
1776 sheet::DataPilotTableHeaderData aSourceData;
1777 pDPObj->GetHeaderPositionData( ScAddress( nCol, nRow, rSource.aStart.Tab() ), aSourceData );
1778 if ( aSourceData.Dimension == aDestData.Dimension && !aSourceData.MemberName.isEmpty() )
1780 if ( aMembersSet.find( aSourceData.MemberName ) == aMembersSet.end() )
1782 aMembersSet.insert( aSourceData.MemberName );
1783 aMembersVector.push_back( aSourceData.MemberName );
1785 // duplicates are ignored
1787 else
1788 bValid = false; // empty (subtotal) or different field
1791 if ( bValid )
1793 bool bIsDataLayout;
1794 OUString aDimName = pDPObj->GetDimName( aDestData.Dimension, bIsDataLayout );
1795 if ( !bIsDataLayout )
1797 ScDPSaveData aData( *pDPObj->GetSaveData() );
1798 ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
1800 // get all member names in source order
1801 uno::Sequence<OUString> aMemberNames;
1802 pDPObj->GetMemberNames( aDestData.Dimension, aMemberNames );
1804 bool bInserted = false;
1806 sal_Int32 nMemberCount = aMemberNames.getLength();
1807 for (sal_Int32 nMemberPos=0; nMemberPos<nMemberCount; ++nMemberPos)
1809 OUString aMemberStr( aMemberNames[nMemberPos] );
1811 if ( !bInserted && aMemberNames[nMemberPos] == aDestData.MemberName )
1813 // insert dragged items before this item
1814 for ( std::vector<OUString>::const_iterator aIter = aMembersVector.begin();
1815 aIter != aMembersVector.end(); ++aIter )
1816 lcl_MoveToEnd( *pDim, *aIter );
1817 bInserted = true;
1820 if ( aMembersSet.find( aMemberStr ) == aMembersSet.end() ) // skip dragged items
1821 lcl_MoveToEnd( *pDim, aMemberStr );
1823 // insert dragged item at end if dest wasn't found (for example, empty)
1824 if ( !bInserted )
1825 for ( std::vector<OUString>::const_iterator aIter = aMembersVector.begin();
1826 aIter != aMembersVector.end(); ++aIter )
1827 lcl_MoveToEnd( *pDim, *aIter );
1829 // Items that were in SaveData, but not in the source, end up at the start of the list.
1831 // set flag for manual sorting
1832 sheet::DataPilotFieldSortInfo aSortInfo;
1833 aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
1834 pDim->SetSortInfo( &aSortInfo );
1836 // apply changes
1837 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1838 boost::scoped_ptr<ScDPObject> pNewObj(new ScDPObject( *pDPObj ));
1839 pNewObj->SetSaveData( aData );
1840 aFunc.DataPilotUpdate( pDPObj, pNewObj.get(), true, false ); //! bApi for drag&drop?
1841 pNewObj.reset();
1843 Unmark(); // entry was moved - no use in leaving the old cell selected
1845 bRet = true;
1850 return bRet;
1853 bool ScDBFunc::HasSelectionForDrillDown( sal_uInt16& rOrientation )
1855 bool bRet = false;
1857 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
1858 GetViewData().GetCurY(), GetViewData().GetTabNo() );
1859 if ( pDPObj )
1861 ScDPUniqueStringSet aEntries;
1862 long nSelectDimension = -1;
1863 GetSelectedMemberList( aEntries, nSelectDimension );
1865 if (!aEntries.empty())
1867 bool bIsDataLayout;
1868 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1869 if ( !bIsDataLayout )
1871 ScDPSaveData* pSaveData = pDPObj->GetSaveData();
1872 ScDPSaveDimension* pDim = pSaveData->GetExistingDimensionByName( aDimName );
1873 if ( pDim )
1875 sal_uInt16 nDimOrient = pDim->GetOrientation();
1876 ScDPSaveDimension* pInner = pSaveData->GetInnermostDimension( nDimOrient );
1877 if ( pDim == pInner )
1879 rOrientation = nDimOrient;
1880 bRet = true;
1887 return bRet;
1890 void ScDBFunc::SetDataPilotDetails(bool bShow, const OUString* pNewDimensionName)
1892 ScDPObject* pDPObj = GetViewData().GetDocument()->GetDPAtCursor( GetViewData().GetCurX(),
1893 GetViewData().GetCurY(), GetViewData().GetTabNo() );
1894 if ( pDPObj )
1896 ScDPUniqueStringSet aEntries;
1897 long nSelectDimension = -1;
1898 GetSelectedMemberList( aEntries, nSelectDimension );
1900 if (!aEntries.empty())
1902 bool bIsDataLayout;
1903 OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
1904 if ( !bIsDataLayout )
1906 ScDPSaveData aData( *pDPObj->GetSaveData() );
1907 ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
1909 if ( bShow && pNewDimensionName )
1911 // add the new dimension with the same orientation, at the end
1913 ScDPSaveDimension* pNewDim = aData.GetDimensionByName( *pNewDimensionName );
1914 ScDPSaveDimension* pDuplicated = NULL;
1915 if ( pNewDim->GetOrientation() == sheet::DataPilotFieldOrientation_DATA )
1917 // Need to duplicate the dimension, create column/row in addition to data:
1918 // The duplicated dimension inherits the existing settings, pNewDim is modified below.
1919 pDuplicated = aData.DuplicateDimension( *pNewDimensionName );
1922 sal_uInt16 nOrientation = pDim->GetOrientation();
1923 pNewDim->SetOrientation( nOrientation );
1925 long nPosition = LONG_MAX;
1926 aData.SetPosition( pNewDim, nPosition );
1928 ScDPSaveDimension* pDataLayout = aData.GetDataLayoutDimension();
1929 if ( pDataLayout->GetOrientation() == nOrientation &&
1930 aData.GetDataDimensionCount() <= 1 )
1932 // If there is only one data dimension, the data layout dimension
1933 // must still be the last one in its orientation.
1934 aData.SetPosition( pDataLayout, nPosition );
1937 if ( pDuplicated )
1939 // The duplicated (data) dimension needs to be behind the original dimension
1940 aData.SetPosition( pDuplicated, nPosition );
1943 // Hide details for all visible members (selected are changed below).
1944 //! Use all members from source level instead (including non-visible)?
1946 ScDPUniqueStringSet aVisibleEntries;
1947 pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
1949 ScDPUniqueStringSet::const_iterator it = aVisibleEntries.begin(), itEnd = aVisibleEntries.end();
1950 for (; it != itEnd; ++it)
1952 const OUString& aVisName = *it;
1953 ScDPSaveMember* pMember = pDim->GetMemberByName( aVisName );
1954 pMember->SetShowDetails( false );
1958 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
1959 for (; it != itEnd; ++it)
1961 ScDPSaveMember* pMember = pDim->GetMemberByName(*it);
1962 pMember->SetShowDetails( bShow );
1965 // apply changes
1966 ScDBDocFunc aFunc( *GetViewData().GetDocShell() );
1967 boost::scoped_ptr<ScDPObject> pNewObj(new ScDPObject( *pDPObj ));
1968 pNewObj->SetSaveData( aData );
1969 aFunc.DataPilotUpdate( pDPObj, pNewObj.get(), true, false );
1970 pNewObj.reset();
1972 // unmark cell selection
1973 Unmark();
1979 void ScDBFunc::ShowDataPilotSourceData( ScDPObject& rDPObj, const Sequence<sheet::DataPilotFieldFilter>& rFilters )
1981 ScDocument* pDoc = GetViewData().GetDocument();
1982 if (pDoc->GetDocumentShell()->IsReadOnly())
1984 ErrorMessage(STR_READONLYERR);
1985 return;
1988 Reference<sheet::XDimensionsSupplier> xDimSupplier = rDPObj.GetSource();
1989 Reference<container::XNameAccess> xDims = xDimSupplier->getDimensions();
1990 Reference<sheet::XDrillDownDataSupplier> xDDSupplier(xDimSupplier, UNO_QUERY);
1991 if (!xDDSupplier.is())
1992 return;
1994 Sequence< Sequence<Any> > aTabData = xDDSupplier->getDrillDownData(rFilters);
1995 sal_Int32 nRowSize = aTabData.getLength();
1996 if (nRowSize <= 1)
1997 // There is no data to show. Bail out.
1998 return;
2000 sal_Int32 nColSize = aTabData[0].getLength();
2002 SCTAB nNewTab = GetViewData().GetTabNo();
2004 boost::scoped_ptr<ScDocument> pInsDoc(new ScDocument(SCDOCMODE_CLIP));
2005 pInsDoc->ResetClip( pDoc, nNewTab );
2006 for (SCROW nRow = 0; nRow < nRowSize; ++nRow)
2008 for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
2010 const Any& rAny = aTabData[nRow][nCol];
2011 OUString aStr;
2012 double fVal;
2013 if (rAny >>= aStr)
2015 ScSetStringParam aParam;
2016 aParam.setTextInput();
2017 pInsDoc->SetString(ScAddress(nCol,nRow,nNewTab), aStr);
2019 else if (rAny >>= fVal)
2020 pInsDoc->SetValue(nCol, nRow, nNewTab, fVal);
2024 // set number format (important for dates)
2025 for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
2027 OUString aStr;
2028 if (!(aTabData[0][nCol] >>= aStr))
2029 continue;
2031 Reference<XPropertySet> xPropSet(xDims->getByName(aStr), UNO_QUERY);
2032 if (!xPropSet.is())
2033 continue;
2035 Any any = xPropSet->getPropertyValue( OUString(SC_UNO_DP_NUMBERFO) );
2036 sal_Int32 nNumFmt = 0;
2037 if (!(any >>= nNumFmt))
2038 continue;
2040 ScPatternAttr aPattern( pInsDoc->GetPool() );
2041 aPattern.GetItemSet().Put( SfxUInt32Item(ATTR_VALUE_FORMAT, static_cast<sal_uInt32>(nNumFmt)) );
2042 pInsDoc->ApplyPatternAreaTab(nCol, 1, nCol, nRowSize-1, nNewTab, aPattern);
2045 SCCOL nEndCol = 0;
2046 SCROW nEndRow = 0;
2047 pInsDoc->GetCellArea( nNewTab, nEndCol, nEndRow );
2048 pInsDoc->SetClipArea( ScRange( 0, 0, nNewTab, nEndCol, nEndRow, nNewTab ) );
2050 ::svl::IUndoManager* pMgr = GetViewData().GetDocShell()->GetUndoManager();
2051 OUString aUndo = ScGlobal::GetRscString( STR_UNDO_DOOUTLINE );
2052 pMgr->EnterListAction( aUndo, aUndo );
2054 OUString aNewTabName;
2055 pDoc->CreateValidTabName(aNewTabName);
2056 if ( InsertTable(aNewTabName, nNewTab) )
2057 PasteFromClip( IDF_ALL, pInsDoc.get() );
2059 pMgr->LeaveListAction();
2062 // DB-Operationen (Sortieren, Filtern, Teilergebnisse) wiederholen
2064 void ScDBFunc::RepeatDB( bool bRecord )
2066 SCCOL nCurX = GetViewData().GetCurX();
2067 SCROW nCurY = GetViewData().GetCurY();
2068 SCTAB nTab = GetViewData().GetTabNo();
2069 ScDocument* pDoc = GetViewData().GetDocument();
2070 ScDBData* pDBData = GetDBData();
2071 if (bRecord && !pDoc->IsUndoEnabled())
2072 bRecord = false;
2074 ScQueryParam aQueryParam;
2075 pDBData->GetQueryParam( aQueryParam );
2076 bool bQuery = aQueryParam.GetEntry(0).bDoQuery;
2078 ScSortParam aSortParam;
2079 pDBData->GetSortParam( aSortParam );
2080 bool bSort = aSortParam.maKeyState[0].bDoSort;
2082 ScSubTotalParam aSubTotalParam;
2083 pDBData->GetSubTotalParam( aSubTotalParam );
2084 bool bSubTotal = aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly;
2086 if ( bQuery || bSort || bSubTotal )
2088 bool bQuerySize = false;
2089 ScRange aOldQuery;
2090 ScRange aNewQuery;
2091 if (bQuery && !aQueryParam.bInplace)
2093 ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
2094 aQueryParam.nDestTab, true );
2095 if (pDest && pDest->IsDoSize())
2097 pDest->GetArea( aOldQuery );
2098 bQuerySize = true;
2102 SCTAB nDummy;
2103 SCCOL nStartCol;
2104 SCROW nStartRow;
2105 SCCOL nEndCol;
2106 SCROW nEndRow;
2107 pDBData->GetArea( nDummy, nStartCol, nStartRow, nEndCol, nEndRow );
2109 //! Undo nur benoetigte Daten ?
2111 ScDocument* pUndoDoc = NULL;
2112 ScOutlineTable* pUndoTab = NULL;
2113 ScRangeName* pUndoRange = NULL;
2114 ScDBCollection* pUndoDB = NULL;
2116 if (bRecord)
2118 SCTAB nTabCount = pDoc->GetTableCount();
2119 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
2120 ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
2121 if (pTable)
2123 pUndoTab = new ScOutlineTable( *pTable );
2125 SCCOLROW nOutStartCol; // Zeilen/Spaltenstatus
2126 SCCOLROW nOutStartRow;
2127 SCCOLROW nOutEndCol;
2128 SCCOLROW nOutEndRow;
2129 pTable->GetColArray().GetRange( nOutStartCol, nOutEndCol );
2130 pTable->GetRowArray().GetRange( nOutStartRow, nOutEndRow );
2132 pUndoDoc->InitUndo( pDoc, nTab, nTab, true, true );
2133 pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, false, pUndoDoc );
2134 pDoc->CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, false, pUndoDoc );
2136 else
2137 pUndoDoc->InitUndo( pDoc, nTab, nTab, false, true );
2139 // Datenbereich sichern - incl. Filter-Ergebnis
2140 pDoc->CopyToDocument( 0,nStartRow,nTab, MAXCOL,nEndRow,nTab, IDF_ALL, false, pUndoDoc );
2142 // alle Formeln wegen Referenzen
2143 pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1, IDF_FORMULA, false, pUndoDoc );
2145 // DB- und andere Bereiche
2146 ScRangeName* pDocRange = pDoc->GetRangeName();
2147 if (!pDocRange->empty())
2148 pUndoRange = new ScRangeName( *pDocRange );
2149 ScDBCollection* pDocDB = pDoc->GetDBCollection();
2150 if (!pDocDB->empty())
2151 pUndoDB = new ScDBCollection( *pDocDB );
2154 if (bSort && bSubTotal)
2156 // Sortieren ohne SubTotals
2158 aSubTotalParam.bRemoveOnly = true; // wird unten wieder zurueckgesetzt
2159 DoSubTotals( aSubTotalParam, false );
2162 if (bSort)
2164 pDBData->GetSortParam( aSortParam ); // Bereich kann sich geaendert haben
2165 Sort( aSortParam, false, false);
2167 if (bQuery)
2169 pDBData->GetQueryParam( aQueryParam ); // Bereich kann sich geaendert haben
2170 ScRange aAdvSource;
2171 if (pDBData->GetAdvancedQuerySource(aAdvSource))
2173 pDoc->CreateQueryParam(
2174 aAdvSource.aStart.Col(), aAdvSource.aStart.Row(),
2175 aAdvSource.aEnd.Col(), aAdvSource.aEnd.Row(),
2176 aAdvSource.aStart.Tab(), aQueryParam );
2177 Query( aQueryParam, &aAdvSource, false );
2179 else
2180 Query( aQueryParam, NULL, false );
2182 // bei nicht-inplace kann die Tabelle umgestellt worden sein
2183 if ( !aQueryParam.bInplace && aQueryParam.nDestTab != nTab )
2184 SetTabNo( nTab );
2186 if (bSubTotal)
2188 pDBData->GetSubTotalParam( aSubTotalParam ); // Bereich kann sich geaendert haben
2189 aSubTotalParam.bRemoveOnly = false;
2190 DoSubTotals( aSubTotalParam, false );
2193 if (bRecord)
2195 SCTAB nDummyTab;
2196 SCCOL nDummyCol;
2197 SCROW nDummyRow, nNewEndRow;
2198 pDBData->GetArea( nDummyTab, nDummyCol,nDummyRow, nDummyCol,nNewEndRow );
2200 const ScRange* pOld = NULL;
2201 const ScRange* pNew = NULL;
2202 if (bQuerySize)
2204 ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
2205 aQueryParam.nDestTab, true );
2206 if (pDest)
2208 pDest->GetArea( aNewQuery );
2209 pOld = &aOldQuery;
2210 pNew = &aNewQuery;
2214 GetViewData().GetDocShell()->GetUndoManager()->AddUndoAction(
2215 new ScUndoRepeatDB( GetViewData().GetDocShell(), nTab,
2216 nStartCol, nStartRow, nEndCol, nEndRow,
2217 nNewEndRow,
2218 nCurX, nCurY,
2219 pUndoDoc, pUndoTab,
2220 pUndoRange, pUndoDB,
2221 pOld, pNew ) );
2224 GetViewData().GetDocShell()->PostPaint(
2225 ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab),
2226 PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE);
2228 else // "Keine Operationen auszufuehren"
2229 ErrorMessage(STR_MSSG_REPEATDB_0);
2232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */