1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 #include <ChartTools.hxx>
13 #include <drwlayer.hxx>
15 #include <com/sun/star/chart2/data/XPivotTableDataProvider.hpp>
16 #include <com/sun/star/chart2/XChartDocument.hpp>
17 #include <com/sun/star/embed/XEmbeddedObject.hpp>
18 #include <svx/svditer.hxx>
19 #include <svx/svdoole2.hxx>
20 #include <svx/svdpage.hxx>
28 uno::Reference
<chart2::data::XPivotTableDataProvider
>
29 getPivotTableDataProvider(const SdrOle2Obj
* pOleObject
)
31 uno::Reference
<chart2::data::XPivotTableDataProvider
> xPivotTableDataProvider
;
33 const uno::Reference
<embed::XEmbeddedObject
>& xObject
= pOleObject
->GetObjRef();
36 uno::Reference
<chart2::XChartDocument
> xChartDoc(xObject
->getComponent(), uno::UNO_QUERY
);
39 xPivotTableDataProvider
.set(uno::Reference
<chart2::data::XPivotTableDataProvider
>(
40 xChartDoc
->getDataProvider(), uno::UNO_QUERY
));
43 return xPivotTableDataProvider
;
46 OUString
getAssociatedPivotTableName(const SdrOle2Obj
* pOleObject
)
48 OUString aPivotTableName
;
49 uno::Reference
<chart2::data::XPivotTableDataProvider
> xPivotTableDataProvider
;
50 xPivotTableDataProvider
.set(getPivotTableDataProvider(pOleObject
));
51 if (xPivotTableDataProvider
.is())
52 aPivotTableName
= xPivotTableDataProvider
->getPivotTableName();
53 return aPivotTableName
;
56 } // end anonymous namespace
58 ChartIterator::ChartIterator(ScDocShell
* pDocShell
, SCTAB nTab
, ChartSourceType eChartSourceType
)
59 : m_eChartSourceType(eChartSourceType
)
63 ScDocument
& rDoc
= pDocShell
->GetDocument();
64 ScDrawLayer
* pDrawLayer
= rDoc
.GetDrawLayer();
67 SdrPage
* pPage
= pDrawLayer
->GetPage(sal_uInt16(nTab
));
70 m_oIterator
.emplace(pPage
, SdrIterMode::DeepNoGroups
);
73 SdrOle2Obj
* ChartIterator::next()
78 SdrObject
* pObject
= m_oIterator
->Next();
81 if (pObject
->GetObjIdentifier() == SdrObjKind::OLE2
&& ScDocument::IsChart(pObject
))
83 SdrOle2Obj
* pOleObject
= static_cast<SdrOle2Obj
*>(pObject
);
85 uno::Reference
<chart2::data::XPivotTableDataProvider
> xPivotTableDataProvider
;
86 xPivotTableDataProvider
.set(getPivotTableDataProvider(pOleObject
));
88 if (xPivotTableDataProvider
.is() && m_eChartSourceType
== ChartSourceType::PIVOT_TABLE
)
90 else if (!xPivotTableDataProvider
.is() && m_eChartSourceType
== ChartSourceType::CELL_RANGE
)
93 pObject
= m_oIterator
->Next();
98 SdrOle2Obj
* findChartsByName(ScDocShell
* pDocShell
, SCTAB nTab
, std::u16string_view rName
, ChartSourceType eChartSourceType
)
103 ChartIterator
aIterator(pDocShell
, nTab
, eChartSourceType
);
105 SdrOle2Obj
* pObject
= aIterator
.next();
108 uno::Reference
<embed::XEmbeddedObject
> xObject
= pObject
->GetObjRef();
111 OUString aObjectName
= pDocShell
->GetEmbeddedObjectContainer().GetEmbeddedObjectName(xObject
);
112 if (aObjectName
== rName
)
115 pObject
= aIterator
.next();
120 SdrOle2Obj
* getChartByIndex(ScDocShell
* pDocShell
, SCTAB nTab
, ::tools::Long nIndex
, ChartSourceType eChartSourceType
)
125 ChartIterator
aIterator(pDocShell
, nTab
, eChartSourceType
);
127 SdrOle2Obj
* pObject
= aIterator
.next();
137 pObject
= aIterator
.next();
142 std::vector
<SdrOle2Obj
*> getAllPivotChartsConnectedTo(std::u16string_view sPivotTableName
, ScDocShell
* pDocShell
)
144 std::vector
<SdrOle2Obj
*> aObjects
;
146 ScDocument
& rDocument
= pDocShell
->GetDocument();
147 ScDrawLayer
* pModel
= rDocument
.GetDrawLayer();
151 sal_uInt16 nPageCount
= pModel
->GetPageCount();
152 for (sal_uInt16 nPageNo
= 0; nPageNo
< nPageCount
; nPageNo
++)
154 SdrPage
* pPage
= pModel
->GetPage(nPageNo
);
158 sc::tools::ChartIterator
aIterator(pDocShell
, nPageNo
, ChartSourceType::PIVOT_TABLE
);
159 SdrOle2Obj
* pObject
= aIterator
.next();
162 if (sPivotTableName
== getAssociatedPivotTableName(pObject
))
164 aObjects
.push_back(pObject
);
166 pObject
= aIterator
.next();
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */