tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / vba / vbaaxes.cxx
blobd5396cf16b0a8c5f99a3b84311da2756d2667e7c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <basic/sberrors.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
25 #include <cppuhelper/implbase.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>
32 using namespace ::com::sun::star;
33 using namespace ::ooo::vba;
34 using namespace ::ooo::vba::excel::XlAxisType;
35 using namespace ::ooo::vba::excel::XlAxisGroup;
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
41 typedef ::std::pair<sal_Int32, sal_Int32 > AxesCoordinate; // type and group combination
43 namespace {
45 class EnumWrapper : public EnumerationHelper_BASE
47 uno::Reference<container::XIndexAccess > m_xIndexAccess;
48 sal_Int32 nIndex;
49 public:
50 explicit EnumWrapper( uno::Reference< container::XIndexAccess > xIndexAccess ) : m_xIndexAccess(std::move( xIndexAccess )), nIndex( 0 ) {}
51 virtual sal_Bool SAL_CALL hasMoreElements( ) override
53 return ( nIndex < m_xIndexAccess->getCount() );
56 virtual uno::Any SAL_CALL nextElement( ) override
58 if ( nIndex < m_xIndexAccess->getCount() )
59 return m_xIndexAccess->getByIndex( nIndex++ );
60 throw container::NoSuchElementException();
66 uno::Reference< excel::XAxis >
67 ScVbaAxes::createAxis( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext, sal_Int32 nType, sal_Int32 nAxisGroup )
69 ScVbaChart* pChart = static_cast< ScVbaChart* >( xChart.get() );
70 if ( !pChart )
71 throw uno::RuntimeException(u"Object failure, can't access chart implementation"_ustr );
73 uno::Reference< beans::XPropertySet > xAxisPropertySet;
74 if ((nType == xlCategory) || (nType == xlSeriesAxis) || (nType == xlValue))
76 if ((nAxisGroup != xlPrimary) && (nAxisGroup != xlSecondary))
77 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
78 xAxisPropertySet.set( pChart->getAxisPropertySet(nType, nAxisGroup), uno::UNO_SET_THROW );
80 else
81 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
82 return new ScVbaAxis( pChart, xContext, xAxisPropertySet, nType, nAxisGroup);
85 namespace {
87 class AxisIndexWrapper : public ::cppu::WeakImplHelper< container::XIndexAccess >
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 rtl::Reference< ScVbaChart > mxChart;
95 public:
96 AxisIndexWrapper( uno::Reference< uno::XComponentContext > xContext, rtl::Reference< ScVbaChart > xChart ) : mxContext(std::move( xContext )), mxChart(std::move( xChart ))
98 if ( !mxChart.is() )
99 return;
101 ScVbaChart* pChart = mxChart.get();
102 // primary
103 bool bBool = false;
104 uno::Reference< beans::XPropertySet > xDiagramPropertySet( pChart->xDiagramPropertySet() );
105 if ( ( xDiagramPropertySet->getPropertyValue(u"HasXAxis"_ustr) >>= bBool ) && bBool )
106 mCoordinates.emplace_back( xlPrimary, xlCategory );
107 if ( ( xDiagramPropertySet->getPropertyValue(u"HasYAxis"_ustr) >>= bBool ) && bBool )
108 mCoordinates.emplace_back( xlPrimary, xlSeriesAxis );
110 if ( pChart->is3D() )
111 mCoordinates.emplace_back( xlPrimary, xlValue );
113 // secondary
114 if ( ( xDiagramPropertySet->getPropertyValue(u"HasSecondaryXAxis"_ustr) >>= bBool ) && bBool )
115 mCoordinates.emplace_back( xlSecondary, xlCategory );
116 if ( ( xDiagramPropertySet->getPropertyValue(u"HasSecondaryYAxis"_ustr) >>= bBool ) && bBool )
117 mCoordinates.emplace_back( xlSecondary, xlSeriesAxis );
120 virtual ::sal_Int32 SAL_CALL getCount() override { return mCoordinates.size(); }
121 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
125 AxesCoordinate dIndexes = mCoordinates[ Index ];
126 return uno::Any( ScVbaAxes::createAxis( mxChart, mxContext, dIndexes.second, dIndexes.first ) );
128 catch (const css::script::BasicErrorException&)
130 css::uno::Any anyEx = cppu::getCaughtException();
131 throw css::lang::WrappedTargetException(
132 u"Error Getting Index!"_ustr,
133 getXWeak(),
134 anyEx );
137 // XElementAccess
138 virtual uno::Type SAL_CALL getElementType() override
140 return cppu::UnoType<excel::XAxis>::get();
142 virtual sal_Bool SAL_CALL hasElements( ) override
144 return ( !mCoordinates.empty() );
148 uno::Reference< container::XIndexAccess > createIndexWrapper( const rtl::Reference< ScVbaChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext )
150 return new AxisIndexWrapper( xContext, xChart );
155 // #FIXME The collection semantics will never work as this object is not yet initialised correctly
156 ScVbaAxes::ScVbaAxes( const uno::Reference< XHelperInterface >& xParent,const uno::Reference< uno::XComponentContext > & xContext, const rtl::Reference< ScVbaChart >& xChart ) : ScVbaAxes_BASE( xParent, xContext, createIndexWrapper( xChart, xContext )), moChartParent( xChart )
160 uno::Type SAL_CALL
161 ScVbaAxes::getElementType()
163 return cppu::UnoType<excel::XAxes>::get();
166 uno::Reference< container::XEnumeration > SAL_CALL
167 ScVbaAxes::createEnumeration()
169 return new EnumWrapper( m_xIndexAccess );
172 uno::Any SAL_CALL
173 ScVbaAxes::Item( const css::uno::Any& _nType, const css::uno::Any& _oAxisGroup)
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(u"Axes::Item Failed to extract type"_ustr );
183 if ( _oAxisGroup.hasValue() )
184 _oAxisGroup >>= nAxisGroup ;
186 return uno::Any( createAxis( moChartParent, mxContext, nType, nAxisGroup ) );
189 uno::Any
190 ScVbaAxes::createCollectionObject(const css::uno::Any& aSource)
192 return aSource; // pass through ( it's already an XAxis object
195 OUString
196 ScVbaAxes::getServiceImplName()
198 return u"ScVbaAxes"_ustr;
201 uno::Sequence< OUString >
202 ScVbaAxes::getServiceNames()
204 static uno::Sequence< OUString > const aServiceNames
206 u"ooo.vba.excel.Axes"_ustr
208 return aServiceNames;
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */