diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2017-04-12 22:17:28 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2017-04-17 22:39:06 +0200 |
commit | 1b0e9b080d4a47ba3be788cb4848786228d2322f (patch) | |
tree | 8f357378f9c837fb465f442e3f24b291ee8fe3bf /sc | |
parent | a900c72c675c8605fc004a4f63ba1d82eaeed9d5 (diff) |
ask to delete pivot chart if pivot table gets deleted
Change-Id: I2a0e1aedf870fa587b5596e0adde0b9e61278c88
Reviewed-on: https://gerrit.libreoffice.org/36624
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'sc')
-rw-r--r-- | sc/inc/ChartTools.hxx | 2 | ||||
-rw-r--r-- | sc/inc/globstr.hrc | 1 | ||||
-rw-r--r-- | sc/source/ui/docshell/dbdocfun.cxx | 37 | ||||
-rw-r--r-- | sc/source/ui/src/globstr.src | 4 | ||||
-rw-r--r-- | sc/source/ui/unoobj/ChartTools.cxx | 87 | ||||
-rw-r--r-- | sc/source/ui/unoobj/PivotTableDataProvider.cxx | 4 |
6 files changed, 115 insertions, 20 deletions
diff --git a/sc/inc/ChartTools.hxx b/sc/inc/ChartTools.hxx index dc9a5c52fb5b..6fb62208c4ed 100644 --- a/sc/inc/ChartTools.hxx +++ b/sc/inc/ChartTools.hxx @@ -43,6 +43,8 @@ SdrOle2Obj* findChartsByName(ScDocShell* pDocShell, SCTAB nTab, SdrOle2Obj* getChartByIndex(ScDocShell* pDocShell, SCTAB nTab, long nIndex, ChartSourceType eChartSourceType); +std::vector<SdrOle2Obj*> getAllPivotChartsConntectedTo(OUString const & sPivotTableName, ScDocShell* pDocShell); + }} // end sc::tools #endif diff --git a/sc/inc/globstr.hrc b/sc/inc/globstr.hrc index eadfc9f5737c..80751fad3839 100644 --- a/sc/inc/globstr.hrc +++ b/sc/inc/globstr.hrc @@ -404,6 +404,7 @@ #define STR_QUICKHELP_REF 321 #define STR_PIVOT_NOTEMPTY 323 +#define STR_PIVOT_REMOVE_PIVOTCHART 324 #define STR_PROGRESS_COMPARING 326 diff --git a/sc/source/ui/docshell/dbdocfun.cxx b/sc/source/ui/docshell/dbdocfun.cxx index 9bb41cdbee74..7fb1f3a19858 100644 --- a/sc/source/ui/docshell/dbdocfun.cxx +++ b/sc/source/ui/docshell/dbdocfun.cxx @@ -21,7 +21,7 @@ #include <vcl/msgbox.hxx> #include <vcl/waitobj.hxx> #include <svx/dataaccessdescriptor.hxx> - +#include <svx/svdpage.hxx> #include <com/sun/star/sdb/CommandType.hpp> #include "dbdocfun.hxx" @@ -50,6 +50,9 @@ #include <undosort.hxx> #include <inputopt.hxx> +#include "chartlis.hxx" +#include "ChartTools.hxx" + #include <set> #include <memory> @@ -1318,13 +1321,43 @@ bool ScDBDocFunc::RemovePivotTable(ScDPObject& rDPObj, bool bRecord, bool bApi) if (!isEditable(rDocShell, rDPObj.GetOutRange(), bApi)) return false; + ScDocument& rDoc = rDocShell.GetDocument(); + + if (!bApi) + { + // If we come from GUI - ask to delete the associated pivot charts too.. + std::vector<SdrOle2Obj*> aListOfObjects = + sc::tools::getAllPivotChartsConntectedTo(rDPObj.GetName(), &rDocShell); + + ScDrawLayer* pModel = rDoc.GetDrawLayer(); + + if (pModel && !aListOfObjects.empty()) + { + ScopedVclPtrInstance<QueryBox> aBox( + ScDocShell::GetActiveDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES), + ScGlobal::GetRscString(STR_PIVOT_REMOVE_PIVOTCHART)); + if (aBox->Execute() == RET_NO) + { + return false; + } + else + { + for (SdrOle2Obj* pChartObject : aListOfObjects) + { + rDoc.GetChartListenerCollection()->removeByName(pChartObject->GetName()); + pModel->AddUndo(new SdrUndoDelObj(*pChartObject)); + pChartObject->GetPage()->RemoveObject(pChartObject->GetOrdNum()); + } + } + } + } + std::unique_ptr<ScDocument> pOldUndoDoc; std::unique_ptr<ScDPObject> pUndoDPObj; if (bRecord) pUndoDPObj.reset(new ScDPObject(rDPObj)); // copy old settings for undo - ScDocument& rDoc = rDocShell.GetDocument(); if (bRecord && !rDoc.IsUndoEnabled()) bRecord = false; diff --git a/sc/source/ui/src/globstr.src b/sc/source/ui/src/globstr.src index 56b44ec86a41..932f1b1ba36d 100644 --- a/sc/source/ui/src/globstr.src +++ b/sc/source/ui/src/globstr.src @@ -491,6 +491,10 @@ String STR_PIVOT_NOTEMPTY+RID_GLOBSTR_OFFSET { Text [ en-US ] = "The destination range is not empty. Overwrite existing contents?" ; }; +String STR_PIVOT_REMOVE_PIVOTCHART+RID_GLOBSTR_OFFSET +{ + Text [ en-US ] = "There is at least one pivot chart associated with this pivot table. Should remove all or abort?" ; +}; String STR_DATAPILOT_SUBTOTAL+RID_GLOBSTR_OFFSET { Text [ en-US ] = "The source range contains subtotals which may distort the results. Use it anyway?"; diff --git a/sc/source/ui/unoobj/ChartTools.cxx b/sc/source/ui/unoobj/ChartTools.cxx index 77818723c7aa..920c27541040 100644 --- a/sc/source/ui/unoobj/ChartTools.cxx +++ b/sc/source/ui/unoobj/ChartTools.cxx @@ -29,6 +29,38 @@ using namespace css; namespace sc { namespace tools { +namespace { + +uno::Reference<chart2::data::XPivotTableDataProvider> +getPivotTableDataProvider(SdrOle2Obj* pOleObject) +{ + uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider; + + uno::Reference<embed::XEmbeddedObject> xObject = pOleObject->GetObjRef(); + if (xObject.is()) + { + uno::Reference<chart2::XChartDocument> xChartDoc(xObject->getComponent(), uno::UNO_QUERY); + if (xChartDoc.is()) + { + xPivotTableDataProvider.set(uno::Reference<chart2::data::XPivotTableDataProvider>( + xChartDoc->getDataProvider(), uno::UNO_QUERY)); + } + } + return xPivotTableDataProvider; +} + +OUString getAssociatedPivotTableName(SdrOle2Obj* pOleObject) +{ + OUString aPivotTableName; + uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider; + xPivotTableDataProvider.set(getPivotTableDataProvider(pOleObject)); + if (xPivotTableDataProvider.is()) + aPivotTableName = xPivotTableDataProvider->getPivotTableName(); + return aPivotTableName; +} + +} // end anonymous namespace + ChartIterator::ChartIterator(ScDocShell* pDocShell, SCTAB nTab, ChartSourceType eChartSourceType) : m_eChartSourceType(eChartSourceType) { @@ -55,23 +87,14 @@ SdrOle2Obj* ChartIterator::next() if (pObject->GetObjIdentifier() == OBJ_OLE2 && ScDocument::IsChart(pObject)) { SdrOle2Obj* pOleObject = static_cast<SdrOle2Obj*>(pObject); - uno::Reference<embed::XEmbeddedObject> xObject = pOleObject->GetObjRef(); - if (xObject.is()) - { - uno::Reference<chart2::XChartDocument> xChartDoc(xObject->getComponent(), uno::UNO_QUERY); - if (xChartDoc.is()) - { - uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider(xChartDoc->getDataProvider(), uno::UNO_QUERY); - if (xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::PIVOT_TABLE) - { - return pOleObject; - } - else if (!xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::CELL_RANGE) - { - return pOleObject; - } - } - } + + uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider; + xPivotTableDataProvider.set(getPivotTableDataProvider(pOleObject)); + + if (xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::PIVOT_TABLE) + return pOleObject; + else if (!xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::CELL_RANGE) + return pOleObject; } pObject = m_pIterator->Next(); } @@ -122,6 +145,36 @@ SdrOle2Obj* getChartByIndex(ScDocShell* pDocShell, SCTAB nTab, long nIndex, Char return nullptr; } +std::vector<SdrOle2Obj*> getAllPivotChartsConntectedTo(OUString const & sPivotTableName, ScDocShell* pDocShell) +{ + std::vector<SdrOle2Obj*> aObjects; + + ScDocument& rDocument = pDocShell->GetDocument(); + ScDrawLayer* pModel = rDocument.GetDrawLayer(); + if (!pModel) + return aObjects; + + sal_uInt16 nPageCount = pModel->GetPageCount(); + for (sal_uInt16 nPageNo = 0; nPageNo < nPageCount; nPageNo++) + { + SdrPage* pPage = pModel->GetPage(nPageNo); + if (!pPage) + continue; + + sc::tools::ChartIterator aIterator(pDocShell, nPageNo, ChartSourceType::PIVOT_TABLE); + SdrOle2Obj* pObject = aIterator.next(); + while (pObject) + { + if (sPivotTableName == getAssociatedPivotTableName(pObject)) + { + aObjects.push_back(pObject); + } + pObject = aIterator.next(); + } + } + return aObjects; +} + }} // end sc::tools /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/unoobj/PivotTableDataProvider.cxx b/sc/source/ui/unoobj/PivotTableDataProvider.cxx index df7b180c6892..35e24b6197fc 100644 --- a/sc/source/ui/unoobj/PivotTableDataProvider.cxx +++ b/sc/source/ui/unoobj/PivotTableDataProvider.cxx @@ -670,6 +670,8 @@ uno::Reference<sheet::XRangeSelection> SAL_CALL PivotTableDataProvider::getRange return xResult; } +// XPivotTableDataProvider ======================================================== + uno::Sequence<chart2::data::PivotTableFieldEntry> PivotTableDataProvider::getColumnFields() { return comphelper::containerToSequence(m_aColumnFields); @@ -776,7 +778,7 @@ void SAL_CALL PivotTableDataProvider::removeModifyListener(const uno::Reference< } } -// DataProvider XPropertySet ------------------------------------------------- +// DataProvider XPropertySet ======================================================== uno::Reference< beans::XPropertySetInfo> SAL_CALL PivotTableDataProvider::getPropertySetInfo() |