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