kspread/latex/export/table.cpp
1/*
2** A program to convert the XML rendered by KWord into LATEX.
3**
4** Copyright (C) 2000 Robert JACOLIN
5**
6** This library is free software; you can redistribute it and/or
7** modify it under the terms of the GNU Library General Public
8** License as published by the Free Software Foundation; either
9** version 2 of the License, or (at your option) any later version.
10**
11** This library is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14** Library General Public License for more details.
15**
16** To receive a copy of the GNU Library General Public License, write to the
17** Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19**
20*/
21
22#include <kdebug.h> /* for kdDebug stream */
23#include <tqbitarray.h>
24#include "cell.h"
25#include "column.h"
26#include "row.h"
27#include "table.h"
28
29/*******************************************/
30/* Constructor */
31/*******************************************/
33{
34 _maxCol = 0;
35 _maxRow = 0;
36}
37
38/*******************************************/
39/* Destructor */
40/*******************************************/
41Table::~Table()
42{
43}
44
45void Table::setMaxColumn(int col)
46{
47 if(_maxCol < col) _maxCol = col;
48}
49
50void Table::setMaxRow(int row)
51{
52 if(_maxRow < row) _maxRow = row;
53}
54
55void Table::analyse(const TQDomNode balise)
56{
57 kdDebug(30522) << "New table" << endl;
58 if(getAttr(balise, "columnnumber") == "1")
59 setColumnNumber();
60 if(getAttr(balise, "borders") == "1")
61 setBorders();
62 if(getAttr(balise, "hide") == "1")
63 setHide();
64 if(getAttr(balise, "hidezero") == "1")
65 setHideZero();
66 if(getAttr(balise, "firstletterupper") == "1")
67 setFirstletterupper();
68 if(getAttr(balise, "grid") == "1")
69 setGrid();
70 if(getAttr(balise, "printgrid") == "1")
71 setPrintGrid();
72 if(getAttr(balise, "printCommentIndicator") == "1")
73 setPrintCommentIndicator();
74 if(getAttr(balise, "printFormulaIndicator") == "1")
75 setPrintFormulaIndicator();
76 if(getAttr(balise, "showFormula") == "1")
77 setShowFormula();
78 if(getAttr(balise, "showFormulaIndicator") == "1")
79 setShowFormulaIndicator();
80 if(getAttr(balise, "lcmode") == "1")
81 setLCMode();
82 setName(getAttr(balise, "name"));
83
84 analysePaper(getChild(balise, "paper"));
85
86 int max = getNbChild(balise);
87 for(int index = 0; index < max; index++)
88 {
89 TQString name = getChildName(balise, index);
90 if(name == "cell")
91 {
92 kdDebug(30522) << "----- cell -----" << endl;
93 Cell* cell = new Cell();
94 cell->analyse(getChild(balise, index));
95 _cells.append(cell);
96 setMaxColumn(cell->getCol());
97 setMaxRow(cell->getRow());
98 }
99 else if(name == "column")
100 {
101 kdDebug(30522) << "----- column -----" << endl;
102 Column* column = new Column();
103 column->analyse(getChild(balise, index));
104 _columns.append(column);
105 }
106 else if(name == "row")
107 {
108 kdDebug(30522) << "----- row -----" << endl;
109 Row* row = new Row();
110 row->analyse(getChild(balise, index));
111 _rows.append(row);
112 }
113 else
114 kdDebug(30522) << "name : " << name << endl;
115 }
116}
117
118void Table::analysePaper(const TQDomNode balise)
119{
120 setFormat(getAttr(balise, "format"));
121 setOrientation(getAttr(balise, "orientation"));
122
123 /* borders */
124 TQDomNode border = getChild(balise, "borders");
125 setBorderRight(getAttr(balise, "right").toLong());
126 setBorderLeft(getAttr(balise, "left").toLong());
127 setBorderBottom(getAttr(balise, "bottom").toLong());
128 setBorderTop(getAttr(balise, "top").toLong());
129}
130
131Cell* Table::searchCell(int col, int row)
132{
133 TQPtrListIterator<Cell> it(_cells);
134
135 kdDebug(30522) << "search in list of " << _cells.count() << " cells" << endl;
136 Cell *cell = 0;
137 while ( (cell = it.current()) != 0 )
138 {
139 ++it;
140 kdDebug(30522) << "cell: " << cell->getRow() << "-" << cell->getCol() << endl;
141 if(cell->getCol() == col && cell->getRow() == row)
142 return cell;
143 }
144 return NULL;
145}
146
148{
149 TQPtrListIterator<Column> it(_columns);
150
151 Column *column;
152 while ( (column = it.current()) != 0 )
153 {
154 ++it;
155 if(column->getCol() == col)
156 return column;
157 }
158 return NULL;
159}
160
161Row* Table::searchRow(int rowNumber)
162{
163 TQPtrListIterator<Row> it(_rows);
164
165 Row *row;
166 while ( (row = it.current()) != 0 )
167 {
168 ++it;
169 if(row->getRow() == rowNumber)
170 return row;
171 }
172 return NULL;
173}
174
175/*******************************************/
176/* generate */
177/*******************************************/
178void Table::generate(TQTextStream& out)
179{
180 kdDebug(30522) << "GENERATION OF A TABLE " << getMaxRow() << " - " << getMaxColumn()
181 << endl;
182 out << endl << "%% " << getName() << endl;
183 if(getOrientation() == "Portrait")
184 {
185 out << "\\begin{sidewaystable}" << endl << endl;
186 indent();
187 writeIndent(out);
188 }
189
190 out << "\\begin{tabular}";
191 generateTableHeader(out);
192 out << endl;
193 indent();
194 int rowNumber = 1;
195 while(rowNumber <= getMaxRow())
196 {
197 generateTopLineBorder(out, rowNumber);
198 Row* row = searchRow(rowNumber);
199 if(row != NULL)
200 row->generate(out);
201
202 for(int col = 1; col <= getMaxColumn(); col++)
203 {
204 writeIndent(out);
205 generateCell(out, rowNumber, col);
206
207 if(col < getMaxColumn())
208 out << " & "<< endl;
209 }
210 out << "\\\\" << endl;
211 rowNumber++;
212 }
213 generateBottomLineBorder(out, rowNumber - 1);
214 desindent();
215 writeIndent(out);
216 out << "\\end{tabular}" << endl << endl;
217 desindent();
218
219 if(getOrientation() == "Portrait")
220 {
221 out << "\\end{sidewaystable}" << endl;
222 desindent();
223 }
224 /*Element* elt = 0;
225 kdDebug(30522) << "GENERATION OF A TABLE " << count() << endl;
226 out << endl << "\\begin{tabular}";
227 generateTableHeader(out);
228 out << endl;
229 indent();
230
231 int row= 0;
232 while(row <= getMaxRow())
233 {
234 generateTopLineBorder(out, row);
235 for(int col= 0; col <= getMaxCol(); col++)
236 {
237 writeIndent(out);
238 */
239 /* Search the cell in the list */
240 /* elt = searchCell(row, col);
241
242 out << "\\multicolumn{1}{";
243 if(elt->hasLeftBorder())
244 out << "|";
245 out << "m{" << getCellSize(col) << "pt}";
246
247 if(elt->hasRightBorder())
248 out << "|";
249 out << "}{" << endl;
250
251 generateCell(out, row, col);
252 out << "}" << endl;
253 if(col < getMaxCol())
254 out << "&" << endl;
255 }
256 out << "\\\\" << endl;
257 writeIndent(out);
258 row = row + 1;
259 }
260 generateBottomLineBorder(out, row - 1);
261 out << "\\end{tabular}" << endl << endl;
262 desindent();*/
263 kdDebug(30522) << "END OF GENERATINO OF A TABLE" << endl;
264}
265
266/*******************************************/
267/* generateTopLineBorder */
268/*******************************************/
269void Table::generateTopLineBorder(TQTextStream& out, int row)
270{
271
272 Cell* cell = 0;
273 TQBitArray border( getMaxColumn() );
274 bool fullLine = true;
275 for(int index = 1; index <= getMaxColumn(); index++)
276 {
277 /* Search the cell in the list */
278 kdDebug(30522) << "search " << row << ", " << index << endl;
279 cell = searchCell(index, row);
280
281 if(cell == NULL)
282 cell = new Cell(row, index);
283
284 /* If the element has a border display it here */
285 border[ index ] = cell->hasTopBorder();
286 if( ! cell->hasTopBorder() )
287 fullLine = false;
288 }
289
290 if(fullLine)
291 {
292 /* All column have a top border */
293 writeIndent(out);
294 out << "\\hline" << endl;
295 }
296 else
297 {
298 int index = 0;
299 while(index < getMaxColumn())
300 {
301 if(border[index])
302 {
303 int begin = index;
304 int end;
305 index = index + 1;
306 while(border[index] && index < getMaxColumn())
307 {
308 index = index + 1;
309 }
310 end = index - 1;
311 out << "\\cline{" << begin << "-" << end << "} " << endl;
312 }
313 index = index + 1;
314 }
315 }
316
317 /*Row * row;
318 row = searchRow(row);
319 if(row != NULL)
320 row->generate(out);*/
321}
322
323/*******************************************/
324/* generateBottomLineBorder */
325/*******************************************/
326void Table::generateBottomLineBorder(TQTextStream& out, int row)
327{
328 Cell* cell = 0;
329 TQBitArray border( getMaxColumn() );
330 bool fullLine = true;
331
332 for(int index = 1; index <= getMaxColumn(); index++)
333 {
334 /* Search the cell in the list */
335 cell = searchCell(index, row);
336
337 if(cell == NULL)
338 cell = new Cell(row, index);
339
340 /* If the element has a border display it here */
341 border[ index ] = cell->hasBottomBorder();
342 if( ! cell->hasBottomBorder() )
343 fullLine = false;
344 }
345
346 if(fullLine)
347 {
348 /* All column have a bottom border */
349 writeIndent(out);
350 out << "\\hline" << endl;
351 }
352 else
353 {
354 int index = 0;
355 while(index < getMaxColumn())
356 {
357 if(border[index])
358 {
359 int begin = index;
360 int end;
361 index = index + 1;
362 while(border[index] && index < getMaxColumn())
363 {
364 index = index + 1;
365 }
366 end = index - 1;
367 out << "\\cline{" << begin << "-" << end << "} " << endl;
368 }
369 index = index + 1;
370 }
371 }
372}
373
374/*******************************************/
375/* generateCell */
376/*******************************************/
377void Table::generateCell(TQTextStream& out, int row, int col)
378{
379 kdDebug(30522) << "GENERATE CELL : " << row << "," << col << endl;
380
381 /* Search the cell in the list */
382 Cell *cell = searchCell(col, row);
383 if(cell != NULL)
384 {
385 kdDebug(30522) << "generate cell with text: " << cell->getText() << endl;
386 cell->generate(out, this);
387 }
388
389 kdDebug(30522) << "END OF A CELL" << endl;
390}
391
392/*******************************************/
393/* generateTableHeader */
394/*******************************************/
395void Table::generateTableHeader(TQTextStream& out)
396{
397 Column* column = 0;
398
399 out << "{";
400
401 for(int col = 1; col <= getMaxColumn(); col++)
402 {
403 column = searchColumn(col);
404 if(column != NULL)
405 column->generate(out);
406 else
407 {
408 out << "m{20pt}";
409 }
410 }
411 out << "}";
412
413}
414
This class hold a column.
Definition column.h:38
void analyse(const TQDomNode)
Helpfull functions.
Definition column.cpp:41
long getCol() const
getters
Definition column.h:67
This class hold a row.
Definition row.h:38
void analyse(const TQDomNode)
Helpfull functions.
Definition row.cpp:41
long getRow() const
getters
Definition row.h:67
Table()
Constructors.
Cell * searchCell(int col, int row)
Helpfull functions.
Column * searchColumn(int col)
Return one specific column which describe the format of the column.
int getMaxRow() const
getters
Row * searchRow(int row)
Return one specific row which describe the format of the row.
void analyse(const TQDomNode)
Helpfull functions.
void setMaxRow(int r)
setters