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
|
/* This file is part of the KDE project
Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
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; using
version 2 of the License.
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 "KWTableTemplate.h"
#include "KWDocument.h"
#include "KWFrameStyle.h"
#include "KoParagStyle.h"
#include <kdebug.h>
#include <tdelocale.h>
#include <tqdom.h>
/******************************************************************/
/* Class: KWTableTemplateCollection */
/******************************************************************/
KWTableTemplateCollection::KWTableTemplateCollection()
{
m_templateList.setAutoDelete( false );
m_deletedTemplates.setAutoDelete( true );
m_lastTemplate = 0L;
}
KWTableTemplateCollection::~KWTableTemplateCollection()
{
//kdDebug() << "KWTableTemplateCollection::destructor" << endl;
clear();
}
void KWTableTemplateCollection::clear()
{
m_templateList.setAutoDelete( true );
m_templateList.clear();
m_deletedTemplates.clear();
m_lastTemplate = 0;
}
KWTableTemplate* KWTableTemplateCollection::findTableTemplate( const TQString & _name )
{
// Caching, to speed things up
if ( m_lastTemplate && m_lastTemplate->name() == _name )
return m_lastTemplate;
TQPtrListIterator<KWTableTemplate> templateIt( m_templateList );
for ( ; templateIt.current(); ++templateIt )
{
if ( templateIt.current()->name() == _name ) {
m_lastTemplate = templateIt.current();
return m_lastTemplate;
}
}
// if(_name == "Plain") return m_styleList.at(0); // fallback..
return 0L;
}
KWTableTemplate* KWTableTemplateCollection::addTableTemplate( KWTableTemplate * tt )
{
// First check for duplicates.
for ( KWTableTemplate* p = m_templateList.first(); p != 0L; p = m_templateList.next() )
{
if ( p->name() == tt->name() ) {
// Replace existing template
if ( tt != p )
{
*p = *tt;
delete tt;
}
return p;
}
}
m_templateList.append( tt );
return tt;
}
void KWTableTemplateCollection::removeTableTemplate ( KWTableTemplate *tt )
{
if( m_templateList.removeRef( tt )) {
if ( m_lastTemplate == tt )
m_lastTemplate = 0L;
// Remember to delete this template when deleting the document
m_deletedTemplates.append( tt );
}
}
/******************************************************************/
/* Class: KWTableTemplate */
/******************************************************************/
KWTableTemplate::KWTableTemplate( const TQString & name, KWTableStyle * _firstRow, KWTableStyle * _firstCol,
KWTableStyle *_lastRow, KWTableStyle *_lastCol, KWTableStyle *_bodyCell,
KWTableStyle *_topLeftCorner, KWTableStyle *_topRightCorner,
KWTableStyle *_bottomLeftCorner, KWTableStyle *_bottomRightCorner )
{
m_name = name;
m_firstRow = _firstRow;
m_firstCol = _firstCol;
m_lastRow = _lastRow;
m_lastCol = _lastCol;
m_bodyCell = _bodyCell;
m_topLeftCorner = _topLeftCorner;
m_topRightCorner = _topRightCorner;
m_bottomRightCorner = _bottomRightCorner;
m_bottomLeftCorner = _bottomLeftCorner;
}
KWTableTemplate::KWTableTemplate( TQDomElement & parentElem, KWDocument *_doc, int /*docVersion*/ )
{
m_topRightCorner = 0L;
m_topLeftCorner = 0L;
m_bottomRightCorner = 0L;
m_bottomLeftCorner = 0L;
TQDomElement element = parentElem.namedItem( "NAME" ).toElement();
if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) )
m_name = element.attribute( "value" );
element = parentElem.namedItem( "BODYCELL" ).toElement();
if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
&& ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
m_bodyCell = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
else {
KWTableStyle *ts = _doc->tableStyleCollection()->findStyle( "Plain" );
if (ts) {
setBodyCell( ts );
}
else {
ts = new KWTableStyle( "Plain", 0L, 0L );
KWFrameStyle *fs = _doc->frameStyleCollection()->findStyle( "Plain" );
KoParagStyle *s = _doc->styleCollection()->findStyle( "Standard" );
if ( fs )
ts->setFrameStyle( fs );
else {
KWFrameStyle * standardFrameStyle = new KWFrameStyle( "Plain" );
standardFrameStyle->setBackgroundColor(TQColor("white"));
standardFrameStyle->setTopBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
standardFrameStyle->setRightBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
standardFrameStyle->setLeftBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
standardFrameStyle->setBottomBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
_doc->frameStyleCollection()->addStyle( standardFrameStyle );
ts->setFrameStyle( fs );
}
if ( s )
ts->setParagraphStyle( s );
else {
KoParagStyle * standardStyle = new KoParagStyle( "Standard" );
standardStyle->format().setFont( _doc->defaultFont() );
_doc->styleCollection()->addStyle( standardStyle );
ts->setParagraphStyle( s );
}
setBodyCell ( ts );
}
}
element = parentElem.namedItem( "FIRSTROW" ).toElement();
if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
&& ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
{
m_firstRow = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
if ( element.hasAttribute("topleftcorner") )
m_topLeftCorner = m_firstRow;
if ( element.hasAttribute("toprightcorner") )
m_topRightCorner = m_firstRow;
}
else
m_firstRow = m_bodyCell;
element = parentElem.namedItem( "FIRSTCOL" ).toElement();
if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
&& ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
{
m_firstCol = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
if ( element.hasAttribute("topleftcorner") )
m_topLeftCorner = m_firstCol;
if ( element.hasAttribute("bottomleftcorner") )
m_bottomLeftCorner = m_firstCol;
}
else
m_firstCol = m_bodyCell;
element = parentElem.namedItem( "LASTROW" ).toElement();
if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
&& ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
{
m_lastRow = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
if ( ( !element.isNull() ) && ( element.hasAttribute("bottomrightcorner") ) )
m_bottomRightCorner = m_lastRow;
if ( ( !element.isNull() ) && ( element.hasAttribute("bottomleftcorner") ) )
m_bottomLeftCorner = m_lastRow;
}
else
m_lastRow = m_bodyCell;
element = parentElem.namedItem( "LASTCOL" ).toElement();
if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
&& ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
{
m_lastCol = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
if ( element.hasAttribute("toprightcorner") )
m_topRightCorner = m_lastCol;
if ( element.hasAttribute("bottomrightcorner") )
m_bottomRightCorner = m_lastCol;
}
else
m_lastCol = m_bodyCell;
if (!m_topRightCorner) m_topRightCorner = m_bodyCell;
if (!m_topLeftCorner) m_topLeftCorner = m_bodyCell;
if (!m_bottomRightCorner) m_bottomRightCorner = m_bodyCell;
if (!m_bottomLeftCorner) m_bottomLeftCorner = m_bodyCell;
}
void KWTableTemplate::operator=( const KWTableTemplate &rhs )
{
m_name = rhs.m_name;
m_firstRow = rhs.pFirstRow();
m_firstCol = rhs.pFirstCol();
m_lastRow = rhs.pLastRow();
m_lastCol = rhs.pLastCol();
m_bodyCell = rhs.pBodyCell();
m_topLeftCorner = rhs.pTopLeftCorner();
m_topRightCorner = rhs.pTopRightCorner();
m_bottomRightCorner = rhs.pBottomRightCorner();
m_bottomLeftCorner = rhs.pBottomLeftCorner();
}
TQString KWTableTemplate::displayName() const
{
return i18n( "Style name", name().utf8() );
}
// TODO
void KWTableTemplate::saveTableTemplate( TQDomElement & parentElem )
{
TQDomDocument doc = parentElem.ownerDocument();
TQDomElement element = doc.createElement( "NAME" );
parentElem.appendChild( element );
element.setAttribute( "value", name() );
if (m_bodyCell)
{
element = doc.createElement( "BODYCELL" );
parentElem.appendChild( element );
element.setAttribute( "name", m_bodyCell->name() );
}
if (m_firstRow)
{
element = doc.createElement( "FIRSTROW" );
parentElem.appendChild( element );
element.setAttribute( "name", m_firstRow->name() );
}
if (m_firstCol)
{
element = doc.createElement( "FIRSTCOL" );
parentElem.appendChild( element );
element.setAttribute( "name", m_firstCol->name() );
}
if (m_lastRow)
{
element = doc.createElement( "LASTROW" );
parentElem.appendChild( element );
element.setAttribute( "name", m_lastRow->name() );
}
if (m_lastCol)
{
element = doc.createElement( "LASTCOL" );
parentElem.appendChild( element );
element.setAttribute( "name", m_lastCol->name() );
}
}
KWTableTemplate *KWTableTemplate::loadTemplate( TQDomElement & parentElem, KWDocument *_doc, int docVersion )
{
return new KWTableTemplate(parentElem, _doc, docVersion);
}
|