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