LibreOffice Module sc (master) 1
TablePivotCharts.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#include <memory>
11#include <com/sun/star/embed/Aspects.hpp>
12#include <com/sun/star/embed/XEmbeddedObject.hpp>
13#include <com/sun/star/awt/Size.hpp>
14#include <com/sun/star/chart/ChartDataRowSource.hpp>
15#include <com/sun/star/chart2/data/XDataReceiver.hpp>
16#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
17
18#include <tools/gen.hxx>
19#include <svx/svdoole2.hxx>
20#include <svx/svdpage.hxx>
21#include <svx/svdundo.hxx>
26#include <tools/globname.hxx>
27#include <svtools/embedhlp.hxx>
29#include <vcl/svapp.hxx>
30
31#include <TablePivotChart.hxx>
32#include <TablePivotCharts.hxx>
34#include <ChartTools.hxx>
35
36#include <miscuno.hxx>
37#include <docsh.hxx>
38#include <drwlayer.hxx>
39
40using namespace css;
41
42namespace sc
43{
44
45SC_SIMPLE_SERVICE_INFO(TablePivotCharts, "TablePivotCharts", "com.sun.star.table.TablePivotCharts")
46
48 : m_pDocShell(pDocShell)
49 , m_nTab(nTab)
50{
51 m_pDocShell->GetDocument().AddUnoObject(*this);
52}
53
55{
56 SolarMutexGuard aGuard;
57
58 if (m_pDocShell)
60}
61
62void TablePivotCharts::Notify(SfxBroadcaster& /*rBroadcaster*/, const SfxHint& rHint)
63{
64 if (rHint.GetId() == SfxHintId::Dying)
65 m_pDocShell = nullptr;
66}
67
68// XTablePivotCharts
69void SAL_CALL TablePivotCharts::addNewByName(OUString const & rName,
70 const awt::Rectangle& aRect,
71 OUString const & rDataPilotName)
72{
73 SolarMutexGuard aGuard;
74
75 if (!m_pDocShell)
76 return;
77
80 SdrPage* pPage = pModel->GetPage(sal_uInt16(m_nTab));
81 if (!pPage)
82 return;
83
84 // chart can't be inserted if any ole object with that name exists on any table
85 // (empty string: generate valid name)
86
87 OUString aName = rName;
88 SCTAB nDummy;
89 if (!aName.isEmpty() && pModel->GetNamedObject(aName, SdrObjKind::OLE2, nDummy))
90 {
91 // object exists - only RuntimeException is specified
92 throw uno::RuntimeException();
93 }
94
95 uno::Reference<embed::XEmbeddedObject> xObject;
96
97 if (SvtModuleOptions().IsChart())
99
100 if (!xObject.is())
101 return;
102
103 Point aRectPos(aRect.X, aRect.Y);
104 bool bLayoutRTL = rDoc.IsLayoutRTL(m_nTab);
105 if ((aRectPos.X() < 0 && !bLayoutRTL) || (aRectPos.X() > 0 && bLayoutRTL))
106 aRectPos.setX( 0 );
107
108 if (aRectPos.Y() < 0)
109 aRectPos.setY( 0 );
110
111 Size aRectSize(aRect.Width, aRect.Height);
112 if (aRectSize.Width() <= 0)
113 aRectSize.setWidth( 5000 ); // default size
114
115 if (aRectSize.Height() <= 0)
116 aRectSize.setHeight( 5000 );
117
118 ::tools::Rectangle aInsRect(aRectPos, aRectSize);
119
120 sal_Int64 nAspect(embed::Aspects::MSOLE_CONTENT);
121 MapUnit aMapUnit(VCLUnoHelper::UnoEmbed2VCLMapUnit(xObject->getMapUnit(nAspect)));
122 Size aSize(aInsRect.GetSize());
123 aSize = OutputDevice::LogicToLogic(aSize, MapMode(MapUnit::Map100thMM), MapMode(aMapUnit));
124 awt::Size aAwtSize;
125 aAwtSize.Width = aSize.Width();
126 aAwtSize.Height = aSize.Height();
127
129 pPivotTableDataProvider->setPivotTableName(rDataPilotName);
130
131 uno::Reference<chart2::data::XDataProvider> xDataProvider(pPivotTableDataProvider);
132
133 uno::Reference<chart2::data::XDataReceiver> xReceiver;
134
135 if (xObject.is())
136 xReceiver.set(xObject->getComponent(), uno::UNO_QUERY);
137
138 if (xReceiver.is())
139 {
140 xReceiver->attachDataProvider(xDataProvider);
141
142 uno::Reference<util::XNumberFormatsSupplier> xNumberFormatsSupplier(cppu::getXWeak(m_pDocShell->GetModel()), uno::UNO_QUERY);
143 xReceiver->attachNumberFormatsSupplier(xNumberFormatsSupplier);
144
145 uno::Sequence<beans::PropertyValue> aArgs( comphelper::InitPropertySequence({
146 { "CellRangeRepresentation", uno::Any(rDataPilotName) },
147 { "HasCategories", uno::Any(true) },
148 { "DataRowSource", uno::Any(chart::ChartDataRowSource_COLUMNS) }
149 }));
150 xReceiver->setArguments(aArgs);
151 }
152
154 *pModel,
155 svt::EmbeddedObjectRef(xObject, embed::Aspects::MSOLE_CONTENT),
156 aName,
157 aInsRect);
158
159 if (xObject.is())
160 xObject->setVisualAreaSize(nAspect, aAwtSize);
161
162 pPage->InsertObject(pObject.get());
163 pModel->AddUndo(std::make_unique<SdrUndoInsertObj>(*pObject));
164}
165
166void SAL_CALL TablePivotCharts::removeByName(const OUString& rName)
167{
168 SolarMutexGuard aGuard;
170 if (pObject)
171 {
173 ScDrawLayer* pModel = rDoc.GetDrawLayer();
174 SdrPage* pPage = pModel->GetPage(sal_uInt16(m_nTab));
175 pModel->AddUndo(std::make_unique<SdrUndoDelObj>(*pObject));
176 pPage->RemoveObject(pObject->GetOrdNum());
177 }
178}
179
180// XIndexAccess
181sal_Int32 SAL_CALL TablePivotCharts::getCount()
182{
183 SolarMutexGuard aGuard;
184 sal_Int32 nCount = 0;
185
186 if (!m_pDocShell)
187 return nCount;
188
190
191 SdrOle2Obj* pOleObject = aIterator.next();
192 while (pOleObject)
193 {
194 if (pOleObject->GetObjRef().is())
195 nCount++;
196 pOleObject = aIterator.next();
197 }
198 return nCount;
199}
200
201uno::Any SAL_CALL TablePivotCharts::getByIndex(sal_Int32 nIndex)
202{
203 SolarMutexGuard aGuard;
206 if (!pObject)
207 throw lang::IndexOutOfBoundsException();
208
209 OUString aName;
210 uno::Reference<embed::XEmbeddedObject> xObject = pObject->GetObjRef();
211 if (xObject.is())
213
214 if (aName.isEmpty())
215 throw lang::IndexOutOfBoundsException();
216
217 uno::Reference<table::XTablePivotChart> xChart(new TablePivotChart(m_pDocShell, m_nTab, aName));
218 if (!xChart.is())
219 throw lang::IndexOutOfBoundsException();
220
221 return uno::Any(xChart);
222}
223
225{
227}
228
230{
231 SolarMutexGuard aGuard;
232 return getCount() != 0;
233}
234
235uno::Any SAL_CALL TablePivotCharts::getByName(OUString const & rName)
236{
237 SolarMutexGuard aGuard;
238
240 throw container::NoSuchElementException();
241
242 uno::Reference<table::XTablePivotChart> xChart(new TablePivotChart(m_pDocShell, m_nTab, rName));
243 if (!xChart.is())
244 throw container::NoSuchElementException();
245
246 return uno::Any(xChart);
247}
248
249uno::Sequence<OUString> SAL_CALL TablePivotCharts::getElementNames()
250{
251 SolarMutexGuard aGuard;
252
253 std::vector<OUString> aElements;
255
256 SdrOle2Obj* pOleObject = aIterator.next();
257 while (pOleObject)
258 {
259 uno::Reference<embed::XEmbeddedObject> xObject = pOleObject->GetObjRef();
260 if (xObject.is())
261 {
263 aElements.push_back(aName);
264 }
265 pOleObject = aIterator.next();
266 }
267 return comphelper::containerToSequence(aElements);
268}
269
270sal_Bool SAL_CALL TablePivotCharts::hasByName(OUString const & rName)
271{
272 SolarMutexGuard aGuard;
273
275}
276
277} // end sc namespace
278
279/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_WARN_UNUSED_RESULT Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
constexpr tools::Long Y() const
void setX(tools::Long nX)
void setY(tools::Long nY)
constexpr tools::Long X() const
const ScDocument & GetDocument() const
Definition: docsh.hxx:219
ScDrawLayer * MakeDrawLayer()
Definition: docsh2.cxx:169
ScModelObj * GetModel() const
Definition: docsh.hxx:432
SC_DLLPUBLIC ScDrawLayer * GetDrawLayer()
Definition: document.hxx:1084
void RemoveUnoObject(SfxListener &rObject)
Definition: documen3.cxx:909
SC_DLLPUBLIC bool IsLayoutRTL(SCTAB nTab) const
Definition: document.cxx:974
SdrObject * GetNamedObject(std::u16string_view rName, SdrObjKind nId, SCTAB &rFoundTab) const
Definition: drwlayer.cxx:2450
void AddUndo(std::unique_ptr< SdrUndoAction > pUndo)
const SdrPage * GetPage(sal_uInt16 nPgNum) const
virtual void InsertObject(SdrObject *pObj, size_t nPos=SAL_MAX_SIZE)
virtual rtl::Reference< SdrObject > RemoveObject(size_t nObjNum)
css::uno::Reference< css::embed::XEmbeddedObject > const & GetObjRef() const
SfxHintId GetId() const
comphelper::EmbeddedObjectContainer & GetEmbeddedObjectContainer() const
constexpr tools::Long Height() const
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
static MapUnit UnoEmbed2VCLMapUnit(sal_Int32 nUnoEmbedMapUnit)
OUString GetEmbeddedObjectName(const css::uno::Reference< css::embed::XEmbeddedObject > &) const
css::uno::Reference< css::embed::XEmbeddedObject > CreateEmbeddedObject(const css::uno::Sequence< sal_Int8 > &, OUString &, OUString const *pBaseURL=nullptr)
css::uno::Type const & get()
#define SO3_SCH_CLASSID
virtual sal_Bool SAL_CALL hasByName(OUString const &aName) override
virtual sal_Bool SAL_CALL hasElements() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual void SAL_CALL addNewByName(OUString const &aName, const css::awt::Rectangle &aRect, OUString const &aDataPilotName) override
virtual ~TablePivotCharts() override
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
virtual void SAL_CALL removeByName(OUString const &aName) override
virtual css::uno::Any SAL_CALL getByName(OUString const &aName) override
virtual sal_Int32 SAL_CALL getCount() override
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 nIndex) override
constexpr Size GetSize() const
int nCount
EmbeddedObjectRef * pObject
sal_Int32 nIndex
OUString aName
MapUnit
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
SdrOle2Obj * getChartByIndex(ScDocShell *pDocShell, SCTAB nTab, ::tools::Long nIndex, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:120
SdrOle2Obj * findChartsByName(ScDocShell *pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
Definition: ChartTools.cxx:98
CAUTION! The following defines must be in the same namespace as the respective type.
Definition: broadcast.cxx:15
unsigned char sal_Bool
sal_Int16 SCTAB
Definition: types.hxx:22