1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
26 #include "vbachartobjects.hxx"
27 #include "vbachartobject.hxx"
29 #include <cellsuno.hxx>
31 #include <string_view>
34 #include <basic/sberrors.hxx>
35 #include <comphelper/sequence.hxx>
36 #include <comphelper/servicehelper.hxx>
37 #include <comphelper/diagnose_ex.hxx>
39 using namespace ::com::sun::star
;
40 using namespace ::ooo::vba
;
44 class ChartObjectEnumerationImpl
: public EnumerationHelperImpl
46 uno::Reference
< drawing::XDrawPageSupplier
> xDrawPageSupplier
;
49 /// @throws uno::RuntimeException
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
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
) );
61 catch (const lang::WrappedTargetException
&)
65 catch (const container::NoSuchElementException
&)
69 catch (const uno::RuntimeException
&)
73 catch (const uno::Exception
&)
75 css::uno::Any
anyEx(cppu::getCaughtException());
76 throw lang::WrappedTargetException(
77 u
"Error creating ScVbaChartObject!"_ustr
,
87 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
, 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
))
93 ScVbaChartObjects::removeByName(const OUString
& _sChartName
)
95 xTableCharts
->removeByName( _sChartName
);
98 uno::Sequence
< OUString
>
99 ScVbaChartObjects::getChartObjectNames() const
101 uno::Sequence
< OUString
> sChartNames
;
105 uno::Reference
< uno::XInterface
> xIf( xDrawPageSupplier
, uno::UNO_QUERY_THROW
);
106 ScCellRangesBase
* pUno
= dynamic_cast< ScCellRangesBase
* >( xIf
.get() );
107 ScDocShell
* pDocShell
= nullptr;
109 throw uno::RuntimeException(u
"Failed to obtain the impl class from the drawpage"_ustr
);
110 pDocShell
= pUno
->GetDocShell();
112 throw uno::RuntimeException(u
"Failed to obtain the docshell implclass"_ustr
);
114 uno::Reference
< sheet::XSpreadsheets
> xSpreadsheets
= pDocShell
->GetModel()->getSheets();
115 std::vector
< OUString
> aChartNamesVector
;
117 const uno::Sequence
< OUString
> sSheetNames
= xSpreadsheets
->getElementNames();
118 for (const auto& rSheetName
: sSheetNames
)
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() );
124 sChartNames
= comphelper::containerToSequence( aChartNamesVector
);
126 catch (uno::Exception
& )
128 throw script::BasicErrorException( OUString(), uno::Reference
< uno::XInterface
>(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED
), OUString() );
135 ScVbaChartObjects::Add( double _nX
, double _nY
, double _nWidth
, double _nHeight
)
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(), u
"Chart "_ustr
, 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
);
152 catch (const uno::Exception
&)
154 DBG_UNHANDLED_EXCEPTION("sc");
158 void SAL_CALL
ScVbaChartObjects::Delete( )
160 const uno::Sequence
< OUString
> sChartNames
= xTableCharts
->getElementNames();
161 for (const auto& rChartName
: sChartNames
)
162 removeByName(rChartName
);
165 // XEnumerationAccess
167 uno::Reference
< container::XEnumeration
>
168 ScVbaChartObjects::createEnumeration()
170 css::uno::Reference
< container::XEnumerationAccess
> xEnumAccess( xTableCharts
, uno::UNO_QUERY_THROW
);
171 return new ChartObjectEnumerationImpl( mxContext
, xEnumAccess
->createEnumeration(), xDrawPageSupplier
, getParent() /* sheet */);
177 ScVbaChartObjects::getElementType()
179 return cppu::UnoType
<excel::XChartObject
>::get();
182 // ScVbaCollectionBaseImpl
184 ScVbaChartObjects::createCollectionObject( const css::uno::Any
& aSource
)
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
) ) );
192 ScVbaChartObjects::getServiceImplName()
194 return u
"ScVbaChartObjects"_ustr
;
197 css::uno::Sequence
<OUString
>
198 ScVbaChartObjects::getServiceNames()
200 static uno::Sequence
< OUString
> const sNames
202 u
"ooo.vba.excel.ChartObjects"_ustr
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */