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