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 "vbaaxes.hxx"
21 #include "vbaaxis.hxx"
22 #include "vbachart.hxx"
23 #include <ooo/vba/excel/XlAxisType.hpp>
24 #include <ooo/vba/excel/XlAxisGroup.hpp>
25 #include <ooo/vba/excel/XAxis.hpp>
28 using namespace ::com::sun::star
;
29 using namespace ::ooo::vba
;
30 using namespace ::ooo::vba::excel::XlAxisType
;
31 using namespace ::ooo::vba::excel::XlAxisGroup
;
33 // each 'Item' in the Axes collection is indexed via 2 indexes, group and type.
34 // We need to 'flatten' this into a single index in order to be able to wrap
35 // iteration over the set of Axis(s) in a XIndexAccess implementation
37 typedef ::std::pair
<sal_Int32
, sal_Int32
> AxesCoordinate
; // type and group combination
38 typedef ::std::vector
< AxesCoordinate
> vecAxesIndices
;
40 typedef ::cppu::WeakImplHelper1
< container::XIndexAccess
> AxisIndexWrapper_BASE
;
44 class EnumWrapper
: public EnumerationHelper_BASE
46 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
49 EnumWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
) : m_xIndexAccess( xIndexAccess
), nIndex( 0 ) {}
50 virtual sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
52 return ( nIndex
< m_xIndexAccess
->getCount() );
55 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
57 if ( nIndex
< m_xIndexAccess
->getCount() )
58 return m_xIndexAccess
->getByIndex( nIndex
++ );
59 throw container::NoSuchElementException();
65 uno::Reference
< excel::XAxis
>
66 ScVbaAxes::createAxis( const uno::Reference
< excel::XChart
>& xChart
, const uno::Reference
< uno::XComponentContext
>& xContext
, sal_Int32 nType
, sal_Int32 nAxisGroup
) throw ( uno::RuntimeException
, script::BasicErrorException
)
68 ScVbaChart
* pChart
= static_cast< ScVbaChart
* >( xChart
.get() );
70 throw uno::RuntimeException("Object failure, can't access chart implementation" );
72 uno::Reference
< beans::XPropertySet
> xAxisPropertySet
;
73 if (((nType
== xlCategory
) || (nType
== xlSeriesAxis
) || (nType
== xlValue
)))
75 if ((nAxisGroup
!= xlPrimary
) && (nAxisGroup
!= xlSecondary
))
76 DebugHelper::runtimeexception(SbERR_METHOD_FAILED
, OUString());
77 xAxisPropertySet
.set( pChart
->getAxisPropertySet(nType
, nAxisGroup
), uno::UNO_QUERY_THROW
);
80 DebugHelper::runtimeexception(SbERR_METHOD_FAILED
, OUString());
81 uno::Reference
< XHelperInterface
> xParent( xChart
, uno::UNO_QUERY_THROW
);
82 return new ScVbaAxis( xParent
, xContext
, xAxisPropertySet
, nType
, nAxisGroup
);
87 class AxisIndexWrapper
: public AxisIndexWrapper_BASE
89 // if necessary for better performance we could change this into a map and cache the
90 // indices -> Axis, currently we create a new Axis object
92 uno::Reference
< uno::XComponentContext
> mxContext
;
93 vecAxesIndices mCoordinates
;
94 uno::Reference
< excel::XChart
> mxChart
;
96 AxisIndexWrapper( const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< excel::XChart
>& xChart
) : mxContext( xContext
), mxChart( xChart
)
100 ScVbaChart
* pChart
= static_cast< ScVbaChart
* >( mxChart
.get() );
103 uno::Reference
< beans::XPropertySet
> xDiagramPropertySet( pChart
->xDiagramPropertySet() );
104 if ( ( xDiagramPropertySet
->getPropertyValue("HasXAxis") >>= bBool
) && bBool
)
105 mCoordinates
.push_back( AxesCoordinate( xlPrimary
, xlCategory
) );
106 if ( ( xDiagramPropertySet
->getPropertyValue("HasYAxis") >>= bBool
) && bBool
)
107 mCoordinates
.push_back( AxesCoordinate( xlPrimary
, xlSeriesAxis
) );
109 if ( pChart
->is3D() )
110 mCoordinates
.push_back( AxesCoordinate( xlPrimary
, xlValue
) );
113 if ( ( xDiagramPropertySet
->getPropertyValue("HasSecondaryXAxis") >>= bBool
) && bBool
)
114 mCoordinates
.push_back( AxesCoordinate( xlSecondary
, xlCategory
) );
115 if ( ( xDiagramPropertySet
->getPropertyValue("HasSecondaryYAxis") >>= bBool
) && bBool
)
116 mCoordinates
.push_back( AxesCoordinate( xlSecondary
, xlSeriesAxis
) );
120 virtual ::sal_Int32 SAL_CALL
getCount() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return mCoordinates
.size(); }
121 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw (lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, ::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
125 AxesCoordinate dIndexes
= mCoordinates
[ Index
];
126 return uno::makeAny( ScVbaAxes::createAxis( mxChart
, mxContext
, dIndexes
.second
, dIndexes
.first
) );
128 catch (const css::script::BasicErrorException
& e
)
130 throw css::lang::WrappedTargetException(
131 "Error Getting Index!",
132 static_cast < OWeakObject
* > ( this ),
137 virtual uno::Type SAL_CALL
getElementType() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
139 return cppu::UnoType
<excel::XAxis
>::get();
141 virtual sal_Bool SAL_CALL
hasElements( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
143 return ( mCoordinates
.size() > 0 );
147 uno::Reference
< container::XIndexAccess
> createIndexWrapper( const uno::Reference
< excel::XChart
>& xChart
, const uno::Reference
< uno::XComponentContext
>& xContext
)
149 return new AxisIndexWrapper( xContext
, xChart
);
154 // #FIXME The collection semantics will never work as this object is not yet initialised correctly
155 ScVbaAxes::ScVbaAxes( const uno::Reference
< XHelperInterface
>& xParent
,const uno::Reference
< uno::XComponentContext
> & xContext
, const uno::Reference
< excel::XChart
>& xChart
) : ScVbaAxes_BASE( xParent
, xContext
, createIndexWrapper( xChart
, xContext
)), moChartParent( xChart
)
160 ScVbaAxes::getElementType() throw (css::uno::RuntimeException
)
162 return cppu::UnoType
<excel::XAxes
>::get();
165 uno::Reference
< container::XEnumeration
> SAL_CALL
166 ScVbaAxes::createEnumeration() throw (css::uno::RuntimeException
)
168 return new EnumWrapper( m_xIndexAccess
);
172 ScVbaAxes::Item( const css::uno::Any
& _nType
, const css::uno::Any
& _oAxisGroup
) throw (css::script::BasicErrorException
, css::uno::RuntimeException
)
174 // #TODO map the possible index combinations to a container::XIndexAccess wrapper impl
175 // using a vector of valid std::pair maybe?
176 // bodgy helperapi port bits
177 sal_Int32 nAxisGroup
= xlPrimary
;
178 sal_Int32 nType
= -1;
179 if ( !_nType
.hasValue() || !( _nType
>>= nType
) )
180 throw uno::RuntimeException("Axes::Item Failed to extract type" );
182 if ( _oAxisGroup
.hasValue() )
183 _oAxisGroup
>>= nAxisGroup
;
185 return uno::makeAny( createAxis( moChartParent
, mxContext
, nType
, nAxisGroup
) );
189 ScVbaAxes::createCollectionObject(const css::uno::Any
& aSource
)
191 return aSource
; // pass through ( it's already an XAxis object
195 ScVbaAxes::getServiceImplName()
197 return OUString("ScVbaAxes");
200 uno::Sequence
< OUString
>
201 ScVbaAxes::getServiceNames()
203 static uno::Sequence
< OUString
> aServiceNames
;
204 if ( aServiceNames
.getLength() == 0 )
206 aServiceNames
.realloc( 1 );
207 aServiceNames
[ 0 ] = "ooo.vba.excel.Axes";
209 return aServiceNames
;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */