Line data Source code
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 : #include "vbachart.hxx"
20 : #include <com/sun/star/beans/XPropertySet.hpp>
21 : #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
22 : #include <com/sun/star/table/XTableChartsSupplier.hpp>
23 : #include <com/sun/star/table/XTableChart.hpp>
24 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
25 : #include <ooo/vba/excel/XlChartType.hpp>
26 :
27 :
28 : #include "vbachartobjects.hxx"
29 : #include "vbachartobject.hxx"
30 : #include "vbaglobals.hxx"
31 : #include "cellsuno.hxx"
32 : #include <vector>
33 : #include <basic/sberrors.hxx>
34 :
35 : using namespace ::com::sun::star;
36 : using namespace ::ooo::vba;
37 :
38 :
39 0 : class ChartObjectEnumerationImpl : public EnumerationHelperImpl
40 : {
41 : uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier;
42 :
43 : public:
44 :
45 0 : ChartObjectEnumerationImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier, const uno::Reference< XHelperInterface >& _xParent ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( _xParent, xContext, xEnumeration ), xDrawPageSupplier( _xDrawPageSupplier ) {}
46 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
47 : {
48 0 : uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
49 : // parent Object is sheet
50 0 : return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) ) );
51 : }
52 : };
53 :
54 :
55 0 : ScVbaChartObjects::ScVbaChartObjects( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, const css::uno::Reference< css::table::XTableCharts >& _xTableCharts, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier ) : ChartObjects_BASE(_xParent, _xContext, css::uno::Reference< css::container::XIndexAccess >( _xTableCharts, css::uno::UNO_QUERY ) ), xTableCharts( _xTableCharts ) , xDrawPageSupplier( _xDrawPageSupplier )
56 : {
57 :
58 0 : }
59 :
60 : void
61 0 : ScVbaChartObjects::removeByName(const rtl::OUString& _sChartName)
62 : {
63 0 : xTableCharts->removeByName( _sChartName );
64 0 : }
65 :
66 : uno::Sequence< rtl::OUString >
67 0 : ScVbaChartObjects::getChartObjectNames() throw( css::script::BasicErrorException )
68 : {
69 0 : uno::Sequence< rtl::OUString > sChartNames;
70 : try
71 : {
72 : // c++ hackery
73 0 : uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW );
74 0 : ScCellRangesBase* pUno= dynamic_cast< ScCellRangesBase* >( xIf.get() );
75 0 : ScDocShell* pDocShell = NULL;
76 0 : if ( !pUno )
77 0 : throw uno::RuntimeException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Failed to obtain the impl class from the drawpage")), uno::Reference< uno::XInterface >() );
78 0 : pDocShell = pUno->GetDocShell();
79 0 : if ( !pDocShell )
80 0 : throw uno::RuntimeException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Failed to obtain the docshell implclass")), uno::Reference< uno::XInterface >() );
81 :
82 0 : uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument( pDocShell->GetModel(), uno::UNO_QUERY_THROW );
83 0 : uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSpreadsheetDocument->getSheets();
84 0 : std::vector< rtl::OUString > aChartNamesVector;
85 :
86 0 : uno::Sequence< rtl::OUString > sSheetNames = xSpreadsheets->getElementNames();
87 0 : sal_Int32 nItems = sSheetNames.getLength();
88 0 : for (sal_Int32 i = 0; i < nItems; i++)
89 : {
90 0 : uno::Reference< table::XTableChartsSupplier > xLocTableChartsSupplier( xSpreadsheets->getByName(sSheetNames[i]), uno::UNO_QUERY_THROW );
91 0 : uno::Sequence< rtl::OUString > scurchartnames = xLocTableChartsSupplier->getCharts()->getElementNames();
92 0 : sal_Int32 nChartNames = scurchartnames.getLength();
93 0 : for (sal_Int32 n = 0; n < nChartNames; n++ )
94 0 : aChartNamesVector.push_back(scurchartnames[n]);
95 0 : }
96 0 : sChartNames.realloc( aChartNamesVector.size() );
97 0 : std::vector< rtl::OUString > ::const_iterator it = aChartNamesVector.begin();
98 0 : std::vector< rtl::OUString > ::const_iterator it_end = aChartNamesVector.end();
99 0 : for ( sal_Int32 index = 0 ; it != it_end; ++it, ++index )
100 0 : sChartNames[index] = *it;
101 : }
102 0 : catch (uno::Exception& )
103 : {
104 0 : throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
105 : }
106 0 : return sChartNames;
107 : }
108 :
109 : // XChartObjects
110 : uno::Any SAL_CALL
111 0 : ScVbaChartObjects::Add( double _nX, double _nY, double _nWidth, double _nHeight ) throw (script::BasicErrorException)
112 : {
113 : try
114 : {
115 0 : uno::Sequence< table::CellRangeAddress > aCellRangeAddress( 1 );
116 0 : awt::Rectangle aRectangle;
117 0 : aRectangle.X = Millimeter::getInHundredthsOfOneMillimeter(_nX);
118 0 : aRectangle.Y = Millimeter::getInHundredthsOfOneMillimeter(_nY);
119 0 : aRectangle.Width = Millimeter::getInHundredthsOfOneMillimeter(_nWidth);
120 0 : aRectangle.Height = Millimeter::getInHundredthsOfOneMillimeter(_nHeight);
121 : // Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem
122 0 : rtl::OUString sPersistChartName = ContainerUtilities::getUniqueName( getChartObjectNames(), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Chart " ) ) , rtl::OUString(), 1);
123 0 : xTableCharts->addNewByName(sPersistChartName, aRectangle, aCellRangeAddress, true, false );
124 0 : uno::Reference< excel::XChartObject > xChartObject( getItemByStringIndex( sPersistChartName ), uno::UNO_QUERY_THROW );
125 0 : xChartObject->getChart()->setChartType(excel::XlChartType::xlColumnClustered);
126 0 : return uno::makeAny( xChartObject );
127 : }
128 0 : catch (const uno::Exception& ex)
129 : {
130 : OSL_TRACE("AddItem caught exception ->%s", rtl::OUStringToOString( ex.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
131 : }
132 0 : return aNULL();
133 : }
134 0 : void SAL_CALL ScVbaChartObjects::Delete( ) throw (script::BasicErrorException)
135 : {
136 0 : uno::Sequence< rtl::OUString > sChartNames = xTableCharts->getElementNames();
137 0 : sal_Int32 ncount = sChartNames.getLength();
138 0 : for (sal_Int32 i = 0; i < ncount ; i++)
139 0 : removeByName(sChartNames[i]);
140 0 : }
141 :
142 : // XEnumerationAccess
143 :
144 : uno::Reference< container::XEnumeration >
145 0 : ScVbaChartObjects::createEnumeration() throw (uno::RuntimeException)
146 : {
147 0 : css::uno::Reference< container::XEnumerationAccess > xEnumAccess( xTableCharts, uno::UNO_QUERY_THROW );
148 0 : return new ChartObjectEnumerationImpl( mxContext, xEnumAccess->createEnumeration(), xDrawPageSupplier, getParent() /* sheet */);
149 : }
150 :
151 : // XElementAccess
152 :
153 : uno::Type
154 0 : ScVbaChartObjects::getElementType() throw (uno::RuntimeException)
155 : {
156 0 : return excel::XChartObject::static_type(0);
157 : }
158 :
159 : // ScVbaCollectionBaseImpl
160 : uno::Any
161 0 : ScVbaChartObjects::createCollectionObject( const css::uno::Any& aSource )
162 : {
163 0 : uno::Reference< table::XTableChart > xTableChart( aSource, uno::UNO_QUERY_THROW );
164 : // correct parent object is sheet
165 0 : return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( getParent(), mxContext, xTableChart, xDrawPageSupplier ) ) );
166 : }
167 :
168 : rtl::OUString
169 0 : ScVbaChartObjects::getServiceImplName()
170 : {
171 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObjects"));
172 : }
173 :
174 : css::uno::Sequence<rtl::OUString>
175 0 : ScVbaChartObjects::getServiceNames()
176 : {
177 0 : static uno::Sequence< rtl::OUString > sNames;
178 0 : if ( sNames.getLength() == 0 )
179 : {
180 0 : sNames.realloc( 1 );
181 0 : sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ChartObjects") );
182 : }
183 0 : return sNames;
184 : }
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|