[Libreoffice-commits] core.git: sc/inc sc/source

Tomaž Vajngerl tomaz.vajngerl at collabora.co.uk
Mon Apr 17 20:39:36 UTC 2017


 sc/inc/ChartTools.hxx                          |    2 
 sc/inc/globstr.hrc                             |    1 
 sc/source/ui/docshell/dbdocfun.cxx             |   37 ++++++++++
 sc/source/ui/src/globstr.src                   |    4 +
 sc/source/ui/unoobj/ChartTools.cxx             |   87 ++++++++++++++++++++-----
 sc/source/ui/unoobj/PivotTableDataProvider.cxx |    4 -
 6 files changed, 115 insertions(+), 20 deletions(-)

New commits:
commit 1b0e9b080d4a47ba3be788cb4848786228d2322f
Author: Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
Date:   Wed Apr 12 22:17:28 2017 +0200

    ask to delete pivot chart if pivot table gets deleted
    
    Change-Id: I2a0e1aedf870fa587b5596e0adde0b9e61278c88
    Reviewed-on: https://gerrit.libreoffice.org/36624
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>

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()


More information about the Libreoffice-commits mailing list