kword/latex/export/table.cpp
1/*
2** A program to convert the XML rendered by KWord into LATEX.
3**
4** Copyright (C) 2000,2002 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 "listtable.h"
25#include "textFrame.h"
26
27/*******************************************/
28/* Constructor */
29/*******************************************/
31{
32 setMaxCol(0);
33 setMaxRow(0);
34}
35
36Table::Table(TQString grpMgr)
37{
38 setGrpMgr(grpMgr);
39 setMaxCol(0);
40 setMaxRow(0);
41}
42
43/*******************************************/
44/* Destructor */
45/*******************************************/
46Table::~Table()
47{
48 kdDebug(30522) << "Destruction of a list of frames" << endl;
49}
50
51/*******************************************/
52/* getCellFlow */
53/*******************************************/
54EEnv Table::getCellFlow(int col)
55{
56 for(int row = 0; row<= getMaxRow(); row++)
57 {
58 Element* elt = at(row * getMaxRow() + col);
59 if(elt->getType() == ST_TEXT)
60 {
61 kdDebug(30522) << ((TextFrame*) elt)->getFirstPara()->getEnv() << endl;
62 return ((TextFrame*) elt)->getFirstPara()->getEnv();
63 }
64 }
65 kdDebug(30522) << "Default flow for cell" << endl;
66 return ENV_JUSTIFY;
67}
68
69/*******************************************/
70/* getCellFlow */
71/*******************************************/
72double Table::getCellSize(int col)
73{
74
75 for(int row = 0; row<= getMaxRow(); row++)
76 {
77 Element* elt = at(row * getMaxRow() + col);
78 if(elt->getType() == ST_TEXT)
79 {
80 kdDebug(30522) << "size : " << ((TextFrame*) elt)->getLeft() << endl;
81 return ((TextFrame*) elt)->getRight() - ((TextFrame*) elt)->getLeft();
82 }
83 }
84 kdDebug(30522) << "Default size for cell" << endl;
85 return 3;
86}
87
88/*******************************************/
89/* searchCell */
90/*******************************************/
91Element* Table::searchCell(int row, int col)
92{
93 Element* current = 0;
94
95 /* Parcourir les tables et tester chaque nom de table */
96 for(current = first(); current != 0; current = next())
97 {
98 kdDebug(30522) << "+" << current->getRow() << "," << current->getCol() << endl;
99 if(current->getRow() == row && current->getCol() == col)
100 return current;
101 }
102 return 0;
103}
104
105/*******************************************/
106/* append */
107/*******************************************/
108void Table::append(Element* elt)
109{
110 if(elt->getRow() > getMaxRow())
111 setMaxRow(elt->getRow());
112
113 if(elt->getCol() > getMaxCol())
114 setMaxCol(elt->getCol());
115
116 TQPtrList<Element>::append(elt);
117}
118
119/*******************************************/
120/* generate */
121/*******************************************/
122void Table::generate(TQTextStream& out)
123{
124 Element* elt = 0;
125 kdDebug(30522) << "GENERATION OF A TABLE " << count() << endl;
126 out << endl << "\\begin{tabular}";
127 generateTableHeader(out);
128 out << endl;
129 Config::instance()->indent();
130
131 int row= 0;
132 while(row <= getMaxRow())
133 {
134 generateTopLineBorder(out, row);
135 for(int col= 0; col <= getMaxCol(); col++)
136 {
137 Config::instance()->writeIndent(out);
138
139 /* Search the cell in the list */
140 elt = searchCell(row, col);
141
142 out << "\\multicolumn{1}{";
143 if(elt->hasLeftBorder())
144 out << "|";
145 out << "m{" << getCellSize(col) << "pt}";
146
147 if(elt->hasRightBorder())
148 out << "|";
149 out << "}{" << endl;
150
151 generateCell(out, row, col);
152 out << "}" << endl;
153 if(col < getMaxCol())
154 out << "&" << endl;
155 }
156 out << "\\\\" << endl;
157 Config::instance()->writeIndent(out);
158 row = row + 1;
159 }
160 generateBottomLineBorder(out, row - 1);
161 out << "\\end{tabular}" << endl << endl;
162 Config::instance()->desindent();
163 kdDebug(30522) << "END OF GENERATINO OF A TABLE" << endl;
164}
165
166/*******************************************/
167/* generateTopLineBorder */
168/*******************************************/
169void Table::generateTopLineBorder(TQTextStream& out, int row)
170{
171 Element* elt = 0;
172 TQBitArray border(getMaxCol());
173 bool fullLine = true;
174
175 for(int index = 0; index <= getMaxCol(); index++)
176 {
177 /* Search the cell in the list */
178 elt = searchCell(row, index);
179 kdDebug(30522) << endl << "name (" << row << ", " << index << ") = " << elt->getName() << endl << endl;
180
181 /* If the element has a border display it here */
182 if(elt->hasTopBorder())
183 {
184 border[index] = 1;
185 }
186 else
187 {
188 border[index] = 0;
189 fullLine = false;
190 }
191 }
192
193 if(fullLine)
194 {
195 /* All column have a top border */
196 Config::instance()->writeIndent(out);
197 out << "\\hline" << endl;
198 }
199 else
200 {
201 int index = 0;
202 while(index <= getMaxCol())
203 {
204 if(border[index])
205 {
206 int begin = index;
207 int end = index;
208 while(border[index] && index < getMaxCol())
209 {
210 index = index + 1;
211 }
212 end = index - 1;
213 out << "\\cline{" << (begin + 1) << "-" << (end + 1) << "} " << endl;
214 }
215 index = index + 1;
216 }
217 }
218}
219
220/*******************************************/
221/* generateBottomLineBorder */
222/*******************************************/
223void Table::generateBottomLineBorder(TQTextStream& out, int row)
224{
225 Element* elt = 0;
226 TQBitArray border(getMaxCol());
227 bool fullLine = true;
228
229 for(int index = 0; index <= getMaxCol(); index++)
230 {
231 /* Search the cell in the list */
232 elt = searchCell(row, index);
233
234 /* If the element has a border display it here */
235 if(elt->hasBottomBorder())
236 {
237 border[index] = 1;
238 }
239 else
240 {
241 border[index] = 0;
242 fullLine = false;
243 }
244 }
245
246 if(fullLine)
247 {
248 /* All column have a top border */
249 Config::instance()->writeIndent(out);
250 out << "\\hline" << endl;
251 }
252 else
253 {
254 int index = 0;
255 while(index <= getMaxCol())
256 {
257 if(border[index])
258 {
259 int begin = index;
260 int end = index;
261 while(border[index] && index <= getMaxCol())
262 {
263 index = index + 1;
264 }
265 end = index - 1;
266 out << "\\cline{" << (begin + 1) << "-" << (end + 1) << "} " << endl;
267 }
268 index = index + 1;
269 }
270 }
271}
272
273/*******************************************/
274/* generateCell */
275/*******************************************/
276void Table::generateCell(TQTextStream& out, int row, int col)
277{
278 Element* elt = 0;
279
280 kdDebug(30522) << "NEW CELL : " << row << "," << col << endl;
281
282 /* Search the cell in the list */
283 elt = searchCell(row, col);
284
285 /* Generate it */
286 if(elt != 0)
287 elt->generate(out);
288 kdDebug(30522) << "END OF A CELL" << endl;
289}
290
291/*******************************************/
292/* generateTableHeader */
293/*******************************************/
294void Table::generateTableHeader(TQTextStream& out)
295{
296 Element* elt = 0;
297 bool fullRightBorder = true;
298 bool fullLeftBorder = true;
299
300 out << "{";
301
302 for(int col = 0; col <= getMaxCol(); col++)
303 {
304 for(int row = 0; row < getMaxRow(); row++)
305 {
306 /* Search the cell in the list */
307 elt = searchCell(row, col);
308
309 /* If the element has a border display it here */
310 if(!elt->hasRightBorder())
311 fullRightBorder = false;
312 if(!elt->hasLeftBorder())
313 fullLeftBorder = false;
314 }
315 if(fullLeftBorder)
316 out << "|";
317 out << "m{" << getCellSize(col) << "pt}";
318 if(fullRightBorder)
319 out << "|";
320 }
321 out << "}";
322}
323
bool hasLeftBorder() const
Accessors.
Definition border.h:105
This class is a element.
Table()
Constructors.
Cell * searchCell(int col, int row)
Helpfull functions.
int getMaxRow() const
getters
void setMaxRow(int r)
setters
This class hold 2 lists of paragraphs for the text and for the footnotes.
Definition textFrame.h:40