kword

KWTableTemplate.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; using
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KWTableTemplate.h"
21
22#include "KWDocument.h"
23#include "KWFrameStyle.h"
24#include "KoParagStyle.h"
25
26#include <kdebug.h>
27#include <tdelocale.h>
28#include <tqdom.h>
29
30
31/******************************************************************/
32/* Class: KWTableTemplateCollection */
33/******************************************************************/
34
35KWTableTemplateCollection::KWTableTemplateCollection()
36{
37 m_templateList.setAutoDelete( false );
38 m_deletedTemplates.setAutoDelete( true );
39 m_lastTemplate = 0L;
40}
41
42KWTableTemplateCollection::~KWTableTemplateCollection()
43{
44 //kdDebug() << "KWTableTemplateCollection::destructor" << endl;
45 clear();
46}
47
48void KWTableTemplateCollection::clear()
49{
50 m_templateList.setAutoDelete( true );
51 m_templateList.clear();
52 m_deletedTemplates.clear();
53 m_lastTemplate = 0;
54}
55
56KWTableTemplate* KWTableTemplateCollection::findTableTemplate( const TQString & _name )
57{
58 // Caching, to speed things up
59 if ( m_lastTemplate && m_lastTemplate->name() == _name )
60 return m_lastTemplate;
61
62 TQPtrListIterator<KWTableTemplate> templateIt( m_templateList );
63 for ( ; templateIt.current(); ++templateIt )
64 {
65 if ( templateIt.current()->name() == _name ) {
66 m_lastTemplate = templateIt.current();
67 return m_lastTemplate;
68 }
69 }
70
71// if(_name == "Plain") return m_styleList.at(0); // fallback..
72
73 return 0L;
74}
75
76
77KWTableTemplate* KWTableTemplateCollection::addTableTemplate( KWTableTemplate * tt )
78{
79 // First check for duplicates.
80 for ( KWTableTemplate* p = m_templateList.first(); p != 0L; p = m_templateList.next() )
81 {
82 if ( p->name() == tt->name() ) {
83 // Replace existing template
84 if ( tt != p )
85 {
86 *p = *tt;
87 delete tt;
88 }
89 return p;
90 }
91 }
92 m_templateList.append( tt );
93 return tt;
94}
95
96void KWTableTemplateCollection::removeTableTemplate ( KWTableTemplate *tt )
97{
98 if( m_templateList.removeRef( tt )) {
99 if ( m_lastTemplate == tt )
100 m_lastTemplate = 0L;
101 // Remember to delete this template when deleting the document
102 m_deletedTemplates.append( tt );
103 }
104}
105
106/******************************************************************/
107/* Class: KWTableTemplate */
108/******************************************************************/
109
110KWTableTemplate::KWTableTemplate( const TQString & name, KWTableStyle * _firstRow, KWTableStyle * _firstCol,
111 KWTableStyle *_lastRow, KWTableStyle *_lastCol, KWTableStyle *_bodyCell,
112 KWTableStyle *_topLeftCorner, KWTableStyle *_topRightCorner,
113 KWTableStyle *_bottomLeftCorner, KWTableStyle *_bottomRightCorner )
114{
115 m_name = name;
116 m_firstRow = _firstRow;
117 m_firstCol = _firstCol;
118 m_lastRow = _lastRow;
119 m_lastCol = _lastCol;
120 m_bodyCell = _bodyCell;
121 m_topLeftCorner = _topLeftCorner;
122 m_topRightCorner = _topRightCorner;
123 m_bottomRightCorner = _bottomRightCorner;
124 m_bottomLeftCorner = _bottomLeftCorner;
125}
126
127KWTableTemplate::KWTableTemplate( TQDomElement & parentElem, KWDocument *_doc, int /*docVersion*/ )
128{
129 m_topRightCorner = 0L;
130 m_topLeftCorner = 0L;
131 m_bottomRightCorner = 0L;
132 m_bottomLeftCorner = 0L;
133
134 TQDomElement element = parentElem.namedItem( "NAME" ).toElement();
135 if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) )
136 m_name = element.attribute( "value" );
137
138 element = parentElem.namedItem( "BODYCELL" ).toElement();
139 if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
140 && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
141 m_bodyCell = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
142 else {
143 KWTableStyle *ts = _doc->tableStyleCollection()->findStyle( "Plain" );
144 if (ts) {
145 setBodyCell( ts );
146 }
147 else {
148 ts = new KWTableStyle( "Plain", 0L, 0L );
149 KWFrameStyle *fs = _doc->frameStyleCollection()->findStyle( "Plain" );
150 KoParagStyle *s = _doc->styleCollection()->findStyle( "Standard" );
151 if ( fs )
152 ts->setFrameStyle( fs );
153 else {
154 KWFrameStyle * standardFrameStyle = new KWFrameStyle( "Plain" );
155 standardFrameStyle->setBackgroundColor(TQColor("white"));
156 standardFrameStyle->setTopBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
157 standardFrameStyle->setRightBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
158 standardFrameStyle->setLeftBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
159 standardFrameStyle->setBottomBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
160 _doc->frameStyleCollection()->addStyle( standardFrameStyle );
161 ts->setFrameStyle( fs );
162 }
163
164 if ( s )
165 ts->setParagraphStyle( s );
166 else {
167 KoParagStyle * standardStyle = new KoParagStyle( "Standard" );
168 standardStyle->format().setFont( _doc->defaultFont() );
169 _doc->styleCollection()->addStyle( standardStyle );
170 ts->setParagraphStyle( s );
171 }
172 setBodyCell ( ts );
173 }
174 }
175 element = parentElem.namedItem( "FIRSTROW" ).toElement();
176 if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
177 && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
178 {
179 m_firstRow = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
180
181 if ( element.hasAttribute("topleftcorner") )
182 m_topLeftCorner = m_firstRow;
183 if ( element.hasAttribute("toprightcorner") )
184 m_topRightCorner = m_firstRow;
185 }
186 else
187 m_firstRow = m_bodyCell;
188
189 element = parentElem.namedItem( "FIRSTCOL" ).toElement();
190 if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
191 && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
192 {
193 m_firstCol = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
194
195 if ( element.hasAttribute("topleftcorner") )
196 m_topLeftCorner = m_firstCol;
197 if ( element.hasAttribute("bottomleftcorner") )
198 m_bottomLeftCorner = m_firstCol;
199 }
200 else
201 m_firstCol = m_bodyCell;
202
203 element = parentElem.namedItem( "LASTROW" ).toElement();
204 if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
205 && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
206 {
207 m_lastRow = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
208
209 if ( ( !element.isNull() ) && ( element.hasAttribute("bottomrightcorner") ) )
210 m_bottomRightCorner = m_lastRow;
211 if ( ( !element.isNull() ) && ( element.hasAttribute("bottomleftcorner") ) )
212 m_bottomLeftCorner = m_lastRow;
213 }
214 else
215 m_lastRow = m_bodyCell;
216
217 element = parentElem.namedItem( "LASTCOL" ).toElement();
218 if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
219 && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
220 {
221 m_lastCol = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
222
223 if ( element.hasAttribute("toprightcorner") )
224 m_topRightCorner = m_lastCol;
225 if ( element.hasAttribute("bottomrightcorner") )
226 m_bottomRightCorner = m_lastCol;
227 }
228 else
229 m_lastCol = m_bodyCell;
230
231 if (!m_topRightCorner) m_topRightCorner = m_bodyCell;
232 if (!m_topLeftCorner) m_topLeftCorner = m_bodyCell;
233 if (!m_bottomRightCorner) m_bottomRightCorner = m_bodyCell;
234 if (!m_bottomLeftCorner) m_bottomLeftCorner = m_bodyCell;
235}
236
237void KWTableTemplate::operator=( const KWTableTemplate &rhs )
238{
239 m_name = rhs.m_name;
240 m_firstRow = rhs.pFirstRow();
241 m_firstCol = rhs.pFirstCol();
242 m_lastRow = rhs.pLastRow();
243 m_lastCol = rhs.pLastCol();
244 m_bodyCell = rhs.pBodyCell();
245 m_topLeftCorner = rhs.pTopLeftCorner();
246 m_topRightCorner = rhs.pTopRightCorner();
247 m_bottomRightCorner = rhs.pBottomRightCorner();
248 m_bottomLeftCorner = rhs.pBottomLeftCorner();
249}
250
251TQString KWTableTemplate::displayName() const
252{
253 return i18n( "Style name", name().utf8() );
254}
255
256// TODO
257void KWTableTemplate::saveTableTemplate( TQDomElement & parentElem )
258{
259 TQDomDocument doc = parentElem.ownerDocument();
260 TQDomElement element = doc.createElement( "NAME" );
261 parentElem.appendChild( element );
262 element.setAttribute( "value", name() );
263
264 if (m_bodyCell)
265 {
266 element = doc.createElement( "BODYCELL" );
267 parentElem.appendChild( element );
268 element.setAttribute( "name", m_bodyCell->name() );
269 }
270 if (m_firstRow)
271 {
272 element = doc.createElement( "FIRSTROW" );
273 parentElem.appendChild( element );
274 element.setAttribute( "name", m_firstRow->name() );
275 }
276 if (m_firstCol)
277 {
278 element = doc.createElement( "FIRSTCOL" );
279 parentElem.appendChild( element );
280 element.setAttribute( "name", m_firstCol->name() );
281 }
282 if (m_lastRow)
283 {
284 element = doc.createElement( "LASTROW" );
285 parentElem.appendChild( element );
286 element.setAttribute( "name", m_lastRow->name() );
287 }
288 if (m_lastCol)
289 {
290 element = doc.createElement( "LASTCOL" );
291 parentElem.appendChild( element );
292 element.setAttribute( "name", m_lastCol->name() );
293 }
294}
295
296KWTableTemplate *KWTableTemplate::loadTemplate( TQDomElement & parentElem, KWDocument *_doc, int docVersion )
297{
298 return new KWTableTemplate(parentElem, _doc, docVersion);
299}
KWFrameStyle * findStyle(const TQString &name) const
Find style based on the untranslated name name.
KWFrameStyle * addStyle(KWFrameStyle *sty)
See KoUserStyleCollection::addStyle.
KWTableStyle * findStyle(const TQString &name) const
Find style based on the untranslated name name.
Table style (can created/modified/deleted by the user) A table style is simply a paragraph style and ...