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>
38
39using namespace ::com::sun::star;
40using namespace ::ooo::vba;
41
42namespace {
43
44class ChartObjectEnumerationImpl : public EnumerationHelperImpl
45{
46 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier;
47
48public:
50 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 )) {}
51 virtual uno::Any SAL_CALL nextElement( ) override
52 {
53 uno::Any ret;
54
55 try
56 {
57 uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
58 // parent Object is sheet
59 ret <<= uno::Reference< excel::XChartObject > ( new ScVbaChartObject( m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) );
60 }
61 catch (const lang::WrappedTargetException&)
62 {
63 throw;
64 }
65 catch (const container::NoSuchElementException&)
66 {
67 throw;
68 }
69 catch (const uno::RuntimeException&)
70 {
71 throw;
72 }
73 catch (const uno::Exception&)
74 {
75 css::uno::Any anyEx(cppu::getCaughtException());
76 throw lang::WrappedTargetException(
77 "Error creating ScVbaChartObject!",
78 static_cast < OWeakObject * > ( this ),
79 anyEx );
80 }
81 return ret;
82 }
83};
84
85}
86
87ScVbaChartObjects::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 ))
88{
89
90}
91
92void
93ScVbaChartObjects::removeByName(const OUString& _sChartName)
94{
95 xTableCharts->removeByName( _sChartName );
96}
97
98uno::Sequence< OUString >
100{
101 uno::Sequence< OUString > sChartNames;
102 try
103 {
104 // c++ hackery
105 uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW );
106 ScCellRangesBase* pUno = dynamic_cast< ScCellRangesBase* >( xIf.get() );
107 ScDocShell* pDocShell = nullptr;
108 if ( !pUno )
109 throw uno::RuntimeException("Failed to obtain the impl class from the drawpage" );
110 pDocShell = pUno->GetDocShell();
111 if ( !pDocShell )
112 throw uno::RuntimeException("Failed to obtain the docshell implclass" );
113
114 uno::Reference< sheet::XSpreadsheets > xSpreadsheets = pDocShell->GetModel()->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
ScModelObj * GetModel() const
Definition: docsh.hxx:434
virtual css::uno::Reference< css::sheet::XSpreadsheets > SAL_CALL getSheets() override
XSpreadsheetDocument.
Definition: docuno.cxx:1422
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::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