summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/unoobj/ChartTools.cxx
blob: 6ce72bd9e792f8ebaaab4bb93a118c771c44ee1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <ChartTools.hxx>
#include <docsh.hxx>
#include <drwlayer.hxx>

#include <com/sun/star/chart2/data/XPivotTableDataProvider.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/embed/XEmbeddedObject.hpp>
#include <svx/svditer.hxx>
#include <svx/svdoole2.hxx>
#include <svx/svdpage.hxx>

using namespace css;

namespace sc::tools {

namespace {

uno::Reference<chart2::data::XPivotTableDataProvider>
getPivotTableDataProvider(const SdrOle2Obj* pOleObject)
{
    uno::Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider;

    const 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(const 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)
{
    if (!pDocShell)
        return;
    ScDocument& rDoc = pDocShell->GetDocument();
    ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
    if (!pDrawLayer)
        return;
    SdrPage* pPage = pDrawLayer->GetPage(sal_uInt16(nTab));
    if (!pPage)
        return;
    m_pIterator.reset(new SdrObjListIter(pPage, SdrIterMode::DeepNoGroups));
}

SdrOle2Obj* ChartIterator::next()
{
    if (!m_pIterator)
        return nullptr;

    SdrObject* pObject = m_pIterator->Next();
    while (pObject)
    {
        if (pObject->GetObjIdentifier() == SdrObjKind::OLE2 && ScDocument::IsChart(pObject))
        {
            SdrOle2Obj* pOleObject = static_cast<SdrOle2Obj*>(pObject);

            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();
    }
    return nullptr;
}

SdrOle2Obj* findChartsByName(ScDocShell* pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
{
    if (!pDocShell)
        return nullptr;

    ChartIterator aIterator(pDocShell, nTab, eChartSourceType);

    SdrOle2Obj* pObject = aIterator.next();
    while (pObject)
    {
        uno::Reference<embed::XEmbeddedObject> xObject = pObject->GetObjRef();
        if (xObject.is())
        {
            OUString aObjectName = pDocShell->GetEmbeddedObjectContainer().GetEmbeddedObjectName(xObject);
            if (aObjectName == rName)
                return pObject;
        }
        pObject = aIterator.next();
    }
    return nullptr;
}

SdrOle2Obj* getChartByIndex(ScDocShell* pDocShell, SCTAB nTab, ::tools::Long nIndex, ChartSourceType eChartSourceType)
{
    if (!pDocShell)
        return nullptr;

    ChartIterator aIterator(pDocShell, nTab, eChartSourceType);

    SdrOle2Obj* pObject = aIterator.next();
    ::tools::Long i = 0;
    while (pObject)
    {
        if (i == nIndex)
        {
            return pObject;
        }

        i++;
        pObject = aIterator.next();
    }
    return nullptr;
}

std::vector<SdrOle2Obj*> getAllPivotChartsConnectedTo(std::u16string_view 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: */