~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to filters/kspread/latex/export/table.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place - Suite 330,
 
18
** Boston, MA  02111-1307, USA.
 
19
**
 
20
*/
 
21
 
 
22
#include <kdebug.h>             /* for kdDebug stream */
 
23
#include <qbitarray.h>
 
24
#include "cell.h"
 
25
#include "column.h"
 
26
#include "row.h"
 
27
#include "table.h"
 
28
 
 
29
/*******************************************/
 
30
/* Constructor                             */
 
31
/*******************************************/
 
32
Table::Table()
 
33
{
 
34
        _maxCol = 0;
 
35
        _maxRow = 0;
 
36
}
 
37
 
 
38
/*******************************************/
 
39
/* Destructor                              */
 
40
/*******************************************/
 
41
Table::~Table()
 
42
{
 
43
}
 
44
 
 
45
void Table::setMaxColumn(int col)
 
46
{
 
47
        if(_maxCol < col) _maxCol = col;
 
48
}
 
49
 
 
50
void Table::setMaxRow(int row)
 
51
{
 
52
        if(_maxRow < row) _maxRow = row;
 
53
}
 
54
 
 
55
void Table::analyse(const QDomNode balise)
 
56
{
 
57
        kdDebug() << "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
                QString name = getChildName(balise, index);             
 
90
                if(name == "cell")
 
91
                {
 
92
                        kdDebug() << "----- 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() << "----- 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() << "----- row -----" << endl;
 
109
                        Row* row = new Row();
 
110
                        row->analyse(getChild(balise, index));
 
111
                        _rows.append(row);
 
112
                }
 
113
                else
 
114
                        kdDebug() << "name : " << name << endl;
 
115
        }
 
116
}
 
117
 
 
118
void Table::analysePaper(const QDomNode balise)
 
119
{
 
120
        setFormat(getAttr(balise, "format"));
 
121
        setOrientation(getAttr(balise, "orientation"));
 
122
 
 
123
        /* borders */
 
124
        QDomNode 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
 
 
131
Cell* Table::searchCell(int col, int row)
 
132
{
 
133
        QPtrListIterator<Cell> it(_cells);
 
134
 
 
135
        kdDebug() << "search in list of " << _cells.count() << " cells" << endl;
 
136
        Cell *cell = 0;
 
137
        while ( (cell = it.current()) != 0 )
 
138
        {
 
139
                ++it;
 
140
                kdDebug() << "cell: " << cell->getRow() << "-" << cell->getCol() << endl;
 
141
                if(cell->getCol() == col && cell->getRow() == row)
 
142
                        return cell;
 
143
        }
 
144
        return NULL;
 
145
}
 
146
 
 
147
Column* Table::searchColumn(int col)
 
148
{
 
149
        QPtrListIterator<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
 
 
161
Row* Table::searchRow(int rowNumber)
 
162
{
 
163
        QPtrListIterator<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
/*******************************************/
 
178
void Table::generate(QTextStream& out)
 
179
{
 
180
        kdDebug() << "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() << "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() << "END OF GENERATINO OF A TABLE" << endl;
 
264
}
 
265
 
 
266
/*******************************************/
 
267
/* generateTopLineBorder                   */
 
268
/*******************************************/
 
269
void Table::generateTopLineBorder(QTextStream& out, int row)
 
270
{
 
271
        
 
272
        Cell* cell = 0;
 
273
        QBitArray border( getMaxColumn() );
 
274
        bool fullLine = true;
 
275
        for(int index = 1; index <= getMaxColumn(); index++)
 
276
        {
 
277
                /* Search the cell in the list */
 
278
                kdDebug() << "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
/*******************************************/
 
326
void Table::generateBottomLineBorder(QTextStream& out, int row)
 
327
{
 
328
        Cell* cell = 0;
 
329
        QBitArray 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
/*******************************************/
 
377
void Table::generateCell(QTextStream& out, int row, int col)
 
378
{
 
379
        kdDebug() << "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() << "generate cell with text: " << cell->getText() << endl;
 
386
                cell->generate(out, this);
 
387
        }
 
388
 
 
389
        kdDebug() << "END OF A CELL" << endl;
 
390
}
 
391
 
 
392
/*******************************************/
 
393
/* generateTableHeader                     */
 
394
/*******************************************/
 
395
void Table::generateTableHeader(QTextStream& 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