1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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 )
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" );
65 // GetColor should be const but unfortunately isn't
66 return const_cast< XColorEntry
& >( m_aColorEntries
[ _nIndex
] ).GetColor().GetRGBColor();
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 )
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
)
129 if (sDefaultNamePrefix
.Len() == 0)
131 String
aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW
) );
132 xub_StrLen nPos
= aResName
.SearchAscii( "$(ROW)" );
133 if( nPos
!= STRING_NOTFOUND
)
135 sDefaultNamePrefix
= String( aResName
, 0, nPos
);
136 sDefaultNamePostfix
= String( aResName
, nPos
+ sizeof( "$(ROW)" ) - 1, STRING_LEN
);
140 sDefaultNamePrefix
= aResName
;
144 aName
= sDefaultNamePrefix
;
145 aName
.Append( String::CreateFromInt32 ( _nIndex
+ 1 ) );
146 aName
.Append( sDefaultNamePostfix
);
147 nNextElementNumber
++;
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() );
160 for( size_t i
= 0; i
< m_aColorEntries
.size(); ++i
)
162 if( getColorData( i
) != _rOther
.getColorData( i
))
173 // ====================
174 // class SvxChartOptions
175 // ====================
177 SvxChartOptions::SvxChartOptions() :
178 ::utl::ConfigItem( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Chart") ) ),
179 mbIsInitialized( sal_False
)
181 maPropertyNames
.realloc( 1 );
182 maPropertyNames
[ 0 ] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultColor/Series") );
185 SvxChartOptions::~SvxChartOptions()
189 const SvxChartColorTable
& SvxChartOptions::GetDefaultColors()
191 if ( !mbIsInitialized
)
192 mbIsInitialized
= RetrieveOptions();
196 void SvxChartOptions::SetDefaultColors( const SvxChartColorTable
& aCol
)
202 sal_Bool
SvxChartOptions::RetrieveOptions()
204 // get sequence containing all properties
206 uno::Sequence
< ::rtl::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
214 uno::Sequence
< sal_Int64
> aColorSeq
;
215 aProperties
[ 0 ] >>= aColorSeq
;
217 sal_Int32 nCount
= aColorSeq
.getLength();
220 // create strings for entry names
221 String
aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW
) );
222 String aPrefix
, aPostfix
, aName
;
223 xub_StrLen nPos
= aResName
.SearchAscii( "$(ROW)" );
224 if( nPos
!= STRING_NOTFOUND
)
226 aPrefix
= String( aResName
, 0, nPos
);
227 aPostfix
= String( aResName
, nPos
+ sizeof( "$(ROW)" ) - 1, STRING_LEN
);
233 for( sal_Int32 i
=0; i
< nCount
; i
++ )
235 aCol
.SetColor( (static_cast< ColorData
>(aColorSeq
[ i
] )));
238 aName
.Append( String::CreateFromInt32( i
+ 1 ));
239 aName
.Append( aPostfix
);
241 maDefColors
.append( XColorEntry( aCol
, aName
));
248 void SvxChartOptions::Commit()
250 uno::Sequence
< ::rtl::OUString
> aNames
= GetPropertyNames();
251 uno::Sequence
< uno::Any
> aValues( aNames
.getLength());
253 if( aValues
.getLength() >= 1 )
255 // 1. default colors for series
256 // convert list to sequence
257 const size_t nCount
= maDefColors
.size();
258 uno::Sequence
< sal_Int64
> aColors( nCount
);
259 for( size_t i
=0; i
< nCount
; i
++ )
261 ColorData aData
= maDefColors
.getColorData( i
);
262 aColors
[ i
] = aData
;
265 aValues
[ 0 ] <<= aColors
;
268 PutProperties( aNames
, aValues
);
271 void SvxChartOptions::Notify( const com::sun::star::uno::Sequence
< rtl::OUString
>& )
275 // --------------------
276 // class SvxChartColorTableItem
277 // --------------------
279 SvxChartColorTableItem::SvxChartColorTableItem( sal_uInt16 nWhich_
, const SvxChartColorTable
& aTable
) :
280 SfxPoolItem( nWhich_
),
281 m_aColorTable( aTable
)
285 SvxChartColorTableItem::SvxChartColorTableItem( const SvxChartColorTableItem
& rOther
) :
286 SfxPoolItem( rOther
),
287 m_aColorTable( rOther
.m_aColorTable
)
291 SfxPoolItem
* SvxChartColorTableItem::Clone( SfxItemPool
* ) const
293 return new SvxChartColorTableItem( *this );
296 int SvxChartColorTableItem::operator==( const SfxPoolItem
& rAttr
) const
298 DBG_ASSERT( SfxPoolItem::operator==( rAttr
), "SvxChartColorTableItem::operator== : types differ" );
300 const SvxChartColorTableItem
* rCTItem( dynamic_cast< const SvxChartColorTableItem
* >( & rAttr
));
303 return (this->m_aColorTable
== rCTItem
->GetColorList());
309 void SvxChartColorTableItem::SetOptions( SvxChartOptions
* pOpts
) const
312 pOpts
->SetDefaultColors( m_aColorTable
);
316 SvxChartColorTable
& SvxChartColorTableItem::GetColorList()
318 return m_aColorTable
;
321 const SvxChartColorTable
& SvxChartColorTableItem::GetColorList() const
323 return m_aColorTable
;
326 void SvxChartColorTableItem::ReplaceColorByIndex( size_t _nIndex
, const XColorEntry
& _rEntry
)
328 m_aColorTable
.replace( _nIndex
, _rEntry
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */