LibreOffice Module sc (master) 1
vbachartobject.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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19#include "vbachart.hxx"
20#include <com/sun/star/beans/XPropertySet.hpp>
21#include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
22#include <com/sun/star/container/XNamed.hpp>
23#include <com/sun/star/script/BasicErrorException.hpp>
24#include <com/sun/star/view/XSelectionSupplier.hpp>
25#include <basic/sberrors.hxx>
26#include <utility>
27#include "vbachartobject.hxx"
28#include "vbachartobjects.hxx"
29
30using namespace ::com::sun::star;
31using namespace ::ooo::vba;
32
33constexpr OUStringLiteral PERSIST_NAME(u"PersistName");
34
35ScVbaChartObject::ScVbaChartObject( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, css::uno::Reference< css::table::XTableChart > _xTableChart, css::uno::Reference< css::drawing::XDrawPageSupplier > _xDrawPageSupplier ) : ChartObjectImpl_BASE( _xParent, _xContext ), xTableChart(std::move( _xTableChart )), xDrawPageSupplier(std::move( _xDrawPageSupplier ))
36{
37 xDrawPage = xDrawPageSupplier->getDrawPage();
38 xEmbeddedObjectSupplier.set( xTableChart, uno::UNO_QUERY_THROW );
39 xNamed.set( xTableChart, uno::UNO_QUERY_THROW );
41 xShape = setShape();
43 oShapeHelper.emplace(xShape);
44}
45
47{
48 if ( sPersistName.isEmpty() )
49 sPersistName = xNamed->getName();
50 return sPersistName;
51}
52
53uno::Reference< drawing::XShape >
55{
56 try
57 {
58 sal_Int32 nItems = xDrawPage->getCount();
59 for (int i = 0; i < nItems; i++)
60 {
61 xShape.set( xDrawPage->getByIndex(i), uno::UNO_QUERY_THROW );
62 if (xShape->getShapeType() == "com.sun.star.drawing.OLE2Shape")
63 {
64 uno::Reference< beans::XPropertySet > xShapePropertySet(xShape, uno::UNO_QUERY_THROW );
65 OUString sName;
66 xShapePropertySet->getPropertyValue(PERSIST_NAME ) >>=sName;
67 if ( sName == sPersistName )
68 {
69 xNamedShape.set( xShape, uno::UNO_QUERY_THROW );
70 return xShape;
71 }
72 }
73 }
74 }
75 catch (uno::Exception& )
76 {
77 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
78 }
79 return nullptr;
80}
81
82void SAL_CALL
83ScVbaChartObject::setName( const OUString& sName )
84{
85 xNamedShape->setName(sName);
86}
87
88OUString SAL_CALL
90{
91 return xNamedShape->getName();
92}
93
94void SAL_CALL
96{
97 // parent of this object is sheet
98 uno::Reference< excel::XWorksheet > xParent( getParent(), uno::UNO_QUERY_THROW );
99 uno::Reference< excel::XChartObjects > xColl( xParent->ChartObjects( uno::Any() ), uno::UNO_QUERY_THROW );
100 ScVbaChartObjects* pChartObjectsImpl = static_cast< ScVbaChartObjects* >( xColl.get() );
101 if (!pChartObjectsImpl)
102 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), "Parent is not ChartObjects" );
103
104 pChartObjectsImpl->removeByName( getPersistName() );
105
106}
107
108void
110{
111 try
112 {
113 // #TODO #FIXME should be ThisWorkbook or equivalent, or in
114 // fact probably the chart object should be created with
115 // the XModel owner
116 //uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getXModel().getCurrentController());
117 uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getCurrentExcelDoc(mxContext)->getCurrentController(), uno::UNO_QUERY_THROW );
118 xSelectionSupplier->select(uno::Any(xShape));
119 }
120 catch (uno::Exception& )
121 {
122 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), "ChartObject Activate internal error" );
123 }
124}
125
126uno::Reference< excel::XChart > SAL_CALL
128{
129 return new ScVbaChart( this, mxContext, xEmbeddedObjectSupplier->getEmbeddedObject(), xTableChart );
130}
131
132OUString
134{
135 return "ScVbaChartObject";
136}
137
138uno::Sequence< OUString >
140{
141 static uno::Sequence< OUString > const aServiceNames
142 {
143 "ooo.vba.excel.ChartObject"
144 };
145 return aServiceNames;
146}
147
148/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
virtual css::uno::Reference< ov::excel::XChart > SAL_CALL getChart() override
ScVbaChartObject(const css::uno::Reference< ov::XHelperInterface > &_xParent, const css::uno::Reference< css::uno::XComponentContext > &_xContext, css::uno::Reference< css::table::XTableChart > _xTableChart, css::uno::Reference< css::drawing::XDrawPageSupplier > _xDrawPageSupplier)
virtual OUString SAL_CALL getName() override
css::uno::Reference< css::drawing::XDrawPage > xDrawPage
css::uno::Reference< css::container::XNamed > xNamedShape
css::uno::Reference< css::drawing::XDrawPageSupplier > xDrawPageSupplier
virtual css::uno::Sequence< OUString > getServiceNames() override
css::uno::Reference< css::drawing::XShape > xShape
virtual OUString getServiceImplName() override
css::uno::Reference< css::document::XEmbeddedObjectSupplier > xEmbeddedObjectSupplier
virtual void SAL_CALL setName(const OUString &sName) override
std::optional< ov::ShapeHelper > oShapeHelper
css::uno::Reference< css::table::XTableChart > xTableChart
css::uno::Reference< css::drawing::XShape > setShape()
OUString const & getPersistName()
virtual void SAL_CALL Delete() override
css::uno::Reference< css::container::XNamed > xNamed
void removeByName(const OUString &_sChartName)
Sequence< OUString > aServiceNames
OUString sName
int i
VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getCurrentExcelDoc(const css::uno::Reference< css::uno::XComponentContext > &xContext)
#define ERRCODE_BASIC_METHOD_FAILED
Reference< XComponentContext > _xContext
constexpr OUStringLiteral PERSIST_NAME(u"PersistName")