LibreOffice Module sc (master) 1
vbaaxes.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 "vbaaxes.hxx"
21#include "vbaaxis.hxx"
22#include "vbachart.hxx"
23#include <basic/sberrors.hxx>
26#include <com/sun/star/script/BasicErrorException.hpp>
27#include <ooo/vba/excel/XlAxisType.hpp>
28#include <ooo/vba/excel/XlAxisGroup.hpp>
29#include <ooo/vba/excel/XAxis.hpp>
30
31using namespace ::com::sun::star;
32using namespace ::ooo::vba;
33using namespace ::ooo::vba::excel::XlAxisType;
34using namespace ::ooo::vba::excel::XlAxisGroup;
35
36// each 'Item' in the Axes collection is indexed via 2 indexes, group and type.
37// We need to 'flatten' this into a single index in order to be able to wrap
38// iteration over the set of Axis(s) in a XIndexAccess implementation
39
40typedef ::std::pair<sal_Int32, sal_Int32 > AxesCoordinate; // type and group combination
41
42namespace {
43
44class EnumWrapper : public EnumerationHelper_BASE
45{
46 uno::Reference<container::XIndexAccess > m_xIndexAccess;
47 sal_Int32 nIndex;
48public:
49 explicit EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
50 virtual sal_Bool SAL_CALL hasMoreElements( ) override
51 {
52 return ( nIndex < m_xIndexAccess->getCount() );
53 }
54
55 virtual uno::Any SAL_CALL nextElement( ) override
56 {
57 if ( nIndex < m_xIndexAccess->getCount() )
58 return m_xIndexAccess->getByIndex( nIndex++ );
59 throw container::NoSuchElementException();
60 }
61};
62
63}
64
65uno::Reference< excel::XAxis >
66ScVbaAxes::createAxis( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext, sal_Int32 nType, sal_Int32 nAxisGroup )
67{
68 ScVbaChart* pChart = static_cast< ScVbaChart* >( xChart.get() );
69 if ( !pChart )
70 throw uno::RuntimeException("Object failure, can't access chart implementation" );
71
72 uno::Reference< beans::XPropertySet > xAxisPropertySet;
73 if ((nType == xlCategory) || (nType == xlSeriesAxis) || (nType == xlValue))
74 {
75 if ((nAxisGroup != xlPrimary) && (nAxisGroup != xlSecondary))
76 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
77 xAxisPropertySet.set( pChart->getAxisPropertySet(nType, nAxisGroup), uno::UNO_SET_THROW );
78 }
79 else
80 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
81 uno::Reference< XHelperInterface > xParent( xChart, uno::UNO_QUERY_THROW );
82 return new ScVbaAxis( xParent, xContext, xAxisPropertySet, nType, nAxisGroup);
83}
84
85namespace {
86
87class AxisIndexWrapper : public ::cppu::WeakImplHelper< container::XIndexAccess >
88{
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
91 // on each getByIndex
92 uno::Reference< uno::XComponentContext > mxContext;
93 std::vector< AxesCoordinate > mCoordinates;
94 uno::Reference< excel::XChart > mxChart;
95public:
96 AxisIndexWrapper( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XChart >& xChart ) : mxContext( xContext ), mxChart( xChart )
97 {
98 if ( !mxChart.is() )
99 return;
100
101 ScVbaChart* pChart = static_cast< ScVbaChart* >( mxChart.get() );
102 // primary
103 bool bBool = false;
104 uno::Reference< beans::XPropertySet > xDiagramPropertySet( pChart->xDiagramPropertySet() );
105 if ( ( xDiagramPropertySet->getPropertyValue("HasXAxis") >>= bBool ) && bBool )
106 mCoordinates.emplace_back( xlPrimary, xlCategory );
107 if ( ( xDiagramPropertySet->getPropertyValue("HasYAxis") >>= bBool ) && bBool )
108 mCoordinates.emplace_back( xlPrimary, xlSeriesAxis );
109
110 if ( pChart->is3D() )
111 mCoordinates.emplace_back( xlPrimary, xlValue );
112
113 // secondary
114 if ( ( xDiagramPropertySet->getPropertyValue("HasSecondaryXAxis") >>= bBool ) && bBool )
115 mCoordinates.emplace_back( xlSecondary, xlCategory );
116 if ( ( xDiagramPropertySet->getPropertyValue("HasSecondaryYAxis") >>= bBool ) && bBool )
117 mCoordinates.emplace_back( xlSecondary, xlSeriesAxis );
118
119 }
120 virtual ::sal_Int32 SAL_CALL getCount() override { return mCoordinates.size(); }
121 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
122 {
123 try
124 {
125 AxesCoordinate dIndexes = mCoordinates[ Index ];
126 return uno::Any( ScVbaAxes::createAxis( mxChart, mxContext, dIndexes.second, dIndexes.first ) );
127 }
128 catch (const css::script::BasicErrorException&)
129 {
130 css::uno::Any anyEx = cppu::getCaughtException();
131 throw css::lang::WrappedTargetException(
132 "Error Getting Index!",
133 static_cast < OWeakObject * > ( this ),
134 anyEx );
135 }
136 }
137 // XElementAccess
138 virtual uno::Type SAL_CALL getElementType() override
139 {
141 }
142 virtual sal_Bool SAL_CALL hasElements( ) override
143 {
144 return ( !mCoordinates.empty() );
145 }
146};
147
148uno::Reference< container::XIndexAccess > createIndexWrapper( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext )
149{
150 return new AxisIndexWrapper( xContext, xChart );
151}
152
153}
154
155// #FIXME The collection semantics will never work as this object is not yet initialised correctly
156ScVbaAxes::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 )
157{
158}
159
160uno::Type SAL_CALL
162{
164}
165
166uno::Reference< container::XEnumeration > SAL_CALL
168{
169 return new EnumWrapper( m_xIndexAccess );
170}
171
172uno::Any SAL_CALL
173ScVbaAxes::Item( const css::uno::Any& _nType, const css::uno::Any& _oAxisGroup)
174{
175 // #TODO map the possible index combinations to a container::XIndexAccess wrapper impl
176 // using a vector of valid std::pair maybe?
177 // body helper api port bits
178 sal_Int32 nAxisGroup = xlPrimary;
179 sal_Int32 nType = -1;
180 if ( !_nType.hasValue() || !( _nType >>= nType ) )
181 throw uno::RuntimeException("Axes::Item Failed to extract type" );
182
183 if ( _oAxisGroup.hasValue() )
184 _oAxisGroup >>= nAxisGroup ;
185
186 return uno::Any( createAxis( moChartParent, mxContext, nType, nAxisGroup ) );
187}
188
190ScVbaAxes::createCollectionObject(const css::uno::Any& aSource)
191{
192 return aSource; // pass through ( it's already an XAxis object
193}
194
195OUString
197{
198 return "ScVbaAxes";
199}
200
201uno::Sequence< OUString >
203{
204 static uno::Sequence< OUString > const aServiceNames
205 {
206 "ooo.vba.excel.Axes"
207 };
208 return aServiceNames;
209}
210
211/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbaaxes.cxx:202
css::uno::Reference< ov::excel::XChart > moChartParent
Definition: vbaaxes.hxx:30
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbaaxes.cxx:167
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbaaxes.cxx:161
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &aIndex, const css::uno::Any &aIndex2) override
Definition: vbaaxes.cxx:173
virtual OUString getServiceImplName() override
Definition: vbaaxes.cxx:196
virtual css::uno::Any createCollectionObject(const css::uno::Any &) override
Definition: vbaaxes.cxx:190
static css::uno::Reference< ov::excel::XAxis > createAxis(const css::uno::Reference< ov::excel::XChart > &xChart, const css::uno::Reference< css::uno::XComponentContext > &xContext, sal_Int32 nType, sal_Int32 nAxisGroup)
Definition: vbaaxes.cxx:66
ScVbaAxes(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< ov::excel::XChart > &xChart)
Definition: vbaaxes.cxx:156
const css::uno::Reference< css::beans::XPropertySet > & xDiagramPropertySet() const
Definition: vbachart.hxx:73
css::uno::Reference< css::beans::XPropertySet > getAxisPropertySet(sal_Int32 _nAxisType, sal_Int32 _nAxisGroup)
Definition: vbachart.cxx:1009
bool is3D()
Definition: vbachart.cxx:878
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
css::uno::Type const & get()
uno::Reference< uno::XComponentContext > mxContext
Sequence< OUString > aServiceNames
sal_Int32 nIndex
Any SAL_CALL getCaughtException()
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
#define ERRCODE_BASIC_METHOD_FAILED
unsigned char sal_Bool
::std::pair< sal_Int32, sal_Int32 > AxesCoordinate
Definition: vbaaxes.cxx:40
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE