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 : #include <com/sun/star/uno/Sequence.hxx>
21 : #include <tools/stream.hxx> // header for SvStream
22 : #include "cfgchart.hxx"
23 : #include <dialmgr.hxx>
24 : #include <cuires.hrc>
25 :
26 : #define ROW_COLOR_COUNT 12
27 :
28 : using namespace com::sun::star;
29 :
30 0 : TYPEINIT1( SvxChartColorTableItem, SfxPoolItem );
31 :
32 0 : SvxChartColorTable::SvxChartColorTable()
33 0 : {}
34 :
35 0 : SvxChartColorTable::SvxChartColorTable( const SvxChartColorTable & _rSource ) :
36 : m_aColorEntries( _rSource.m_aColorEntries ),
37 0 : nNextElementNumber( m_aColorEntries.size() + 1 )
38 0 : {}
39 :
40 : // accessors
41 0 : size_t SvxChartColorTable::size() const
42 : {
43 0 : return m_aColorEntries.size();
44 : }
45 :
46 0 : const XColorEntry & SvxChartColorTable::operator[]( size_t _nIndex ) const
47 : {
48 0 : if ( _nIndex >= m_aColorEntries.size() )
49 : {
50 : SAL_WARN( "cui.options", "SvxChartColorTable::[] invalid index" );
51 0 : return m_aColorEntries[ 0 ];
52 : }
53 :
54 0 : return m_aColorEntries[ _nIndex ];
55 : }
56 :
57 0 : ColorData SvxChartColorTable::getColorData( size_t _nIndex ) const
58 : {
59 0 : if ( _nIndex >= m_aColorEntries.size() )
60 : {
61 : SAL_WARN( "cui.options", "SvxChartColorTable::getColorData invalid index" );
62 0 : return COL_BLACK;
63 : }
64 :
65 : // GetColor should be const but unfortunately isn't
66 0 : return const_cast< XColorEntry & >( m_aColorEntries[ _nIndex ] ).GetColor().GetRGBColor();
67 : }
68 :
69 : // mutators
70 0 : void SvxChartColorTable::clear()
71 : {
72 0 : m_aColorEntries.clear();
73 0 : nNextElementNumber = 1;
74 0 : }
75 :
76 0 : void SvxChartColorTable::append( const XColorEntry & _rEntry )
77 : {
78 0 : m_aColorEntries.push_back( _rEntry );
79 0 : }
80 :
81 0 : void SvxChartColorTable::remove( size_t _nIndex )
82 : {
83 0 : if (m_aColorEntries.size() > 0)
84 0 : m_aColorEntries.erase( m_aColorEntries.begin() + _nIndex);
85 :
86 0 : for (size_t i=0 ; i<m_aColorEntries.size(); i++)
87 : {
88 0 : m_aColorEntries[ i ].SetName( getDefaultName( i ) );
89 : }
90 0 : }
91 :
92 0 : void SvxChartColorTable::replace( size_t _nIndex, const XColorEntry & _rEntry )
93 : {
94 : DBG_ASSERT( _nIndex <= m_aColorEntries.size(),
95 : "SvxChartColorTable::replace invalid index" );
96 :
97 0 : m_aColorEntries[ _nIndex ] = _rEntry;
98 0 : }
99 :
100 0 : void SvxChartColorTable::useDefault()
101 : {
102 : ColorData aColors[] = {
103 : RGB_COLORDATA( 0x00, 0x45, 0x86 ),
104 : RGB_COLORDATA( 0xff, 0x42, 0x0e ),
105 : RGB_COLORDATA( 0xff, 0xd3, 0x20 ),
106 : RGB_COLORDATA( 0x57, 0x9d, 0x1c ),
107 : RGB_COLORDATA( 0x7e, 0x00, 0x21 ),
108 : RGB_COLORDATA( 0x83, 0xca, 0xff ),
109 : RGB_COLORDATA( 0x31, 0x40, 0x04 ),
110 : RGB_COLORDATA( 0xae, 0xcf, 0x00 ),
111 : RGB_COLORDATA( 0x4b, 0x1f, 0x6f ),
112 : RGB_COLORDATA( 0xff, 0x95, 0x0e ),
113 : RGB_COLORDATA( 0xc5, 0x00, 0x0b ),
114 : RGB_COLORDATA( 0x00, 0x84, 0xd1 )
115 0 : };
116 :
117 0 : clear();
118 :
119 0 : for( sal_Int32 i=0; i<ROW_COLOR_COUNT; i++ )
120 : {
121 0 : append( XColorEntry( aColors[ i % sizeof( aColors ) ], getDefaultName( i ) ));
122 : }
123 0 : }
124 :
125 0 : String SvxChartColorTable::getDefaultName( size_t _nIndex )
126 : {
127 0 : OUString aName;
128 :
129 0 : if (sDefaultNamePrefix.getLength() == 0)
130 : {
131 0 : OUString aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) );
132 0 : sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
133 0 : if( nPos != -1 )
134 : {
135 0 : sDefaultNamePrefix = aResName.copy( 0, nPos );
136 0 : sDefaultNamePostfix = aResName.copy( nPos + sizeof( "$(ROW)" ) - 1 );
137 : }
138 : else
139 : {
140 0 : sDefaultNamePrefix = aResName;
141 0 : }
142 : }
143 :
144 0 : aName = sDefaultNamePrefix + OUString::number(_nIndex + 1) + sDefaultNamePostfix;
145 0 : nNextElementNumber++;
146 :
147 0 : return aName;
148 : }
149 :
150 : // comparison
151 0 : bool SvxChartColorTable::operator==( const SvxChartColorTable & _rOther ) const
152 : {
153 : // note: XColorEntry has no operator ==
154 0 : bool bEqual = ( this->m_aColorEntries.size() == _rOther.m_aColorEntries.size() );
155 :
156 0 : if( bEqual )
157 : {
158 0 : for( size_t i = 0; i < m_aColorEntries.size(); ++i )
159 : {
160 0 : if( getColorData( i ) != _rOther.getColorData( i ))
161 : {
162 0 : bEqual = false;
163 0 : break;
164 : }
165 : }
166 : }
167 :
168 0 : return bEqual;
169 : }
170 :
171 : // ====================
172 : // class SvxChartOptions
173 : // ====================
174 :
175 0 : SvxChartOptions::SvxChartOptions() :
176 : ::utl::ConfigItem( OUString("Office.Chart") ),
177 0 : mbIsInitialized( sal_False )
178 : {
179 0 : maPropertyNames.realloc( 1 );
180 0 : maPropertyNames[ 0 ] = OUString("DefaultColor/Series");
181 0 : }
182 :
183 0 : SvxChartOptions::~SvxChartOptions()
184 : {
185 0 : }
186 :
187 0 : const SvxChartColorTable& SvxChartOptions::GetDefaultColors()
188 : {
189 0 : if ( !mbIsInitialized )
190 0 : mbIsInitialized = RetrieveOptions();
191 0 : return maDefColors;
192 : }
193 :
194 0 : void SvxChartOptions::SetDefaultColors( const SvxChartColorTable& aCol )
195 : {
196 0 : maDefColors = aCol;
197 0 : SetModified();
198 0 : }
199 :
200 0 : sal_Bool SvxChartOptions::RetrieveOptions()
201 : {
202 : // get sequence containing all properties
203 :
204 0 : uno::Sequence< OUString > aNames = GetPropertyNames();
205 0 : uno::Sequence< uno::Any > aProperties( aNames.getLength());
206 0 : aProperties = GetProperties( aNames );
207 :
208 0 : if( aProperties.getLength() == aNames.getLength())
209 : {
210 : // 1. default colors for series
211 0 : maDefColors.clear();
212 0 : uno::Sequence< sal_Int64 > aColorSeq;
213 0 : aProperties[ 0 ] >>= aColorSeq;
214 :
215 0 : sal_Int32 nCount = aColorSeq.getLength();
216 0 : Color aCol;
217 :
218 : // create strings for entry names
219 0 : OUString aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) );
220 0 : OUString aPrefix, aPostfix, aName;
221 0 : sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
222 0 : if( nPos != -1 )
223 : {
224 0 : aPrefix = aResName.copy( 0, nPos );
225 0 : sal_Int32 idx = nPos + sizeof( "$(ROW)" ) - 1;
226 0 : aPostfix = aResName.copy( idx, aResName.getLength()-idx );
227 : }
228 : else
229 0 : aPrefix = aResName;
230 :
231 : // set color values
232 0 : for( sal_Int32 i=0; i < nCount; i++ )
233 : {
234 0 : aCol.SetColor( (static_cast< ColorData >(aColorSeq[ i ] )));
235 :
236 0 : aName = aPrefix + OUString::number(i + 1) + aPostfix;
237 :
238 0 : maDefColors.append( XColorEntry( aCol, aName ));
239 : }
240 0 : return sal_True;
241 : }
242 0 : return sal_False;
243 : }
244 :
245 0 : void SvxChartOptions::Commit()
246 : {
247 0 : uno::Sequence< OUString > aNames = GetPropertyNames();
248 0 : uno::Sequence< uno::Any > aValues( aNames.getLength());
249 :
250 0 : if( aValues.getLength() >= 1 )
251 : {
252 : // 1. default colors for series
253 : // convert list to sequence
254 0 : const size_t nCount = maDefColors.size();
255 0 : uno::Sequence< sal_Int64 > aColors( nCount );
256 0 : for( size_t i=0; i < nCount; i++ )
257 : {
258 0 : ColorData aData = maDefColors.getColorData( i );
259 0 : aColors[ i ] = aData;
260 : }
261 :
262 0 : aValues[ 0 ] <<= aColors;
263 : }
264 :
265 0 : PutProperties( aNames, aValues );
266 0 : }
267 :
268 0 : void SvxChartOptions::Notify( const com::sun::star::uno::Sequence< OUString >& )
269 : {
270 0 : }
271 :
272 : // --------------------
273 : // class SvxChartColorTableItem
274 : // --------------------
275 :
276 0 : SvxChartColorTableItem::SvxChartColorTableItem( sal_uInt16 nWhich_, const SvxChartColorTable& aTable ) :
277 : SfxPoolItem( nWhich_ ),
278 0 : m_aColorTable( aTable )
279 : {
280 0 : }
281 :
282 0 : SvxChartColorTableItem::SvxChartColorTableItem( const SvxChartColorTableItem& rOther ) :
283 : SfxPoolItem( rOther ),
284 0 : m_aColorTable( rOther.m_aColorTable )
285 : {
286 0 : }
287 :
288 0 : SfxPoolItem* SvxChartColorTableItem::Clone( SfxItemPool * ) const
289 : {
290 0 : return new SvxChartColorTableItem( *this );
291 : }
292 :
293 0 : int SvxChartColorTableItem::operator==( const SfxPoolItem& rAttr ) const
294 : {
295 : DBG_ASSERT( SfxPoolItem::operator==( rAttr ), "SvxChartColorTableItem::operator== : types differ" );
296 :
297 0 : const SvxChartColorTableItem * rCTItem( dynamic_cast< const SvxChartColorTableItem * >( & rAttr ));
298 0 : if( rCTItem )
299 : {
300 0 : return (this->m_aColorTable == rCTItem->GetColorList());
301 : }
302 :
303 0 : return 0;
304 : }
305 :
306 0 : void SvxChartColorTableItem::SetOptions( SvxChartOptions* pOpts ) const
307 : {
308 0 : if ( pOpts )
309 0 : pOpts->SetDefaultColors( m_aColorTable );
310 0 : }
311 :
312 :
313 0 : SvxChartColorTable & SvxChartColorTableItem::GetColorList()
314 : {
315 0 : return m_aColorTable;
316 : }
317 :
318 0 : const SvxChartColorTable & SvxChartColorTableItem::GetColorList() const
319 : {
320 0 : return m_aColorTable;
321 : }
322 :
323 0 : void SvxChartColorTableItem::ReplaceColorByIndex( size_t _nIndex, const XColorEntry & _rEntry )
324 : {
325 0 : m_aColorTable.replace( _nIndex, _rEntry );
326 0 : }
327 :
328 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|