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