1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "ConfigColorScheme.hxx"
28cdf0e10cSrcweir #include "ContainerHelper.hxx"
29cdf0e10cSrcweir #include "macros.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <unotools/configitem.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <set>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
38cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
39cdf0e10cSrcweir using ::rtl::OUString;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir static const OUString aSeriesPropName( RTL_CONSTASCII_USTRINGPARAM("Series"));
45cdf0e10cSrcweir 
46cdf0e10cSrcweir } // anonymous namespace
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // --------------------------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace chart
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 
createConfigColorScheme(const uno::Reference<uno::XComponentContext> & xContext)53cdf0e10cSrcweir uno::Reference< chart2::XColorScheme > createConfigColorScheme( const uno::Reference< uno::XComponentContext > & xContext )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     return new ConfigColorScheme( xContext );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace impl
59cdf0e10cSrcweir {
60cdf0e10cSrcweir class ChartConfigItem : public ::utl::ConfigItem
61cdf0e10cSrcweir {
62cdf0e10cSrcweir public:
63cdf0e10cSrcweir     explicit ChartConfigItem( ConfigItemListener & rListener );
64cdf0e10cSrcweir     virtual ~ChartConfigItem();
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     void addPropertyNotification( const OUString & rPropertyName );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     uno::Any getProperty( const OUString & aPropertyName );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir protected:
71cdf0e10cSrcweir     // ____ ::utl::ConfigItem ____
72cdf0e10cSrcweir     virtual void                    Commit();
73cdf0e10cSrcweir     virtual void Notify( const Sequence< OUString > & aPropertyNames );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir private:
76cdf0e10cSrcweir     ConfigItemListener & m_rListener;
77cdf0e10cSrcweir     ::std::set< OUString >        m_aPropertiesToNotify;
78cdf0e10cSrcweir };
79cdf0e10cSrcweir 
ChartConfigItem(ConfigItemListener & rListener)80cdf0e10cSrcweir ChartConfigItem::ChartConfigItem( ConfigItemListener & rListener ) :
81cdf0e10cSrcweir         ::utl::ConfigItem( C2U("Office.Chart/DefaultColor")),
82cdf0e10cSrcweir     m_rListener( rListener )
83cdf0e10cSrcweir {}
84cdf0e10cSrcweir 
~ChartConfigItem()85cdf0e10cSrcweir ChartConfigItem::~ChartConfigItem()
86cdf0e10cSrcweir {}
87cdf0e10cSrcweir 
Notify(const Sequence<OUString> & aPropertyNames)88cdf0e10cSrcweir void ChartConfigItem::Notify( const Sequence< OUString > & aPropertyNames )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     for( sal_Int32 nIdx=0; nIdx<aPropertyNames.getLength(); ++nIdx )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         if( m_aPropertiesToNotify.find( aPropertyNames[nIdx] ) != m_aPropertiesToNotify.end())
93cdf0e10cSrcweir             m_rListener.notify( aPropertyNames[nIdx] );
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
Commit()97cdf0e10cSrcweir void ChartConfigItem::Commit()
98cdf0e10cSrcweir {}
99cdf0e10cSrcweir 
addPropertyNotification(const OUString & rPropertyName)100cdf0e10cSrcweir void ChartConfigItem::addPropertyNotification( const OUString & rPropertyName )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     m_aPropertiesToNotify.insert( rPropertyName );
103cdf0e10cSrcweir     EnableNotification( ContainerHelper::ContainerToSequence( m_aPropertiesToNotify ));
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
getProperty(const OUString & aPropertyName)106cdf0e10cSrcweir uno::Any ChartConfigItem::getProperty( const OUString & aPropertyName )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     Sequence< uno::Any > aValues(
109cdf0e10cSrcweir         GetProperties( Sequence< OUString >( &aPropertyName, 1 )));
110cdf0e10cSrcweir     if( ! aValues.getLength())
111cdf0e10cSrcweir         return uno::Any();
112cdf0e10cSrcweir     return aValues[0];
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir } // namespace impl
116cdf0e10cSrcweir 
117cdf0e10cSrcweir // --------------------------------------------------------------------------------
118cdf0e10cSrcweir 
119cdf0e10cSrcweir // explicit
ConfigColorScheme(const Reference<uno::XComponentContext> & xContext)120cdf0e10cSrcweir ConfigColorScheme::ConfigColorScheme(
121cdf0e10cSrcweir     const Reference< uno::XComponentContext > & xContext ) :
122cdf0e10cSrcweir         m_xContext( xContext  ),
123cdf0e10cSrcweir         m_nNumberOfColors( 0 ),
124cdf0e10cSrcweir         m_bNeedsUpdate( true )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
~ConfigColorScheme()128cdf0e10cSrcweir ConfigColorScheme::~ConfigColorScheme()
129cdf0e10cSrcweir {}
130cdf0e10cSrcweir 
retrieveConfigColors()131cdf0e10cSrcweir void ConfigColorScheme::retrieveConfigColors()
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     if( ! m_xContext.is())
134cdf0e10cSrcweir         return;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     // create config item if necessary
137cdf0e10cSrcweir     if( ! m_apChartConfigItem.get())
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         m_apChartConfigItem.reset(
140cdf0e10cSrcweir             new impl::ChartConfigItem( *this ));
141cdf0e10cSrcweir         m_apChartConfigItem->addPropertyNotification( aSeriesPropName );
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir     OSL_ASSERT( m_apChartConfigItem.get());
144cdf0e10cSrcweir     if( ! m_apChartConfigItem.get())
145cdf0e10cSrcweir         return;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     // retrieve colors
148cdf0e10cSrcweir     uno::Any aValue(
149cdf0e10cSrcweir         m_apChartConfigItem->getProperty( aSeriesPropName ));
150cdf0e10cSrcweir     if( aValue >>= m_aColorSequence )
151cdf0e10cSrcweir         m_nNumberOfColors = m_aColorSequence.getLength();
152cdf0e10cSrcweir     m_bNeedsUpdate = false;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir // ____ XColorScheme ____
getColorByIndex(::sal_Int32 nIndex)156cdf0e10cSrcweir ::sal_Int32 SAL_CALL ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex )
157cdf0e10cSrcweir     throw (uno::RuntimeException)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     if( m_bNeedsUpdate )
160cdf0e10cSrcweir         retrieveConfigColors();
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     if( m_nNumberOfColors > 0 )
163cdf0e10cSrcweir         return static_cast< sal_Int32 >( m_aColorSequence[ nIndex % m_nNumberOfColors ] );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     // fall-back: hard-coded standard colors
166cdf0e10cSrcweir     static sal_Int32 nDefaultColors[] =  {
167cdf0e10cSrcweir         0x9999ff, 0x993366, 0xffffcc,
168cdf0e10cSrcweir         0xccffff, 0x660066, 0xff8080,
169cdf0e10cSrcweir         0x0066cc, 0xccccff, 0x000080,
170cdf0e10cSrcweir         0xff00ff, 0x00ffff, 0xffff00
171cdf0e10cSrcweir     };
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     static const sal_Int32 nMaxDefaultColors = sizeof( nDefaultColors ) / sizeof( sal_Int32 );
174cdf0e10cSrcweir     return nDefaultColors[ nIndex % nMaxDefaultColors ];
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
notify(const OUString & rPropertyName)177cdf0e10cSrcweir void ConfigColorScheme::notify( const OUString & rPropertyName )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     if( rPropertyName.equals( aSeriesPropName ))
180cdf0e10cSrcweir         m_bNeedsUpdate = true;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir // ================================================================================
184cdf0e10cSrcweir 
getSupportedServiceNames_Static()185cdf0e10cSrcweir Sequence< OUString > ConfigColorScheme::getSupportedServiceNames_Static()
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     Sequence< OUString > aServices( 1 );
188cdf0e10cSrcweir     aServices[ 0 ] = C2U( "com.sun.star.chart2.ColorScheme" );
189cdf0e10cSrcweir     return aServices;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
193cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( ConfigColorScheme,
194cdf0e10cSrcweir                              C2U( "com.sun.star.comp.chart2.ConfigDefaultColorScheme" ))
195cdf0e10cSrcweir 
196cdf0e10cSrcweir // ================================================================================
197cdf0e10cSrcweir 
198cdf0e10cSrcweir } //  namespace chart
199