xref: /AOO41X/main/chart2/source/tools/ConfigColorScheme.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "ConfigColorScheme.hxx"
32 #include "ContainerHelper.hxx"
33 #include "macros.hxx"
34 
35 #include <unotools/configitem.hxx>
36 
37 #include <set>
38 
39 using namespace ::com::sun::star;
40 
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::rtl::OUString;
44 
45 namespace
46 {
47 
48 static const OUString aSeriesPropName( RTL_CONSTASCII_USTRINGPARAM("Series"));
49 
50 } // anonymous namespace
51 
52 // --------------------------------------------------------------------------------
53 
54 namespace chart
55 {
56 
57 uno::Reference< chart2::XColorScheme > createConfigColorScheme( const uno::Reference< uno::XComponentContext > & xContext )
58 {
59     return new ConfigColorScheme( xContext );
60 }
61 
62 namespace impl
63 {
64 class ChartConfigItem : public ::utl::ConfigItem
65 {
66 public:
67     explicit ChartConfigItem( ConfigItemListener & rListener );
68     virtual ~ChartConfigItem();
69 
70     void addPropertyNotification( const OUString & rPropertyName );
71 
72     uno::Any getProperty( const OUString & aPropertyName );
73 
74 protected:
75     // ____ ::utl::ConfigItem ____
76     virtual void                    Commit();
77     virtual void Notify( const Sequence< OUString > & aPropertyNames );
78 
79 private:
80     ConfigItemListener & m_rListener;
81     ::std::set< OUString >        m_aPropertiesToNotify;
82 };
83 
84 ChartConfigItem::ChartConfigItem( ConfigItemListener & rListener ) :
85         ::utl::ConfigItem( C2U("Office.Chart/DefaultColor")),
86     m_rListener( rListener )
87 {}
88 
89 ChartConfigItem::~ChartConfigItem()
90 {}
91 
92 void ChartConfigItem::Notify( const Sequence< OUString > & aPropertyNames )
93 {
94     for( sal_Int32 nIdx=0; nIdx<aPropertyNames.getLength(); ++nIdx )
95     {
96         if( m_aPropertiesToNotify.find( aPropertyNames[nIdx] ) != m_aPropertiesToNotify.end())
97             m_rListener.notify( aPropertyNames[nIdx] );
98     }
99 }
100 
101 void ChartConfigItem::Commit()
102 {}
103 
104 void ChartConfigItem::addPropertyNotification( const OUString & rPropertyName )
105 {
106     m_aPropertiesToNotify.insert( rPropertyName );
107     EnableNotification( ContainerHelper::ContainerToSequence( m_aPropertiesToNotify ));
108 }
109 
110 uno::Any ChartConfigItem::getProperty( const OUString & aPropertyName )
111 {
112     Sequence< uno::Any > aValues(
113         GetProperties( Sequence< OUString >( &aPropertyName, 1 )));
114     if( ! aValues.getLength())
115         return uno::Any();
116     return aValues[0];
117 }
118 
119 } // namespace impl
120 
121 // --------------------------------------------------------------------------------
122 
123 // explicit
124 ConfigColorScheme::ConfigColorScheme(
125     const Reference< uno::XComponentContext > & xContext ) :
126         m_xContext( xContext  ),
127         m_nNumberOfColors( 0 ),
128         m_bNeedsUpdate( true )
129 {
130 }
131 
132 ConfigColorScheme::~ConfigColorScheme()
133 {}
134 
135 void ConfigColorScheme::retrieveConfigColors()
136 {
137     if( ! m_xContext.is())
138         return;
139 
140     // create config item if necessary
141     if( ! m_apChartConfigItem.get())
142     {
143         m_apChartConfigItem.reset(
144             new impl::ChartConfigItem( *this ));
145         m_apChartConfigItem->addPropertyNotification( aSeriesPropName );
146     }
147     OSL_ASSERT( m_apChartConfigItem.get());
148     if( ! m_apChartConfigItem.get())
149         return;
150 
151     // retrieve colors
152     uno::Any aValue(
153         m_apChartConfigItem->getProperty( aSeriesPropName ));
154     if( aValue >>= m_aColorSequence )
155         m_nNumberOfColors = m_aColorSequence.getLength();
156     m_bNeedsUpdate = false;
157 }
158 
159 // ____ XColorScheme ____
160 ::sal_Int32 SAL_CALL ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex )
161     throw (uno::RuntimeException)
162 {
163     if( m_bNeedsUpdate )
164         retrieveConfigColors();
165 
166     if( m_nNumberOfColors > 0 )
167         return static_cast< sal_Int32 >( m_aColorSequence[ nIndex % m_nNumberOfColors ] );
168 
169     // fall-back: hard-coded standard colors
170     static sal_Int32 nDefaultColors[] =  {
171         0x9999ff, 0x993366, 0xffffcc,
172         0xccffff, 0x660066, 0xff8080,
173         0x0066cc, 0xccccff, 0x000080,
174         0xff00ff, 0x00ffff, 0xffff00
175     };
176 
177     static const sal_Int32 nMaxDefaultColors = sizeof( nDefaultColors ) / sizeof( sal_Int32 );
178     return nDefaultColors[ nIndex % nMaxDefaultColors ];
179 }
180 
181 void ConfigColorScheme::notify( const OUString & rPropertyName )
182 {
183     if( rPropertyName.equals( aSeriesPropName ))
184         m_bNeedsUpdate = true;
185 }
186 
187 // ================================================================================
188 
189 Sequence< OUString > ConfigColorScheme::getSupportedServiceNames_Static()
190 {
191     Sequence< OUString > aServices( 1 );
192     aServices[ 0 ] = C2U( "com.sun.star.chart2.ColorScheme" );
193     return aServices;
194 }
195 
196 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
197 APPHELPER_XSERVICEINFO_IMPL( ConfigColorScheme,
198                              C2U( "com.sun.star.comp.chart2.ConfigDefaultColorScheme" ))
199 
200 // ================================================================================
201 
202 } //  namespace chart
203