Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / chart2 / workbench / addin / sampleaddin.cxx
blobd2cce93008f99c2235f335b64704c22055625008
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 .
19 #include "sampleaddin.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <cppuhelper/factory.hxx>
23 #include <osl/diagnose.h>
25 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
26 #include <com/sun/star/drawing/XDrawPage.hpp>
27 #include <com/sun/star/chart/XChartDataArray.hpp>
28 #include <com/sun/star/text/XTextRange.hpp>
29 #include <com/sun/star/chart/X3DDisplay.hpp>
31 using namespace com::sun::star;
33 // code for creating instances of SampleAddIn
35 extern "C" {
37 sal_Bool SAL_CALL component_writeInfo(
38 void * /*pServiceManager*/, registry::XRegistryKey * pRegistryKey )
40 if( pRegistryKey )
42 try
44 OUString aImpl( "/" );
45 aImpl += SampleAddIn::getImplementationName_Static();
46 aImpl += "/UNO/SERVICES";
48 uno::Reference< registry::XRegistryKey> xNewKey(
49 reinterpret_cast<registry::XRegistryKey*>( pRegistryKey )->createKey( aImpl ) );
51 uno::Sequence< OUString > aSequ = SampleAddIn::getSupportedServiceNames_Static();
52 const OUString * pArray = aSequ.getConstArray();
53 for( sal_Int32 i = 0; i < aSequ.getLength(); i++ )
54 xNewKey->createKey( pArray[i] );
56 return sal_True;
58 catch( const registry::InvalidRegistryException& )
60 OSL_FAIL( "### InvalidRegistryException!" );
63 return sal_False;
66 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
67 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
69 void* pRet = 0;
71 if ( pServiceManager &&
72 OUString::createFromAscii( pImplName ) == SampleAddIn::getImplementationName_Static() )
74 uno::Reference< lang::XSingleServiceFactory> xFactory( cppu::createSingleFactory(
75 reinterpret_cast<lang::XMultiServiceFactory*>( pServiceManager ),
76 SampleAddIn::getImplementationName_Static(),
77 SampleAddIn_CreateInstance,
78 SampleAddIn::getSupportedServiceNames_Static() ) );
80 if( xFactory.is())
82 xFactory->acquire();
83 pRet = xFactory.get();
87 return pRet;
90 } // extern C
92 // class SampleAddIn
94 SampleAddIn::SampleAddIn()
99 SampleAddIn::~SampleAddIn()
102 // this functionality should be provided by the chart API some day
103 sal_Bool SampleAddIn::getLogicalPosition( uno::Reference< drawing::XShape >& xAxis,
104 double fValue,
105 sal_Bool bVertical,
106 awt::Point& aOutPosition )
108 sal_Bool bRet = sal_False;
110 if( xAxis.is())
112 awt::Size aSize = xAxis->getSize();
113 sal_Int32 nLength = bVertical? aSize.Height: aSize.Width;
115 uno::Reference< beans::XPropertySet > xProp( xAxis, uno::UNO_QUERY );
116 if( xProp.is())
120 double fMin(0.0), fMax(0.0);
121 uno::Any aAny = xProp->getPropertyValue( "Min" );
122 aAny >>= fMin;
123 aAny = xProp->getPropertyValue( "Max" );
124 aAny >>= fMax;
126 double fRange = fMax - fMin;
127 if( fMin <= fValue && fValue <= fMax &&
128 fRange != 0.0 )
130 double fPercentage = (fValue - fMin) / fRange;
131 awt::Point aPos = xAxis->getPosition();
133 if( bVertical )
135 aOutPosition.X = aPos.X;
136 aOutPosition.Y = static_cast<sal_Int32>(aPos.Y + nLength * (1.0 - fPercentage)); // y scale goes from top to bottom
138 else
140 aOutPosition.X = static_cast<sal_Int32>(aPos.X + nLength * fPercentage);
141 aOutPosition.Y = aPos.Y;
143 bRet = sal_True;
146 catch( const beans::UnknownPropertyException& )
148 // the shape xAxis was no chart axis
153 return bRet;
156 OUString SampleAddIn::getImplementationName_Static()
158 return "SampleAddIn";
161 uno::Sequence< OUString > SampleAddIn::getSupportedServiceNames_Static()
163 uno::Sequence< OUString > aSeq( 4 );
165 aSeq[ 0 ] = "com.sun.star.chart.ChartAxisXSupplier";
166 aSeq[ 1 ] = "com.sun.star.chart.ChartAxisYSupplier";
167 aSeq[ 2 ] = "com.sun.star.chart.Diagram";
168 aSeq[ 3 ] = "com.sun.star.chart.SampleAddIn";
170 return aSeq;
173 uno::Reference< uno::XInterface > SAL_CALL SampleAddIn_CreateInstance(
174 const uno::Reference< lang::XMultiServiceFactory >& )
176 uno::Reference< uno::XInterface > xInst = (cppu::OWeakObject*)new SampleAddIn();
178 return xInst;
181 // implementation of interface methods
183 // XInitialization
184 void SAL_CALL SampleAddIn::initialize( const uno::Sequence< uno::Any >& aArguments )
185 throw( uno::Exception, uno::RuntimeException )
187 // first argument should be the XChartDocument
188 OSL_ENSURE( aArguments.getLength() > 0, "Please initialize Chart AddIn with ChartDocument!" );
190 if( aArguments.getLength())
192 aArguments[ 0 ] >>= mxChartDoc;
193 OSL_ENSURE( mxChartDoc.is(), "First argument in initialization is not an XChartDocument!" );
195 // set XY chart as base type to be drawn
196 uno::Reference< beans::XPropertySet > xDocProp( mxChartDoc, uno::UNO_QUERY );
197 if( xDocProp.is())
199 uno::Any aBaseType;
200 aBaseType <<= "com.sun.star.chart.XYDiagram";
203 xDocProp->setPropertyValue( "BaseDiagram" , aBaseType );
205 catch( ... )
209 // change background of plot area to light blue
210 uno::Reference< chart::X3DDisplay > xWallSupplier( mxChartDoc->getDiagram(), uno::UNO_QUERY );
211 if( xWallSupplier.is())
213 uno::Reference< beans::XPropertySet > xDiaProp( xWallSupplier->getWall(), uno::UNO_QUERY );
214 uno::Reference< beans::XPropertySet > xLegendProp( mxChartDoc->getLegend(), uno::UNO_QUERY );
215 if( xDiaProp.is() &&
216 xLegendProp.is())
218 uno::Any aAny;
219 aAny <<= (sal_Int32)( 0xe0e0f0 );
220 xDiaProp->setPropertyValue( "FillColor" , aAny );
221 xLegendProp->setPropertyValue( "FillColor" , aAny );
227 // XRefreshable
228 /********************************************************************************
230 * The method refresh is the most important method - here all objects that
231 * are necessary for the chart are created
233 * in the first implementation you will have to insert everything in this
234 * routine - all old objects are deleted beforehand
236 ********************************************************************************/
237 void SAL_CALL SampleAddIn::refresh() throw( uno::RuntimeException )
239 if( ! mxChartDoc.is())
240 return;
242 // first of all get the draw page
243 uno::Reference< drawing::XDrawPageSupplier > xPageSupp( mxChartDoc, uno::UNO_QUERY );
244 uno::Reference< lang::XMultiServiceFactory > xFactory( mxChartDoc, uno::UNO_QUERY );
245 if( xPageSupp.is() &&
246 xFactory.is() )
248 uno::Reference< drawing::XDrawPage > xPage = xPageSupp->getDrawPage();
249 if( xPage.is())
251 // now we have the page to insert objects
253 // add a horizontal line at the middle value of the first series
255 // get the logical position from the coordinate
256 // get x- and y-axis
257 uno::Reference< drawing::XShape > xYAxisShape( getYAxis(), uno::UNO_QUERY );
258 uno::Reference< drawing::XShape > xXAxisShape( getXAxis(), uno::UNO_QUERY );
260 if( xXAxisShape.is() &&
261 xYAxisShape.is() )
263 // create line first time
264 if( ! mxMyRedLine.is())
266 mxMyRedLine.set(
267 xFactory->createInstance( "com.sun.star.drawing.LineShape" ),
268 uno::UNO_QUERY );
269 xPage->add( mxMyRedLine );
271 // make line red and thick
272 uno::Reference< beans::XPropertySet > xShapeProp( mxMyRedLine, uno::UNO_QUERY );
273 if( xShapeProp.is())
275 uno::Any aColor, aWidth;
276 aColor <<= (sal_Int32)(0xe01010);
277 aWidth <<= (sal_Int32)(50); // 0.5 mm
280 xShapeProp->setPropertyValue( "LineColor" , aColor );
281 xShapeProp->setPropertyValue( "LineWidth" , aWidth );
283 catch( ... )
287 // create text object first time
288 if( ! mxMyText.is())
290 mxMyText.set(
291 xFactory->createInstance( "com.sun.star.drawing.TextShape" ),
292 uno::UNO_QUERY );
293 xPage->add( mxMyText );
295 // change text
296 OUString aText( "Little Example" );
297 uno::Reference< beans::XPropertySet > xTextProp( mxMyText, uno::UNO_QUERY );
298 if( xTextProp.is())
300 uno::Any aTrueAny;
301 aTrueAny <<= (sal_Bool)(sal_True);
304 xTextProp->setPropertyValue( "TextAutoGrowWidth" , aTrueAny );
306 catch( ... )
310 uno::Reference< text::XTextRange > xTextRange( mxMyText, uno::UNO_QUERY );
311 if( xTextRange.is())
313 xTextRange->setString( aText );
317 // position line and text
319 // get the array. Note: the first dimension is the length
320 // of each series and the second one is the number of series
321 // this should be changed in the future
322 uno::Sequence< uno::Sequence< double > > aData;
323 uno::Reference< chart::XChartData > xData = mxChartDoc->getData();
324 uno::Reference< chart::XChartDataArray > xDataArray( xData, uno::UNO_QUERY );
325 if( xDataArray.is())
326 aData = xDataArray->getData();
328 // get row count == length of each series
329 sal_Int32 nSize = aData.getLength();
330 sal_Int32 nMiddle = nSize / 2;
331 // get value for first series
332 double fMiddleVal = xData->getNotANumber(); // set to NaN
333 if( aData[ nMiddle ].getLength()) // we have at least one series
334 fMiddleVal = aData[ nMiddle ][ 0 ];
336 awt::Point aPos;
337 getLogicalPosition( xYAxisShape, fMiddleVal, sal_True, aPos );
338 awt::Size aSize = xXAxisShape->getSize();
340 if( mxMyRedLine.is())
342 awt::Point aEnd = aPos;
343 aEnd.X += aSize.Width;
345 uno::Sequence< uno::Sequence< awt::Point > > aPtSeq( 1 );
346 aPtSeq[ 0 ].realloc( 2 );
347 aPtSeq[ 0 ][ 0 ] = aPos;
348 aPtSeq[ 0 ][ 1 ] = aEnd;
350 uno::Reference< beans::XPropertySet > xShapeProp( mxMyRedLine, uno::UNO_QUERY );
351 if( xShapeProp.is())
353 xShapeProp->setPropertyValue( "PolyPolygon" , Any(aPtSeq) );
356 if( mxMyText.is())
358 // put the text centered below the red line
359 aPos.X += ( aSize.Width - mxMyRedLine->getPosition().X ) / 2;
360 aPos.Y += 1000;
361 aPos.Y += static_cast<sal_Int32>(0.1 * xYAxisShape->getSize().Height);
362 mxMyText->setPosition( aPos );
369 void SAL_CALL SampleAddIn::addRefreshListener( const uno::Reference< util::XRefreshListener >& )
370 throw( uno::RuntimeException )
372 // not implemented - this is not necessary
373 // (this method exists just because the interface requires it)
376 void SAL_CALL SampleAddIn::removeRefreshListener( const uno::Reference< util::XRefreshListener >& )
377 throw( uno::RuntimeException )
379 // not implemented - this is not necessary
380 // (this method exists just because the interface requires it)
383 // XDiagram
384 OUString SAL_CALL SampleAddIn::getDiagramType() throw( uno::RuntimeException )
386 return "com.sun.star.chart.SampleDiagram";
389 // the following methods just delegate to the "parent diagram" (which in the future might no longer exist)
391 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getDataRowProperties( sal_Int32 nRow )
392 throw( lang::IndexOutOfBoundsException,
393 uno::RuntimeException )
395 if( mxChartDoc.is())
397 uno::Reference< chart::XDiagram > xDia = mxChartDoc->getDiagram();
398 if( xDia.is())
399 return xDia->getDataRowProperties( nRow );
402 return uno::Reference< beans::XPropertySet >();
405 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getDataPointProperties( sal_Int32 nCol, sal_Int32 nRow )
406 throw( lang::IndexOutOfBoundsException,
407 uno::RuntimeException )
409 if( mxChartDoc.is())
411 uno::Reference< chart::XDiagram > xDia = mxChartDoc->getDiagram();
412 if( xDia.is())
413 return xDia->getDataPointProperties( nCol, nRow );
416 return uno::Reference< beans::XPropertySet >();
419 // XShape ( ::XDiagram )
420 awt::Size SAL_CALL SampleAddIn::getSize()
421 throw( uno::RuntimeException )
423 if( mxChartDoc.is())
425 uno::Reference< drawing::XShape > xShape( mxChartDoc->getDiagram(), uno::UNO_QUERY );
426 if( xShape.is())
427 return xShape->getSize();
430 return awt::Size();
433 void SAL_CALL SampleAddIn::setSize( const awt::Size& aSize )
434 throw( beans::PropertyVetoException, uno::RuntimeException )
436 if( mxChartDoc.is())
438 uno::Reference< drawing::XShape > xShape( mxChartDoc->getDiagram(), uno::UNO_QUERY );
439 if( xShape.is())
440 xShape->setSize( aSize );
444 awt::Point SAL_CALL SampleAddIn::getPosition()
445 throw( uno::RuntimeException )
447 if( mxChartDoc.is())
449 uno::Reference< drawing::XShape > xShape( mxChartDoc->getDiagram(), uno::UNO_QUERY );
450 if( xShape.is())
451 return xShape->getPosition();
454 return awt::Point();
457 void SAL_CALL SampleAddIn::setPosition( const awt::Point& aPos )
458 throw( uno::RuntimeException )
460 if( mxChartDoc.is())
462 uno::Reference< drawing::XShape > xShape( mxChartDoc->getDiagram(), uno::UNO_QUERY );
463 if( xShape.is())
464 xShape->setPosition( aPos );
468 // XShapeDescriptor ( ::XShape ::XDiagram )
469 OUString SAL_CALL SampleAddIn::getShapeType() throw( css::uno::RuntimeException )
471 return "com.sun.star.chart.SampleAddinShape";
474 // XAxisXSupplier
475 uno::Reference< drawing::XShape > SAL_CALL SampleAddIn::getXAxisTitle()
476 throw( uno::RuntimeException )
478 if( mxChartDoc.is())
480 uno::Reference< chart::XAxisXSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
481 if( xAxisSupp.is())
482 return xAxisSupp->getXAxisTitle();
485 return uno::Reference< drawing::XShape >();
488 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getXAxis()
489 throw( uno::RuntimeException )
491 if( mxChartDoc.is())
493 uno::Reference< chart::XAxisXSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
494 if( xAxisSupp.is())
495 return xAxisSupp->getXAxis();
498 return uno::Reference< beans::XPropertySet >();
501 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getXMainGrid()
502 throw( uno::RuntimeException )
504 if( mxChartDoc.is())
506 uno::Reference< chart::XAxisXSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
507 if( xAxisSupp.is())
508 return xAxisSupp->getXMainGrid();
511 return uno::Reference< beans::XPropertySet >();
514 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getXHelpGrid()
515 throw( uno::RuntimeException )
517 if( mxChartDoc.is())
519 uno::Reference< chart::XAxisXSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
520 if( xAxisSupp.is())
521 return xAxisSupp->getXHelpGrid();
524 return uno::Reference< beans::XPropertySet >();
527 // XAxisYSupplier
528 uno::Reference< drawing::XShape > SAL_CALL SampleAddIn::getYAxisTitle()
529 throw( uno::RuntimeException )
531 if( mxChartDoc.is())
533 uno::Reference< chart::XAxisYSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
534 if( xAxisSupp.is())
535 return xAxisSupp->getYAxisTitle();
538 return uno::Reference< drawing::XShape >();
541 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getYAxis()
542 throw( uno::RuntimeException )
544 if( mxChartDoc.is())
546 uno::Reference< chart::XAxisYSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
547 if( xAxisSupp.is())
548 return xAxisSupp->getYAxis();
551 return uno::Reference< beans::XPropertySet >();
554 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getYMainGrid()
555 throw( uno::RuntimeException )
557 if( mxChartDoc.is())
559 uno::Reference< chart::XAxisYSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
560 if( xAxisSupp.is())
561 return xAxisSupp->getYMainGrid();
564 return uno::Reference< beans::XPropertySet >();
567 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getYHelpGrid()
568 throw( uno::RuntimeException )
570 if( mxChartDoc.is())
572 uno::Reference< chart::XAxisYSupplier > xAxisSupp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
573 if( xAxisSupp.is())
574 return xAxisSupp->getYHelpGrid();
577 return uno::Reference< beans::XPropertySet >();
580 // XStatisticDisplay
581 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getUpBar()
582 throw( uno::RuntimeException )
584 if( mxChartDoc.is())
586 uno::Reference< chart::XStatisticDisplay > xStatDisp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
587 if( xStatDisp.is())
588 return xStatDisp->getUpBar();
591 return uno::Reference< beans::XPropertySet >();
594 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getDownBar()
595 throw( uno::RuntimeException )
597 if( mxChartDoc.is())
599 uno::Reference< chart::XStatisticDisplay > xStatDisp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
600 if( xStatDisp.is())
601 return xStatDisp->getDownBar();
604 return uno::Reference< beans::XPropertySet >();
607 uno::Reference< beans::XPropertySet > SAL_CALL SampleAddIn::getMinMaxLine()
608 throw( uno::RuntimeException )
610 if( mxChartDoc.is())
612 uno::Reference< chart::XStatisticDisplay > xStatDisp( mxChartDoc->getDiagram(), uno::UNO_QUERY );
613 if( xStatDisp.is())
614 return xStatDisp->getMinMaxLine();
617 return uno::Reference< beans::XPropertySet >();
620 // XServiceName
621 OUString SAL_CALL SampleAddIn::getServiceName() throw( uno::RuntimeException )
623 return "com.sun.star.chart.SampleAddIn";
626 // XServiceInfo
627 OUString SAL_CALL SampleAddIn::getImplementationName() throw( uno::RuntimeException )
629 return getImplementationName_Static();
632 sal_Bool SAL_CALL SampleAddIn::supportsService( const OUString& ServiceName )
633 throw( uno::RuntimeException )
635 return cppu::supportsService(this, ServiceName);
638 uno::Sequence< OUString > SAL_CALL SampleAddIn::getSupportedServiceNames()
639 throw( uno::RuntimeException )
641 return getSupportedServiceNames_Static();
644 // XLocalizable
645 void SAL_CALL SampleAddIn::setLocale( const lang::Locale& eLocale )
646 throw( uno::RuntimeException )
648 maLocale = eLocale;
651 lang::Locale SAL_CALL SampleAddIn::getLocale()
652 throw( uno::RuntimeException )
654 return maLocale;
657 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */