LibreOffice Module sc (master)  1
vbachartobjects.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 
20 #include <com/sun/star/table/XTableChartsSupplier.hpp>
21 #include <com/sun/star/table/XTableChart.hpp>
22 #include <com/sun/star/script/BasicErrorException.hpp>
23 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
24 #include <ooo/vba/excel/XlChartType.hpp>
25 
26 #include "vbachartobjects.hxx"
27 #include "vbachartobject.hxx"
28 #include <docsh.hxx>
29 #include <cellsuno.hxx>
30 
31 #include <string_view>
32 #include <vector>
33 #include <basic/sberrors.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <tools/diagnose_ex.h>
36 
37 using namespace ::com::sun::star;
38 using namespace ::ooo::vba;
39 
40 namespace {
41 
42 class ChartObjectEnumerationImpl : public EnumerationHelperImpl
43 {
44  uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier;
45 
46 public:
48  ChartObjectEnumerationImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier, const uno::Reference< XHelperInterface >& _xParent ) : EnumerationHelperImpl( _xParent, xContext, xEnumeration ), xDrawPageSupplier( _xDrawPageSupplier ) {}
49  virtual uno::Any SAL_CALL nextElement( ) override
50  {
51  uno::Any ret;
52 
53  try
54  {
55  uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
56  // parent Object is sheet
57  ret <<= uno::Reference< excel::XChartObject > ( new ScVbaChartObject( m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) );
58  }
59  catch (const lang::WrappedTargetException&)
60  {
61  throw;
62  }
63  catch (const container::NoSuchElementException&)
64  {
65  throw;
66  }
67  catch (const uno::RuntimeException&)
68  {
69  throw;
70  }
71  catch (const uno::Exception&)
72  {
73  css::uno::Any anyEx(cppu::getCaughtException());
74  throw lang::WrappedTargetException(
75  "Error creating ScVbaChartObject!",
76  static_cast < OWeakObject * > ( this ),
77  anyEx );
78  }
79  return ret;
80  }
81 };
82 
83 }
84 
85 ScVbaChartObjects::ScVbaChartObjects( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, const css::uno::Reference< css::table::XTableCharts >& _xTableCharts, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier ) : ChartObjects_BASE(_xParent, _xContext, css::uno::Reference< css::container::XIndexAccess >( _xTableCharts, css::uno::UNO_QUERY ) ), xTableCharts( _xTableCharts ) , xDrawPageSupplier( _xDrawPageSupplier )
86 {
87 
88 }
89 
90 void
91 ScVbaChartObjects::removeByName(const OUString& _sChartName)
92 {
93  xTableCharts->removeByName( _sChartName );
94 }
95 
96 uno::Sequence< OUString >
98 {
99  uno::Sequence< OUString > sChartNames;
100  try
101  {
102  // c++ hackery
103  uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW );
104  ScCellRangesBase* pUno= dynamic_cast< ScCellRangesBase* >( xIf.get() );
105  ScDocShell* pDocShell = nullptr;
106  if ( !pUno )
107  throw uno::RuntimeException("Failed to obtain the impl class from the drawpage" );
108  pDocShell = pUno->GetDocShell();
109  if ( !pDocShell )
110  throw uno::RuntimeException("Failed to obtain the docshell implclass" );
111 
112  uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument( pDocShell->GetModel(), uno::UNO_QUERY_THROW );
113  uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSpreadsheetDocument->getSheets();
114  std::vector< OUString > aChartNamesVector;
115 
116  const uno::Sequence< OUString > sSheetNames = xSpreadsheets->getElementNames();
117  for (const auto& rSheetName : sSheetNames)
118  {
119  uno::Reference< table::XTableChartsSupplier > xLocTableChartsSupplier( xSpreadsheets->getByName(rSheetName), uno::UNO_QUERY_THROW );
120  const uno::Sequence< OUString > scurchartnames = xLocTableChartsSupplier->getCharts()->getElementNames();
121  aChartNamesVector.insert( aChartNamesVector.end(), scurchartnames.begin(), scurchartnames.end() );
122  }
123  sChartNames = comphelper::containerToSequence( aChartNamesVector );
124  }
125  catch (uno::Exception& )
126  {
127  throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
128  }
129  return sChartNames;
130 }
131 
132 // XChartObjects
133 uno::Any SAL_CALL
134 ScVbaChartObjects::Add( double _nX, double _nY, double _nWidth, double _nHeight )
135 {
136  try
137  {
138  uno::Sequence< table::CellRangeAddress > aCellRangeAddress( 1 );
139  awt::Rectangle aRectangle;
140  aRectangle.X = Millimeter::getInHundredthsOfOneMillimeter(_nX);
141  aRectangle.Y = Millimeter::getInHundredthsOfOneMillimeter(_nY);
142  aRectangle.Width = Millimeter::getInHundredthsOfOneMillimeter(_nWidth);
143  aRectangle.Height = Millimeter::getInHundredthsOfOneMillimeter(_nHeight);
144  // Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem
145  OUString sPersistChartName = ContainerUtilities::getUniqueName( getChartObjectNames(), "Chart " , std::u16string_view(), 1);
146  xTableCharts->addNewByName(sPersistChartName, aRectangle, aCellRangeAddress, true, false );
147  uno::Reference< excel::XChartObject > xChartObject( getItemByStringIndex( sPersistChartName ), uno::UNO_QUERY_THROW );
148  xChartObject->getChart()->setChartType(excel::XlChartType::xlColumnClustered);
149  return uno::makeAny( xChartObject );
150  }
151  catch (const uno::Exception&)
152  {
154  }
155  return aNULL();
156 }
158 {
159  const uno::Sequence< OUString > sChartNames = xTableCharts->getElementNames();
160  for (const auto& rChartName : sChartNames)
161  removeByName(rChartName);
162 }
163 
164 // XEnumerationAccess
165 
166 uno::Reference< container::XEnumeration >
168 {
169  css::uno::Reference< container::XEnumerationAccess > xEnumAccess( xTableCharts, uno::UNO_QUERY_THROW );
170  return new ChartObjectEnumerationImpl( mxContext, xEnumAccess->createEnumeration(), xDrawPageSupplier, getParent() /* sheet */);
171 }
172 
173 // XElementAccess
174 
175 uno::Type
177 {
179 }
180 
181 // ScVbaCollectionBaseImpl
182 uno::Any
183 ScVbaChartObjects::createCollectionObject( const css::uno::Any& aSource )
184 {
185  uno::Reference< table::XTableChart > xTableChart( aSource, uno::UNO_QUERY_THROW );
186  // correct parent object is sheet
187  return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( getParent(), mxContext, xTableChart, xDrawPageSupplier ) ) );
188 }
189 
190 OUString
192 {
193  return "ScVbaChartObjects";
194 }
195 
196 css::uno::Sequence<OUString>
198 {
199  static uno::Sequence< OUString > const sNames
200  {
201  "ooo.vba.excel.ChartObjects"
202  };
203  return sNames;
204 }
205 
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Any SAL_CALL Add(double Left, double Top, double Width, double Height) override
ScDocShell * GetDocShell() const
Definition: cellsuno.hxx:242
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Reference
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex)
css::uno::Reference< css::frame::XModel > GetModel() const
void removeByName(const OUString &_sChartName)
exports com.sun.star. container
const uno::Any & aNULL()
Any SAL_CALL getCaughtException()
css::uno::Sequence< OUString > getChartObjectNames() const
#define DBG_UNHANDLED_EXCEPTION(...)
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual void SAL_CALL Delete() override
virtual css::uno::Type SAL_CALL getElementType() override
css::uno::Type const & get()
virtual OUString getServiceImplName() override
ScVbaChartObjects(const css::uno::Reference< ov::XHelperInterface > &_xParent, const css::uno::Reference< css::uno::XComponentContext > &_xContext, const css::uno::Reference< css::table::XTableCharts > &_xTableCharts, const css::uno::Reference< css::drawing::XDrawPageSupplier > &_xDrawPageSupplier)
css::uno::Reference< css::drawing::XDrawPageSupplier > xDrawPageSupplier
virtual css::uno::Sequence< OUString > getServiceNames() override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
#define ERRCODE_BASIC_METHOD_FAILED
css::uno::Reference< css::table::XTableCharts > xTableCharts