update credits
[LibreOffice.git] / cui / source / options / cfgchart.cxx
blobfaac1b59497931de875122a68a88680a6e413f96
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
26 #define ROW_COLOR_COUNT 12
28 using namespace com::sun::star;
30 TYPEINIT1( SvxChartColorTableItem, SfxPoolItem );
32 SvxChartColorTable::SvxChartColorTable()
35 SvxChartColorTable::SvxChartColorTable( const SvxChartColorTable & _rSource ) :
36 m_aColorEntries( _rSource.m_aColorEntries ),
37 nNextElementNumber( m_aColorEntries.size() + 1 )
40 // accessors
41 size_t SvxChartColorTable::size() const
43 return m_aColorEntries.size();
46 const XColorEntry & SvxChartColorTable::operator[]( size_t _nIndex ) const
48 if ( _nIndex >= m_aColorEntries.size() )
50 SAL_WARN( "cui.options", "SvxChartColorTable::[] invalid index" );
51 return m_aColorEntries[ 0 ];
54 return m_aColorEntries[ _nIndex ];
57 ColorData SvxChartColorTable::getColorData( size_t _nIndex ) const
59 if ( _nIndex >= m_aColorEntries.size() )
61 SAL_WARN( "cui.options", "SvxChartColorTable::getColorData invalid index" );
62 return COL_BLACK;
65 // GetColor should be const but unfortunately isn't
66 return const_cast< XColorEntry & >( m_aColorEntries[ _nIndex ] ).GetColor().GetRGBColor();
69 // mutators
70 void SvxChartColorTable::clear()
72 m_aColorEntries.clear();
73 nNextElementNumber = 1;
76 void SvxChartColorTable::append( const XColorEntry & _rEntry )
78 m_aColorEntries.push_back( _rEntry );
81 void SvxChartColorTable::remove( size_t _nIndex )
83 if (m_aColorEntries.size() > 0)
84 m_aColorEntries.erase( m_aColorEntries.begin() + _nIndex);
86 for (size_t i=0 ; i<m_aColorEntries.size(); i++)
88 m_aColorEntries[ i ].SetName( getDefaultName( i ) );
92 void SvxChartColorTable::replace( size_t _nIndex, const XColorEntry & _rEntry )
94 DBG_ASSERT( _nIndex <= m_aColorEntries.size(),
95 "SvxChartColorTable::replace invalid index" );
97 m_aColorEntries[ _nIndex ] = _rEntry;
100 void SvxChartColorTable::useDefault()
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 )
117 clear();
119 for( sal_Int32 i=0; i<ROW_COLOR_COUNT; i++ )
121 append( XColorEntry( aColors[ i % sizeof( aColors ) ], getDefaultName( i ) ));
125 String SvxChartColorTable::getDefaultName( size_t _nIndex )
127 OUString aName;
129 if (sDefaultNamePrefix.getLength() == 0)
131 OUString aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) );
132 sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
133 if( nPos != -1 )
135 sDefaultNamePrefix = aResName.copy( 0, nPos );
136 sDefaultNamePostfix = aResName.copy( nPos + sizeof( "$(ROW)" ) - 1 );
138 else
140 sDefaultNamePrefix = aResName;
144 aName = sDefaultNamePrefix + OUString::number(_nIndex + 1) + sDefaultNamePostfix;
145 nNextElementNumber++;
147 return aName;
150 // comparison
151 bool SvxChartColorTable::operator==( const SvxChartColorTable & _rOther ) const
153 // note: XColorEntry has no operator ==
154 bool bEqual = ( this->m_aColorEntries.size() == _rOther.m_aColorEntries.size() );
156 if( bEqual )
158 for( size_t i = 0; i < m_aColorEntries.size(); ++i )
160 if( getColorData( i ) != _rOther.getColorData( i ))
162 bEqual = false;
163 break;
168 return bEqual;
171 // ====================
172 // class SvxChartOptions
173 // ====================
175 SvxChartOptions::SvxChartOptions() :
176 ::utl::ConfigItem( OUString("Office.Chart") ),
177 mbIsInitialized( sal_False )
179 maPropertyNames.realloc( 1 );
180 maPropertyNames[ 0 ] = OUString("DefaultColor/Series");
183 SvxChartOptions::~SvxChartOptions()
187 const SvxChartColorTable& SvxChartOptions::GetDefaultColors()
189 if ( !mbIsInitialized )
190 mbIsInitialized = RetrieveOptions();
191 return maDefColors;
194 void SvxChartOptions::SetDefaultColors( const SvxChartColorTable& aCol )
196 maDefColors = aCol;
197 SetModified();
200 sal_Bool SvxChartOptions::RetrieveOptions()
202 // get sequence containing all properties
204 uno::Sequence< OUString > aNames = GetPropertyNames();
205 uno::Sequence< uno::Any > aProperties( aNames.getLength());
206 aProperties = GetProperties( aNames );
208 if( aProperties.getLength() == aNames.getLength())
210 // 1. default colors for series
211 maDefColors.clear();
212 uno::Sequence< sal_Int64 > aColorSeq;
213 aProperties[ 0 ] >>= aColorSeq;
215 sal_Int32 nCount = aColorSeq.getLength();
216 Color aCol;
218 // create strings for entry names
219 OUString aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) );
220 OUString aPrefix, aPostfix, aName;
221 sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
222 if( nPos != -1 )
224 aPrefix = aResName.copy( 0, nPos );
225 sal_Int32 idx = nPos + sizeof( "$(ROW)" ) - 1;
226 aPostfix = aResName.copy( idx, aResName.getLength()-idx );
228 else
229 aPrefix = aResName;
231 // set color values
232 for( sal_Int32 i=0; i < nCount; i++ )
234 aCol.SetColor( (static_cast< ColorData >(aColorSeq[ i ] )));
236 aName = aPrefix + OUString::number(i + 1) + aPostfix;
238 maDefColors.append( XColorEntry( aCol, aName ));
240 return sal_True;
242 return sal_False;
245 void SvxChartOptions::Commit()
247 uno::Sequence< OUString > aNames = GetPropertyNames();
248 uno::Sequence< uno::Any > aValues( aNames.getLength());
250 if( aValues.getLength() >= 1 )
252 // 1. default colors for series
253 // convert list to sequence
254 const size_t nCount = maDefColors.size();
255 uno::Sequence< sal_Int64 > aColors( nCount );
256 for( size_t i=0; i < nCount; i++ )
258 ColorData aData = maDefColors.getColorData( i );
259 aColors[ i ] = aData;
262 aValues[ 0 ] <<= aColors;
265 PutProperties( aNames, aValues );
268 void SvxChartOptions::Notify( const com::sun::star::uno::Sequence< OUString >& )
272 // --------------------
273 // class SvxChartColorTableItem
274 // --------------------
276 SvxChartColorTableItem::SvxChartColorTableItem( sal_uInt16 nWhich_, const SvxChartColorTable& aTable ) :
277 SfxPoolItem( nWhich_ ),
278 m_aColorTable( aTable )
282 SvxChartColorTableItem::SvxChartColorTableItem( const SvxChartColorTableItem& rOther ) :
283 SfxPoolItem( rOther ),
284 m_aColorTable( rOther.m_aColorTable )
288 SfxPoolItem* SvxChartColorTableItem::Clone( SfxItemPool * ) const
290 return new SvxChartColorTableItem( *this );
293 int SvxChartColorTableItem::operator==( const SfxPoolItem& rAttr ) const
295 DBG_ASSERT( SfxPoolItem::operator==( rAttr ), "SvxChartColorTableItem::operator== : types differ" );
297 const SvxChartColorTableItem * rCTItem( dynamic_cast< const SvxChartColorTableItem * >( & rAttr ));
298 if( rCTItem )
300 return (this->m_aColorTable == rCTItem->GetColorList());
303 return 0;
306 void SvxChartColorTableItem::SetOptions( SvxChartOptions* pOpts ) const
308 if ( pOpts )
309 pOpts->SetDefaultColors( m_aColorTable );
313 SvxChartColorTable & SvxChartColorTableItem::GetColorList()
315 return m_aColorTable;
318 const SvxChartColorTable & SvxChartColorTableItem::GetColorList() const
320 return m_aColorTable;
323 void SvxChartColorTableItem::ReplaceColorByIndex( size_t _nIndex, const XColorEntry & _rEntry )
325 m_aColorTable.replace( _nIndex, _rEntry );
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */