tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / unoobj / ChartTools.cxx
blobe9fdc996a386479f86b4c46cdfc1681173b65c59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
11 #include <ChartTools.hxx>
12 #include <docsh.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>
22 using namespace css;
24 namespace sc::tools {
26 namespace {
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();
34 if (xObject.is())
36 uno::Reference<chart2::XChartDocument> xChartDoc(xObject->getComponent(), uno::UNO_QUERY);
37 if (xChartDoc.is())
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)
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);
73 SdrOle2Obj* ChartIterator::next()
75 if (!m_oIterator)
76 return nullptr;
78 SdrObject* pObject = m_oIterator->Next();
79 while (pObject)
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)
89 return pOleObject;
90 else if (!xPivotTableDataProvider.is() && m_eChartSourceType == ChartSourceType::CELL_RANGE)
91 return pOleObject;
93 pObject = m_oIterator->Next();
95 return nullptr;
98 SdrOle2Obj* findChartsByName(ScDocShell* pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
100 if (!pDocShell)
101 return nullptr;
103 ChartIterator aIterator(pDocShell, nTab, eChartSourceType);
105 SdrOle2Obj* pObject = aIterator.next();
106 while (pObject)
108 uno::Reference<embed::XEmbeddedObject> xObject = pObject->GetObjRef();
109 if (xObject.is())
111 OUString aObjectName = pDocShell->GetEmbeddedObjectContainer().GetEmbeddedObjectName(xObject);
112 if (aObjectName == rName)
113 return pObject;
115 pObject = aIterator.next();
117 return nullptr;
120 SdrOle2Obj* getChartByIndex(ScDocShell* pDocShell, SCTAB nTab, ::tools::Long nIndex, ChartSourceType eChartSourceType)
122 if (!pDocShell)
123 return nullptr;
125 ChartIterator aIterator(pDocShell, nTab, eChartSourceType);
127 SdrOle2Obj* pObject = aIterator.next();
128 ::tools::Long i = 0;
129 while (pObject)
131 if (i == nIndex)
133 return pObject;
136 i++;
137 pObject = aIterator.next();
139 return nullptr;
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();
148 if (!pModel)
149 return aObjects;
151 sal_uInt16 nPageCount = pModel->GetPageCount();
152 for (sal_uInt16 nPageNo = 0; nPageNo < nPageCount; nPageNo++)
154 SdrPage* pPage = pModel->GetPage(nPageNo);
155 if (!pPage)
156 continue;
158 sc::tools::ChartIterator aIterator(pDocShell, nPageNo, ChartSourceType::PIVOT_TABLE);
159 SdrOle2Obj* pObject = aIterator.next();
160 while (pObject)
162 if (sPivotTableName == getAssociatedPivotTableName(pObject))
164 aObjects.push_back(pObject);
166 pObject = aIterator.next();
169 return aObjects;
172 } // end sc::tools
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */