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 <basic/sberrors.hxx>
32 #include <comphelper/sequence.hxx>
33 #include <tools/diagnose_ex.h>
35 using namespace ::com::sun::star
;
36 using namespace ::ooo::vba
;
40 class ChartObjectEnumerationImpl
: public EnumerationHelperImpl
42 uno::Reference
< drawing::XDrawPageSupplier
> xDrawPageSupplier
;
45 /// @throws uno::RuntimeException
46 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
) {}
47 virtual uno::Any SAL_CALL
nextElement( ) override
53 uno::Reference
< table::XTableChart
> xTableChart( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
54 // parent Object is sheet
55 ret
<<= uno::Reference
< excel::XChartObject
> ( new ScVbaChartObject( m_xParent
, m_xContext
, xTableChart
, xDrawPageSupplier
) );
57 catch (const lang::WrappedTargetException
&)
61 catch (const container::NoSuchElementException
&)
65 catch (const uno::RuntimeException
&)
69 catch (const uno::Exception
&)
71 css::uno::Any
anyEx(cppu::getCaughtException());
72 throw lang::WrappedTargetException(
73 "Error creating ScVbaChartObject!",
74 static_cast < OWeakObject
* > ( this ),
83 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
)
89 ScVbaChartObjects::removeByName(const OUString
& _sChartName
)
91 xTableCharts
->removeByName( _sChartName
);
94 uno::Sequence
< OUString
>
95 ScVbaChartObjects::getChartObjectNames() const
97 uno::Sequence
< OUString
> sChartNames
;
101 uno::Reference
< uno::XInterface
> xIf( xDrawPageSupplier
, uno::UNO_QUERY_THROW
);
102 ScCellRangesBase
* pUno
= dynamic_cast< ScCellRangesBase
* >( xIf
.get() );
103 ScDocShell
* pDocShell
= nullptr;
105 throw uno::RuntimeException("Failed to obtain the impl class from the drawpage" );
106 pDocShell
= pUno
->GetDocShell();
108 throw uno::RuntimeException("Failed to obtain the docshell implclass" );
110 uno::Reference
< sheet::XSpreadsheetDocument
> xSpreadsheetDocument( pDocShell
->GetModel(), uno::UNO_QUERY_THROW
);
111 uno::Reference
< sheet::XSpreadsheets
> xSpreadsheets
= xSpreadsheetDocument
->getSheets();
112 std::vector
< OUString
> aChartNamesVector
;
114 const uno::Sequence
< OUString
> sSheetNames
= xSpreadsheets
->getElementNames();
115 for (const auto& rSheetName
: sSheetNames
)
117 uno::Reference
< table::XTableChartsSupplier
> xLocTableChartsSupplier( xSpreadsheets
->getByName(rSheetName
), uno::UNO_QUERY_THROW
);
118 const uno::Sequence
< OUString
> scurchartnames
= xLocTableChartsSupplier
->getCharts()->getElementNames();
119 aChartNamesVector
.insert( aChartNamesVector
.end(), scurchartnames
.begin(), scurchartnames
.end() );
121 sChartNames
= comphelper::containerToSequence( aChartNamesVector
);
123 catch (uno::Exception
& )
125 throw script::BasicErrorException( OUString(), uno::Reference
< uno::XInterface
>(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED
), OUString() );
132 ScVbaChartObjects::Add( double _nX
, double _nY
, double _nWidth
, double _nHeight
)
136 uno::Sequence
< table::CellRangeAddress
> aCellRangeAddress( 1 );
137 awt::Rectangle aRectangle
;
138 aRectangle
.X
= Millimeter::getInHundredthsOfOneMillimeter(_nX
);
139 aRectangle
.Y
= Millimeter::getInHundredthsOfOneMillimeter(_nY
);
140 aRectangle
.Width
= Millimeter::getInHundredthsOfOneMillimeter(_nWidth
);
141 aRectangle
.Height
= Millimeter::getInHundredthsOfOneMillimeter(_nHeight
);
142 // Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem
143 OUString sPersistChartName
= ContainerUtilities::getUniqueName( getChartObjectNames(), "Chart " , OUString(), 1);
144 xTableCharts
->addNewByName(sPersistChartName
, aRectangle
, aCellRangeAddress
, true, false );
145 uno::Reference
< excel::XChartObject
> xChartObject( getItemByStringIndex( sPersistChartName
), uno::UNO_QUERY_THROW
);
146 xChartObject
->getChart()->setChartType(excel::XlChartType::xlColumnClustered
);
147 return uno::makeAny( xChartObject
);
149 catch (const uno::Exception
&)
151 DBG_UNHANDLED_EXCEPTION("sc");
155 void SAL_CALL
ScVbaChartObjects::Delete( )
157 const uno::Sequence
< OUString
> sChartNames
= xTableCharts
->getElementNames();
158 for (const auto& rChartName
: sChartNames
)
159 removeByName(rChartName
);
162 // XEnumerationAccess
164 uno::Reference
< container::XEnumeration
>
165 ScVbaChartObjects::createEnumeration()
167 css::uno::Reference
< container::XEnumerationAccess
> xEnumAccess( xTableCharts
, uno::UNO_QUERY_THROW
);
168 return new ChartObjectEnumerationImpl( mxContext
, xEnumAccess
->createEnumeration(), xDrawPageSupplier
, getParent() /* sheet */);
174 ScVbaChartObjects::getElementType()
176 return cppu::UnoType
<excel::XChartObject
>::get();
179 // ScVbaCollectionBaseImpl
181 ScVbaChartObjects::createCollectionObject( const css::uno::Any
& aSource
)
183 uno::Reference
< table::XTableChart
> xTableChart( aSource
, uno::UNO_QUERY_THROW
);
184 // correct parent object is sheet
185 return uno::makeAny( uno::Reference
< excel::XChartObject
> ( new ScVbaChartObject( getParent(), mxContext
, xTableChart
, xDrawPageSupplier
) ) );
189 ScVbaChartObjects::getServiceImplName()
191 return "ScVbaChartObjects";
194 css::uno::Sequence
<OUString
>
195 ScVbaChartObjects::getServiceNames()
197 static uno::Sequence
< OUString
> const sNames
199 "ooo.vba.excel.ChartObjects"
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */