1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "ConfigColorScheme.hxx"
21 #include "ContainerHelper.hxx"
24 #include <unotools/configitem.hxx>
25 #include <sal/macros.h>
26 #include <cppuhelper/supportsservice.hxx>
30 using namespace ::com::sun::star
;
32 using ::com::sun::star::uno::Reference
;
33 using ::com::sun::star::uno::Sequence
;
38 static const char aSeriesPropName
[] = "Series";
40 } // anonymous namespace
45 uno::Reference
< chart2::XColorScheme
> createConfigColorScheme( const uno::Reference
< uno::XComponentContext
> & xContext
)
47 return new ConfigColorScheme( xContext
);
52 class ChartConfigItem
: public ::utl::ConfigItem
55 explicit ChartConfigItem( ConfigItemListener
& rListener
);
56 virtual ~ChartConfigItem();
58 void addPropertyNotification( const OUString
& rPropertyName
);
60 uno::Any
getProperty( const OUString
& aPropertyName
);
63 // ____ ::utl::ConfigItem ____
64 virtual void ImplCommit() SAL_OVERRIDE
;
65 virtual void Notify( const Sequence
< OUString
> & aPropertyNames
) SAL_OVERRIDE
;
68 ConfigItemListener
& m_rListener
;
69 ::std::set
< OUString
> m_aPropertiesToNotify
;
72 ChartConfigItem::ChartConfigItem( ConfigItemListener
& rListener
) :
73 ::utl::ConfigItem( "Office.Chart/DefaultColor" ),
74 m_rListener( rListener
)
77 ChartConfigItem::~ChartConfigItem()
80 void ChartConfigItem::Notify( const Sequence
< OUString
> & aPropertyNames
)
82 for( sal_Int32 nIdx
=0; nIdx
<aPropertyNames
.getLength(); ++nIdx
)
84 if( m_aPropertiesToNotify
.find( aPropertyNames
[nIdx
] ) != m_aPropertiesToNotify
.end())
85 m_rListener
.notify( aPropertyNames
[nIdx
] );
89 void ChartConfigItem::ImplCommit()
92 void ChartConfigItem::addPropertyNotification( const OUString
& rPropertyName
)
94 m_aPropertiesToNotify
.insert( rPropertyName
);
95 EnableNotification( ContainerHelper::ContainerToSequence( m_aPropertiesToNotify
));
98 uno::Any
ChartConfigItem::getProperty( const OUString
& aPropertyName
)
100 Sequence
< uno::Any
> aValues(
101 GetProperties( Sequence
< OUString
>( &aPropertyName
, 1 )));
102 if( ! aValues
.getLength())
110 ConfigColorScheme::ConfigColorScheme(
111 const Reference
< uno::XComponentContext
> & xContext
) :
112 m_xContext( xContext
),
113 m_nNumberOfColors( 0 ),
114 m_bNeedsUpdate( true )
118 ConfigColorScheme::~ConfigColorScheme()
121 void ConfigColorScheme::retrieveConfigColors()
123 if( ! m_xContext
.is())
126 // create config item if necessary
127 if( ! m_apChartConfigItem
.get())
129 m_apChartConfigItem
.reset(
130 new impl::ChartConfigItem( *this ));
131 m_apChartConfigItem
->addPropertyNotification( aSeriesPropName
);
133 OSL_ASSERT( m_apChartConfigItem
.get());
134 if( ! m_apChartConfigItem
.get())
139 m_apChartConfigItem
->getProperty( aSeriesPropName
));
140 if( aValue
>>= m_aColorSequence
)
141 m_nNumberOfColors
= m_aColorSequence
.getLength();
142 m_bNeedsUpdate
= false;
145 // ____ XColorScheme ____
146 ::sal_Int32 SAL_CALL
ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex
)
147 throw (uno::RuntimeException
, std::exception
)
150 retrieveConfigColors();
152 if( m_nNumberOfColors
> 0 )
153 return static_cast< sal_Int32
>( m_aColorSequence
[ nIndex
% m_nNumberOfColors
] );
155 // fall-back: hard-coded standard colors
156 static const sal_Int32 nDefaultColors
[] = {
157 0x9999ff, 0x993366, 0xffffcc,
158 0xccffff, 0x660066, 0xff8080,
159 0x0066cc, 0xccccff, 0x000080,
160 0xff00ff, 0x00ffff, 0xffff00
163 static const sal_Int32 nMaxDefaultColors
= SAL_N_ELEMENTS( nDefaultColors
);
164 return nDefaultColors
[ nIndex
% nMaxDefaultColors
];
167 void ConfigColorScheme::notify( const OUString
& rPropertyName
)
169 if( rPropertyName
== aSeriesPropName
)
170 m_bNeedsUpdate
= true;
173 Sequence
< OUString
> ConfigColorScheme::getSupportedServiceNames_Static()
175 Sequence
< OUString
> aServices( 1 );
176 aServices
[ 0 ] = "com.sun.star.chart2.ColorScheme";
180 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
181 OUString SAL_CALL
ConfigColorScheme::getImplementationName()
182 throw( css::uno::RuntimeException
, std::exception
)
184 return getImplementationName_Static();
187 OUString
ConfigColorScheme::getImplementationName_Static()
189 return OUString("com.sun.star.comp.chart2.ConfigDefaultColorScheme") ;
192 sal_Bool SAL_CALL
ConfigColorScheme::supportsService( const OUString
& rServiceName
)
193 throw( css::uno::RuntimeException
, std::exception
)
195 return cppu::supportsService(this, rServiceName
);
198 css::uno::Sequence
< OUString
> SAL_CALL
ConfigColorScheme::getSupportedServiceNames()
199 throw( css::uno::RuntimeException
, std::exception
)
201 return getSupportedServiceNames_Static();
206 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
207 com_sun_star_comp_chart2_ConfigDefaultColorScheme_get_implementation(css::uno::XComponentContext
*context
,
208 css::uno::Sequence
<css::uno::Any
> const &)
210 return cppu::acquire(new ::chart::ConfigColorScheme(context
));
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */