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 
22 using namespace css;
23 
24 namespace sc::tools {
25 
26 namespace {
27 
28 uno::Reference<chart2::data::XPivotTableDataProvider>
29 getPivotTableDataProvider(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 
46 OUString 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 
58 ChartIterator::ChartIterator(ScDocShell* pDocShell, SCTAB nTab, ChartSourceType eChartSourceType)
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_pIterator.reset(new SdrObjListIter(pPage, SdrIterMode::DeepNoGroups));
71 }
72 
74 {
75  if (!m_pIterator)
76  return nullptr;
77 
78  SdrObject* pObject = m_pIterator->Next();
79  while (pObject)
80  {
81  if (pObject->GetObjIdentifier() == OBJ_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_pIterator->Next();
94  }
95  return nullptr;
96 }
97 
98 SdrOle2Obj* 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 
120 SdrOle2Obj* 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 
142 std::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: */
css::uno::Reference< css::embed::XEmbeddedObject > const & GetObjRef() const
long Long
virtual SdrObjKind GetObjIdentifier() const
EmbeddedObjectRef * pObject
SdrOle2Obj * getChartByIndex(ScDocShell *pDocShell, SCTAB nTab,::tools::Long nIndex, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:120
SC_DLLPUBLIC ScDrawLayer * GetDrawLayer()
Definition: document.hxx:1058
int i
const SdrPage * GetPage(sal_uInt16 nPgNum) const
std::unique_ptr< SdrObjListIter > m_pIterator
Definition: ChartTools.hxx:36
OUString GetEmbeddedObjectName(const css::uno::Reference< css::embed::XEmbeddedObject > &) const
std::vector< SdrOle2Obj * > getAllPivotChartsConnectedTo(std::u16string_view sPivotTableName, ScDocShell *pDocShell)
Definition: ChartTools.cxx:142
comphelper::EmbeddedObjectContainer & GetEmbeddedObjectContainer() const
ChartSourceType m_eChartSourceType
Definition: ChartTools.hxx:37
SdrOle2Obj * findChartsByName(ScDocShell *pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:98
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
static bool IsChart(const SdrObject *pObject)
Definition: documen9.cxx:203
OBJ_OLE2
sal_uInt16 GetPageCount() const
sal_Int16 SCTAB
Definition: types.hxx:22