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
;
42 class EnumWrapper
: public EnumerationHelper_BASE
44 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
47 EnumWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
) : m_xIndexAccess( xIndexAccess
), nIndex( 0 ) {}
48 virtual ::sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
)
50 return ( nIndex
< m_xIndexAccess
->getCount() );
53 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
55 if ( nIndex
< m_xIndexAccess
->getCount() )
56 return m_xIndexAccess
->getByIndex( nIndex
++ );
57 throw container::NoSuchElementException();
62 uno::Reference
< excel::XAxis
>
63 ScVbaAxes::createAxis( const uno::Reference
< excel::XChart
>& xChart
, const uno::Reference
< uno::XComponentContext
>& xContext
, sal_Int32 nType
, sal_Int32 nAxisGroup
) throw ( uno::RuntimeException
)
65 ScVbaChart
* pChart
= static_cast< ScVbaChart
* >( xChart
.get() );
67 throw uno::RuntimeException("Object failure, can't access chart implementation", uno::Reference
< uno::XInterface
>() );
69 uno::Reference
< beans::XPropertySet
> xAxisPropertySet
;
70 if (((nType
== xlCategory
) || (nType
== xlSeriesAxis
) || (nType
== xlValue
)))
72 if ((nAxisGroup
!= xlPrimary
) && (nAxisGroup
!= xlSecondary
))
73 throw script::BasicErrorException( OUString(), NULL
, SbERR_METHOD_FAILED
, OUString());
74 xAxisPropertySet
.set( pChart
->getAxisPropertySet(nType
, nAxisGroup
), uno::UNO_QUERY_THROW
);
77 throw script::BasicErrorException( OUString(), NULL
, SbERR_METHOD_FAILED
, OUString());
78 uno::Reference
< XHelperInterface
> xParent( xChart
, uno::UNO_QUERY_THROW
);
79 return new ScVbaAxis( xParent
, xContext
, xAxisPropertySet
, nType
, nAxisGroup
);
82 class AxisIndexWrapper
: public AxisIndexWrapper_BASE
84 // if necessary for better performance we could change this into a map and cache the
85 // indices -> Axis, currently we create a new Axis object
87 uno::Reference
< uno::XComponentContext
> mxContext
;
88 vecAxesIndices mCoordinates
;
89 uno::Reference
< excel::XChart
> mxChart
;
91 AxisIndexWrapper( const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< excel::XChart
>& xChart
) : mxContext( xContext
), mxChart( xChart
)
95 ScVbaChart
* pChart
= static_cast< ScVbaChart
* >( mxChart
.get() );
97 sal_Bool bBool
= false;
98 uno::Reference
< beans::XPropertySet
> xDiagramPropertySet( pChart
->xDiagramPropertySet() );
99 if ( ( xDiagramPropertySet
->getPropertyValue("HasXAxis") >>= bBool
) && bBool
)
100 mCoordinates
.push_back( AxesCoordinate( xlPrimary
, xlCategory
) );
101 if ( ( xDiagramPropertySet
->getPropertyValue("HasYAxis") >>= bBool
) && bBool
)
102 mCoordinates
.push_back( AxesCoordinate( xlPrimary
, xlSeriesAxis
) );
104 if ( pChart
->is3D() )
105 mCoordinates
.push_back( AxesCoordinate( xlPrimary
, xlValue
) );
108 if ( ( xDiagramPropertySet
->getPropertyValue("HasSecondaryXAxis") >>= bBool
) && bBool
)
109 mCoordinates
.push_back( AxesCoordinate( xlSecondary
, xlCategory
) );
110 if ( ( xDiagramPropertySet
->getPropertyValue("HasSecondaryYAxis") >>= bBool
) && bBool
)
111 mCoordinates
.push_back( AxesCoordinate( xlSecondary
, xlSeriesAxis
) );
115 virtual ::sal_Int32 SAL_CALL
getCount() throw (uno::RuntimeException
) { return mCoordinates
.size(); }
116 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw (lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, ::uno::RuntimeException
)
118 AxesCoordinate dIndexes
= mCoordinates
[ Index
];
119 return uno::makeAny( ScVbaAxes::createAxis( mxChart
, mxContext
, dIndexes
.second
, dIndexes
.first
) );
122 virtual uno::Type SAL_CALL
getElementType() throw (uno::RuntimeException
)
124 return excel::XAxis::static_type(0);
126 virtual ::sal_Bool SAL_CALL
hasElements( ) throw (uno::RuntimeException
)
128 return ( mCoordinates
.size() > 0 );
132 uno::Reference
< container::XIndexAccess
> createIndexWrapper( const uno::Reference
< excel::XChart
>& xChart
, const uno::Reference
< uno::XComponentContext
>& xContext
)
134 return new AxisIndexWrapper( xContext
, xChart
);
137 // #FIXME The collection semantics will never work as this object is not yet initialised correctly
138 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
)
143 ScVbaAxes::getElementType() throw (css::uno::RuntimeException
)
145 return excel::XAxes::static_type(0);
148 uno::Reference
< container::XEnumeration
> SAL_CALL
149 ScVbaAxes::createEnumeration() throw (css::uno::RuntimeException
)
151 return new EnumWrapper( m_xIndexAccess
);
155 ScVbaAxes::Item( const css::uno::Any
& _nType
, const css::uno::Any
& _oAxisGroup
) throw (css::uno::RuntimeException
)
157 // #TODO map the possible index combinations to a container::XIndexAccess wrapper impl
158 // using a vector of valid std::pair maybe?
159 // bodgy helperapi port bits
160 sal_Int32 nAxisGroup
= xlPrimary
;
161 sal_Int32 nType
= -1;
162 if ( !_nType
.hasValue() || ( ( _nType
>>= nType
) == false ) )
163 throw uno::RuntimeException("Axes::Item Failed to extract type", uno::Reference
< uno::XInterface
>() );
165 if ( _oAxisGroup
.hasValue() )
166 _oAxisGroup
>>= nAxisGroup
;
168 return uno::makeAny( createAxis( moChartParent
, mxContext
, nType
, nAxisGroup
) );
172 ScVbaAxes::createCollectionObject(const css::uno::Any
& aSource
)
174 return aSource
; // pass through ( it's already an XAxis object
178 ScVbaAxes::getServiceImplName()
180 return OUString("ScVbaAxes");
183 uno::Sequence
< OUString
>
184 ScVbaAxes::getServiceNames()
186 static uno::Sequence
< OUString
> aServiceNames
;
187 if ( aServiceNames
.getLength() == 0 )
189 aServiceNames
.realloc( 1 );
190 aServiceNames
[ 0 ] = "ooo.vba.excel.Axes";
192 return aServiceNames
;
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */