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>
22 #include <unotools/configitem.hxx>
23 #include <cppuhelper/supportsservice.hxx>
25 using namespace ::com::sun::star
;
27 using ::com::sun::star::uno::Reference
;
28 using ::com::sun::star::uno::Sequence
;
33 constexpr OUString aSeriesPropName
= u
"Series"_ustr
;
35 } // anonymous namespace
40 uno::Reference
< chart2::XColorScheme
> createConfigColorScheme( const uno::Reference
< uno::XComponentContext
> & xContext
)
42 return new ConfigColorScheme( xContext
);
47 class ChartConfigItem
: public ::utl::ConfigItem
50 explicit ChartConfigItem( ConfigColorScheme
& rListener
);
52 uno::Any
getProperty( const OUString
& aPropertyName
);
55 // ____ ::utl::ConfigItem ____
56 virtual void ImplCommit() override
;
57 virtual void Notify( const Sequence
< OUString
> & aPropertyNames
) override
;
60 ConfigColorScheme
& m_rListener
;
63 ChartConfigItem::ChartConfigItem( ConfigColorScheme
& rListener
) :
64 ::utl::ConfigItem( u
"Office.Chart/DefaultColor"_ustr
),
65 m_rListener( rListener
)
67 EnableNotification( { aSeriesPropName
} );
70 void ChartConfigItem::Notify( const Sequence
< OUString
> & aPropertyNames
)
72 for( OUString
const & s
: aPropertyNames
)
74 if( s
== aSeriesPropName
)
79 void ChartConfigItem::ImplCommit()
82 uno::Any
ChartConfigItem::getProperty( const OUString
& aPropertyName
)
84 Sequence
< uno::Any
> aValues(
85 GetProperties( Sequence
< OUString
>( &aPropertyName
, 1 )));
86 if( ! aValues
.hasElements())
94 ConfigColorScheme::ConfigColorScheme(
95 const Reference
< uno::XComponentContext
> & xContext
) :
96 m_xContext( xContext
),
97 m_nNumberOfColors( 0 ),
98 m_bNeedsUpdate( true )
102 ConfigColorScheme::~ConfigColorScheme()
105 void ConfigColorScheme::retrieveConfigColors()
107 if( ! m_xContext
.is())
110 // create config item if necessary
111 if (!m_apChartConfigItem
)
113 m_apChartConfigItem
.reset(
114 new impl::ChartConfigItem( *this ));
116 assert(m_apChartConfigItem
&& "this can only be set at this point");
120 m_apChartConfigItem
->getProperty( aSeriesPropName
));
121 if( aValue
>>= m_aColorSequence
)
122 m_nNumberOfColors
= m_aColorSequence
.getLength();
123 m_bNeedsUpdate
= false;
126 // ____ XColorScheme ____
127 ::sal_Int32 SAL_CALL
ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex
)
130 retrieveConfigColors();
132 if( m_nNumberOfColors
> 0 )
133 return static_cast< sal_Int32
>( m_aColorSequence
[ nIndex
% m_nNumberOfColors
] );
135 // fall-back: hard-coded standard colors
136 static const sal_Int32 nDefaultColors
[] = {
137 0x9999ff, 0x993366, 0xffffcc,
138 0xccffff, 0x660066, 0xff8080,
139 0x0066cc, 0xccccff, 0x000080,
140 0xff00ff, 0x00ffff, 0xffff00
143 static const sal_Int32 nMaxDefaultColors
= std::size( nDefaultColors
);
144 return nDefaultColors
[ nIndex
% nMaxDefaultColors
];
147 void ConfigColorScheme::notify()
149 m_bNeedsUpdate
= true;
152 OUString SAL_CALL
ConfigColorScheme::getImplementationName()
154 return u
"com.sun.star.comp.chart2.ConfigDefaultColorScheme"_ustr
;
157 sal_Bool SAL_CALL
ConfigColorScheme::supportsService( const OUString
& rServiceName
)
159 return cppu::supportsService(this, rServiceName
);
162 css::uno::Sequence
< OUString
> SAL_CALL
ConfigColorScheme::getSupportedServiceNames()
164 return { u
"com.sun.star.chart2.ColorScheme"_ustr
};
169 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
170 com_sun_star_comp_chart2_ConfigDefaultColorScheme_get_implementation(css::uno::XComponentContext
*context
,
171 css::uno::Sequence
<css::uno::Any
> const &)
173 return cppu::acquire(new ::chart::ConfigColorScheme(context
));
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */