bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / options / cfgchart.cxx
blob0e21077b69310f3a08dba3f8309b21a89e595b2d
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>
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()
33 : nNextElementNumber(0)
37 SvxChartColorTable::SvxChartColorTable(const SvxChartColorTable & _rSource)
38 : m_aColorEntries(_rSource.m_aColorEntries)
39 , nNextElementNumber(m_aColorEntries.size() + 1)
43 // accessors
44 size_t SvxChartColorTable::size() const
46 return m_aColorEntries.size();
49 const XColorEntry & SvxChartColorTable::operator[]( size_t _nIndex ) const
51 if ( _nIndex >= m_aColorEntries.size() )
53 SAL_WARN( "cui.options", "SvxChartColorTable::[] invalid index" );
54 return m_aColorEntries[ 0 ];
57 return m_aColorEntries[ _nIndex ];
60 ColorData SvxChartColorTable::getColorData( size_t _nIndex ) const
62 if ( _nIndex >= m_aColorEntries.size() )
64 SAL_WARN( "cui.options", "SvxChartColorTable::getColorData invalid index" );
65 return COL_BLACK;
68 return m_aColorEntries[ _nIndex ].GetColor().GetRGBColor();
71 // mutators
72 void SvxChartColorTable::clear()
74 m_aColorEntries.clear();
75 nNextElementNumber = 1;
78 void SvxChartColorTable::append( const XColorEntry & _rEntry )
80 m_aColorEntries.push_back( _rEntry );
83 void SvxChartColorTable::remove( size_t _nIndex )
85 if (m_aColorEntries.size() > 0)
86 m_aColorEntries.erase( m_aColorEntries.begin() + _nIndex);
88 for (size_t i=0 ; i<m_aColorEntries.size(); i++)
90 m_aColorEntries[ i ].SetName( getDefaultName( i ) );
94 void SvxChartColorTable::replace( size_t _nIndex, const XColorEntry & _rEntry )
96 DBG_ASSERT( _nIndex <= m_aColorEntries.size(),
97 "SvxChartColorTable::replace invalid index" );
99 m_aColorEntries[ _nIndex ] = _rEntry;
102 void SvxChartColorTable::useDefault()
104 ColorData aColors[] = {
105 RGB_COLORDATA( 0x00, 0x45, 0x86 ),
106 RGB_COLORDATA( 0xff, 0x42, 0x0e ),
107 RGB_COLORDATA( 0xff, 0xd3, 0x20 ),
108 RGB_COLORDATA( 0x57, 0x9d, 0x1c ),
109 RGB_COLORDATA( 0x7e, 0x00, 0x21 ),
110 RGB_COLORDATA( 0x83, 0xca, 0xff ),
111 RGB_COLORDATA( 0x31, 0x40, 0x04 ),
112 RGB_COLORDATA( 0xae, 0xcf, 0x00 ),
113 RGB_COLORDATA( 0x4b, 0x1f, 0x6f ),
114 RGB_COLORDATA( 0xff, 0x95, 0x0e ),
115 RGB_COLORDATA( 0xc5, 0x00, 0x0b ),
116 RGB_COLORDATA( 0x00, 0x84, 0xd1 )
119 clear();
121 for( sal_Int32 i=0; i<ROW_COLOR_COUNT; i++ )
123 append( XColorEntry( aColors[ i % sizeof( aColors ) ], getDefaultName( i ) ));
127 OUString SvxChartColorTable::getDefaultName( size_t _nIndex )
129 OUString aName;
131 if (sDefaultNamePrefix.getLength() == 0)
133 OUString aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) );
134 sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
135 if( nPos != -1 )
137 sDefaultNamePrefix = aResName.copy( 0, nPos );
138 sDefaultNamePostfix = aResName.copy( nPos + sizeof( "$(ROW)" ) - 1 );
140 else
142 sDefaultNamePrefix = aResName;
146 aName = sDefaultNamePrefix + OUString::number(_nIndex + 1) + sDefaultNamePostfix;
147 nNextElementNumber++;
149 return aName;
152 // comparison
153 bool SvxChartColorTable::operator==( const SvxChartColorTable & _rOther ) const
155 // note: XColorEntry has no operator ==
156 bool bEqual = ( this->m_aColorEntries.size() == _rOther.m_aColorEntries.size() );
158 if( bEqual )
160 for( size_t i = 0; i < m_aColorEntries.size(); ++i )
162 if( getColorData( i ) != _rOther.getColorData( i ))
164 bEqual = false;
165 break;
170 return bEqual;
174 // class SvxChartOptions
177 SvxChartOptions::SvxChartOptions() :
178 ::utl::ConfigItem( OUString("Office.Chart") ),
179 mbIsInitialized( false )
181 maPropertyNames.realloc( 1 );
182 maPropertyNames[ 0 ] = "DefaultColor/Series";
185 SvxChartOptions::~SvxChartOptions()
189 const SvxChartColorTable& SvxChartOptions::GetDefaultColors()
191 if ( !mbIsInitialized )
192 mbIsInitialized = RetrieveOptions();
193 return maDefColors;
196 void SvxChartOptions::SetDefaultColors( const SvxChartColorTable& aCol )
198 maDefColors = aCol;
199 SetModified();
202 bool SvxChartOptions::RetrieveOptions()
204 // get sequence containing all properties
206 uno::Sequence< OUString > aNames = GetPropertyNames();
207 uno::Sequence< uno::Any > aProperties( aNames.getLength());
208 aProperties = GetProperties( aNames );
210 if( aProperties.getLength() == aNames.getLength())
212 // 1. default colors for series
213 maDefColors.clear();
214 uno::Sequence< sal_Int64 > aColorSeq;
215 aProperties[ 0 ] >>= aColorSeq;
217 sal_Int32 nCount = aColorSeq.getLength();
218 Color aCol;
220 // create strings for entry names
221 OUString aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) );
222 OUString aPrefix, aPostfix, aName;
223 sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
224 if( nPos != -1 )
226 aPrefix = aResName.copy( 0, nPos );
227 sal_Int32 idx = nPos + sizeof( "$(ROW)" ) - 1;
228 aPostfix = aResName.copy( idx, aResName.getLength()-idx );
230 else
231 aPrefix = aResName;
233 // set color values
234 for( sal_Int32 i=0; i < nCount; i++ )
236 aCol.SetColor( (static_cast< ColorData >(aColorSeq[ i ] )));
238 aName = aPrefix + OUString::number(i + 1) + aPostfix;
240 maDefColors.append( XColorEntry( aCol, aName ));
242 return true;
244 return false;
247 void SvxChartOptions::ImplCommit()
249 uno::Sequence< OUString > aNames = GetPropertyNames();
250 uno::Sequence< uno::Any > aValues( aNames.getLength());
252 if( aValues.getLength() >= 1 )
254 // 1. default colors for series
255 // convert list to sequence
256 const size_t nCount = maDefColors.size();
257 uno::Sequence< sal_Int64 > aColors( nCount );
258 for( size_t i=0; i < nCount; i++ )
260 ColorData aData = maDefColors.getColorData( i );
261 aColors[ i ] = aData;
264 aValues[ 0 ] <<= aColors;
267 PutProperties( aNames, aValues );
270 void SvxChartOptions::Notify( const com::sun::star::uno::Sequence< OUString >& )
275 // class SvxChartColorTableItem
278 SvxChartColorTableItem::SvxChartColorTableItem( sal_uInt16 nWhich_, const SvxChartColorTable& aTable ) :
279 SfxPoolItem( nWhich_ ),
280 m_aColorTable( aTable )
284 SvxChartColorTableItem::SvxChartColorTableItem( const SvxChartColorTableItem& rOther ) :
285 SfxPoolItem( rOther ),
286 m_aColorTable( rOther.m_aColorTable )
290 SfxPoolItem* SvxChartColorTableItem::Clone( SfxItemPool * ) const
292 return new SvxChartColorTableItem( *this );
295 bool SvxChartColorTableItem::operator==( const SfxPoolItem& rAttr ) const
297 DBG_ASSERT( SfxPoolItem::operator==( rAttr ), "SvxChartColorTableItem::operator== : types differ" );
299 const SvxChartColorTableItem * rCTItem( dynamic_cast< const SvxChartColorTableItem * >( & rAttr ));
300 if( rCTItem )
302 return (this->m_aColorTable == rCTItem->GetColorList());
305 return false;
308 void SvxChartColorTableItem::SetOptions( SvxChartOptions* pOpts ) const
310 if ( pOpts )
311 pOpts->SetDefaultColors( m_aColorTable );
317 void SvxChartColorTableItem::ReplaceColorByIndex( size_t _nIndex, const XColorEntry & _rEntry )
319 m_aColorTable.replace( _nIndex, _rEntry );
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */