Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "ConfigColorScheme.hxx"
22 : #include "ContainerHelper.hxx"
23 : #include "macros.hxx"
24 :
25 : #include <unotools/configitem.hxx>
26 : #include <sal/macros.h>
27 :
28 : #include <set>
29 :
30 : using namespace ::com::sun::star;
31 :
32 : using ::com::sun::star::uno::Reference;
33 : using ::com::sun::star::uno::Sequence;
34 : using ::rtl::OUString;
35 :
36 : namespace
37 : {
38 :
39 1 : static const OUString aSeriesPropName( RTL_CONSTASCII_USTRINGPARAM("Series"));
40 :
41 : } // anonymous namespace
42 :
43 : // --------------------------------------------------------------------------------
44 :
45 : namespace chart
46 : {
47 :
48 41 : uno::Reference< chart2::XColorScheme > createConfigColorScheme( const uno::Reference< uno::XComponentContext > & xContext )
49 : {
50 41 : return new ConfigColorScheme( xContext );
51 : }
52 :
53 : namespace impl
54 : {
55 : class ChartConfigItem : public ::utl::ConfigItem
56 : {
57 : public:
58 : explicit ChartConfigItem( ConfigItemListener & rListener );
59 : virtual ~ChartConfigItem();
60 :
61 : void addPropertyNotification( const OUString & rPropertyName );
62 :
63 : uno::Any getProperty( const OUString & aPropertyName );
64 :
65 : protected:
66 : // ____ ::utl::ConfigItem ____
67 : virtual void Commit();
68 : virtual void Notify( const Sequence< OUString > & aPropertyNames );
69 :
70 : private:
71 : ConfigItemListener & m_rListener;
72 : ::std::set< OUString > m_aPropertiesToNotify;
73 : };
74 :
75 41 : ChartConfigItem::ChartConfigItem( ConfigItemListener & rListener ) :
76 : ::utl::ConfigItem( C2U("Office.Chart/DefaultColor")),
77 41 : m_rListener( rListener )
78 41 : {}
79 :
80 82 : ChartConfigItem::~ChartConfigItem()
81 82 : {}
82 :
83 0 : void ChartConfigItem::Notify( const Sequence< OUString > & aPropertyNames )
84 : {
85 0 : for( sal_Int32 nIdx=0; nIdx<aPropertyNames.getLength(); ++nIdx )
86 : {
87 0 : if( m_aPropertiesToNotify.find( aPropertyNames[nIdx] ) != m_aPropertiesToNotify.end())
88 0 : m_rListener.notify( aPropertyNames[nIdx] );
89 : }
90 0 : }
91 :
92 0 : void ChartConfigItem::Commit()
93 0 : {}
94 :
95 41 : void ChartConfigItem::addPropertyNotification( const OUString & rPropertyName )
96 : {
97 41 : m_aPropertiesToNotify.insert( rPropertyName );
98 41 : EnableNotification( ContainerHelper::ContainerToSequence( m_aPropertiesToNotify ));
99 41 : }
100 :
101 41 : uno::Any ChartConfigItem::getProperty( const OUString & aPropertyName )
102 : {
103 : Sequence< uno::Any > aValues(
104 41 : GetProperties( Sequence< OUString >( &aPropertyName, 1 )));
105 41 : if( ! aValues.getLength())
106 0 : return uno::Any();
107 41 : return aValues[0];
108 : }
109 :
110 : } // namespace impl
111 :
112 : // --------------------------------------------------------------------------------
113 :
114 : // explicit
115 41 : ConfigColorScheme::ConfigColorScheme(
116 : const Reference< uno::XComponentContext > & xContext ) :
117 : m_xContext( xContext ),
118 : m_nNumberOfColors( 0 ),
119 41 : m_bNeedsUpdate( true )
120 : {
121 41 : }
122 :
123 82 : ConfigColorScheme::~ConfigColorScheme()
124 82 : {}
125 :
126 41 : void ConfigColorScheme::retrieveConfigColors()
127 : {
128 41 : if( ! m_xContext.is())
129 : return;
130 :
131 : // create config item if necessary
132 41 : if( ! m_apChartConfigItem.get())
133 : {
134 : m_apChartConfigItem.reset(
135 41 : new impl::ChartConfigItem( *this ));
136 41 : m_apChartConfigItem->addPropertyNotification( aSeriesPropName );
137 : }
138 : OSL_ASSERT( m_apChartConfigItem.get());
139 41 : if( ! m_apChartConfigItem.get())
140 : return;
141 :
142 : // retrieve colors
143 : uno::Any aValue(
144 41 : m_apChartConfigItem->getProperty( aSeriesPropName ));
145 41 : if( aValue >>= m_aColorSequence )
146 41 : m_nNumberOfColors = m_aColorSequence.getLength();
147 41 : m_bNeedsUpdate = false;
148 : }
149 :
150 : // ____ XColorScheme ____
151 123 : ::sal_Int32 SAL_CALL ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex )
152 : throw (uno::RuntimeException)
153 : {
154 123 : if( m_bNeedsUpdate )
155 41 : retrieveConfigColors();
156 :
157 123 : if( m_nNumberOfColors > 0 )
158 123 : return static_cast< sal_Int32 >( m_aColorSequence[ nIndex % m_nNumberOfColors ] );
159 :
160 : // fall-back: hard-coded standard colors
161 : static sal_Int32 nDefaultColors[] = {
162 : 0x9999ff, 0x993366, 0xffffcc,
163 : 0xccffff, 0x660066, 0xff8080,
164 : 0x0066cc, 0xccccff, 0x000080,
165 : 0xff00ff, 0x00ffff, 0xffff00
166 : };
167 :
168 : static const sal_Int32 nMaxDefaultColors = SAL_N_ELEMENTS( nDefaultColors );
169 0 : return nDefaultColors[ nIndex % nMaxDefaultColors ];
170 : }
171 :
172 0 : void ConfigColorScheme::notify( const OUString & rPropertyName )
173 : {
174 0 : if( rPropertyName.equals( aSeriesPropName ))
175 0 : m_bNeedsUpdate = true;
176 0 : }
177 :
178 : // ================================================================================
179 :
180 0 : Sequence< OUString > ConfigColorScheme::getSupportedServiceNames_Static()
181 : {
182 0 : Sequence< OUString > aServices( 1 );
183 0 : aServices[ 0 ] = C2U( "com.sun.star.chart2.ColorScheme" );
184 0 : return aServices;
185 : }
186 :
187 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
188 3 : APPHELPER_XSERVICEINFO_IMPL( ConfigColorScheme,
189 : C2U( "com.sun.star.comp.chart2.ConfigDefaultColorScheme" ))
190 :
191 : // ================================================================================
192 :
193 3 : } // namespace chart
194 :
195 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|