Use correct object
[LibreOffice.git] / cui / source / options / cfgchart.cxx
blob1ac75f6db361fb2a2d8d8aa3e392c385d5456058
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/debug.hxx>
22 #include <sal/log.hxx>
23 #include "cfgchart.hxx"
24 #include <dialmgr.hxx>
25 #include <strings.hrc>
26 #include <utility>
27 #include <officecfg/Office/Chart.hxx>
29 #define ROW_COLOR_COUNT 12
31 using namespace com::sun::star;
33 // accessors
34 size_t SvxChartColorTable::size() const
36 return m_aColorEntries.size();
39 const XColorEntry & SvxChartColorTable::operator[]( size_t _nIndex ) const
41 if ( _nIndex >= m_aColorEntries.size() )
43 SAL_WARN( "cui.options", "SvxChartColorTable::[] invalid index" );
44 return m_aColorEntries[ 0 ];
47 return m_aColorEntries[ _nIndex ];
50 Color SvxChartColorTable::getColor( size_t _nIndex ) const
52 if ( _nIndex >= m_aColorEntries.size() )
54 SAL_WARN( "cui.options", "SvxChartColorTable::getColorData invalid index" );
55 return COL_BLACK;
58 return m_aColorEntries[ _nIndex ].GetColor().GetRGBColor();
61 // mutators
62 void SvxChartColorTable::clear()
64 m_aColorEntries.clear();
67 void SvxChartColorTable::append( const XColorEntry & _rEntry )
69 m_aColorEntries.push_back( _rEntry );
72 void SvxChartColorTable::remove( size_t _nIndex )
74 if (!m_aColorEntries.empty())
75 m_aColorEntries.erase( m_aColorEntries.begin() + _nIndex);
77 for (size_t i=0 ; i<m_aColorEntries.size(); i++)
79 m_aColorEntries[ i ].SetName( getDefaultName( i ) );
83 void SvxChartColorTable::replace( size_t _nIndex, const XColorEntry & _rEntry )
85 DBG_ASSERT( _nIndex <= m_aColorEntries.size(),
86 "SvxChartColorTable::replace invalid index" );
88 m_aColorEntries[ _nIndex ] = _rEntry;
91 void SvxChartColorTable::useDefault()
93 static const Color aColors[] = {
94 Color( 0x00, 0x45, 0x86 ),
95 Color( 0xff, 0x42, 0x0e ),
96 Color( 0xff, 0xd3, 0x20 ),
97 Color( 0x57, 0x9d, 0x1c ),
98 Color( 0x7e, 0x00, 0x21 ),
99 Color( 0x83, 0xca, 0xff ),
100 Color( 0x31, 0x40, 0x04 ),
101 Color( 0xae, 0xcf, 0x00 ),
102 Color( 0x4b, 0x1f, 0x6f ),
103 Color( 0xff, 0x95, 0x0e ),
104 Color( 0xc5, 0x00, 0x0b ),
105 Color( 0x00, 0x84, 0xd1 )
108 clear();
110 for( sal_Int32 i=0; i<ROW_COLOR_COUNT; i++ )
112 append( XColorEntry( aColors[ i % sizeof( aColors ) ], getDefaultName( i ) ));
116 OUString SvxChartColorTable::getDefaultName( size_t _nIndex )
118 OUString aName;
120 std::u16string_view sDefaultNamePrefix;
121 std::u16string_view sDefaultNamePostfix;
122 OUString aResName( CuiResId( RID_CUISTR_DIAGRAM_ROW ) );
123 sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
124 if( nPos != -1 )
126 sDefaultNamePrefix = aResName.subView( 0, nPos );
127 sDefaultNamePostfix = aResName.subView( nPos + sizeof( "$(ROW)" ) - 1 );
129 else
131 sDefaultNamePrefix = aResName;
134 aName = sDefaultNamePrefix + OUString::number(_nIndex + 1) + sDefaultNamePostfix;
136 return aName;
139 // comparison
140 bool SvxChartColorTable::operator==( const SvxChartColorTable & _rOther ) const
142 // note: XColorEntry has no operator ==
143 bool bEqual = ( m_aColorEntries.size() == _rOther.m_aColorEntries.size() );
145 if( bEqual )
147 for( size_t i = 0; i < m_aColorEntries.size(); ++i )
149 if( getColor( i ) != _rOther.getColor( i ))
151 bEqual = false;
152 break;
157 return bEqual;
163 SvxChartColorTable SvxChartOptions::GetDefaultColors()
165 // 1. default colors for series
166 uno::Sequence< sal_Int64 > aColorSeq = officecfg::Office::Chart::DefaultColor::Series::get();
168 sal_Int32 nCount = aColorSeq.getLength();
169 Color aCol;
171 // create strings for entry names
172 OUString aResName( CuiResId( RID_CUISTR_DIAGRAM_ROW ) );
173 std::u16string_view aPrefix, aPostfix;
174 OUString aName;
175 sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
176 if( nPos != -1 )
178 aPrefix = aResName.subView( 0, nPos );
179 sal_Int32 idx = nPos + sizeof( "$(ROW)" ) - 1;
180 aPostfix = aResName.subView( idx );
182 else
183 aPrefix = aResName;
185 // set color values
186 SvxChartColorTable aDefColors;
187 for( sal_Int32 i=0; i < nCount; i++ )
189 aCol = Color(ColorTransparency, aColorSeq[ i ]);
191 aName = aPrefix + OUString::number(i + 1) + aPostfix;
193 aDefColors.append( XColorEntry( aCol, aName ));
196 return aDefColors;
199 void SvxChartOptions::SetDefaultColors( const SvxChartColorTable& rDefColors )
201 // 1. default colors for series
202 // convert list to sequence
203 const size_t nCount = rDefColors.size();
204 uno::Sequence< sal_Int64 > aColors( nCount );
205 auto aColorsRange = asNonConstRange(aColors);
206 for( size_t i=0; i < nCount; i++ )
208 Color aData = rDefColors.getColor( i );
209 aColorsRange[ i ] = sal_uInt32(aData);
211 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
212 officecfg::Office::Chart::DefaultColor::Series::set(aColors, batch);
213 batch->commit();
217 SvxChartColorTableItem::SvxChartColorTableItem( sal_uInt16 nWhich_, SvxChartColorTable aTable ) :
218 SfxPoolItem( nWhich_, SfxItemType::SvxChartColorTableItemType ),
219 m_aColorTable(std::move( aTable ))
223 SvxChartColorTableItem* SvxChartColorTableItem::Clone( SfxItemPool * ) const
225 return new SvxChartColorTableItem( *this );
228 bool SvxChartColorTableItem::operator==( const SfxPoolItem& rAttr ) const
230 assert(SfxPoolItem::operator==(rAttr));
232 return static_cast<const SvxChartColorTableItem & >( rAttr ).m_aColorTable == m_aColorTable;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */