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