LibreOffice Module cui (master)  1
cfgchart.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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 
27 #define ROW_COLOR_COUNT 12
28 
29 using namespace com::sun::star;
30 
31 // accessors
33 {
34  return m_aColorEntries.size();
35 }
36 
37 const XColorEntry & SvxChartColorTable::operator[]( size_t _nIndex ) const
38 {
39  if ( _nIndex >= m_aColorEntries.size() )
40  {
41  SAL_WARN( "cui.options", "SvxChartColorTable::[] invalid index" );
42  return m_aColorEntries[ 0 ];
43  }
44 
45  return m_aColorEntries[ _nIndex ];
46 }
47 
48 Color SvxChartColorTable::getColor( size_t _nIndex ) const
49 {
50  if ( _nIndex >= m_aColorEntries.size() )
51  {
52  SAL_WARN( "cui.options", "SvxChartColorTable::getColorData invalid index" );
53  return COL_BLACK;
54  }
55 
56  return m_aColorEntries[ _nIndex ].GetColor().GetRGBColor();
57 }
58 
59 // mutators
61 {
62  m_aColorEntries.clear();
63 }
64 
65 void SvxChartColorTable::append( const XColorEntry & _rEntry )
66 {
67  m_aColorEntries.push_back( _rEntry );
68 }
69 
70 void SvxChartColorTable::remove( size_t _nIndex )
71 {
72  if (!m_aColorEntries.empty())
73  m_aColorEntries.erase( m_aColorEntries.begin() + _nIndex);
74 
75  for (size_t i=0 ; i<m_aColorEntries.size(); i++)
76  {
77  m_aColorEntries[ i ].SetName( getDefaultName( i ) );
78  }
79 }
80 
81 void SvxChartColorTable::replace( size_t _nIndex, const XColorEntry & _rEntry )
82 {
83  DBG_ASSERT( _nIndex <= m_aColorEntries.size(),
84  "SvxChartColorTable::replace invalid index" );
85 
86  m_aColorEntries[ _nIndex ] = _rEntry;
87 }
88 
90 {
91  static const Color aColors[] = {
92  Color( 0x00, 0x45, 0x86 ),
93  Color( 0xff, 0x42, 0x0e ),
94  Color( 0xff, 0xd3, 0x20 ),
95  Color( 0x57, 0x9d, 0x1c ),
96  Color( 0x7e, 0x00, 0x21 ),
97  Color( 0x83, 0xca, 0xff ),
98  Color( 0x31, 0x40, 0x04 ),
99  Color( 0xae, 0xcf, 0x00 ),
100  Color( 0x4b, 0x1f, 0x6f ),
101  Color( 0xff, 0x95, 0x0e ),
102  Color( 0xc5, 0x00, 0x0b ),
103  Color( 0x00, 0x84, 0xd1 )
104  };
105 
106  clear();
107 
108  for( sal_Int32 i=0; i<ROW_COLOR_COUNT; i++ )
109  {
110  append( XColorEntry( aColors[ i % sizeof( aColors ) ], getDefaultName( i ) ));
111  }
112 }
113 
114 OUString SvxChartColorTable::getDefaultName( size_t _nIndex )
115 {
116  OUString aName;
117 
118  OUString sDefaultNamePrefix;
119  OUString sDefaultNamePostfix;
120  OUString aResName( CuiResId( RID_SVXSTR_DIAGRAM_ROW ) );
121  sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
122  if( nPos != -1 )
123  {
124  sDefaultNamePrefix = aResName.copy( 0, nPos );
125  sDefaultNamePostfix = aResName.copy( nPos + sizeof( "$(ROW)" ) - 1 );
126  }
127  else
128  {
129  sDefaultNamePrefix = aResName;
130  }
131 
132  aName = sDefaultNamePrefix + OUString::number(_nIndex + 1) + sDefaultNamePostfix;
133 
134  return aName;
135 }
136 
137 // comparison
139 {
140  // note: XColorEntry has no operator ==
141  bool bEqual = ( m_aColorEntries.size() == _rOther.m_aColorEntries.size() );
142 
143  if( bEqual )
144  {
145  for( size_t i = 0; i < m_aColorEntries.size(); ++i )
146  {
147  if( getColor( i ) != _rOther.getColor( i ))
148  {
149  bEqual = false;
150  break;
151  }
152  }
153  }
154 
155  return bEqual;
156 }
157 
158 
159 // class SvxChartOptions
160 
161 
163  ::utl::ConfigItem( "Office.Chart" ),
164  mbIsInitialized( false )
165 {
166  maPropertyNames.realloc( 1 );
167  maPropertyNames[ 0 ] = "DefaultColor/Series";
168 }
169 
171 {
172 }
173 
175 {
176  if ( !mbIsInitialized )
178  return maDefColors;
179 }
180 
182 {
183  maDefColors = aCol;
184  SetModified();
185 }
186 
188 {
189  // get sequence containing all properties
190 
191  uno::Sequence< OUString > aNames = GetPropertyNames();
192  uno::Sequence< uno::Any > aProperties( aNames.getLength());
193  aProperties = GetProperties( aNames );
194 
195  if( aProperties.getLength() == aNames.getLength())
196  {
197  // 1. default colors for series
198  maDefColors.clear();
199  uno::Sequence< sal_Int64 > aColorSeq;
200  aProperties[ 0 ] >>= aColorSeq;
201 
202  sal_Int32 nCount = aColorSeq.getLength();
203  Color aCol;
204 
205  // create strings for entry names
206  OUString aResName( CuiResId( RID_SVXSTR_DIAGRAM_ROW ) );
207  OUString aPrefix, aPostfix, aName;
208  sal_Int32 nPos = aResName.indexOf( "$(ROW)" );
209  if( nPos != -1 )
210  {
211  aPrefix = aResName.copy( 0, nPos );
212  sal_Int32 idx = nPos + sizeof( "$(ROW)" ) - 1;
213  aPostfix = aResName.copy( idx );
214  }
215  else
216  aPrefix = aResName;
217 
218  // set color values
219  for( sal_Int32 i=0; i < nCount; i++ )
220  {
221  aCol = Color(aColorSeq[ i ]);
222 
223  aName = aPrefix + OUString::number(i + 1) + aPostfix;
224 
225  maDefColors.append( XColorEntry( aCol, aName ));
226  }
227  return true;
228  }
229  return false;
230 }
231 
233 {
234  uno::Sequence< OUString > aNames = GetPropertyNames();
235  uno::Sequence< uno::Any > aValues( aNames.getLength());
236 
237  if( aValues.hasElements() )
238  {
239  // 1. default colors for series
240  // convert list to sequence
241  const size_t nCount = maDefColors.size();
242  uno::Sequence< sal_Int64 > aColors( nCount );
243  for( size_t i=0; i < nCount; i++ )
244  {
246  aColors[ i ] = sal_uInt32(aData);
247  }
248 
249  aValues[ 0 ] <<= aColors;
250  }
251 
252  PutProperties( aNames, aValues );
253 }
254 
255 void SvxChartOptions::Notify( const css::uno::Sequence< OUString >& )
256 {
257 }
258 
259 
260 // class SvxChartColorTableItem
261 
262 
264  SfxPoolItem( nWhich_ ),
265  m_aColorTable( aTable )
266 {
267 }
268 
270 {
271  return new SvxChartColorTableItem( *this );
272 }
273 
275 {
276  assert(SfxPoolItem::operator==(rAttr));
277 
278  return static_cast<const SvxChartColorTableItem & >( rAttr ).m_aColorTable == m_aColorTable;
279 }
280 
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
const char aData[]
virtual void ImplCommit() override
Definition: cfgchart.cxx:232
static OUString getDefaultName(size_t _nIndex)
Definition: cfgchart.cxx:114
virtual ~SvxChartOptions() override
Definition: cfgchart.cxx:170
SvxChartColorTable m_aColorTable
Definition: cfgchart.hxx:98
void append(const XColorEntry &_rEntry)
Definition: cfgchart.cxx:65
size_t size() const
Definition: cfgchart.cxx:32
SvxChartColorTable maDefColors
Definition: cfgchart.hxx:58
bool mbIsInitialized
Definition: cfgchart.hxx:59
void remove(size_t _nIndex)
Definition: cfgchart.cxx:70
const css::uno::Sequence< OUString > & GetPropertyNames() const
Definition: cfgchart.hxx:64
const SvxChartColorTable & GetDefaultColors()
Definition: cfgchart.cxx:174
#define DBG_ASSERT(sCon, aError)
bool PutProperties(const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues)
bool operator==(const SvxChartColorTable &_rOther) const
Definition: cfgchart.cxx:138
int i
SvxChartColorTableItem(sal_uInt16 nWhich, const SvxChartColorTable &)
Definition: cfgchart.cxx:263
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
const XColorEntry & operator[](size_t _nIndex) const
Definition: cfgchart.cxx:37
virtual void Notify(const css::uno::Sequence< OUString > &_rPropertyNames) override
Definition: cfgchart.cxx:255
void replace(size_t _nIndex, const XColorEntry &_rEntry)
Definition: cfgchart.cxx:81
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
virtual bool operator==(const SfxPoolItem &) const override
Definition: cfgchart.cxx:274
Color getColor(size_t _nIndex) const
Definition: cfgchart.cxx:48
bool RetrieveOptions()
Definition: cfgchart.cxx:187
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cfgchart.cxx:269
#define ROW_COLOR_COUNT
Definition: cfgchart.cxx:27
std::vector< XColorEntry > m_aColorEntries
Definition: cfgchart.hxx:32
OString const aName
#define SAL_WARN(area, stream)
void SetDefaultColors(const SvxChartColorTable &aCol)
Definition: cfgchart.cxx:181
sal_Int32 nPos
css::uno::Sequence< OUString > maPropertyNames
Definition: cfgchart.hxx:62