LibreOffice Module sc (master) 1
ChartTools.cxx
Go to the documentation of this file.
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 */
10
11#include <ChartTools.hxx>
12#include <docsh.hxx>
13#include <drwlayer.hxx>
14
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>
21
22using namespace css;
23
24namespace sc::tools {
25
26namespace {
27
28uno::Reference<chart2::data::XPivotTableDataProvider>
29getPivotTableDataProvider(const SdrOle2Obj* pOleObject)
30{
31 uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider;
32
33 const uno::Reference<embed::XEmbeddedObject>& xObject = pOleObject->GetObjRef();
34 if (xObject.is())
35 {
36 uno::Reference<chart2::XChartDocument> xChartDoc(xObject->getComponent(), uno::UNO_QUERY);
37 if (xChartDoc.is())
38 {
39 xPivotTableDataProvider.set(uno::Reference<chart2::data::XPivotTableDataProvider>(
40 xChartDoc->getDataProvider(), uno::UNO_QUERY));
41 }
42 }
43 return xPivotTableDataProvider;
44}
45
46OUString getAssociatedPivotTableName(const SdrOle2Obj* pOleObject)
47{
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;
54}
55
56} // end anonymous namespace
57
59 : m_eChartSourceType(eChartSourceType)
60{
61 if (!pDocShell)
62 return;
63 ScDocument& rDoc = pDocShell->GetDocument();
64 ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
65 if (!pDrawLayer)
66 return;
67 SdrPage* pPage = pDrawLayer->GetPage(sal_uInt16(nTab));
68 if (!pPage)
69 return;
70 m_oIterator.emplace(pPage, SdrIterMode::DeepNoGroups);
71}
72
74{
75 if (!m_oIterator)
76 return nullptr;
77
78 SdrObject* pObject = m_oIterator->Next();
79 while (pObject)
80 {
81 if (pObject->GetObjIdentifier() == SdrObjKind::OLE2 && ScDocument::IsChart(pObject))
82 {
83 SdrOle2Obj* pOleObject = static_cast<SdrOle2Obj*>(pObject);
84
85 uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider;
86 xPivotTableDataProvider.set(getPivotTableDataProvider(pOleObject));
87
88 if (xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::PIVOT_TABLE)
89 return pOleObject;
90 else if (!xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::CELL_RANGE)
91 return pOleObject;
92 }
93 pObject = m_oIterator->Next();
94 }
95 return nullptr;
96}
97
98SdrOle2Obj* findChartsByName(ScDocShell* pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
99{
100 if (!pDocShell)
101 return nullptr;
102
103 ChartIterator aIterator(pDocShell, nTab, eChartSourceType);
104
105 SdrOle2Obj* pObject = aIterator.next();
106 while (pObject)
107 {
108 uno::Reference<embed::XEmbeddedObject> xObject = pObject->GetObjRef();
109 if (xObject.is())
110 {
111 OUString aObjectName = pDocShell->GetEmbeddedObjectContainer().GetEmbeddedObjectName(xObject);
112 if (aObjectName == rName)
113 return pObject;
114 }
115 pObject = aIterator.next();
116 }
117 return nullptr;
118}
119
120SdrOle2Obj* getChartByIndex(ScDocShell* pDocShell, SCTAB nTab, ::tools::Long nIndex, ChartSourceType eChartSourceType)
121{
122 if (!pDocShell)
123 return nullptr;
124
125 ChartIterator aIterator(pDocShell, nTab, eChartSourceType);
126
127 SdrOle2Obj* pObject = aIterator.next();
128 ::tools::Long i = 0;
129 while (pObject)
130 {
131 if (i == nIndex)
132 {
133 return pObject;
134 }
135
136 i++;
137 pObject = aIterator.next();
138 }
139 return nullptr;
140}
141
142std::vector<SdrOle2Obj*> getAllPivotChartsConnectedTo(std::u16string_view sPivotTableName, ScDocShell* pDocShell)
143{
144 std::vector<SdrOle2Obj*> aObjects;
145
146 ScDocument& rDocument = pDocShell->GetDocument();
147 ScDrawLayer* pModel = rDocument.GetDrawLayer();
148 if (!pModel)
149 return aObjects;
150
151 sal_uInt16 nPageCount = pModel->GetPageCount();
152 for (sal_uInt16 nPageNo = 0; nPageNo < nPageCount; nPageNo++)
153 {
154 SdrPage* pPage = pModel->GetPage(nPageNo);
155 if (!pPage)
156 continue;
157
158 sc::tools::ChartIterator aIterator(pDocShell, nPageNo, ChartSourceType::PIVOT_TABLE);
159 SdrOle2Obj* pObject = aIterator.next();
160 while (pObject)
161 {
162 if (sPivotTableName == getAssociatedPivotTableName(pObject))
163 {
164 aObjects.push_back(pObject);
165 }
166 pObject = aIterator.next();
167 }
168 }
169 return aObjects;
170}
171
172} // end sc::tools
173
174/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const ScDocument & GetDocument() const
Definition: docsh.hxx:219
SC_DLLPUBLIC ScDrawLayer * GetDrawLayer()
Definition: document.hxx:1084
static bool IsChart(const SdrObject *pObject)
Definition: documen9.cxx:209
const SdrPage * GetPage(sal_uInt16 nPgNum) const
sal_uInt16 GetPageCount() const
css::uno::Reference< css::embed::XEmbeddedObject > const & GetObjRef() const
comphelper::EmbeddedObjectContainer & GetEmbeddedObjectContainer() const
OUString GetEmbeddedObjectName(const css::uno::Reference< css::embed::XEmbeddedObject > &) const
std::optional< SdrObjListIter > m_oIterator
Definition: ChartTools.hxx:36
ChartIterator(ScDocShell *pDocShell, SCTAB nTab, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:58
ChartSourceType m_eChartSourceType
Definition: ChartTools.hxx:37
EmbeddedObjectRef * pObject
sal_Int32 nIndex
int i
SdrOle2Obj * getChartByIndex(ScDocShell *pDocShell, SCTAB nTab, ::tools::Long nIndex, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:120
std::vector< SdrOle2Obj * > getAllPivotChartsConnectedTo(std::u16string_view sPivotTableName, ScDocShell *pDocShell)
Definition: ChartTools.cxx:142
SdrOle2Obj * findChartsByName(ScDocShell *pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:98
long Long
sal_Int16 SCTAB
Definition: types.hxx:22