LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/ui/view - dbfunc3.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 52 1139 4.6 %
Date: 2013-07-09 Functions: 3 37 8.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "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>
      39             : 
      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"
      69             : 
      70             : #include <boost/unordered_set.hpp>
      71             : #include <boost/unordered_map.hpp>
      72             : #include <memory>
      73             : #include <list>
      74             : #include <vector>
      75             : 
      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::auto_ptr;
      85             : using ::std::list;
      86             : using ::std::vector;
      87             : 
      88             : // STATIC DATA -----------------------------------------------------------
      89             : 
      90             : 
      91             : //==================================================================
      92             : 
      93             : //
      94             : //          Outliner
      95             : //
      96             : 
      97             : //  Outline-Gruppierung erzeugen
      98             : 
      99           0 : void ScDBFunc::MakeOutline( sal_Bool bColumns, sal_Bool bRecord )
     100             : {
     101           0 :     ScRange aRange;
     102           0 :     if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
     103             :     {
     104           0 :         ScDocShell* pDocSh = GetViewData()->GetDocShell();
     105           0 :         ScOutlineDocFunc aFunc(*pDocSh);
     106           0 :         aFunc.MakeOutline( aRange, bColumns, bRecord, false );
     107             :     }
     108             :     else
     109           0 :         ErrorMessage(STR_NOMULTISELECT);
     110           0 : }
     111             : 
     112             : //  Outline-Gruppierung loeschen
     113             : 
     114           0 : void ScDBFunc::RemoveOutline( sal_Bool bColumns, sal_Bool bRecord )
     115             : {
     116           0 :     ScRange aRange;
     117           0 :     if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
     118             :     {
     119           0 :         ScDocShell* pDocSh = GetViewData()->GetDocShell();
     120           0 :         ScOutlineDocFunc aFunc(*pDocSh);
     121           0 :         aFunc.RemoveOutline( aRange, bColumns, bRecord, false );
     122             :     }
     123             :     else
     124           0 :         ErrorMessage(STR_NOMULTISELECT);
     125           0 : }
     126             : 
     127             : //  Menue-Status: Outlines loeschen
     128             : 
     129           0 : void ScDBFunc::TestRemoveOutline( sal_Bool& rCol, sal_Bool& rRow )
     130             : {
     131           0 :     sal_Bool bColFound = false;
     132           0 :     sal_Bool bRowFound = false;
     133             : 
     134             :     SCCOL nStartCol, nEndCol;
     135             :     SCROW nStartRow, nEndRow;
     136             :     SCTAB nStartTab, nEndTab;
     137           0 :     if (GetViewData()->GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
     138             :     {
     139           0 :         SCTAB nTab = nStartTab;
     140           0 :         ScDocument* pDoc = GetViewData()->GetDocument();
     141           0 :         ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
     142           0 :         if (pTable)
     143             :         {
     144             :             ScOutlineArray* pArray;
     145             :             ScOutlineEntry* pEntry;
     146             :             SCCOLROW nStart;
     147             :             SCCOLROW nEnd;
     148           0 :             sal_Bool bColMarked = ( nStartRow == 0 && nEndRow == MAXROW );
     149           0 :             sal_Bool bRowMarked = ( nStartCol == 0 && nEndCol == MAXCOL );
     150             : 
     151             :             //  Spalten
     152             : 
     153           0 :             if ( !bRowMarked || bColMarked )        // nicht wenn ganze Zeilen markiert
     154             :             {
     155           0 :                 pArray = pTable->GetColArray();
     156           0 :                 ScSubOutlineIterator aColIter( pArray );
     157           0 :                 while ((pEntry=aColIter.GetNext()) != NULL && !bColFound)
     158             :                 {
     159           0 :                     nStart = pEntry->GetStart();
     160           0 :                     nEnd   = pEntry->GetEnd();
     161           0 :                     if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
     162           0 :                         bColFound = sal_True;
     163             :                 }
     164             :             }
     165             : 
     166             :             //  Zeilen
     167             : 
     168           0 :             if ( !bColMarked || bRowMarked )        // nicht wenn ganze Spalten markiert
     169             :             {
     170           0 :                 pArray = pTable->GetRowArray();
     171           0 :                 ScSubOutlineIterator aRowIter( pArray );
     172           0 :                 while ((pEntry=aRowIter.GetNext()) != NULL && !bRowFound)
     173             :                 {
     174           0 :                     nStart = pEntry->GetStart();
     175           0 :                     nEnd   = pEntry->GetEnd();
     176           0 :                     if ( nStartRow<=nEnd && nEndRow>=nStart )
     177           0 :                         bRowFound = sal_True;
     178             :                 }
     179             :             }
     180             :         }
     181             :     }
     182             : 
     183           0 :     rCol = bColFound;
     184           0 :     rRow = bRowFound;
     185           0 : }
     186             : 
     187           0 : void ScDBFunc::RemoveAllOutlines( sal_Bool bRecord )
     188             : {
     189           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     190           0 :     ScDocShell* pDocSh = GetViewData()->GetDocShell();
     191           0 :     ScOutlineDocFunc aFunc(*pDocSh);
     192             : 
     193           0 :     sal_Bool bOk = aFunc.RemoveAllOutlines( nTab, bRecord, false );
     194             : 
     195           0 :     if (bOk)
     196           0 :         UpdateScrollBars();
     197           0 : }
     198             : 
     199             : //  Auto-Outlines
     200             : 
     201           0 : void ScDBFunc::AutoOutline( sal_Bool bRecord )
     202             : {
     203           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     204           0 :     ScRange aRange( 0,0,nTab, MAXCOL,MAXROW,nTab );     // ganze Tabelle, wenn nichts markiert
     205           0 :     ScMarkData& rMark = GetViewData()->GetMarkData();
     206           0 :     if ( rMark.IsMarked() || rMark.IsMultiMarked() )
     207             :     {
     208           0 :         rMark.MarkToMulti();
     209           0 :         rMark.GetMultiMarkArea( aRange );
     210             :     }
     211             : 
     212           0 :     ScDocShell* pDocSh = GetViewData()->GetDocShell();
     213           0 :     ScOutlineDocFunc aFunc(*pDocSh);
     214           0 :     aFunc.AutoOutline( aRange, bRecord, false );
     215           0 : }
     216             : 
     217             : //  Outline-Ebene auswaehlen
     218             : 
     219           0 : void ScDBFunc::SelectLevel( sal_Bool bColumns, sal_uInt16 nLevel, sal_Bool bRecord, sal_Bool bPaint )
     220             : {
     221           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     222           0 :     ScDocShell* pDocSh = GetViewData()->GetDocShell();
     223           0 :     ScOutlineDocFunc aFunc(*pDocSh);
     224             : 
     225           0 :     sal_Bool bOk = aFunc.SelectLevel( nTab, bColumns, nLevel, bRecord, bPaint, false );
     226             : 
     227           0 :     if (bOk)
     228           0 :         UpdateScrollBars();
     229           0 : }
     230             : 
     231             : //  einzelne Outline-Gruppe einblenden
     232             : 
     233           0 : void ScDBFunc::ShowOutline( sal_Bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, sal_Bool bRecord, sal_Bool bPaint )
     234             : {
     235           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     236           0 :     ScDocShell* pDocSh = GetViewData()->GetDocShell();
     237           0 :     ScOutlineDocFunc aFunc(*pDocSh);
     238             : 
     239           0 :     sal_Bool bOk = aFunc.ShowOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint, false );
     240             : 
     241           0 :     if ( bOk && bPaint )
     242           0 :         UpdateScrollBars();
     243           0 : }
     244             : 
     245             : //  einzelne Outline-Gruppe ausblenden
     246             : 
     247           0 : void ScDBFunc::HideOutline( sal_Bool bColumns, sal_uInt16 nLevel, sal_uInt16 nEntry, sal_Bool bRecord, sal_Bool bPaint )
     248             : {
     249           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     250           0 :     ScDocShell* pDocSh = GetViewData()->GetDocShell();
     251           0 :     ScOutlineDocFunc aFunc(*pDocSh);
     252             : 
     253           0 :     sal_Bool bOk = aFunc.HideOutline( nTab, bColumns, nLevel, nEntry, bRecord, bPaint, false );
     254             : 
     255           0 :     if ( bOk && bPaint )
     256           0 :         UpdateScrollBars();
     257           0 : }
     258             : 
     259             : //  Menue-Status: markierten Bereich ein-/ausblenden
     260             : 
     261           0 : sal_Bool ScDBFunc::OutlinePossible(sal_Bool bHide)
     262             : {
     263           0 :     sal_Bool bEnable = false;
     264             : 
     265             :     SCCOL nStartCol;
     266             :     SCROW nStartRow;
     267             :     SCTAB nStartTab;
     268             :     SCCOL nEndCol;
     269             :     SCROW nEndRow;
     270             :     SCTAB nEndTab;
     271             : 
     272           0 :     if (GetViewData()->GetSimpleArea(nStartCol,nStartRow,nStartTab,nEndCol,nEndRow,nEndTab) == SC_MARK_SIMPLE)
     273             :     {
     274           0 :         ScDocument* pDoc = GetViewData()->GetDocument();
     275           0 :         SCTAB nTab = GetViewData()->GetTabNo();
     276           0 :         ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
     277           0 :         if (pTable)
     278             :         {
     279             :             ScOutlineArray* pArray;
     280             :             ScOutlineEntry* pEntry;
     281             :             SCCOLROW nStart;
     282             :             SCCOLROW nEnd;
     283             : 
     284             :             //  Spalten
     285             : 
     286           0 :             pArray = pTable->GetColArray();
     287           0 :             ScSubOutlineIterator aColIter( pArray );
     288           0 :             while ((pEntry=aColIter.GetNext()) != NULL && !bEnable)
     289             :             {
     290           0 :                 nStart = pEntry->GetStart();
     291           0 :                 nEnd   = pEntry->GetEnd();
     292           0 :                 if ( bHide )
     293             :                 {
     294           0 :                     if ( nStartCol<=static_cast<SCCOL>(nEnd) && nEndCol>=static_cast<SCCOL>(nStart) )
     295           0 :                         if (!pEntry->IsHidden())
     296           0 :                             bEnable = sal_True;
     297             :                 }
     298             :                 else
     299             :                 {
     300           0 :                     if ( nStart>=nStartCol && nEnd<=nEndCol )
     301           0 :                         if (pEntry->IsHidden())
     302           0 :                             bEnable = sal_True;
     303             :                 }
     304             :             }
     305             : 
     306             :             //  Zeilen
     307             : 
     308           0 :             pArray = pTable->GetRowArray();
     309           0 :             ScSubOutlineIterator aRowIter( pArray );
     310           0 :             while ((pEntry=aRowIter.GetNext()) != NULL)
     311             :             {
     312           0 :                 nStart = pEntry->GetStart();
     313           0 :                 nEnd   = pEntry->GetEnd();
     314           0 :                 if ( bHide )
     315             :                 {
     316           0 :                     if ( nStartRow<=nEnd && nEndRow>=nStart )
     317           0 :                         if (!pEntry->IsHidden())
     318           0 :                             bEnable = sal_True;
     319             :                 }
     320             :                 else
     321             :                 {
     322           0 :                     if ( nStart>=nStartRow && nEnd<=nEndRow )
     323           0 :                         if (pEntry->IsHidden())
     324           0 :                             bEnable = sal_True;
     325             :                 }
     326             :             }
     327             :         }
     328             :     }
     329             : 
     330           0 :     return bEnable;
     331             : }
     332             : 
     333             : //  markierten Bereich einblenden
     334             : 
     335           0 : void ScDBFunc::ShowMarkedOutlines( sal_Bool bRecord )
     336             : {
     337           0 :     ScRange aRange;
     338           0 :     if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
     339             :     {
     340           0 :         ScDocShell* pDocSh = GetViewData()->GetDocShell();
     341           0 :         ScOutlineDocFunc aFunc(*pDocSh);
     342           0 :         sal_Bool bDone = aFunc.ShowMarkedOutlines( aRange, bRecord );
     343           0 :         if (bDone)
     344           0 :             UpdateScrollBars();
     345             :     }
     346             :     else
     347           0 :         ErrorMessage(STR_NOMULTISELECT);
     348           0 : }
     349             : 
     350             : //  markierten Bereich ausblenden
     351             : 
     352           0 : void ScDBFunc::HideMarkedOutlines( sal_Bool bRecord )
     353             : {
     354           0 :     ScRange aRange;
     355           0 :     if (GetViewData()->GetSimpleArea(aRange) == SC_MARK_SIMPLE)
     356             :     {
     357           0 :         ScDocShell* pDocSh = GetViewData()->GetDocShell();
     358           0 :         ScOutlineDocFunc aFunc(*pDocSh);
     359           0 :         sal_Bool bDone = aFunc.HideMarkedOutlines( aRange, bRecord, false );
     360           0 :         if (bDone)
     361           0 :             UpdateScrollBars();
     362             :     }
     363             :     else
     364           0 :         ErrorMessage(STR_NOMULTISELECT);
     365           0 : }
     366             : 
     367             : //  --------------------------------------------------------------------------
     368             : 
     369             : //
     370             : //          Teilergebnisse
     371             : //
     372             : 
     373           0 : void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, sal_Bool bRecord,
     374             :                             const ScSortParam* pForceNewSort )
     375             : {
     376           0 :     sal_Bool bDo = !rParam.bRemoveOnly;                         // sal_False = nur loeschen
     377             : 
     378           0 :     ScDocShell* pDocSh = GetViewData()->GetDocShell();
     379           0 :     ScDocument* pDoc = pDocSh->GetDocument();
     380           0 :     ScMarkData& rMark = GetViewData()->GetMarkData();
     381           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     382           0 :     if (bRecord && !pDoc->IsUndoEnabled())
     383           0 :         bRecord = false;
     384             : 
     385             :     ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
     386           0 :                                                 rParam.nCol2, rParam.nRow2 );
     387           0 :     if (!pDBData)
     388             :     {
     389             :         OSL_FAIL( "SubTotals: keine DBData" );
     390           0 :         return;
     391             :     }
     392             : 
     393           0 :     ScEditableTester aTester( pDoc, nTab, 0,rParam.nRow1+1, MAXCOL,MAXROW );
     394           0 :     if (!aTester.IsEditable())
     395             :     {
     396           0 :         ErrorMessage(aTester.GetMessageId());
     397           0 :         return;
     398             :     }
     399             : 
     400           0 :     if (pDoc->HasAttrib( rParam.nCol1, rParam.nRow1+1, nTab,
     401           0 :                          rParam.nCol2, rParam.nRow2, nTab, HASATTR_MERGED | HASATTR_OVERLAPPED ))
     402             :     {
     403           0 :         ErrorMessage(STR_MSSG_INSERTCELLS_0);   // nicht in zusammengefasste einfuegen
     404           0 :         return;
     405             :     }
     406             : 
     407           0 :     WaitObject aWait( GetViewData()->GetDialogParent() );
     408           0 :     sal_Bool bOk = true;
     409           0 :     if (rParam.bReplace)
     410           0 :         if (pDoc->TestRemoveSubTotals( nTab, rParam ))
     411             :         {
     412             :             bOk = ( MessBox( GetViewData()->GetDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
     413             :                 // "StarCalc" "Daten loeschen?"
     414           0 :                 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
     415           0 :                 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_1 ) ).Execute()
     416           0 :                 == RET_YES );
     417             :         }
     418             : 
     419           0 :     if (bOk)
     420             :     {
     421           0 :         ScDocShellModificator aModificator( *pDocSh );
     422             : 
     423           0 :         ScSubTotalParam aNewParam( rParam );        // Bereichsende wird veraendert
     424           0 :         ScDocument*     pUndoDoc = NULL;
     425           0 :         ScOutlineTable* pUndoTab = NULL;
     426           0 :         ScRangeName*    pUndoRange = NULL;
     427           0 :         ScDBCollection* pUndoDB = NULL;
     428             : 
     429           0 :         if (bRecord)                                        // alte Daten sichern
     430             :         {
     431           0 :             sal_Bool bOldFilter = bDo && rParam.bDoSort;
     432           0 :             SCTAB nTabCount = pDoc->GetTableCount();
     433           0 :             pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
     434           0 :             ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
     435           0 :             if (pTable)
     436             :             {
     437           0 :                 pUndoTab = new ScOutlineTable( *pTable );
     438             : 
     439             :                 SCCOLROW nOutStartCol;                          // Zeilen/Spaltenstatus
     440             :                 SCCOLROW nOutStartRow;
     441             :                 SCCOLROW nOutEndCol;
     442             :                 SCCOLROW nOutEndRow;
     443           0 :                 pTable->GetColArray()->GetRange( nOutStartCol, nOutEndCol );
     444           0 :                 pTable->GetRowArray()->GetRange( nOutStartRow, nOutEndRow );
     445             : 
     446           0 :                 pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_True, sal_True );
     447           0 :                 pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, false, pUndoDoc );
     448           0 :                 pDoc->CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, false, pUndoDoc );
     449             :             }
     450             :             else
     451           0 :                 pUndoDoc->InitUndo( pDoc, nTab, nTab, false, bOldFilter );
     452             : 
     453             :             //  Datenbereich sichern - incl. Filter-Ergebnis
     454             :             pDoc->CopyToDocument( 0,rParam.nRow1+1,nTab, MAXCOL,rParam.nRow2,nTab,
     455           0 :                                     IDF_ALL, false, pUndoDoc );
     456             : 
     457             :             //  alle Formeln wegen Referenzen
     458             :             pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1,
     459           0 :                                         IDF_FORMULA, false, pUndoDoc );
     460             : 
     461             :             //  DB- und andere Bereiche
     462           0 :             ScRangeName* pDocRange = pDoc->GetRangeName();
     463           0 :             if (!pDocRange->empty())
     464           0 :                 pUndoRange = new ScRangeName( *pDocRange );
     465           0 :             ScDBCollection* pDocDB = pDoc->GetDBCollection();
     466           0 :             if (!pDocDB->empty())
     467           0 :                 pUndoDB = new ScDBCollection( *pDocDB );
     468             :         }
     469             : 
     470           0 :         ScOutlineTable* pOut = pDoc->GetOutlineTable( nTab );
     471           0 :         if (pOut)
     472             :         {
     473             :             // Remove all existing outlines in the specified range.
     474           0 :             ScOutlineArray* pRowArray = pOut->GetRowArray();
     475           0 :             sal_uInt16 nDepth = pRowArray->GetDepth();
     476           0 :             for (sal_uInt16 i = 0; i < nDepth; ++i)
     477             :             {
     478             :                 bool bSize;
     479           0 :                 pRowArray->Remove(aNewParam.nRow1, aNewParam.nRow2, bSize);
     480             :             }
     481             :         }
     482             : 
     483           0 :         if (rParam.bReplace)
     484           0 :             pDoc->RemoveSubTotals( nTab, aNewParam );
     485           0 :         sal_Bool bSuccess = sal_True;
     486           0 :         if (bDo)
     487             :         {
     488             :             // Sortieren
     489           0 :             if ( rParam.bDoSort || pForceNewSort )
     490             :             {
     491           0 :                 pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
     492             : 
     493             :                 //  Teilergebnis-Felder vor die Sortierung setzen
     494             :                 //  (doppelte werden weggelassen, kann darum auch wieder aufgerufen werden)
     495             : 
     496           0 :                 ScSortParam aOldSort;
     497           0 :                 pDBData->GetSortParam( aOldSort );
     498           0 :                 ScSortParam aSortParam( aNewParam, pForceNewSort ? *pForceNewSort : aOldSort );
     499           0 :                 Sort( aSortParam, false, false );
     500             :             }
     501             : 
     502           0 :             bSuccess = pDoc->DoSubTotals( nTab, aNewParam );
     503             :         }
     504             :         ScRange aDirtyRange( aNewParam.nCol1, aNewParam.nRow1, nTab,
     505           0 :             aNewParam.nCol2, aNewParam.nRow2, nTab );
     506           0 :         pDoc->SetDirty( aDirtyRange );
     507             : 
     508           0 :         if (bRecord)
     509             :         {
     510           0 :             pDocSh->GetUndoManager()->AddUndoAction(
     511             :                 new ScUndoSubTotals( pDocSh, nTab,
     512             :                                         rParam, aNewParam.nRow2,
     513             :                                         pUndoDoc, pUndoTab, // pUndoDBData,
     514           0 :                                         pUndoRange, pUndoDB ) );
     515             :         }
     516             : 
     517           0 :         if (!bSuccess)
     518             :         {
     519             :             // "Kann keine Zeilen einfuegen"
     520           0 :             ErrorMessage(STR_MSSG_DOSUBTOTALS_2);
     521             :         }
     522             : 
     523             :                                                     // merken
     524           0 :         pDBData->SetSubTotalParam( aNewParam );
     525           0 :         pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
     526           0 :         pDoc->CompileDBFormula();
     527             : 
     528           0 :         DoneBlockMode();
     529           0 :         InitOwnBlockMode();
     530             :         rMark.SetMarkArea( ScRange( aNewParam.nCol1,aNewParam.nRow1,nTab,
     531           0 :                                     aNewParam.nCol2,aNewParam.nRow2,nTab ) );
     532           0 :         MarkDataChanged();
     533             : 
     534             :         pDocSh->PostPaint(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab),
     535           0 :                           PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE);
     536             : 
     537           0 :         aModificator.SetDocumentModified();
     538             : 
     539           0 :         SelectionChanged();
     540           0 :     }
     541             : }
     542             : 
     543             : //
     544             : //          Consolidate
     545             : //
     546             : 
     547           0 : void ScDBFunc::Consolidate( const ScConsolidateParam& rParam, sal_Bool bRecord )
     548             : {
     549           0 :     ScDocShell* pDocShell = GetViewData()->GetDocShell();
     550           0 :     pDocShell->DoConsolidate( rParam, bRecord );
     551           0 :     SetTabNo( rParam.nTab, sal_True );
     552           0 : }
     553             : 
     554             : //
     555             : //          Pivot
     556             : //
     557             : 
     558           0 : static OUString lcl_MakePivotTabName( const String& rPrefix, SCTAB nNumber )
     559             : {
     560           0 :     OUString aName = rPrefix + OUString::number( nNumber );
     561           0 :     return aName;
     562             : }
     563             : 
     564           0 : bool ScDBFunc::MakePivotTable(
     565             :     const ScDPSaveData& rData, const ScRange& rDest, bool bNewTable,
     566             :     const ScDPObject& rSource, bool bApi )
     567             : {
     568             :     //  error message if no fields are set
     569             :     //  this must be removed when drag&drop of fields from a toolbox is available
     570             : 
     571           0 :     if ( rData.IsEmpty() && !bApi )
     572             :     {
     573           0 :         ErrorMessage(STR_PIVOT_NODATA);
     574           0 :         return false;
     575             :     }
     576             : 
     577           0 :     ScDocShell* pDocSh  = GetViewData()->GetDocShell();
     578           0 :     ScDocument* pDoc    = GetViewData()->GetDocument();
     579           0 :     bool bUndo = pDoc->IsUndoEnabled();
     580             : 
     581           0 :     ScRange aDestRange = rDest;
     582           0 :     if ( bNewTable )
     583             :     {
     584           0 :         SCTAB nSrcTab = GetViewData()->GetTabNo();
     585             : 
     586           0 :         String aName( ScGlobal::GetRscString(STR_PIVOT_TABLE) );
     587           0 :         OUString aStr;
     588             : 
     589           0 :         pDoc->GetName( nSrcTab, aStr );
     590           0 :         aName += '_';
     591           0 :         aName += String(aStr);
     592           0 :         aName += '_';
     593             : 
     594           0 :         SCTAB nNewTab = nSrcTab+1;
     595             : 
     596           0 :         SCTAB i=1;
     597           0 :         while ( !pDoc->InsertTab( nNewTab, lcl_MakePivotTabName( aName, i ) ) && i <= MAXTAB )
     598           0 :             i++;
     599             : 
     600           0 :         bool bAppend = ( nNewTab+1 == pDoc->GetTableCount() );
     601           0 :         if (bUndo)
     602             :         {
     603           0 :             pDocSh->GetUndoManager()->AddUndoAction(
     604           0 :                         new ScUndoInsertTab( pDocSh, nNewTab, bAppend, lcl_MakePivotTabName( aName, i ) ));
     605             :         }
     606             : 
     607           0 :         GetViewData()->InsertTab( nNewTab );
     608           0 :         SetTabNo(nNewTab, true);
     609             : 
     610           0 :         aDestRange = ScRange( 0, 0, nNewTab );
     611             :     }
     612             : 
     613             :     ScDPObject* pDPObj = pDoc->GetDPAtCursor(
     614           0 :                             aDestRange.aStart.Col(), aDestRange.aStart.Row(), aDestRange.aStart.Tab() );
     615             : 
     616           0 :     ScDPObject aObj( rSource );
     617           0 :     aObj.SetOutRange( aDestRange );
     618           0 :     if ( pDPObj && !rData.GetExistingDimensionData() )
     619             :     {
     620             :         // copy dimension data from old object - lost in the dialog
     621             :         //! change the dialog to keep the dimension data
     622             : 
     623           0 :         ScDPSaveData aNewData( rData );
     624           0 :         const ScDPSaveData* pOldData = pDPObj->GetSaveData();
     625           0 :         if ( pOldData )
     626             :         {
     627           0 :             const ScDPDimensionSaveData* pDimSave = pOldData->GetExistingDimensionData();
     628           0 :             aNewData.SetDimensionData( pDimSave );
     629             :         }
     630           0 :         aObj.SetSaveData( aNewData );
     631             :     }
     632             :     else
     633           0 :         aObj.SetSaveData( rData );
     634             : 
     635           0 :     bool bAllowMove = (pDPObj != NULL);   // allow re-positioning when editing existing table
     636             : 
     637           0 :     ScDBDocFunc aFunc( *pDocSh );
     638           0 :     bool bSuccess = aFunc.DataPilotUpdate(pDPObj, &aObj, true, false, bAllowMove);
     639             : 
     640           0 :     CursorPosChanged();     // shells may be switched
     641             : 
     642           0 :     if ( bNewTable )
     643             :     {
     644           0 :         pDocSh->PostPaintExtras();
     645           0 :         SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
     646             :     }
     647             : 
     648           0 :     return bSuccess;
     649             : }
     650             : 
     651           0 : void ScDBFunc::DeletePivotTable()
     652             : {
     653           0 :     ScDocShell* pDocSh    = GetViewData()->GetDocShell();
     654           0 :     ScDocument* pDoc      = pDocSh->GetDocument();
     655           0 :     ScDPObject* pDPObj    = pDoc->GetDPAtCursor( GetViewData()->GetCurX(),
     656             :                                                   GetViewData()->GetCurY(),
     657           0 :                                                   GetViewData()->GetTabNo() );
     658           0 :     if ( pDPObj )
     659             :     {
     660           0 :         ScDBDocFunc aFunc( *pDocSh );
     661           0 :         aFunc.RemovePivotTable(*pDPObj, true, false);
     662           0 :         CursorPosChanged();     // shells may be switched
     663             :     }
     664             :     else
     665           0 :         ErrorMessage(STR_PIVOT_NOTFOUND);
     666           0 : }
     667             : 
     668           0 : void ScDBFunc::RecalcPivotTable()
     669             : {
     670           0 :     ScDocShell* pDocSh  = GetViewData()->GetDocShell();
     671           0 :     ScDocument* pDoc    = GetViewData()->GetDocument();
     672             : 
     673           0 :     ScDPObject* pDPObj  = pDoc->GetDPAtCursor( GetViewData()->GetCurX(),
     674             :                                                   GetViewData()->GetCurY(),
     675           0 :                                                   GetViewData()->GetTabNo() );
     676           0 :     if (pDPObj)
     677             :     {
     678             :         // Remove existing data cache for the data that this datapilot uses,
     679             :         // to force re-build data cache.
     680           0 :         ScDBDocFunc aFunc(*pDocSh);
     681           0 :         aFunc.RefreshPivotTables(pDPObj, false);
     682             : 
     683           0 :         CursorPosChanged();     // shells may be switched
     684             :     }
     685             :     else
     686           0 :         ErrorMessage(STR_PIVOT_NOTFOUND);
     687           0 : }
     688             : 
     689           0 : void ScDBFunc::GetSelectedMemberList(ScDPUniqueStringSet& rEntries, long& rDimension)
     690             : {
     691           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
     692           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
     693           0 :     if ( !pDPObj )
     694           0 :         return;
     695             : 
     696           0 :     long nStartDimension = -1;
     697           0 :     long nStartHierarchy = -1;
     698           0 :     long nStartLevel     = -1;
     699             : 
     700           0 :     ScRangeListRef xRanges;
     701           0 :     GetViewData()->GetMultiArea( xRanges );         // incl. cursor if nothing is selected
     702           0 :     size_t nRangeCount = xRanges->size();
     703           0 :     sal_Bool bContinue = true;
     704             : 
     705           0 :     for (size_t nRangePos=0; nRangePos < nRangeCount && bContinue; nRangePos++)
     706             :     {
     707           0 :         ScRange aRange = *(*xRanges)[nRangePos];
     708           0 :         SCCOL nStartCol = aRange.aStart.Col();
     709           0 :         SCROW nStartRow = aRange.aStart.Row();
     710           0 :         SCCOL nEndCol = aRange.aEnd.Col();
     711           0 :         SCROW nEndRow = aRange.aEnd.Row();
     712           0 :         SCTAB nTab = aRange.aStart.Tab();
     713             : 
     714           0 :         for (SCROW nRow=nStartRow; nRow<=nEndRow && bContinue; nRow++)
     715           0 :             for (SCCOL nCol=nStartCol; nCol<=nEndCol && bContinue; nCol++)
     716             :             {
     717           0 :                 sheet::DataPilotTableHeaderData aData;
     718           0 :                 pDPObj->GetHeaderPositionData(ScAddress(nCol, nRow, nTab), aData);
     719           0 :                 if ( aData.Dimension < 0 )
     720           0 :                     bContinue = false;              // not part of any dimension
     721             :                 else
     722             :                 {
     723           0 :                     if ( nStartDimension < 0 )      // first member?
     724             :                     {
     725           0 :                         nStartDimension = aData.Dimension;
     726           0 :                         nStartHierarchy = aData.Hierarchy;
     727           0 :                         nStartLevel     = aData.Level;
     728             :                     }
     729           0 :                     if ( aData.Dimension != nStartDimension ||
     730           0 :                          aData.Hierarchy != nStartHierarchy ||
     731           0 :                          aData.Level     != nStartLevel )
     732             :                     {
     733           0 :                         bContinue = false;          // cannot mix dimensions
     734             :                     }
     735             :                 }
     736           0 :                 if ( bContinue )
     737             :                 {
     738             :                     // accept any part of a member description, also subtotals,
     739             :                     // but don't stop if empty parts are contained
     740           0 :                     if ( aData.Flags & sheet::MemberResultFlags::HASMEMBER )
     741           0 :                         rEntries.insert(aData.MemberName);
     742             :                 }
     743           0 :             }
     744             :     }
     745             : 
     746           0 :     rDimension = nStartDimension;   // dimension from which the found members came
     747           0 :     if (!bContinue)
     748           0 :         rEntries.clear();         // remove all if not valid
     749             : }
     750             : 
     751           0 : bool ScDBFunc::HasSelectionForDateGroup( ScDPNumGroupInfo& rOldInfo, sal_Int32& rParts )
     752             : {
     753             :     // determine if the date group dialog has to be shown for the current selection
     754             : 
     755           0 :     bool bFound = false;
     756             : 
     757           0 :     SCCOL nCurX = GetViewData()->GetCurX();
     758           0 :     SCROW nCurY = GetViewData()->GetCurY();
     759           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     760           0 :     ScDocument* pDoc = GetViewData()->GetDocument();
     761             : 
     762           0 :     ScDPObject* pDPObj = pDoc->GetDPAtCursor( nCurX, nCurY, nTab );
     763           0 :     if ( pDPObj )
     764             :     {
     765           0 :         ScDPUniqueStringSet aEntries;
     766           0 :         long nSelectDimension = -1;
     767           0 :         GetSelectedMemberList( aEntries, nSelectDimension );
     768             : 
     769           0 :         if (!aEntries.empty())
     770             :         {
     771             :             bool bIsDataLayout;
     772           0 :             OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
     773           0 :             String aBaseDimName( aDimName );
     774             : 
     775           0 :             sal_Bool bInGroupDim = false;
     776           0 :             sal_Bool bFoundParts = false;
     777             : 
     778             :             ScDPDimensionSaveData* pDimData =
     779           0 :                 const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
     780           0 :             if ( pDimData )
     781             :             {
     782           0 :                 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
     783           0 :                 const ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDim( aDimName );
     784           0 :                 if ( pNumGroupDim )
     785             :                 {
     786             :                     //  existing num group dimension
     787             : 
     788           0 :                     if ( pNumGroupDim->GetDatePart() != 0 )
     789             :                     {
     790             :                         //  dimension has date info -> edit settings of this dimension
     791             :                         //  (parts are collected below)
     792             : 
     793           0 :                         rOldInfo = pNumGroupDim->GetDateInfo();
     794           0 :                         bFound = sal_True;
     795             :                     }
     796           0 :                     else if ( pNumGroupDim->GetInfo().mbDateValues )
     797             :                     {
     798             :                         //  Numerical grouping with DateValues flag is used for grouping
     799             :                         //  of days with a "Number of days" value.
     800             : 
     801           0 :                         rOldInfo = pNumGroupDim->GetInfo();
     802           0 :                         rParts = com::sun::star::sheet::DataPilotFieldGroupBy::DAYS;               // not found in CollectDateParts
     803           0 :                         bFoundParts = sal_True;
     804           0 :                         bFound = sal_True;
     805             :                     }
     806           0 :                     bInGroupDim = sal_True;
     807             :                 }
     808           0 :                 else if ( pGroupDim )
     809             :                 {
     810             :                     //  existing additional group dimension
     811             : 
     812           0 :                     if ( pGroupDim->GetDatePart() != 0 )
     813             :                     {
     814             :                         //  dimension has date info -> edit settings of this dimension
     815             :                         //  (parts are collected below)
     816             : 
     817           0 :                         rOldInfo = pGroupDim->GetDateInfo();
     818           0 :                         aBaseDimName = pGroupDim->GetSourceDimName();
     819           0 :                         bFound = sal_True;
     820             :                     }
     821           0 :                     bInGroupDim = sal_True;
     822             :                 }
     823             :             }
     824           0 :             if ( bFound && !bFoundParts )
     825             :             {
     826             :                 // collect date parts from all group dimensions
     827           0 :                 rParts = pDimData->CollectDateParts( aBaseDimName );
     828             :             }
     829           0 :             if ( !bFound && !bInGroupDim )
     830             :             {
     831             :                 // create new date group dimensions if the selection is a single cell
     832             :                 // in a normal dimension with date content
     833             : 
     834           0 :                 ScRange aSelRange;
     835           0 :                 if ( (GetViewData()->GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
     836           0 :                         aSelRange.aStart == aSelRange.aEnd )
     837             :                 {
     838           0 :                     SCCOL nSelCol = aSelRange.aStart.Col();
     839           0 :                     SCROW nSelRow = aSelRange.aStart.Row();
     840           0 :                     SCTAB nSelTab = aSelRange.aStart.Tab();
     841           0 :                     if ( pDoc->HasValueData( nSelCol, nSelRow, nSelTab ) )
     842             :                     {
     843             :                         sal_uLong nIndex = static_cast<const SfxUInt32Item*>(pDoc->GetAttr(
     844           0 :                                         nSelCol, nSelRow, nSelTab, ATTR_VALUE_FORMAT))->GetValue();
     845           0 :                         short nType = pDoc->GetFormatTable()->GetType(nIndex);
     846           0 :                         if ( nType == NUMBERFORMAT_DATE || nType == NUMBERFORMAT_TIME || nType == NUMBERFORMAT_DATETIME )
     847             :                         {
     848           0 :                             bFound = sal_True;
     849             :                             // use currently selected value for automatic limits
     850           0 :                             if( rOldInfo.mbAutoStart )
     851           0 :                                 rOldInfo.mfStart = pDoc->GetValue( aSelRange.aStart );
     852           0 :                             if( rOldInfo.mbAutoEnd )
     853           0 :                                 rOldInfo.mfEnd = pDoc->GetValue( aSelRange.aStart );
     854             :                         }
     855             :                     }
     856             :                 }
     857           0 :             }
     858           0 :         }
     859             :     }
     860             : 
     861           0 :     return bFound;
     862             : }
     863             : 
     864           0 : bool ScDBFunc::HasSelectionForNumGroup( ScDPNumGroupInfo& rOldInfo )
     865             : {
     866             :     // determine if the numeric group dialog has to be shown for the current selection
     867             : 
     868           0 :     bool bFound = false;
     869             : 
     870           0 :     SCCOL nCurX = GetViewData()->GetCurX();
     871           0 :     SCROW nCurY = GetViewData()->GetCurY();
     872           0 :     SCTAB nTab = GetViewData()->GetTabNo();
     873           0 :     ScDocument* pDoc = GetViewData()->GetDocument();
     874             : 
     875           0 :     ScDPObject* pDPObj = pDoc->GetDPAtCursor( nCurX, nCurY, nTab );
     876           0 :     if ( pDPObj )
     877             :     {
     878           0 :         ScDPUniqueStringSet aEntries;
     879           0 :         long nSelectDimension = -1;
     880           0 :         GetSelectedMemberList( aEntries, nSelectDimension );
     881             : 
     882           0 :         if (!aEntries.empty())
     883             :         {
     884             :             bool bIsDataLayout;
     885           0 :             OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
     886             : 
     887           0 :             sal_Bool bInGroupDim = false;
     888             : 
     889             :             ScDPDimensionSaveData* pDimData =
     890           0 :                 const_cast<ScDPDimensionSaveData*>( pDPObj->GetSaveData()->GetExistingDimensionData() );
     891           0 :             if ( pDimData )
     892             :             {
     893           0 :                 const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
     894           0 :                 if ( pNumGroupDim )
     895             :                 {
     896             :                     //  existing num group dimension
     897             :                     //  -> edit settings of this dimension
     898             : 
     899           0 :                     rOldInfo = pNumGroupDim->GetInfo();
     900           0 :                     bFound = sal_True;
     901             :                 }
     902           0 :                 else if ( pDimData->GetNamedGroupDim( aDimName ) )
     903           0 :                     bInGroupDim = sal_True;                                    // in a group dimension
     904             :             }
     905           0 :             if ( !bFound && !bInGroupDim )
     906             :             {
     907             :                 // create a new num group dimension if the selection is a single cell
     908             :                 // in a normal dimension with numeric content
     909             : 
     910           0 :                 ScRange aSelRange;
     911           0 :                 if ( (GetViewData()->GetSimpleArea( aSelRange ) == SC_MARK_SIMPLE) &&
     912           0 :                         aSelRange.aStart == aSelRange.aEnd )
     913             :                 {
     914           0 :                     if ( pDoc->HasValueData( aSelRange.aStart.Col(), aSelRange.aStart.Row(),
     915           0 :                                              aSelRange.aStart.Tab() ) )
     916             :                     {
     917           0 :                         bFound = sal_True;
     918             :                         // use currently selected value for automatic limits
     919           0 :                         if( rOldInfo.mbAutoStart )
     920           0 :                             rOldInfo.mfStart = pDoc->GetValue( aSelRange.aStart );
     921           0 :                         if( rOldInfo.mbAutoEnd )
     922           0 :                             rOldInfo.mfEnd = pDoc->GetValue( aSelRange.aStart );
     923             :                     }
     924             :                 }
     925           0 :             }
     926           0 :         }
     927             :     }
     928             : 
     929           0 :     return bFound;
     930             : }
     931             : 
     932           0 : void ScDBFunc::DateGroupDataPilot( const ScDPNumGroupInfo& rInfo, sal_Int32 nParts )
     933             : {
     934           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
     935           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
     936           0 :     if (!pDPObj)
     937           0 :         return;
     938             : 
     939           0 :     ScDPUniqueStringSet aEntries;
     940           0 :     long nSelectDimension = -1;
     941           0 :     GetSelectedMemberList( aEntries, nSelectDimension );
     942             : 
     943           0 :     if (aEntries.empty())
     944           0 :         return;
     945             : 
     946           0 :     std::vector<OUString> aDeletedNames;
     947             :     bool bIsDataLayout;
     948           0 :     OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
     949             : 
     950           0 :     ScDPSaveData aData( *pDPObj->GetSaveData() );
     951           0 :     ScDPDimensionSaveData* pDimData = aData.GetDimensionData();     // created if not there
     952             : 
     953             :     // find the source dimension name.
     954           0 :     OUString aBaseDimName = aDimName;
     955           0 :     if( const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName ) )
     956           0 :         aBaseDimName = pBaseGroupDim->GetSourceDimName();
     957             : 
     958             :     // Remove all group dimensions associated with this source dimension. For
     959             :     // date grouping, we need to remove all existing groups for the affected
     960             :     // source dimension and build new one(s) from scratch.  Keep the deleted
     961             :     // names so that they can be reused during re-construction.
     962           0 :     aData.RemoveAllGroupDimensions(aBaseDimName, &aDeletedNames);
     963             : 
     964           0 :     if ( nParts )
     965             :     {
     966             :         // create date group dimensions
     967             : 
     968           0 :         ScDPNumGroupInfo aEmpty;
     969           0 :         bool bFirst = true;
     970           0 :         sal_Int32 nMask = 1;
     971           0 :         for (sal_uInt16 nBit=0; nBit<32; nBit++)
     972             :         {
     973           0 :             if ( nParts & nMask )
     974             :             {
     975           0 :                 if ( bFirst )
     976             :                 {
     977             :                     // innermost part: create NumGroupDimension (replacing original values)
     978             :                     // Dimension name is left unchanged
     979             : 
     980           0 :                     if ( (nParts == sheet::DataPilotFieldGroupBy::DAYS) && (rInfo.mfStep >= 1.0) )
     981             :                     {
     982             :                         // only days, and a step value specified: use numerical grouping
     983             :                         // with DateValues flag, not date grouping
     984             : 
     985           0 :                         ScDPNumGroupInfo aNumInfo( rInfo );
     986           0 :                         aNumInfo.mbDateValues = true;
     987             : 
     988           0 :                         ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, aNumInfo );
     989           0 :                         pDimData->AddNumGroupDimension( aNumGroupDim );
     990             :                     }
     991             :                     else
     992             :                     {
     993           0 :                         ScDPSaveNumGroupDimension aNumGroupDim( aBaseDimName, rInfo, nMask );
     994           0 :                         pDimData->AddNumGroupDimension( aNumGroupDim );
     995             :                     }
     996             : 
     997           0 :                     bFirst = false;
     998             :                 }
     999             :                 else
    1000             :                 {
    1001             :                     // additional parts: create GroupDimension (shown as additional dimensions)
    1002             :                     OUString aGroupDimName =
    1003           0 :                         pDimData->CreateDateGroupDimName(nMask, *pDPObj, true, &aDeletedNames);
    1004           0 :                     ScDPSaveGroupDimension aGroupDim( aBaseDimName, aGroupDimName );
    1005           0 :                     aGroupDim.SetDateInfo( rInfo, nMask );
    1006           0 :                     pDimData->AddGroupDimension( aGroupDim );
    1007             : 
    1008             :                     // set orientation
    1009           0 :                     ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
    1010           0 :                     if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
    1011             :                     {
    1012           0 :                         ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aBaseDimName );
    1013           0 :                         pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
    1014           0 :                         long nPosition = 0;     //! before (immediate) base
    1015           0 :                         aData.SetPosition( pSaveDimension, nPosition );
    1016           0 :                     }
    1017             :                 }
    1018             :             }
    1019           0 :             nMask *= 2;
    1020             :         }
    1021             :     }
    1022             : 
    1023             :     // apply changes
    1024           0 :     ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1025           0 :     pDPObj->SetSaveData( aData );
    1026           0 :     aFunc.RefreshPivotTableGroups(pDPObj);
    1027             : 
    1028             :     // unmark cell selection
    1029           0 :     Unmark();
    1030             : }
    1031             : 
    1032           0 : void ScDBFunc::NumGroupDataPilot( const ScDPNumGroupInfo& rInfo )
    1033             : {
    1034           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
    1035           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
    1036           0 :     if (!pDPObj)
    1037           0 :         return;
    1038             : 
    1039           0 :     ScDPUniqueStringSet aEntries;
    1040           0 :     long nSelectDimension = -1;
    1041           0 :     GetSelectedMemberList( aEntries, nSelectDimension );
    1042             : 
    1043           0 :     if (aEntries.empty())
    1044           0 :         return;
    1045             : 
    1046             :     bool bIsDataLayout;
    1047           0 :     OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
    1048             : 
    1049           0 :     ScDPSaveData aData( *pDPObj->GetSaveData() );
    1050           0 :     ScDPDimensionSaveData* pDimData = aData.GetDimensionData();     // created if not there
    1051             : 
    1052           0 :     ScDPSaveNumGroupDimension* pExisting = pDimData->GetNumGroupDimAcc( aDimName );
    1053           0 :     if ( pExisting )
    1054             :     {
    1055             :         // modify existing group dimension
    1056           0 :         pExisting->SetGroupInfo( rInfo );
    1057             :     }
    1058             :     else
    1059             :     {
    1060             :         // create new group dimension
    1061           0 :         ScDPSaveNumGroupDimension aNumGroupDim( aDimName, rInfo );
    1062           0 :         pDimData->AddNumGroupDimension( aNumGroupDim );
    1063             :     }
    1064             : 
    1065             :     // apply changes
    1066           0 :     ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1067           0 :     pDPObj->SetSaveData( aData );
    1068           0 :     aFunc.RefreshPivotTableGroups(pDPObj);
    1069             : 
    1070             :     // unmark cell selection
    1071           0 :     Unmark();
    1072             : }
    1073             : 
    1074           0 : void ScDBFunc::GroupDataPilot()
    1075             : {
    1076           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
    1077           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
    1078           0 :     if (!pDPObj)
    1079           0 :         return;
    1080             : 
    1081           0 :     ScDPUniqueStringSet aEntries;
    1082           0 :     long nSelectDimension = -1;
    1083           0 :     GetSelectedMemberList( aEntries, nSelectDimension );
    1084             : 
    1085           0 :     if (aEntries.empty())
    1086           0 :         return;
    1087             : 
    1088             :     bool bIsDataLayout;
    1089           0 :     OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
    1090             : 
    1091           0 :     ScDPSaveData aData( *pDPObj->GetSaveData() );
    1092           0 :     ScDPDimensionSaveData* pDimData = aData.GetDimensionData();     // created if not there
    1093             : 
    1094             :     // find original base
    1095           0 :     OUString aBaseDimName = aDimName;
    1096           0 :     const ScDPSaveGroupDimension* pBaseGroupDim = pDimData->GetNamedGroupDim( aDimName );
    1097           0 :     if ( pBaseGroupDim )
    1098             :     {
    1099             :         // any entry's SourceDimName is the original base
    1100           0 :         aBaseDimName = pBaseGroupDim->GetSourceDimName();
    1101             :     }
    1102             : 
    1103             :     // find existing group dimension
    1104             :     // (using the selected dim, can be intermediate group dim)
    1105           0 :     ScDPSaveGroupDimension* pGroupDimension = pDimData->GetGroupDimAccForBase( aDimName );
    1106             : 
    1107             :     // remove the selected items from their groups
    1108             :     // (empty groups are removed, too)
    1109           0 :     if ( pGroupDimension )
    1110             :     {
    1111           0 :         ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
    1112           0 :         for (; it != itEnd; ++it)
    1113             :         {
    1114           0 :             const OUString& aEntryName = *it;
    1115           0 :             if ( pBaseGroupDim )
    1116             :             {
    1117             :                 // for each selected (intermediate) group, remove all its items
    1118             :                 // (same logic as for adding, below)
    1119           0 :                 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
    1120           0 :                 if ( pBaseGroup )
    1121           0 :                     pBaseGroup->RemoveElementsFromGroups( *pGroupDimension );   // remove all elements
    1122             :                 else
    1123           0 :                     pGroupDimension->RemoveFromGroups( aEntryName );
    1124             :             }
    1125             :             else
    1126           0 :                 pGroupDimension->RemoveFromGroups( aEntryName );
    1127             :         }
    1128             :     }
    1129             : 
    1130           0 :     ScDPSaveGroupDimension* pNewGroupDim = NULL;
    1131           0 :     if ( !pGroupDimension )
    1132             :     {
    1133             :         // create a new group dimension
    1134             :         OUString aGroupDimName =
    1135           0 :             pDimData->CreateGroupDimName(aBaseDimName, *pDPObj, false, NULL);
    1136           0 :         pNewGroupDim = new ScDPSaveGroupDimension( aBaseDimName, aGroupDimName );
    1137             : 
    1138           0 :         pGroupDimension = pNewGroupDim;     // make changes to the new dim if none existed
    1139             : 
    1140           0 :         if ( pBaseGroupDim )
    1141             :         {
    1142             :             // If it's a higher-order group dimension, pre-allocate groups for all
    1143             :             // non-selected original groups, so the individual base members aren't
    1144             :             // used for automatic groups (this would make the original groups hard
    1145             :             // to find).
    1146             :             //! Also do this when removing groups?
    1147             :             //! Handle this case dynamically with automatic groups?
    1148             : 
    1149           0 :             long nGroupCount = pBaseGroupDim->GetGroupCount();
    1150           0 :             for ( long nGroup = 0; nGroup < nGroupCount; nGroup++ )
    1151             :             {
    1152           0 :                 const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetGroupByIndex( nGroup );
    1153             : 
    1154           0 :                 if (!aEntries.count(pBaseGroup->GetGroupName()))
    1155             :                 {
    1156             :                     // add an additional group for each item that is not in the selection
    1157           0 :                     ScDPSaveGroupItem aGroup( pBaseGroup->GetGroupName() );
    1158           0 :                     aGroup.AddElementsFromGroup( *pBaseGroup );
    1159           0 :                     pGroupDimension->AddGroupItem( aGroup );
    1160             :                 }
    1161             :             }
    1162           0 :         }
    1163             :     }
    1164           0 :     OUString aGroupDimName = pGroupDimension->GetGroupDimName();
    1165             : 
    1166           0 :     OUString aGroupName = pGroupDimension->CreateGroupName(ScGlobal::GetRscString(STR_PIVOT_GROUP));
    1167           0 :     ScDPSaveGroupItem aGroup( aGroupName );
    1168           0 :     ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
    1169           0 :     for (; it != itEnd; ++it)
    1170             :     {
    1171           0 :         const OUString& aEntryName = *it;
    1172           0 :         if ( pBaseGroupDim )
    1173             :         {
    1174             :             // for each selected (intermediate) group, add all its items
    1175           0 :             const ScDPSaveGroupItem* pBaseGroup = pBaseGroupDim->GetNamedGroup( aEntryName );
    1176           0 :             if ( pBaseGroup )
    1177           0 :                 aGroup.AddElementsFromGroup( *pBaseGroup );
    1178             :             else
    1179           0 :                 aGroup.AddElement( aEntryName );    // no group found -> automatic group, add the item itself
    1180             :         }
    1181             :         else
    1182           0 :             aGroup.AddElement( aEntryName );        // no group dimension, add all items directly
    1183             :     }
    1184             : 
    1185           0 :     pGroupDimension->AddGroupItem( aGroup );
    1186             : 
    1187           0 :     if ( pNewGroupDim )
    1188             :     {
    1189           0 :         pDimData->AddGroupDimension( *pNewGroupDim );
    1190           0 :         delete pNewGroupDim;        // AddGroupDimension copies the object
    1191             :         // don't access pGroupDimension after here
    1192             :     }
    1193           0 :     pGroupDimension = pNewGroupDim = NULL;
    1194             : 
    1195             :     // set orientation
    1196           0 :     ScDPSaveDimension* pSaveDimension = aData.GetDimensionByName( aGroupDimName );
    1197           0 :     if ( pSaveDimension->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN )
    1198             :     {
    1199           0 :         ScDPSaveDimension* pOldDimension = aData.GetDimensionByName( aDimName );
    1200           0 :         pSaveDimension->SetOrientation( pOldDimension->GetOrientation() );
    1201           0 :         long nPosition = 0;     //! before (immediate) base
    1202           0 :         aData.SetPosition( pSaveDimension, nPosition );
    1203             :     }
    1204             : 
    1205             :     // apply changes
    1206           0 :     ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1207           0 :     pDPObj->SetSaveData( aData );
    1208           0 :     aFunc.RefreshPivotTableGroups(pDPObj);
    1209             : 
    1210             :     // unmark cell selection
    1211           0 :     Unmark();
    1212             : }
    1213             : 
    1214           0 : void ScDBFunc::UngroupDataPilot()
    1215             : {
    1216           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
    1217           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
    1218           0 :     if (!pDPObj)
    1219           0 :         return;
    1220             : 
    1221           0 :     ScDPUniqueStringSet aEntries;
    1222           0 :     long nSelectDimension = -1;
    1223           0 :     GetSelectedMemberList( aEntries, nSelectDimension );
    1224             : 
    1225           0 :     if (aEntries.empty())
    1226           0 :         return;
    1227             : 
    1228             :     bool bIsDataLayout;
    1229           0 :     OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
    1230             : 
    1231           0 :     ScDPSaveData aData( *pDPObj->GetSaveData() );
    1232           0 :     if (!aData.GetExistingDimensionData())
    1233             :         // There is nothing to ungroup.
    1234           0 :         return;
    1235             : 
    1236           0 :     ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
    1237             : 
    1238           0 :     ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
    1239           0 :     const ScDPSaveNumGroupDimension* pNumGroupDim = pDimData->GetNumGroupDim( aDimName );
    1240           0 :     if ( ( pGroupDim && pGroupDim->GetDatePart() != 0 ) ||
    1241           0 :          ( pNumGroupDim && pNumGroupDim->GetDatePart() != 0 ) )
    1242             :     {
    1243             :         // Date grouping: need to remove all affected group dimensions.
    1244             :         // This is done using DateGroupDataPilot with nParts=0.
    1245             : 
    1246           0 :         DateGroupDataPilot( ScDPNumGroupInfo(), 0 );
    1247           0 :         return;
    1248             :     }
    1249             : 
    1250           0 :     if ( pGroupDim )
    1251             :     {
    1252           0 :         ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
    1253           0 :         for (; it != itEnd; ++it)
    1254           0 :             pGroupDim->RemoveGroup(*it);
    1255             : 
    1256             :         // remove group dimension if empty
    1257           0 :         bool bEmptyDim = pGroupDim->IsEmpty();
    1258           0 :         if ( !bEmptyDim )
    1259             :         {
    1260             :             // If all remaining groups in the dimension aren't shown, remove
    1261             :             // the dimension too, as if it was completely empty.
    1262           0 :             ScDPUniqueStringSet aVisibleEntries;
    1263           0 :             pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
    1264           0 :             bEmptyDim = pGroupDim->HasOnlyHidden( aVisibleEntries );
    1265             :         }
    1266           0 :         if ( bEmptyDim )
    1267             :         {
    1268           0 :             pDimData->RemoveGroupDimension( aDimName );     // pGroupDim is deleted
    1269             : 
    1270             :             // also remove SaveData settings for the dimension that no longer exists
    1271           0 :             aData.RemoveDimensionByName( aDimName );
    1272             :         }
    1273             :     }
    1274           0 :     else if ( pNumGroupDim )
    1275             :     {
    1276             :         // remove the numerical grouping
    1277           0 :         pDimData->RemoveNumGroupDimension( aDimName );
    1278             :         // SaveData settings can remain unchanged - the same dimension still exists
    1279             :     }
    1280             : 
    1281             :     // apply changes
    1282           0 :     ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1283           0 :     pDPObj->SetSaveData( aData );
    1284           0 :     aFunc.RefreshPivotTableGroups(pDPObj);
    1285             : 
    1286             :     // unmark cell selection
    1287           0 :     Unmark();
    1288             : }
    1289             : 
    1290           0 : static OUString lcl_replaceMemberNameInSubtotal(const OUString& rSubtotal, const OUString& rMemberName)
    1291             : {
    1292           0 :     sal_Int32 n = rSubtotal.getLength();
    1293           0 :     const sal_Unicode* p = rSubtotal.getStr();
    1294           0 :     OUStringBuffer aBuf, aWordBuf;
    1295           0 :     for (sal_Int32 i = 0; i < n; ++i)
    1296             :     {
    1297           0 :         sal_Unicode c = p[i];
    1298           0 :         if (c == sal_Unicode(' '))
    1299             :         {
    1300           0 :             OUString aWord = aWordBuf.makeStringAndClear();
    1301           0 :             if (aWord.equals(rMemberName))
    1302           0 :                 aBuf.append(sal_Unicode('?'));
    1303             :             else
    1304           0 :                 aBuf.append(aWord);
    1305           0 :             aBuf.append(c);
    1306             :         }
    1307           0 :         else if (c == sal_Unicode('\\'))
    1308             :         {
    1309             :             // Escape a backslash character.
    1310           0 :             aWordBuf.append(c);
    1311           0 :             aWordBuf.append(c);
    1312             :         }
    1313           0 :         else if (c == sal_Unicode('?'))
    1314             :         {
    1315             :             // A literal '?' must be escaped with a backslash ('\');
    1316           0 :             aWordBuf.append(sal_Unicode('\\'));
    1317           0 :             aWordBuf.append(c);
    1318             :         }
    1319             :         else
    1320           0 :             aWordBuf.append(c);
    1321             :     }
    1322             : 
    1323           0 :     if (!aWordBuf.isEmpty())
    1324             :     {
    1325           0 :         OUString aWord = aWordBuf.makeStringAndClear();
    1326           0 :         if (aWord.equals(rMemberName))
    1327           0 :             aBuf.append(sal_Unicode('?'));
    1328             :         else
    1329           0 :             aBuf.append(aWord);
    1330             :     }
    1331             : 
    1332           0 :     return aBuf.makeStringAndClear();
    1333             : }
    1334             : 
    1335           0 : void ScDBFunc::DataPilotInput( const ScAddress& rPos, const OUString& rString )
    1336             : {
    1337             :     using namespace ::com::sun::star::sheet;
    1338             : 
    1339           0 :     ScDocument* pDoc = GetViewData()->GetDocument();
    1340           0 :     ScDPObject* pDPObj = pDoc->GetDPAtCursor( rPos.Col(), rPos.Row(), rPos.Tab() );
    1341           0 :     if (!pDPObj)
    1342           0 :         return;
    1343             : 
    1344           0 :     OUString aOldText = pDoc->GetString(rPos.Col(), rPos.Row(), rPos.Tab());
    1345             : 
    1346           0 :     if ( aOldText == rString )
    1347             :     {
    1348             :         // nothing to do: silently exit
    1349           0 :         return;
    1350             :     }
    1351             : 
    1352           0 :     sal_uInt16 nErrorId = 0;
    1353             : 
    1354           0 :     pDPObj->BuildAllDimensionMembers();
    1355           0 :     ScDPSaveData aData( *pDPObj->GetSaveData() );
    1356           0 :     bool bChange = false;
    1357             : 
    1358           0 :     sal_uInt16 nOrient = DataPilotFieldOrientation_HIDDEN;
    1359           0 :     long nField = pDPObj->GetHeaderDim( rPos, nOrient );
    1360           0 :     if ( nField >= 0 )
    1361             :     {
    1362             :         // changing a field title
    1363           0 :         if ( aData.GetExistingDimensionData() )
    1364             :         {
    1365             :             // only group dimensions can be renamed
    1366             : 
    1367           0 :             ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
    1368           0 :             ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aOldText );
    1369           0 :             if ( pGroupDim )
    1370             :             {
    1371             :                 // valid name: not empty, no existing dimension (group or other)
    1372           0 :                 if (!rString.isEmpty() && !pDPObj->IsDimNameInUse(rString))
    1373             :                 {
    1374           0 :                     pGroupDim->Rename( rString );
    1375             : 
    1376             :                     // also rename in SaveData to preserve the field settings
    1377           0 :                     ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aOldText );
    1378           0 :                     pSaveDim->SetName( rString );
    1379             : 
    1380           0 :                     bChange = true;
    1381             :                 }
    1382             :                 else
    1383           0 :                     nErrorId = STR_INVALIDNAME;
    1384             :             }
    1385             :         }
    1386           0 :         else if (nOrient == DataPilotFieldOrientation_COLUMN || nOrient == DataPilotFieldOrientation_ROW)
    1387             :         {
    1388           0 :             bool bDataLayout = false;
    1389           0 :             OUString aDimName = pDPObj->GetDimName(nField, bDataLayout);
    1390           0 :             ScDPSaveDimension* pDim = bDataLayout ? aData.GetDataLayoutDimension() : aData.GetDimensionByName(aDimName);
    1391           0 :             if (pDim)
    1392             :             {
    1393           0 :                 if (!rString.isEmpty())
    1394             :                 {
    1395           0 :                     if (rString.equalsIgnoreAsciiCase(aDimName))
    1396             :                     {
    1397           0 :                         pDim->RemoveLayoutName();
    1398           0 :                         bChange = true;
    1399             :                     }
    1400           0 :                     else if (!pDPObj->IsDimNameInUse(rString))
    1401             :                     {
    1402           0 :                         pDim->SetLayoutName(rString);
    1403           0 :                         bChange = true;
    1404             :                     }
    1405             :                     else
    1406           0 :                         nErrorId = STR_INVALIDNAME;
    1407             :                 }
    1408             :                 else
    1409           0 :                     nErrorId = STR_INVALIDNAME;
    1410           0 :             }
    1411             :         }
    1412             :     }
    1413           0 :     else if (pDPObj->IsDataDescriptionCell(rPos))
    1414             :     {
    1415             :         // There is only one data dimension.
    1416           0 :         ScDPSaveDimension* pDim = aData.GetFirstDimension(sheet::DataPilotFieldOrientation_DATA);
    1417           0 :         if (pDim)
    1418             :         {
    1419           0 :             if (!rString.isEmpty())
    1420             :             {
    1421           0 :                 if (pDim->GetName().equalsIgnoreAsciiCase(rString))
    1422             :                 {
    1423           0 :                     pDim->RemoveLayoutName();
    1424           0 :                     bChange = true;
    1425             :                 }
    1426           0 :                 else if (!pDPObj->IsDimNameInUse(rString))
    1427             :                 {
    1428           0 :                     pDim->SetLayoutName(rString);
    1429           0 :                     bChange = true;
    1430             :                 }
    1431             :                 else
    1432           0 :                     nErrorId = STR_INVALIDNAME;
    1433             :             }
    1434             :             else
    1435           0 :                 nErrorId = STR_INVALIDNAME;
    1436             :         }
    1437             :     }
    1438             :     else
    1439             :     {
    1440             :         // This is not a field header.
    1441           0 :         sheet::DataPilotTableHeaderData aPosData;
    1442           0 :         pDPObj->GetHeaderPositionData(rPos, aPosData);
    1443             : 
    1444           0 :         if ((aPosData.Flags & MemberResultFlags::HASMEMBER) && !aOldText.isEmpty())
    1445             :         {
    1446           0 :             if ( aData.GetExistingDimensionData() && !(aPosData.Flags & MemberResultFlags::SUBTOTAL))
    1447             :             {
    1448             :                 bool bIsDataLayout;
    1449           0 :                 OUString aDimName = pDPObj->GetDimName( aPosData.Dimension, bIsDataLayout );
    1450             : 
    1451           0 :                 ScDPDimensionSaveData* pDimData = aData.GetDimensionData();
    1452           0 :                 ScDPSaveGroupDimension* pGroupDim = pDimData->GetNamedGroupDimAcc( aDimName );
    1453           0 :                 if ( pGroupDim )
    1454             :                 {
    1455             :                     // valid name: not empty, no existing group in this dimension
    1456             :                     //! ignore case?
    1457           0 :                     if (!rString.isEmpty() && !pGroupDim->GetNamedGroup(rString))
    1458             :                     {
    1459           0 :                         ScDPSaveGroupItem* pGroup = pGroupDim->GetNamedGroupAcc( aOldText );
    1460           0 :                         if ( pGroup )
    1461           0 :                             pGroup->Rename( rString );     // rename the existing group
    1462             :                         else
    1463             :                         {
    1464             :                             // create a new group to replace the automatic group
    1465           0 :                             ScDPSaveGroupItem aGroup( rString );
    1466           0 :                             aGroup.AddElement( aOldText );
    1467           0 :                             pGroupDim->AddGroupItem( aGroup );
    1468             :                         }
    1469             : 
    1470             :                         // in both cases also adjust savedata, to preserve member settings (show details)
    1471           0 :                         ScDPSaveDimension* pSaveDim = aData.GetDimensionByName( aDimName );
    1472           0 :                         ScDPSaveMember* pSaveMember = pSaveDim->GetExistingMemberByName( aOldText );
    1473           0 :                         if ( pSaveMember )
    1474           0 :                             pSaveMember->SetName( rString );
    1475             : 
    1476           0 :                         bChange = true;
    1477             :                     }
    1478             :                     else
    1479           0 :                         nErrorId = STR_INVALIDNAME;
    1480           0 :                  }
    1481             :             }
    1482           0 :             else if ((aPosData.Flags & MemberResultFlags::GRANDTOTAL))
    1483             :             {
    1484           0 :                 aData.SetGrandTotalName(rString);
    1485           0 :                 bChange = true;
    1486             :             }
    1487           0 :             else if (aPosData.Dimension >= 0 && !aPosData.MemberName.isEmpty())
    1488             :             {
    1489           0 :                 bool bDataLayout = false;
    1490           0 :                 OUString aDimName = pDPObj->GetDimName(static_cast<long>(aPosData.Dimension), bDataLayout);
    1491           0 :                 if (bDataLayout)
    1492             :                 {
    1493             :                     // data dimension
    1494             :                     do
    1495             :                     {
    1496           0 :                         if ((aPosData.Flags & MemberResultFlags::SUBTOTAL))
    1497           0 :                             break;
    1498             : 
    1499           0 :                         ScDPSaveDimension* pDim = aData.GetDimensionByName(aPosData.MemberName);
    1500           0 :                         if (!pDim)
    1501           0 :                             break;
    1502             : 
    1503           0 :                         if (rString.isEmpty())
    1504             :                         {
    1505           0 :                             nErrorId = STR_INVALIDNAME;
    1506           0 :                             break;
    1507             :                         }
    1508             : 
    1509           0 :                         if (aPosData.MemberName.equalsIgnoreAsciiCase(rString))
    1510             :                         {
    1511           0 :                             pDim->RemoveLayoutName();
    1512           0 :                             bChange = true;
    1513             :                         }
    1514           0 :                         else if (!pDPObj->IsDimNameInUse(rString))
    1515             :                         {
    1516           0 :                             pDim->SetLayoutName(rString);
    1517           0 :                             bChange = true;
    1518             :                         }
    1519             :                         else
    1520           0 :                             nErrorId = STR_INVALIDNAME;
    1521             :                     }
    1522             :                     while (false);
    1523             :                 }
    1524             :                 else
    1525             :                 {
    1526             :                     // field member
    1527             :                     do
    1528             :                     {
    1529           0 :                         ScDPSaveDimension* pDim = aData.GetDimensionByName(aDimName);
    1530           0 :                         if (!pDim)
    1531           0 :                             break;
    1532             : 
    1533           0 :                         ScDPSaveMember* pMem = pDim->GetExistingMemberByName(aPosData.MemberName);
    1534           0 :                         if (!pMem)
    1535           0 :                             break;
    1536             : 
    1537           0 :                         if ((aPosData.Flags & MemberResultFlags::SUBTOTAL))
    1538             :                         {
    1539             :                             // Change subtotal only when the table has one data dimension.
    1540           0 :                             if (aData.GetDataDimensionCount() > 1)
    1541           0 :                                 break;
    1542             : 
    1543             :                             // display name for subtotal is allowed only if the subtotal type is 'Automatic'.
    1544           0 :                             if (pDim->GetSubTotalsCount() != 1)
    1545           0 :                                 break;
    1546             : 
    1547           0 :                             if (pDim->GetSubTotalFunc(0) != sheet::GeneralFunction_AUTO)
    1548           0 :                                 break;
    1549             : 
    1550           0 :                             const OUString* pLayoutName = pMem->GetLayoutName();
    1551           0 :                             String aMemberName;
    1552           0 :                             if (pLayoutName)
    1553           0 :                                 aMemberName = *pLayoutName;
    1554             :                             else
    1555           0 :                                 aMemberName = aPosData.MemberName;
    1556             : 
    1557           0 :                             String aNew = lcl_replaceMemberNameInSubtotal(rString, aMemberName);
    1558           0 :                             pDim->SetSubtotalName(aNew);
    1559           0 :                             bChange = true;
    1560             :                         }
    1561             :                         else
    1562             :                         {
    1563             :                             // Check to make sure the member name isn't
    1564             :                             // already used.
    1565           0 :                             if (!rString.isEmpty())
    1566             :                             {
    1567           0 :                                 if (rString.equalsIgnoreAsciiCase(pMem->GetName()))
    1568             :                                 {
    1569           0 :                                     pMem->RemoveLayoutName();
    1570           0 :                                     bChange = true;
    1571             :                                 }
    1572           0 :                                 else if (!pDim->IsMemberNameInUse(rString))
    1573             :                                 {
    1574           0 :                                     pMem->SetLayoutName(rString);
    1575           0 :                                     bChange = true;
    1576             :                                 }
    1577             :                                 else
    1578           0 :                                     nErrorId = STR_INVALIDNAME;
    1579             :                             }
    1580             :                             else
    1581           0 :                                 nErrorId = STR_INVALIDNAME;
    1582             :                         }
    1583             :                     }
    1584             :                     while (false);
    1585           0 :                 }
    1586             :             }
    1587           0 :         }
    1588             :     }
    1589             : 
    1590           0 :     if ( bChange )
    1591             :     {
    1592             :         // apply changes
    1593           0 :         ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1594           0 :         pDPObj->SetSaveData( aData );
    1595           0 :         aFunc.UpdatePivotTable(*pDPObj, true, false);
    1596             :     }
    1597             :     else
    1598             :     {
    1599           0 :         if ( !nErrorId )
    1600           0 :             nErrorId = STR_ERR_DATAPILOT_INPUT;
    1601           0 :         ErrorMessage( nErrorId );
    1602           0 :     }
    1603             : }
    1604             : 
    1605           0 : static void lcl_MoveToEnd( ScDPSaveDimension& rDim, const String& rItemName )
    1606             : {
    1607           0 :     ScDPSaveMember* pNewMember = NULL;
    1608           0 :     const ScDPSaveMember* pOldMember = rDim.GetExistingMemberByName( rItemName );
    1609           0 :     if ( pOldMember )
    1610           0 :         pNewMember = new ScDPSaveMember( *pOldMember );
    1611             :     else
    1612           0 :         pNewMember = new ScDPSaveMember( rItemName );
    1613           0 :     rDim.AddMember( pNewMember );
    1614             :     // AddMember takes ownership of the new pointer,
    1615             :     // puts it to the end of the list even if it was in the list before.
    1616           0 : }
    1617             : 
    1618             : struct ScOUStringCollate
    1619             : {
    1620             :     CollatorWrapper* mpCollator;
    1621             : 
    1622           0 :     ScOUStringCollate(CollatorWrapper* pColl) : mpCollator(pColl) {}
    1623             : 
    1624           0 :     bool operator()(const OUString& rStr1, const OUString& rStr2) const
    1625             :     {
    1626           0 :         return ( mpCollator->compareString(rStr1, rStr2) < 0 );
    1627             :     }
    1628             : };
    1629             : 
    1630           0 : bool ScDBFunc::DataPilotSort( const ScAddress& rPos, bool bAscending, sal_uInt16* pUserListId )
    1631             : {
    1632           0 :     ScDocument* pDoc = GetViewData()->GetDocument();
    1633           0 :     ScDPObject* pDPObj = pDoc->GetDPAtCursor(rPos.Col(), rPos.Row(), rPos.Tab());
    1634           0 :     if (!pDPObj)
    1635           0 :         return false;
    1636             : 
    1637             :     // We need to run this to get all members later.
    1638           0 :     if ( pUserListId )
    1639           0 :         pDPObj->BuildAllDimensionMembers();
    1640             : 
    1641             :     sal_uInt16 nOrientation;
    1642           0 :     long nDimIndex = pDPObj->GetHeaderDim(rPos, nOrientation);
    1643           0 :     if (nDimIndex < 0)
    1644             :         // Invalid dimension index.  Bail out.
    1645           0 :         return false;
    1646             : 
    1647           0 :     ScDPSaveData* pSaveData = pDPObj->GetSaveData();
    1648           0 :     if (!pSaveData)
    1649           0 :         return false;
    1650             : 
    1651           0 :     ScDPSaveData aNewSaveData(*pSaveData);
    1652             :     bool bDataLayout;
    1653           0 :     OUString aDimName = pDPObj->GetDimName(nDimIndex, bDataLayout);
    1654           0 :     ScDPSaveDimension* pSaveDim = aNewSaveData.GetDimensionByName(aDimName);
    1655           0 :     if (!pSaveDim)
    1656           0 :         return false;
    1657             : 
    1658             :     // manual evaluation of sort order is only needed if a user list id is given
    1659           0 :     if ( pUserListId )
    1660             :     {
    1661             :         typedef ScDPSaveDimension::MemberList MemList;
    1662           0 :         const MemList& rDimMembers = pSaveDim->GetMembers();
    1663           0 :         list<OUString> aMembers;
    1664           0 :         boost::unordered_set<OUString, OUStringHash> aMemberSet;
    1665           0 :         size_t nMemberCount = 0;
    1666           0 :         for (MemList::const_iterator itr = rDimMembers.begin(), itrEnd = rDimMembers.end();
    1667             :               itr != itrEnd; ++itr)
    1668             :         {
    1669           0 :             ScDPSaveMember* pMem = *itr;
    1670           0 :             aMembers.push_back(pMem->GetName());
    1671           0 :             aMemberSet.insert(pMem->GetName());
    1672           0 :             ++nMemberCount;
    1673             :         }
    1674             : 
    1675             :         // Sort the member list in ascending order.
    1676           0 :         ScOUStringCollate aCollate( ScGlobal::GetCollator() );
    1677           0 :         aMembers.sort(aCollate);
    1678             : 
    1679             :         // Collect and rank those custom sort strings that also exist in the member name list.
    1680             : 
    1681             :         typedef boost::unordered_map<OUString, sal_uInt16, OUStringHash> UserSortMap;
    1682           0 :         UserSortMap aSubStrs;
    1683           0 :         sal_uInt16 nSubCount = 0;
    1684           0 :         if (pUserListId)
    1685             :         {
    1686           0 :             ScUserList* pUserList = ScGlobal::GetUserList();
    1687           0 :             if (!pUserList)
    1688           0 :                 return false;
    1689             : 
    1690             :             {
    1691           0 :                 size_t n = pUserList->size();
    1692           0 :                 if (!n || *pUserListId >= static_cast<sal_uInt16>(n))
    1693           0 :                     return false;
    1694             :             }
    1695             : 
    1696           0 :             const ScUserListData* pData = (*pUserList)[*pUserListId];
    1697           0 :             if (pData)
    1698             :             {
    1699           0 :                 sal_uInt16 n = pData->GetSubCount();
    1700           0 :                 for (sal_uInt16 i = 0; i < n; ++i)
    1701             :                 {
    1702           0 :                     OUString aSub = pData->GetSubStr(i);
    1703           0 :                     if (!aMemberSet.count(aSub))
    1704             :                         // This string doesn't exist in the member name set.  Don't add this.
    1705           0 :                         continue;
    1706             : 
    1707           0 :                     aSubStrs.insert(UserSortMap::value_type(aSub, nSubCount++));
    1708           0 :                 }
    1709             :             }
    1710             :         }
    1711             : 
    1712             :         // Rank all members.
    1713             : 
    1714           0 :         vector<OUString> aRankedNames(nMemberCount);
    1715           0 :         sal_uInt16 nCurStrId = 0;
    1716           0 :         for (list<OUString>::const_iterator itr = aMembers.begin(), itrEnd = aMembers.end();
    1717             :               itr != itrEnd; ++itr)
    1718             :         {
    1719           0 :             OUString aName = *itr;
    1720           0 :             sal_uInt16 nRank = 0;
    1721           0 :             UserSortMap::const_iterator itrSub = aSubStrs.find(aName);
    1722           0 :             if (itrSub == aSubStrs.end())
    1723           0 :                 nRank = nSubCount + nCurStrId++;
    1724             :             else
    1725           0 :                 nRank = itrSub->second;
    1726             : 
    1727           0 :             if (!bAscending)
    1728           0 :                 nRank = static_cast< sal_uInt16 >( nMemberCount - nRank - 1 );
    1729             : 
    1730           0 :             aRankedNames[nRank] = aName;
    1731           0 :         }
    1732             : 
    1733             :         // Re-order ScDPSaveMember instances with the new ranks.
    1734             : 
    1735           0 :         for (vector<OUString>::const_iterator itr = aRankedNames.begin(), itrEnd = aRankedNames.end();
    1736             :               itr != itrEnd; ++itr)
    1737             :         {
    1738           0 :             const ScDPSaveMember* pOldMem = pSaveDim->GetExistingMemberByName(*itr);
    1739           0 :             if (!pOldMem)
    1740             :                 // All members are supposed to be present.
    1741           0 :                 continue;
    1742             : 
    1743           0 :             ScDPSaveMember* pNewMem = new ScDPSaveMember(*pOldMem);
    1744           0 :             pSaveDim->AddMember(pNewMem);
    1745             :         }
    1746             : 
    1747             :         // Set the sorting mode to manual for now.  We may introduce a new sorting
    1748             :         // mode later on.
    1749             : 
    1750           0 :         sheet::DataPilotFieldSortInfo aSortInfo;
    1751           0 :         aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
    1752           0 :         pSaveDim->SetSortInfo(&aSortInfo);
    1753             :     }
    1754             :     else
    1755             :     {
    1756             :         // without user list id, just apply sorting mode
    1757             : 
    1758           0 :         sheet::DataPilotFieldSortInfo aSortInfo;
    1759           0 :         aSortInfo.Mode = sheet::DataPilotFieldSortMode::NAME;
    1760           0 :         aSortInfo.IsAscending = bAscending;
    1761           0 :         pSaveDim->SetSortInfo(&aSortInfo);
    1762             :     }
    1763             : 
    1764             :     // Update the datapilot with the newly sorted field members.
    1765             : 
    1766           0 :     auto_ptr<ScDPObject> pNewObj(new ScDPObject(*pDPObj));
    1767           0 :     pNewObj->SetSaveData(aNewSaveData);
    1768           0 :     ScDBDocFunc aFunc(*GetViewData()->GetDocShell());
    1769             : 
    1770           0 :     return aFunc.DataPilotUpdate(pDPObj, pNewObj.get(), true, false);
    1771             : }
    1772             : 
    1773           0 : sal_Bool ScDBFunc::DataPilotMove( const ScRange& rSource, const ScAddress& rDest )
    1774             : {
    1775           0 :     sal_Bool bRet = false;
    1776           0 :     ScDocument* pDoc = GetViewData()->GetDocument();
    1777           0 :     ScDPObject* pDPObj = pDoc->GetDPAtCursor( rSource.aStart.Col(), rSource.aStart.Row(), rSource.aStart.Tab() );
    1778           0 :     if ( pDPObj && pDPObj == pDoc->GetDPAtCursor( rDest.Col(), rDest.Row(), rDest.Tab() ) )
    1779             :     {
    1780           0 :         sheet::DataPilotTableHeaderData aDestData;
    1781           0 :         pDPObj->GetHeaderPositionData( rDest, aDestData );
    1782           0 :         bool bValid = ( aDestData.Dimension >= 0 );        // dropping onto a field
    1783             : 
    1784             :         // look through the source range
    1785           0 :         boost::unordered_set< OUString, OUStringHash, std::equal_to<OUString> > aMembersSet;   // for lookup
    1786           0 :         std::vector< OUString > aMembersVector;  // members in original order, for inserting
    1787           0 :         aMembersVector.reserve( std::max( static_cast<SCSIZE>( rSource.aEnd.Col() - rSource.aStart.Col() + 1 ),
    1788           0 :                                           static_cast<SCSIZE>( rSource.aEnd.Row() - rSource.aStart.Row() + 1 ) ) );
    1789           0 :         for (SCROW nRow = rSource.aStart.Row(); bValid && nRow <= rSource.aEnd.Row(); ++nRow )
    1790           0 :             for (SCCOL nCol = rSource.aStart.Col(); bValid && nCol <= rSource.aEnd.Col(); ++nCol )
    1791             :             {
    1792           0 :                 sheet::DataPilotTableHeaderData aSourceData;
    1793           0 :                 pDPObj->GetHeaderPositionData( ScAddress( nCol, nRow, rSource.aStart.Tab() ), aSourceData );
    1794           0 :                 if ( aSourceData.Dimension == aDestData.Dimension && !aSourceData.MemberName.isEmpty() )
    1795             :                 {
    1796           0 :                     if ( aMembersSet.find( aSourceData.MemberName ) == aMembersSet.end() )
    1797             :                     {
    1798           0 :                         aMembersSet.insert( aSourceData.MemberName );
    1799           0 :                         aMembersVector.push_back( aSourceData.MemberName );
    1800             :                     }
    1801             :                     // duplicates are ignored
    1802             :                 }
    1803             :                 else
    1804           0 :                     bValid = false;     // empty (subtotal) or different field
    1805           0 :             }
    1806             : 
    1807           0 :         if ( bValid )
    1808             :         {
    1809             :             bool bIsDataLayout;
    1810           0 :             OUString aDimName = pDPObj->GetDimName( aDestData.Dimension, bIsDataLayout );
    1811           0 :             if ( !bIsDataLayout )
    1812             :             {
    1813           0 :                 ScDPSaveData aData( *pDPObj->GetSaveData() );
    1814           0 :                 ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
    1815             : 
    1816             :                 // get all member names in source order
    1817           0 :                 uno::Sequence<OUString> aMemberNames;
    1818           0 :                 pDPObj->GetMemberNames( aDestData.Dimension, aMemberNames );
    1819             : 
    1820           0 :                 bool bInserted = false;
    1821             : 
    1822           0 :                 sal_Int32 nMemberCount = aMemberNames.getLength();
    1823           0 :                 for (sal_Int32 nMemberPos=0; nMemberPos<nMemberCount; ++nMemberPos)
    1824             :                 {
    1825           0 :                     String aMemberStr( aMemberNames[nMemberPos] );
    1826             : 
    1827           0 :                     if ( !bInserted && aMemberNames[nMemberPos] == aDestData.MemberName )
    1828             :                     {
    1829             :                         // insert dragged items before this item
    1830           0 :                         for ( std::vector<OUString>::const_iterator aIter = aMembersVector.begin();
    1831           0 :                               aIter != aMembersVector.end(); ++aIter )
    1832           0 :                             lcl_MoveToEnd( *pDim, *aIter );
    1833           0 :                         bInserted = true;
    1834             :                     }
    1835             : 
    1836           0 :                     if ( aMembersSet.find( aMemberStr ) == aMembersSet.end() )  // skip dragged items
    1837           0 :                         lcl_MoveToEnd( *pDim, aMemberStr );
    1838           0 :                 }
    1839             :                 // insert dragged item at end if dest wasn't found (for example, empty)
    1840           0 :                 if ( !bInserted )
    1841           0 :                     for ( std::vector<OUString>::const_iterator aIter = aMembersVector.begin();
    1842           0 :                           aIter != aMembersVector.end(); ++aIter )
    1843           0 :                         lcl_MoveToEnd( *pDim, *aIter );
    1844             : 
    1845             :                 // Items that were in SaveData, but not in the source, end up at the start of the list.
    1846             : 
    1847             :                 // set flag for manual sorting
    1848           0 :                 sheet::DataPilotFieldSortInfo aSortInfo;
    1849           0 :                 aSortInfo.Mode = sheet::DataPilotFieldSortMode::MANUAL;
    1850           0 :                 pDim->SetSortInfo( &aSortInfo );
    1851             : 
    1852             :                 // apply changes
    1853           0 :                 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1854           0 :                 ScDPObject* pNewObj = new ScDPObject( *pDPObj );
    1855           0 :                 pNewObj->SetSaveData( aData );
    1856           0 :                 aFunc.DataPilotUpdate( pDPObj, pNewObj, sal_True, false );      //! bApi for drag&drop?
    1857           0 :                 delete pNewObj;
    1858             : 
    1859           0 :                 Unmark();       // entry was moved - no use in leaving the old cell selected
    1860             : 
    1861           0 :                 bRet = sal_True;
    1862           0 :             }
    1863           0 :         }
    1864             :     }
    1865             : 
    1866           0 :     return bRet;
    1867             : }
    1868             : 
    1869           0 : bool ScDBFunc::HasSelectionForDrillDown( sal_uInt16& rOrientation )
    1870             : {
    1871           0 :     bool bRet = false;
    1872             : 
    1873           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
    1874           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
    1875           0 :     if ( pDPObj )
    1876             :     {
    1877           0 :         ScDPUniqueStringSet aEntries;
    1878           0 :         long nSelectDimension = -1;
    1879           0 :         GetSelectedMemberList( aEntries, nSelectDimension );
    1880             : 
    1881           0 :         if (!aEntries.empty())
    1882             :         {
    1883             :             bool bIsDataLayout;
    1884           0 :             OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
    1885           0 :             if ( !bIsDataLayout )
    1886             :             {
    1887           0 :                 ScDPSaveData* pSaveData = pDPObj->GetSaveData();
    1888           0 :                 ScDPSaveDimension* pDim = pSaveData->GetExistingDimensionByName( aDimName );
    1889           0 :                 if ( pDim )
    1890             :                 {
    1891           0 :                     sal_uInt16 nDimOrient = pDim->GetOrientation();
    1892           0 :                     ScDPSaveDimension* pInner = pSaveData->GetInnermostDimension( nDimOrient );
    1893           0 :                     if ( pDim == pInner )
    1894             :                     {
    1895           0 :                         rOrientation = nDimOrient;
    1896           0 :                         bRet = true;
    1897             :                     }
    1898             :                 }
    1899           0 :             }
    1900           0 :         }
    1901             :     }
    1902             : 
    1903           0 :     return bRet;
    1904             : }
    1905             : 
    1906           0 : void ScDBFunc::SetDataPilotDetails(bool bShow, const OUString* pNewDimensionName)
    1907             : {
    1908           0 :     ScDPObject* pDPObj = GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
    1909           0 :                                         GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
    1910           0 :     if ( pDPObj )
    1911             :     {
    1912           0 :         ScDPUniqueStringSet aEntries;
    1913           0 :         long nSelectDimension = -1;
    1914           0 :         GetSelectedMemberList( aEntries, nSelectDimension );
    1915             : 
    1916           0 :         if (!aEntries.empty())
    1917             :         {
    1918             :             bool bIsDataLayout;
    1919           0 :             OUString aDimName = pDPObj->GetDimName( nSelectDimension, bIsDataLayout );
    1920           0 :             if ( !bIsDataLayout )
    1921             :             {
    1922           0 :                 ScDPSaveData aData( *pDPObj->GetSaveData() );
    1923           0 :                 ScDPSaveDimension* pDim = aData.GetDimensionByName( aDimName );
    1924             : 
    1925           0 :                 if ( bShow && pNewDimensionName )
    1926             :                 {
    1927             :                     //  add the new dimension with the same orientation, at the end
    1928             : 
    1929           0 :                     ScDPSaveDimension* pNewDim = aData.GetDimensionByName( *pNewDimensionName );
    1930           0 :                     ScDPSaveDimension* pDuplicated = NULL;
    1931           0 :                     if ( pNewDim->GetOrientation() == sheet::DataPilotFieldOrientation_DATA )
    1932             :                     {
    1933             :                         // Need to duplicate the dimension, create column/row in addition to data:
    1934             :                         // The duplicated dimension inherits the existing settings, pNewDim is modified below.
    1935           0 :                         pDuplicated = aData.DuplicateDimension( *pNewDimensionName );
    1936             :                     }
    1937             : 
    1938           0 :                     sal_uInt16 nOrientation = pDim->GetOrientation();
    1939           0 :                     pNewDim->SetOrientation( nOrientation );
    1940             : 
    1941           0 :                     long nPosition = LONG_MAX;
    1942           0 :                     aData.SetPosition( pNewDim, nPosition );
    1943             : 
    1944           0 :                     ScDPSaveDimension* pDataLayout = aData.GetDataLayoutDimension();
    1945           0 :                     if ( pDataLayout->GetOrientation() == nOrientation &&
    1946           0 :                          aData.GetDataDimensionCount() <= 1 )
    1947             :                     {
    1948             :                         // If there is only one data dimension, the data layout dimension
    1949             :                         // must still be the last one in its orientation.
    1950           0 :                         aData.SetPosition( pDataLayout, nPosition );
    1951             :                     }
    1952             : 
    1953           0 :                     if ( pDuplicated )
    1954             :                     {
    1955             :                         // The duplicated (data) dimension needs to be behind the original dimension
    1956           0 :                         aData.SetPosition( pDuplicated, nPosition );
    1957             :                     }
    1958             : 
    1959             :                     //  Hide details for all visible members (selected are changed below).
    1960             :                     //! Use all members from source level instead (including non-visible)?
    1961             : 
    1962           0 :                     ScDPUniqueStringSet aVisibleEntries;
    1963           0 :                     pDPObj->GetMemberResultNames( aVisibleEntries, nSelectDimension );
    1964             : 
    1965           0 :                     ScDPUniqueStringSet::const_iterator it = aVisibleEntries.begin(), itEnd = aVisibleEntries.end();
    1966           0 :                     for (; it != itEnd; ++it)
    1967             :                     {
    1968           0 :                         const OUString& aVisName = *it;
    1969           0 :                         ScDPSaveMember* pMember = pDim->GetMemberByName( aVisName );
    1970           0 :                         pMember->SetShowDetails( false );
    1971           0 :                     }
    1972             :                 }
    1973             : 
    1974           0 :                 ScDPUniqueStringSet::const_iterator it = aEntries.begin(), itEnd = aEntries.end();
    1975           0 :                 for (; it != itEnd; ++it)
    1976             :                 {
    1977           0 :                     ScDPSaveMember* pMember = pDim->GetMemberByName(*it);
    1978           0 :                     pMember->SetShowDetails( bShow );
    1979             :                 }
    1980             : 
    1981             :                 // apply changes
    1982           0 :                 ScDBDocFunc aFunc( *GetViewData()->GetDocShell() );
    1983           0 :                 ScDPObject* pNewObj = new ScDPObject( *pDPObj );
    1984           0 :                 pNewObj->SetSaveData( aData );
    1985           0 :                 aFunc.DataPilotUpdate( pDPObj, pNewObj, sal_True, false );
    1986           0 :                 delete pNewObj;
    1987             : 
    1988             :                 // unmark cell selection
    1989           0 :                 Unmark();
    1990           0 :             }
    1991           0 :         }
    1992             :     }
    1993           0 : }
    1994             : 
    1995          36 : void ScDBFunc::ShowDataPilotSourceData( ScDPObject& rDPObj, const Sequence<sheet::DataPilotFieldFilter>& rFilters )
    1996             : {
    1997          36 :     ScDocument* pDoc = GetViewData()->GetDocument();
    1998          36 :     if (pDoc->GetDocumentShell()->IsReadOnly())
    1999             :     {
    2000           0 :         ErrorMessage(STR_READONLYERR);
    2001          20 :         return;
    2002             :     }
    2003             : 
    2004          36 :     Reference<sheet::XDimensionsSupplier> xDimSupplier = rDPObj.GetSource();
    2005          52 :     Reference<container::XNameAccess> xDims = xDimSupplier->getDimensions();
    2006          52 :     Reference<sheet::XDrillDownDataSupplier> xDDSupplier(xDimSupplier, UNO_QUERY);
    2007          36 :     if (!xDDSupplier.is())
    2008           0 :         return;
    2009             : 
    2010          52 :     Sequence< Sequence<Any> > aTabData = xDDSupplier->getDrillDownData(rFilters);
    2011          36 :     sal_Int32 nRowSize = aTabData.getLength();
    2012          36 :     if (nRowSize <= 1)
    2013             :         // There is no data to show.  Bail out.
    2014          20 :         return;
    2015             : 
    2016          16 :     sal_Int32 nColSize = aTabData[0].getLength();
    2017             : 
    2018          16 :     SCTAB nNewTab = GetViewData()->GetTabNo();
    2019             : 
    2020          32 :     auto_ptr<ScDocument> pInsDoc(new ScDocument(SCDOCMODE_CLIP));
    2021          16 :     pInsDoc->ResetClip( pDoc, nNewTab );
    2022          52 :     for (SCROW nRow = 0; nRow < nRowSize; ++nRow)
    2023             :     {
    2024         216 :         for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
    2025             :         {
    2026         180 :             const Any& rAny = aTabData[nRow][nCol];
    2027         180 :             OUString aStr;
    2028             :             double fVal;
    2029         180 :             if (rAny >>= aStr)
    2030             :             {
    2031          80 :                 ScSetStringParam aParam;
    2032          80 :                 aParam.setTextInput();
    2033          80 :                 pInsDoc->SetString(ScAddress(nCol,nRow,nNewTab), aStr);
    2034             :             }
    2035         100 :             else if (rAny >>= fVal)
    2036         100 :                 pInsDoc->SetValue(nCol, nRow, nNewTab, fVal);
    2037         180 :         }
    2038             :     }
    2039             : 
    2040             :     // set number format (important for dates)
    2041          96 :     for (SCCOL nCol = 0; nCol < nColSize; ++nCol)
    2042             :     {
    2043          80 :         OUString aStr;
    2044          80 :         if (!(aTabData[0][nCol] >>= aStr))
    2045           0 :             continue;
    2046             : 
    2047         160 :         Reference<XPropertySet> xPropSet(xDims->getByName(aStr), UNO_QUERY);
    2048          80 :         if (!xPropSet.is())
    2049           0 :             continue;
    2050             : 
    2051         160 :         Any any = xPropSet->getPropertyValue( OUString(SC_UNO_DP_NUMBERFO) );
    2052          80 :         sal_Int32 nNumFmt = 0;
    2053          80 :         if (!(any >>= nNumFmt))
    2054           0 :             continue;
    2055             : 
    2056         160 :         ScPatternAttr aPattern( pInsDoc->GetPool() );
    2057          80 :         aPattern.GetItemSet().Put( SfxUInt32Item(ATTR_VALUE_FORMAT, static_cast<sal_uInt32>(nNumFmt)) );
    2058          80 :         pInsDoc->ApplyPatternAreaTab(nCol, 1, nCol, nRowSize-1, nNewTab, aPattern);
    2059          80 :     }
    2060             : 
    2061          16 :     SCCOL nEndCol = 0;
    2062          16 :     SCROW nEndRow = 0;
    2063          16 :     pInsDoc->GetCellArea( nNewTab, nEndCol, nEndRow );
    2064          16 :     pInsDoc->SetClipArea( ScRange( 0, 0, nNewTab, nEndCol, nEndRow, nNewTab ) );
    2065             : 
    2066          16 :     ::svl::IUndoManager* pMgr = GetViewData()->GetDocShell()->GetUndoManager();
    2067          32 :     String aUndo = ScGlobal::GetRscString( STR_UNDO_DOOUTLINE );
    2068          16 :     pMgr->EnterListAction( aUndo, aUndo );
    2069             : 
    2070          32 :     OUString aNewTabName;
    2071          16 :     pDoc->CreateValidTabName(aNewTabName);
    2072          16 :     if ( InsertTable(aNewTabName, nNewTab) )
    2073          16 :         PasteFromClip( IDF_ALL, pInsDoc.get() );
    2074             : 
    2075          32 :     pMgr->LeaveListAction();
    2076             : }
    2077             : 
    2078             : //
    2079             : //          DB-Operationen (Sortieren, Filtern, Teilergebnisse) wiederholen
    2080             : //
    2081             : 
    2082           0 : void ScDBFunc::RepeatDB( sal_Bool bRecord )
    2083             : {
    2084           0 :     SCCOL nCurX = GetViewData()->GetCurX();
    2085           0 :     SCROW nCurY = GetViewData()->GetCurY();
    2086           0 :     SCTAB nTab = GetViewData()->GetTabNo();
    2087           0 :     ScDocument* pDoc = GetViewData()->GetDocument();
    2088           0 :     ScDBData* pDBData = GetDBData();
    2089           0 :     if (bRecord && !pDoc->IsUndoEnabled())
    2090           0 :         bRecord = false;
    2091             : 
    2092           0 :     ScQueryParam aQueryParam;
    2093           0 :     pDBData->GetQueryParam( aQueryParam );
    2094           0 :     sal_Bool bQuery = aQueryParam.GetEntry(0).bDoQuery;
    2095             : 
    2096           0 :     ScSortParam aSortParam;
    2097           0 :     pDBData->GetSortParam( aSortParam );
    2098           0 :     sal_Bool bSort = aSortParam.maKeyState[0].bDoSort;
    2099             : 
    2100           0 :     ScSubTotalParam aSubTotalParam;
    2101           0 :     pDBData->GetSubTotalParam( aSubTotalParam );
    2102           0 :     sal_Bool bSubTotal = aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly;
    2103             : 
    2104           0 :     if ( bQuery || bSort || bSubTotal )
    2105             :     {
    2106           0 :         sal_Bool bQuerySize = false;
    2107           0 :         ScRange aOldQuery;
    2108           0 :         ScRange aNewQuery;
    2109           0 :         if (bQuery && !aQueryParam.bInplace)
    2110             :         {
    2111             :             ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
    2112           0 :                                                     aQueryParam.nDestTab, sal_True );
    2113           0 :             if (pDest && pDest->IsDoSize())
    2114             :             {
    2115           0 :                 pDest->GetArea( aOldQuery );
    2116           0 :                 bQuerySize = sal_True;
    2117             :             }
    2118             :         }
    2119             : 
    2120             :         SCTAB nDummy;
    2121             :         SCCOL nStartCol;
    2122             :         SCROW nStartRow;
    2123             :         SCCOL nEndCol;
    2124             :         SCROW nEndRow;
    2125           0 :         pDBData->GetArea( nDummy, nStartCol, nStartRow, nEndCol, nEndRow );
    2126             : 
    2127             :         //!     Undo nur benoetigte Daten ?
    2128             : 
    2129           0 :         ScDocument* pUndoDoc = NULL;
    2130           0 :         ScOutlineTable* pUndoTab = NULL;
    2131           0 :         ScRangeName* pUndoRange = NULL;
    2132           0 :         ScDBCollection* pUndoDB = NULL;
    2133             : 
    2134           0 :         if (bRecord)
    2135             :         {
    2136           0 :             SCTAB nTabCount = pDoc->GetTableCount();
    2137           0 :             pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
    2138           0 :             ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
    2139           0 :             if (pTable)
    2140             :             {
    2141           0 :                 pUndoTab = new ScOutlineTable( *pTable );
    2142             : 
    2143             :                 SCCOLROW nOutStartCol;                          // Zeilen/Spaltenstatus
    2144             :                 SCCOLROW nOutStartRow;
    2145             :                 SCCOLROW nOutEndCol;
    2146             :                 SCCOLROW nOutEndRow;
    2147           0 :                 pTable->GetColArray()->GetRange( nOutStartCol, nOutEndCol );
    2148           0 :                 pTable->GetRowArray()->GetRange( nOutStartRow, nOutEndRow );
    2149             : 
    2150           0 :                 pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_True, sal_True );
    2151           0 :                 pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, false, pUndoDoc );
    2152           0 :                 pDoc->CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, false, pUndoDoc );
    2153             :             }
    2154             :             else
    2155           0 :                 pUndoDoc->InitUndo( pDoc, nTab, nTab, false, sal_True );
    2156             : 
    2157             :             //  Datenbereich sichern - incl. Filter-Ergebnis
    2158           0 :             pDoc->CopyToDocument( 0,nStartRow,nTab, MAXCOL,nEndRow,nTab, IDF_ALL, false, pUndoDoc );
    2159             : 
    2160             :             //  alle Formeln wegen Referenzen
    2161           0 :             pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1, IDF_FORMULA, false, pUndoDoc );
    2162             : 
    2163             :             //  DB- und andere Bereiche
    2164           0 :             ScRangeName* pDocRange = pDoc->GetRangeName();
    2165           0 :             if (!pDocRange->empty())
    2166           0 :                 pUndoRange = new ScRangeName( *pDocRange );
    2167           0 :             ScDBCollection* pDocDB = pDoc->GetDBCollection();
    2168           0 :             if (!pDocDB->empty())
    2169           0 :                 pUndoDB = new ScDBCollection( *pDocDB );
    2170             :         }
    2171             : 
    2172           0 :         if (bSort && bSubTotal)
    2173             :         {
    2174             :             //  Sortieren ohne SubTotals
    2175             : 
    2176           0 :             aSubTotalParam.bRemoveOnly = sal_True;      // wird unten wieder zurueckgesetzt
    2177           0 :             DoSubTotals( aSubTotalParam, false );
    2178             :         }
    2179             : 
    2180           0 :         if (bSort)
    2181             :         {
    2182           0 :             pDBData->GetSortParam( aSortParam );            // Bereich kann sich geaendert haben
    2183           0 :             Sort( aSortParam, false, false);
    2184             :         }
    2185           0 :         if (bQuery)
    2186             :         {
    2187           0 :             pDBData->GetQueryParam( aQueryParam );          // Bereich kann sich geaendert haben
    2188           0 :             ScRange aAdvSource;
    2189           0 :             if (pDBData->GetAdvancedQuerySource(aAdvSource))
    2190             :             {
    2191             :                 pDoc->CreateQueryParam(
    2192           0 :                     aAdvSource.aStart.Col(), aAdvSource.aStart.Row(),
    2193           0 :                     aAdvSource.aEnd.Col(), aAdvSource.aEnd.Row(),
    2194           0 :                     aAdvSource.aStart.Tab(), aQueryParam );
    2195           0 :                 Query( aQueryParam, &aAdvSource, false );
    2196             :             }
    2197             :             else
    2198           0 :                 Query( aQueryParam, NULL, false );
    2199             : 
    2200             :             //  bei nicht-inplace kann die Tabelle umgestellt worden sein
    2201           0 :             if ( !aQueryParam.bInplace && aQueryParam.nDestTab != nTab )
    2202           0 :                 SetTabNo( nTab );
    2203             :         }
    2204           0 :         if (bSubTotal)
    2205             :         {
    2206           0 :             pDBData->GetSubTotalParam( aSubTotalParam );    // Bereich kann sich geaendert haben
    2207           0 :             aSubTotalParam.bRemoveOnly = false;
    2208           0 :             DoSubTotals( aSubTotalParam, false );
    2209             :         }
    2210             : 
    2211           0 :         if (bRecord)
    2212             :         {
    2213             :             SCTAB nDummyTab;
    2214             :             SCCOL nDummyCol;
    2215             :             SCROW nDummyRow, nNewEndRow;
    2216           0 :             pDBData->GetArea( nDummyTab, nDummyCol,nDummyRow, nDummyCol,nNewEndRow );
    2217             : 
    2218           0 :             const ScRange* pOld = NULL;
    2219           0 :             const ScRange* pNew = NULL;
    2220           0 :             if (bQuerySize)
    2221             :             {
    2222             :                 ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
    2223           0 :                                                         aQueryParam.nDestTab, sal_True );
    2224           0 :                 if (pDest)
    2225             :                 {
    2226           0 :                     pDest->GetArea( aNewQuery );
    2227           0 :                     pOld = &aOldQuery;
    2228           0 :                     pNew = &aNewQuery;
    2229             :                 }
    2230             :             }
    2231             : 
    2232           0 :             GetViewData()->GetDocShell()->GetUndoManager()->AddUndoAction(
    2233           0 :                 new ScUndoRepeatDB( GetViewData()->GetDocShell(), nTab,
    2234             :                                         nStartCol, nStartRow, nEndCol, nEndRow,
    2235             :                                         nNewEndRow,
    2236             :                                         nCurX, nCurY,
    2237             :                                         pUndoDoc, pUndoTab,
    2238             :                                         pUndoRange, pUndoDB,
    2239           0 :                                         pOld, pNew ) );
    2240             :         }
    2241             : 
    2242             :         GetViewData()->GetDocShell()->PostPaint(
    2243             :             ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab),
    2244           0 :             PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE);
    2245             :     }
    2246             :     else        // "Keine Operationen auszufuehren"
    2247           0 :         ErrorMessage(STR_MSSG_REPEATDB_0);
    2248          93 : }
    2249             : 
    2250             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10