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 "vbachartobject.hxx"
27 #include "vbachartobjects.hxx"
28 
29 using namespace ::com::sun::star;
30 using namespace ::ooo::vba;
31 
32 const OUStringLiteral PERSIST_NAME(u"PersistName");
33 
34 ScVbaChartObject::ScVbaChartObject( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, const css::uno::Reference< css::table::XTableChart >& _xTableChart, const css::uno::Reference< css::drawing::XDrawPageSupplier >& _xDrawPageSupplier ) : ChartObjectImpl_BASE( _xParent, _xContext ), xTableChart( _xTableChart ), xDrawPageSupplier( _xDrawPageSupplier )
35 {
36  xDrawPage = xDrawPageSupplier->getDrawPage();
37  xEmbeddedObjectSupplier.set( xTableChart, uno::UNO_QUERY_THROW );
38  xNamed.set( xTableChart, uno::UNO_QUERY_THROW );
40  xShape = setShape();
42  oShapeHelper.reset(new ShapeHelper(xShape));
43 }
44 
46 {
47  if ( sPersistName.isEmpty() )
48  sPersistName = xNamed->getName();
49  return sPersistName;
50 }
51 
52 uno::Reference< drawing::XShape >
54 {
55  try
56  {
57  sal_Int32 nItems = xDrawPage->getCount();
58  for (int i = 0; i < nItems; i++)
59  {
60  xShape.set( xDrawPage->getByIndex(i), uno::UNO_QUERY_THROW );
61  if (xShape->getShapeType() == "com.sun.star.drawing.OLE2Shape")
62  {
63  uno::Reference< beans::XPropertySet > xShapePropertySet(xShape, uno::UNO_QUERY_THROW );
64  OUString sName;
65  xShapePropertySet->getPropertyValue(PERSIST_NAME ) >>=sName;
66  if ( sName == sPersistName )
67  {
68  xNamedShape.set( xShape, uno::UNO_QUERY_THROW );
69  return xShape;
70  }
71  }
72  }
73  }
74  catch (uno::Exception& )
75  {
76  throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
77  }
78  return nullptr;
79 }
80 
81 void SAL_CALL
82 ScVbaChartObject::setName( const OUString& sName )
83 {
84  xNamedShape->setName(sName);
85 }
86 
87 OUString SAL_CALL
89 {
90  return xNamedShape->getName();
91 }
92 
93 void SAL_CALL
95 {
96  // parent of this object is sheet
97  uno::Reference< excel::XWorksheet > xParent( getParent(), uno::UNO_QUERY_THROW );
98  uno::Reference< excel::XChartObjects > xColl( xParent->ChartObjects( uno::Any() ), uno::UNO_QUERY_THROW );
99  ScVbaChartObjects* pChartObjectsImpl = static_cast< ScVbaChartObjects* >( xColl.get() );
100  if (!pChartObjectsImpl)
101  throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), "Parent is not ChartObjects" );
102 
103  pChartObjectsImpl->removeByName( getPersistName() );
104 
105 }
106 
107 void
109 {
110  try
111  {
112  // #TODO #FIXME should be ThisWorkbook or equivalent, or in
113  // fact probably the chart object should be created with
114  // the XModel owner
115  //uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getXModel().getCurrentController());
116  uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getCurrentExcelDoc(mxContext)->getCurrentController(), uno::UNO_QUERY_THROW );
117  xSelectionSupplier->select(uno::makeAny(xShape));
118  }
119  catch (uno::Exception& )
120  {
121  throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), "ChartObject Activate internal error" );
122  }
123 }
124 
125 uno::Reference< excel::XChart > SAL_CALL
127 {
128  return new ScVbaChart( this, mxContext, xEmbeddedObjectSupplier->getEmbeddedObject(), xTableChart );
129 }
130 
131 OUString
133 {
134  return "ScVbaChartObject";
135 }
136 
137 uno::Sequence< OUString >
139 {
140  static uno::Sequence< OUString > const aServiceNames
141  {
142  "ooo.vba.excel.ChartObject"
143  };
144  return aServiceNames;
145 }
146 
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::document::XEmbeddedObjectSupplier > xEmbeddedObjectSupplier
css::uno::Reference< css::table::XTableChart > xTableChart
virtual OUString getServiceImplName() override
Sequence< OUString > aServiceNames
virtual void SAL_CALL Delete() override
const OUStringLiteral PERSIST_NAME(u"PersistName")
virtual void SAL_CALL setName(const OUString &sName) override
const char * sName
css::uno::Reference< css::drawing::XDrawPageSupplier > xDrawPageSupplier
int i
css::uno::Reference< css::drawing::XShape > setShape()
OUString const & getPersistName()
virtual css::uno::Reference< ov::excel::XChart > SAL_CALL getChart() override
css::uno::Reference< css::container::XNamed > xNamedShape
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
virtual OUString SAL_CALL getName() override
css::uno::Reference< css::drawing::XShape > xShape
#define ERRCODE_BASIC_METHOD_FAILED
uno::Reference< frame::XModel > getCurrentExcelDoc(const uno::Reference< uno::XComponentContext > &xContext)
std::unique_ptr< ov::ShapeHelper > oShapeHelper
css::uno::Reference< css::drawing::XDrawPage > xDrawPage
css::uno::Reference< css::container::XNamed > xNamed
virtual css::uno::Sequence< OUString > getServiceNames() override
ScVbaChartObject(const css::uno::Reference< ov::XHelperInterface > &_xParent, const css::uno::Reference< css::uno::XComponentContext > &_xContext, const css::uno::Reference< css::table::XTableChart > &_xTableChart, const css::uno::Reference< css::drawing::XDrawPageSupplier > &_xDrawPageSupplier)