1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_cui.hxx" 26 27 #include <com/sun/star/uno/Sequence.hxx> 28 // header for SvStream 29 #include <tools/stream.hxx> 30 // header for SAL_STATIC_CAST 31 #include <sal/types.h> 32 #include "cfgchart.hxx" 33 #include <dialmgr.hxx> 34 #include <cuires.hrc> 35 36 #define ROW_COLOR_COUNT 12 37 38 using namespace com::sun::star; 39 40 TYPEINIT1( SvxChartColorTableItem, SfxPoolItem ); 41 42 SvxChartColorTable::SvxChartColorTable() 43 {} 44 45 SvxChartColorTable::SvxChartColorTable( const SvxChartColorTable & _rSource ) : 46 m_aColorEntries( _rSource.m_aColorEntries ) 47 {} 48 49 // accessors 50 size_t SvxChartColorTable::size() const 51 { 52 return m_aColorEntries.size(); 53 } 54 55 const XColorEntry & SvxChartColorTable::operator[]( size_t _nIndex ) const 56 { 57 if ( _nIndex >= m_aColorEntries.size() ) 58 { 59 DBG_ERRORFILE( "SvxChartColorTable::[] invalid index" ); 60 return m_aColorEntries[ 0 ]; 61 } 62 63 return m_aColorEntries[ _nIndex ]; 64 } 65 66 ColorData SvxChartColorTable::getColorData( size_t _nIndex ) const 67 { 68 if ( _nIndex >= m_aColorEntries.size() ) 69 { 70 DBG_ERRORFILE( "SvxChartColorTable::getColorData invalid index" ); 71 return COL_BLACK; 72 } 73 74 // GetColor should be const but unfortunately isn't 75 return const_cast< XColorEntry & >( m_aColorEntries[ _nIndex ] ).GetColor().GetRGBColor(); 76 } 77 78 // mutators 79 void SvxChartColorTable::clear() 80 { 81 m_aColorEntries.clear(); 82 } 83 84 void SvxChartColorTable::append( const XColorEntry & _rEntry ) 85 { 86 m_aColorEntries.push_back( _rEntry ); 87 } 88 89 void SvxChartColorTable::replace( size_t _nIndex, const XColorEntry & _rEntry ) 90 { 91 DBG_ASSERT( _nIndex <= m_aColorEntries.size(), 92 "SvxChartColorTable::replace invalid index" ); 93 94 Color aCol1 = m_aColorEntries[ _nIndex ].GetColor(), aCol2; 95 m_aColorEntries[ _nIndex ] = _rEntry; 96 aCol2 = m_aColorEntries[ _nIndex ].GetColor(); 97 if ( aCol2 != const_cast< XColorEntry& >( _rEntry ).GetColor() ) 98 { 99 DBG_ERRORFILE( "wrong color" ); 100 } 101 } 102 103 void SvxChartColorTable::useDefault() 104 { 105 ColorData aColors[] = { 106 RGB_COLORDATA( 0x00, 0x45, 0x86 ), 107 RGB_COLORDATA( 0xff, 0x42, 0x0e ), 108 RGB_COLORDATA( 0xff, 0xd3, 0x20 ), 109 RGB_COLORDATA( 0x57, 0x9d, 0x1c ), 110 RGB_COLORDATA( 0x7e, 0x00, 0x21 ), 111 RGB_COLORDATA( 0x83, 0xca, 0xff ), 112 RGB_COLORDATA( 0x31, 0x40, 0x04 ), 113 RGB_COLORDATA( 0xae, 0xcf, 0x00 ), 114 RGB_COLORDATA( 0x4b, 0x1f, 0x6f ), 115 RGB_COLORDATA( 0xff, 0x95, 0x0e ), 116 RGB_COLORDATA( 0xc5, 0x00, 0x0b ), 117 RGB_COLORDATA( 0x00, 0x84, 0xd1 ) 118 }; 119 120 clear(); 121 122 String aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) ); 123 String aPrefix, aPostfix, aName; 124 xub_StrLen nPos = aResName.SearchAscii( "$(ROW)" ); 125 if( nPos != STRING_NOTFOUND ) 126 { 127 aPrefix = String( aResName, 0, nPos ); 128 aPostfix = String( aResName, nPos + sizeof( "$(ROW)" ) - 1, STRING_LEN ); 129 } 130 else 131 aPrefix = aResName; 132 133 for( sal_Int32 i=0; i<ROW_COLOR_COUNT; i++ ) 134 { 135 aName = aPrefix; 136 aName.Append( String::CreateFromInt32( i + 1 )); 137 aName.Append( aPostfix ); 138 139 append( XColorEntry( aColors[ i % sizeof( aColors ) ], aName )); 140 } 141 } 142 143 // comparison 144 bool SvxChartColorTable::operator==( const SvxChartColorTable & _rOther ) const 145 { 146 // note: XColorEntry has no operator == 147 bool bEqual = ( this->m_aColorEntries.size() == _rOther.m_aColorEntries.size() ); 148 149 if( bEqual ) 150 { 151 for( size_t i = 0; i < m_aColorEntries.size(); ++i ) 152 { 153 if( getColorData( i ) != _rOther.getColorData( i )) 154 { 155 bEqual = false; 156 break; 157 } 158 } 159 } 160 161 return bEqual; 162 } 163 164 // ==================== 165 // class SvxChartOptions 166 // ==================== 167 168 SvxChartOptions::SvxChartOptions() : 169 ::utl::ConfigItem( rtl::OUString::createFromAscii( "Office.Chart" )), 170 mbIsInitialized( sal_False ) 171 { 172 maPropertyNames.realloc( 1 ); 173 maPropertyNames[ 0 ] = ::rtl::OUString::createFromAscii( "DefaultColor/Series" ); 174 } 175 176 SvxChartOptions::~SvxChartOptions() 177 { 178 } 179 180 const SvxChartColorTable& SvxChartOptions::GetDefaultColors() 181 { 182 if ( !mbIsInitialized ) 183 mbIsInitialized = RetrieveOptions(); 184 return maDefColors; 185 } 186 187 void SvxChartOptions::SetDefaultColors( const SvxChartColorTable& aCol ) 188 { 189 maDefColors = aCol; 190 SetModified(); 191 } 192 193 sal_Bool SvxChartOptions::RetrieveOptions() 194 { 195 // get sequence containing all properties 196 197 uno::Sequence< ::rtl::OUString > aNames = GetPropertyNames(); 198 uno::Sequence< uno::Any > aProperties( aNames.getLength()); 199 aProperties = GetProperties( aNames ); 200 201 if( aProperties.getLength() == aNames.getLength()) 202 { 203 // 1. default colors for series 204 maDefColors.clear(); 205 uno::Sequence< sal_Int64 > aColorSeq; 206 aProperties[ 0 ] >>= aColorSeq; 207 208 sal_Int32 nCount = aColorSeq.getLength(); 209 Color aCol; 210 211 // create strings for entry names 212 String aResName( CUI_RES( RID_SVXSTR_DIAGRAM_ROW ) ); 213 String aPrefix, aPostfix, aName; 214 xub_StrLen nPos = aResName.SearchAscii( "$(ROW)" ); 215 if( nPos != STRING_NOTFOUND ) 216 { 217 aPrefix = String( aResName, 0, nPos ); 218 aPostfix = String( aResName, nPos + sizeof( "$(ROW)" ) - 1, STRING_LEN ); 219 } 220 else 221 aPrefix = aResName; 222 223 // set color values 224 for( sal_Int32 i=0; i < nCount; i++ ) 225 { 226 aCol.SetColor( SAL_STATIC_CAST( ColorData, aColorSeq[ i ] )); 227 228 aName = aPrefix; 229 aName.Append( String::CreateFromInt32( i + 1 )); 230 aName.Append( aPostfix ); 231 232 maDefColors.append( XColorEntry( aCol, aName )); 233 } 234 return sal_True; 235 } 236 return sal_False; 237 } 238 239 void SvxChartOptions::Commit() 240 { 241 uno::Sequence< ::rtl::OUString > aNames = GetPropertyNames(); 242 uno::Sequence< uno::Any > aValues( aNames.getLength()); 243 244 if( aValues.getLength() >= 1 ) 245 { 246 // 1. default colors for series 247 // convert list to sequence 248 const size_t nCount = maDefColors.size(); 249 uno::Sequence< sal_Int64 > aColors( nCount ); 250 for( size_t i=0; i < nCount; i++ ) 251 { 252 ColorData aData = maDefColors.getColorData( i ); 253 aColors[ i ] = aData; 254 } 255 256 aValues[ 0 ] <<= aColors; 257 } 258 259 PutProperties( aNames, aValues ); 260 } 261 262 void SvxChartOptions::Notify( const com::sun::star::uno::Sequence< rtl::OUString >& ) 263 { 264 } 265 266 // -------------------- 267 // class SvxChartColorTableItem 268 // -------------------- 269 270 SvxChartColorTableItem::SvxChartColorTableItem( sal_uInt16 nWhich_, const SvxChartColorTable& aTable ) : 271 SfxPoolItem( nWhich_ ), 272 m_aColorTable( aTable ) 273 { 274 } 275 276 SvxChartColorTableItem::SvxChartColorTableItem( const SvxChartColorTableItem& rOther ) : 277 SfxPoolItem( rOther ), 278 m_aColorTable( rOther.m_aColorTable ) 279 { 280 } 281 282 SfxPoolItem* __EXPORT SvxChartColorTableItem::Clone( SfxItemPool * ) const 283 { 284 return new SvxChartColorTableItem( *this ); 285 } 286 287 int __EXPORT SvxChartColorTableItem::operator==( const SfxPoolItem& rAttr ) const 288 { 289 DBG_ASSERT( SfxPoolItem::operator==( rAttr ), "SvxChartColorTableItem::operator== : types differ" ); 290 291 const SvxChartColorTableItem * rCTItem( dynamic_cast< const SvxChartColorTableItem * >( & rAttr )); 292 if( rCTItem ) 293 { 294 return (this->m_aColorTable == rCTItem->GetColorTable()); 295 } 296 297 return 0; 298 } 299 300 void __EXPORT SvxChartColorTableItem::SetOptions( SvxChartOptions* pOpts ) const 301 { 302 if ( pOpts ) 303 pOpts->SetDefaultColors( m_aColorTable ); 304 } 305 306 307 SvxChartColorTable & SvxChartColorTableItem::GetColorTable() 308 { 309 return m_aColorTable; 310 } 311 312 const SvxChartColorTable & SvxChartColorTableItem::GetColorTable() const 313 { 314 return m_aColorTable; 315 } 316 317 void SvxChartColorTableItem::ReplaceColorByIndex( size_t _nIndex, const XColorEntry & _rEntry ) 318 { 319 m_aColorTable.replace( _nIndex, _rEntry ); 320 } 321