1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* This file is part of the KDE project
Copyright (C) 2003 Norbert Andres, [email protected]
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <tqdom.h>
#include <tqstringlist.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <KoOasisStyles.h>
#include <KoXmlNS.h>
#include "kspread_doc.h"
#include "kspread_style.h"
#include "kspread_style_manager.h"
using namespace KSpread;
StyleManager::StyleManager()
: m_defaultStyle( new CustomStyle() )
{
}
StyleManager::~StyleManager()
{
delete m_defaultStyle;
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
delete iter.data();
++iter;
}
}
void StyleManager::saveOasis( KoGenStyles &mainStyles )
{
kdDebug() << "StyleManager: Saving default cell style" << endl;
KoGenStyle defaultStyle = KoGenStyle( Doc::STYLE_CELL_USER, "table-cell" );
m_defaultStyle->saveOasis( defaultStyle, mainStyles );
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
kdDebug() << "StyleManager: Saving common cell style " << iter.key() <<endl;
CustomStyle * styleData = iter.data();
KoGenStyle customStyle = KoGenStyle( Doc::STYLE_CELL_USER, "table-cell" );
styleData->saveOasis( customStyle, mainStyles );
++iter;
}
}
void StyleManager::loadOasisStyleTemplate( KoOasisStyles& oasisStyles )
{
// reset the map of OpenDocument Styles
m_oasisStyles.clear();
// loading default style first
const TQDomElement* defaultStyle = oasisStyles.defaultStyle( "table-cell" );
if ( defaultStyle )
{
kdDebug() << "StyleManager: Loading default cell style" << endl;
m_defaultStyle->loadOasis( oasisStyles, *defaultStyle, "Default" );
m_defaultStyle->setType( Style::BUILTIN );
}
else
{
delete m_defaultStyle;
m_defaultStyle = new CustomStyle();
}
// insert it into the the map sorted the OpenDocument name
m_oasisStyles["Default"] = m_defaultStyle;
uint nStyles = oasisStyles.userStyles().count();
for (unsigned int item = 0; item < nStyles; item++) {
TQDomElement styleElem = oasisStyles.userStyles()[item];
// assume the name assigned by the application
const TQString oasisName = styleElem.attributeNS( KoXmlNS::style, "name", TQString() );
// then replace by user-visible one (if any)
const TQString name = styleElem.attributeNS( KoXmlNS::style, "display-name", oasisName );
kdDebug() << " StyleManager: Loading common cell style: " << oasisName << " (display name: " << name << ")" << endl;
if ( !name.isEmpty() )
{
CustomStyle * style = 0;
if ( styleElem.hasAttributeNS( KoXmlNS::style, "parent-style-name" ) )
// The style's parent name will be set in Style::loadOasis(..).
// After all styles are loaded the pointer to the parent is set.
style = new CustomStyle( name, 0 );
else
style = new CustomStyle( name, m_defaultStyle );
//fixme test return;
style->loadOasis( oasisStyles, styleElem, name );
m_styles[name] = style;
// insert it into the the map sorted the OpenDocument name
m_oasisStyles[oasisName] = style;
kdDebug() << "Style " << name << ": " << style << endl;
}
}
// set the parent pointers after we loaded all styles
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
CustomStyle * styleData = iter.data();
if ( styleData->name() != "Default" )
if ( !styleData->parent() && !styleData->parentName().isNull() )
styleData->setParent( m_oasisStyles[ styleData->parentName() ] );
++iter;
}
}
TQDomElement StyleManager::save( TQDomDocument & doc )
{
kdDebug() << "Saving styles" << endl;
TQDomElement styles = doc.createElement( "styles" );
kdDebug() << "Saving default style" << endl;
m_defaultStyle->save( doc, styles );
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
kdDebug() << "Saving style" << endl;
CustomStyle * styleData = iter.data();
styleData->save( doc, styles );
++iter;
}
kdDebug() << "End saving styles" << endl;
return styles;
}
bool StyleManager::loadXML( TQDomElement const & styles )
{
TQDomElement e = styles.firstChild().toElement();
while ( !e.isNull() )
{
TQString name;
if ( e.hasAttribute( "name" ) )
name = e.attribute( "name" );
if ( name == "Default" )
{
if ( !m_defaultStyle->loadXML( e, name ) )
return false;
m_defaultStyle->setType( Style::BUILTIN );
}
else if ( !name.isNull() )
{
CustomStyle * style = 0;
if ( e.hasAttribute( "parent" ) && e.attribute( "parent" ) == "Default" )
style = new CustomStyle( name, m_defaultStyle );
else
style = new CustomStyle( name, 0 );
if ( !style->loadXML( e, name ) )
{
delete style;
return false;
}
if ( style->type() == Style::AUTO )
style->setType( Style::CUSTOM );
m_styles[name] = style;
kdDebug() << "Style " << name << ": " << style << endl;
}
e = e.nextSibling().toElement();
}
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
CustomStyle * styleData = iter.data();
if ( !styleData->parent() && !styleData->parentName().isNull() )
styleData->setParent( m_styles[ styleData->parentName() ] );
++iter;
}
m_defaultStyle->setName( "Default" );
m_defaultStyle->setType( Style::BUILTIN );
return true;
}
void StyleManager::createBuiltinStyles()
{
CustomStyle * header1 = new CustomStyle( i18n( "Header" ), m_defaultStyle );
TQFont f( header1->font() );
f.setItalic( true );
f.setPointSize( f.pointSize() + 2 );
f.setBold( true );
header1->changeFont( f );
header1->setType( Style::BUILTIN );
m_styles[ header1->name() ] = header1;
CustomStyle * header2 = new CustomStyle( i18n( "Header1" ), header1 );
TQColor color( "#F0F0FF" );
header2->changeBgColor( color );
TQPen pen( TQt::black, 1, TQt::SolidLine );
header2->changeBottomBorderPen( pen );
header2->setType( Style::BUILTIN );
m_styles[ header2->name() ] = header2;
}
CustomStyle * StyleManager::style( TQString const & name ) const
{
Styles::const_iterator iter( m_styles.find( name ) );
if ( iter != m_styles.end() )
return iter.data();
if ( name == "Default" )
return m_defaultStyle;
return 0;
}
void StyleManager::takeStyle( CustomStyle * style )
{
CustomStyle * parent = style->parent();
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
if ( iter.data()->parent() == style )
iter.data()->setParent( parent );
++iter;
}
Styles::iterator i( m_styles.find( style->name() ) );
if ( i != m_styles.end() )
{
kdDebug() << "Erasing style entry for " << style->name() << endl;
m_styles.erase( i );
}
}
bool StyleManager::checkCircle( TQString const & name, TQString const & parent )
{
CustomStyle * s = style( parent );
if ( !s || s->parent() == 0 )
return true;
if ( s->parentName() == name )
return false;
else
return checkCircle( name, s->parentName() );
}
bool StyleManager::validateStyleName( TQString const & name, CustomStyle * style )
{
if ( m_defaultStyle->name() == name || name == "Default" )
return false;
Styles::const_iterator iter = m_styles.begin();
Styles::const_iterator end = m_styles.end();
while ( iter != end )
{
if ( iter.key() == name && iter.data() != style )
return false;
++iter;
}
return true;
}
void StyleManager::changeName( TQString const & oldName, TQString const & newName )
{
Styles::iterator iter = m_styles.begin();
Styles::iterator end = m_styles.end();
while ( iter != end )
{
if ( iter.data()->parentName() == oldName )
iter.data()->refreshParentName();
++iter;
}
iter = m_styles.find( oldName );
if ( iter != end )
{
CustomStyle * s = iter.data();
m_styles.erase( iter );
m_styles[newName] = s;
}
}
TQStringList StyleManager::styleNames() const
{
TQStringList list;
list.push_back( i18n("Default") );
Styles::const_iterator iter = m_styles.begin();
Styles::const_iterator end = m_styles.end();
while ( iter != end )
{
list.push_back( iter.key() );
++iter;
}
return list;
}
TQDict<Style> StyleManager::loadOasisAutoStyles( KoOasisStyles& oasisStyles )
{
TQDictIterator<TQDomElement> it( oasisStyles.styles("table-cell") );
TQDict<Style> autoStyles;
for (;it.current();++it)
{
if ( it.current()->hasAttributeNS( KoXmlNS::style , "name" ) )
{
TQString name = it.current()->attributeNS( KoXmlNS::style , "name" , TQString() );
kdDebug() << "StyleManager: Preloading automatic cell style: " << name << endl;
autoStyles.insert( name , new Style());
autoStyles[name]->loadOasisStyle( oasisStyles , *(it.current()) );
if ( it.current()->hasAttributeNS( KoXmlNS::style, "parent-style-name" ) )
{
TQString parentStyleName = it.current()->attributeNS( KoXmlNS::style, "parent-style-name", TQString() );
if ( m_oasisStyles.contains( parentStyleName ) )
{
autoStyles[name]->setParent( m_oasisStyles[parentStyleName] );
}
kdDebug() << "\t parent-style-name:" << parentStyleName << endl;
}
else
{
autoStyles[name]->setParent( m_defaultStyle );
}
}
}
return autoStyles;
}
void StyleManager::releaseUnusedAutoStyles( TQDict<Style> autoStyles )
{
TQDictIterator<Style> styleIt( autoStyles );
for (;styleIt.current();++styleIt)
{
Style* style = styleIt.current();
if (style->release())
delete style;
}
// Now, we can clear the map of styles sorted by OpenDocument name.
m_oasisStyles.clear();
}
|