nss: upgrade to release 3.73
[LibreOffice.git] / chart2 / source / tools / ConfigColorScheme.cxx
blob5645fb22b68f9b7438768652e36780cf49fd42ce
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 <ConfigColorScheme.hxx>
22 #include <unotools/configitem.hxx>
23 #include <sal/macros.h>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <comphelper/sequence.hxx>
27 #include <set>
29 using namespace ::com::sun::star;
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Sequence;
34 namespace
37 const char aSeriesPropName[] = "Series";
39 } // anonymous namespace
41 namespace chart
44 uno::Reference< chart2::XColorScheme > createConfigColorScheme( const uno::Reference< uno::XComponentContext > & xContext )
46 return new ConfigColorScheme( xContext );
49 namespace impl
51 class ChartConfigItem : public ::utl::ConfigItem
53 public:
54 explicit ChartConfigItem( ConfigColorScheme & rListener );
56 void addPropertyNotification( const OUString & rPropertyName );
57 uno::Any getProperty( const OUString & aPropertyName );
59 protected:
60 // ____ ::utl::ConfigItem ____
61 virtual void ImplCommit() override;
62 virtual void Notify( const Sequence< OUString > & aPropertyNames ) override;
64 private:
65 ConfigColorScheme & m_rListener;
66 std::set< OUString > m_aPropertiesToNotify;
69 ChartConfigItem::ChartConfigItem( ConfigColorScheme & rListener ) :
70 ::utl::ConfigItem( "Office.Chart/DefaultColor" ),
71 m_rListener( rListener )
74 void ChartConfigItem::Notify( const Sequence< OUString > & aPropertyNames )
76 for( OUString const & s : aPropertyNames )
78 if( m_aPropertiesToNotify.find( s ) != m_aPropertiesToNotify.end())
79 m_rListener.notify( s );
83 void ChartConfigItem::ImplCommit()
86 void ChartConfigItem::addPropertyNotification( const OUString & rPropertyName )
88 m_aPropertiesToNotify.insert( rPropertyName );
89 EnableNotification( comphelper::containerToSequence( m_aPropertiesToNotify ));
92 uno::Any ChartConfigItem::getProperty( const OUString & aPropertyName )
94 Sequence< uno::Any > aValues(
95 GetProperties( Sequence< OUString >( &aPropertyName, 1 )));
96 if( ! aValues.hasElements())
97 return uno::Any();
98 return aValues[0];
101 } // namespace impl
103 // explicit
104 ConfigColorScheme::ConfigColorScheme(
105 const Reference< uno::XComponentContext > & xContext ) :
106 m_xContext( xContext ),
107 m_nNumberOfColors( 0 ),
108 m_bNeedsUpdate( true )
112 ConfigColorScheme::~ConfigColorScheme()
115 void ConfigColorScheme::retrieveConfigColors()
117 if( ! m_xContext.is())
118 return;
120 // create config item if necessary
121 if (!m_apChartConfigItem)
123 m_apChartConfigItem.reset(
124 new impl::ChartConfigItem( *this ));
125 m_apChartConfigItem->addPropertyNotification( aSeriesPropName );
127 OSL_ASSERT(m_apChartConfigItem);
128 if (!m_apChartConfigItem)
129 return;
131 // retrieve colors
132 uno::Any aValue(
133 m_apChartConfigItem->getProperty( aSeriesPropName ));
134 if( aValue >>= m_aColorSequence )
135 m_nNumberOfColors = m_aColorSequence.getLength();
136 m_bNeedsUpdate = false;
139 // ____ XColorScheme ____
140 ::sal_Int32 SAL_CALL ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex )
142 if( m_bNeedsUpdate )
143 retrieveConfigColors();
145 if( m_nNumberOfColors > 0 )
146 return static_cast< sal_Int32 >( m_aColorSequence[ nIndex % m_nNumberOfColors ] );
148 // fall-back: hard-coded standard colors
149 static const sal_Int32 nDefaultColors[] = {
150 0x9999ff, 0x993366, 0xffffcc,
151 0xccffff, 0x660066, 0xff8080,
152 0x0066cc, 0xccccff, 0x000080,
153 0xff00ff, 0x00ffff, 0xffff00
156 static const sal_Int32 nMaxDefaultColors = SAL_N_ELEMENTS( nDefaultColors );
157 return nDefaultColors[ nIndex % nMaxDefaultColors ];
160 void ConfigColorScheme::notify( const OUString & rPropertyName )
162 if( rPropertyName == aSeriesPropName )
163 m_bNeedsUpdate = true;
166 OUString SAL_CALL ConfigColorScheme::getImplementationName()
168 return "com.sun.star.comp.chart2.ConfigDefaultColorScheme" ;
171 sal_Bool SAL_CALL ConfigColorScheme::supportsService( const OUString& rServiceName )
173 return cppu::supportsService(this, rServiceName);
176 css::uno::Sequence< OUString > SAL_CALL ConfigColorScheme::getSupportedServiceNames()
178 return { "com.sun.star.chart2.ColorScheme" };
181 } // namespace chart
183 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
184 com_sun_star_comp_chart2_ConfigDefaultColorScheme_get_implementation(css::uno::XComponentContext *context,
185 css::uno::Sequence<css::uno::Any> const &)
187 return cppu::acquire(new ::chart::ConfigColorScheme(context));
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */