filters

table.cc

00001 /*
00002 ** A program to convert the XML rendered by KWord into LATEX.
00003 **
00004 ** Copyright (C) 2000 Robert JACOLIN
00005 **
00006 ** This library is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU Library General Public
00008 ** License as published by the Free Software Foundation; either
00009 ** version 2 of the License, or (at your option) any later version.
00010 **
00011 ** This library is distributed in the hope that it will be useful,
00012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** Library General Public License for more details.
00015 **
00016 ** To receive a copy of the GNU Library General Public License, write to the
00017 ** Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 **
00020 */
00021 
00022 #include <kdebug.h>     /* for kdDebug stream */
00023 #include <qbitarray.h>
00024 #include "cell.h"
00025 #include "column.h"
00026 #include "row.h"
00027 #include "table.h"
00028 
00029 /*******************************************/
00030 /* Constructor                             */
00031 /*******************************************/
00032 Table::Table()
00033 {
00034     _maxCol = 0;
00035     _maxRow = 0;
00036 }
00037 
00038 /*******************************************/
00039 /* Destructor                              */
00040 /*******************************************/
00041 Table::~Table()
00042 {
00043 }
00044 
00045 void Table::setMaxColumn(int col)
00046 {
00047     if(_maxCol < col) _maxCol = col;
00048 }
00049 
00050 void Table::setMaxRow(int row)
00051 {
00052     if(_maxRow < row) _maxRow = row;
00053 }
00054 
00055 void Table::analyse(const QDomNode balise)
00056 {
00057     kdDebug(30522) << "New table" << endl;
00058     if(getAttr(balise, "columnnumber") == "1")
00059         setColumnNumber();
00060     if(getAttr(balise, "borders") == "1")
00061         setBorders();
00062     if(getAttr(balise, "hide") == "1")
00063         setHide();
00064     if(getAttr(balise, "hidezero") == "1")
00065         setHideZero();
00066     if(getAttr(balise, "firstletterupper") == "1")
00067         setFirstletterupper();
00068     if(getAttr(balise, "grid") == "1")
00069         setGrid();
00070     if(getAttr(balise, "printgrid") == "1")
00071         setPrintGrid();
00072     if(getAttr(balise, "printCommentIndicator") == "1")
00073         setPrintCommentIndicator();
00074     if(getAttr(balise, "printFormulaIndicator") == "1")
00075         setPrintFormulaIndicator();
00076     if(getAttr(balise, "showFormula") == "1")
00077         setShowFormula();
00078     if(getAttr(balise, "showFormulaIndicator") == "1")
00079         setShowFormulaIndicator();
00080     if(getAttr(balise, "lcmode") == "1")
00081         setLCMode();
00082     setName(getAttr(balise, "name"));
00083     
00084     analysePaper(getChild(balise, "paper"));
00085 
00086     int max = getNbChild(balise);
00087     for(int index = 0; index < max; index++)
00088     {
00089         QString name = getChildName(balise, index);     
00090         if(name == "cell")
00091         {
00092             kdDebug(30522) << "----- cell -----" << endl;
00093             Cell* cell = new Cell();
00094             cell->analyse(getChild(balise, index));
00095             _cells.append(cell);
00096             setMaxColumn(cell->getCol());
00097             setMaxRow(cell->getRow());
00098         }
00099         else if(name == "column")
00100         {
00101             kdDebug(30522) << "----- column -----" << endl;
00102             Column* column = new Column();
00103             column->analyse(getChild(balise, index));
00104             _columns.append(column);
00105         }
00106         else if(name == "row")
00107         {
00108             kdDebug(30522) << "----- row -----" << endl;
00109             Row* row = new Row();
00110             row->analyse(getChild(balise, index));
00111             _rows.append(row);
00112         }
00113         else
00114             kdDebug(30522) << "name : " << name << endl;
00115     }
00116 }
00117 
00118 void Table::analysePaper(const QDomNode balise)
00119 {
00120     setFormat(getAttr(balise, "format"));
00121     setOrientation(getAttr(balise, "orientation"));
00122 
00123     /* borders */
00124     QDomNode border = getChild(balise, "borders");
00125     setBorderRight(getAttr(balise, "right").toLong());
00126     setBorderLeft(getAttr(balise, "left").toLong());
00127     setBorderBottom(getAttr(balise, "bottom").toLong());
00128     setBorderTop(getAttr(balise, "top").toLong());
00129 }
00130 
00131 Cell* Table::searchCell(int col, int row)
00132 {
00133     QPtrListIterator<Cell> it(_cells);
00134 
00135     kdDebug(30522) << "search in list of " << _cells.count() << " cells" << endl;
00136     Cell *cell = 0;
00137     while ( (cell = it.current()) != 0 )
00138     {
00139         ++it;
00140         kdDebug(30522) << "cell: " << cell->getRow() << "-" << cell->getCol() << endl;
00141         if(cell->getCol() == col && cell->getRow() == row)
00142             return cell;
00143     }
00144     return NULL;
00145 }
00146 
00147 Column* Table::searchColumn(int col)
00148 {
00149     QPtrListIterator<Column> it(_columns);
00150 
00151     Column *column;
00152     while ( (column = it.current()) != 0 )
00153     {
00154         ++it;
00155         if(column->getCol() == col)
00156             return column;
00157     }
00158     return NULL;
00159 }
00160 
00161 Row* Table::searchRow(int rowNumber)
00162 {
00163     QPtrListIterator<Row> it(_rows);
00164 
00165     Row *row;
00166     while ( (row = it.current()) != 0 )
00167     {
00168         ++it;
00169         if(row->getRow() == rowNumber)
00170             return row;
00171     }
00172     return NULL;
00173 }
00174 
00175 /*******************************************/
00176 /* generate                                */
00177 /*******************************************/
00178 void Table::generate(QTextStream& out)
00179 {
00180     kdDebug(30522) << "GENERATION OF A TABLE " << getMaxRow() << " - " << getMaxColumn()
00181         << endl;
00182     out << endl << "%% " << getName() << endl;
00183     if(getOrientation() == "Portrait")
00184     {
00185         out << "\\begin{sidewaystable}" << endl << endl;
00186         indent();
00187         writeIndent(out);
00188     }
00189     
00190     out << "\\begin{tabular}";
00191     generateTableHeader(out);
00192     out << endl;
00193     indent();
00194     int rowNumber = 1;
00195     while(rowNumber <= getMaxRow())
00196     {
00197         generateTopLineBorder(out, rowNumber);
00198         Row* row = searchRow(rowNumber);
00199         if(row != NULL)
00200             row->generate(out);
00201         
00202         for(int col = 1; col <= getMaxColumn(); col++)
00203         {
00204             writeIndent(out);
00205             generateCell(out, rowNumber, col);
00206             
00207             if(col < getMaxColumn())
00208                 out << " & "<< endl;
00209         }
00210         out << "\\\\" << endl;
00211         rowNumber++;
00212     }
00213     generateBottomLineBorder(out, rowNumber - 1);
00214     desindent();
00215     writeIndent(out);
00216     out << "\\end{tabular}" << endl << endl;
00217     desindent();
00218     
00219     if(getOrientation() == "Portrait")
00220     {
00221         out << "\\end{sidewaystable}" << endl;
00222         desindent();
00223     }
00224     /*Element* elt = 0;
00225     kdDebug(30522) << "GENERATION OF A TABLE " << count() << endl;
00226     out << endl << "\\begin{tabular}";
00227     generateTableHeader(out);
00228     out << endl;
00229     indent();
00230 
00231     int row= 0;
00232     while(row <= getMaxRow())
00233     {
00234         generateTopLineBorder(out, row);
00235         for(int col= 0; col <= getMaxCol(); col++)
00236         {
00237             writeIndent(out);
00238     */
00239             /* Search the cell in the list */
00240         /*  elt = searchCell(row, col);
00241 
00242             out << "\\multicolumn{1}{";
00243             if(elt->hasLeftBorder())
00244                 out << "|";
00245             out << "m{" << getCellSize(col) << "pt}";
00246             
00247             if(elt->hasRightBorder())
00248                 out << "|";
00249             out << "}{" << endl;
00250 
00251             generateCell(out, row, col);
00252             out << "}" << endl;
00253             if(col < getMaxCol())
00254                 out << "&" << endl;
00255         }
00256         out << "\\\\" << endl;
00257         writeIndent(out);
00258         row = row + 1;
00259     }
00260     generateBottomLineBorder(out, row - 1);
00261     out << "\\end{tabular}" << endl << endl;
00262     desindent();*/
00263     kdDebug(30522) << "END OF GENERATINO OF A TABLE" << endl;
00264 }
00265 
00266 /*******************************************/
00267 /* generateTopLineBorder                   */
00268 /*******************************************/
00269 void Table::generateTopLineBorder(QTextStream& out, int row)
00270 {
00271     
00272     Cell* cell = 0;
00273     QBitArray border( getMaxColumn() );
00274     bool fullLine = true;
00275     for(int index = 1; index <= getMaxColumn(); index++)
00276     {
00277         /* Search the cell in the list */
00278         kdDebug(30522) << "search " << row << ", " << index << endl;
00279         cell = searchCell(index, row);
00280 
00281         if(cell == NULL)
00282             cell = new Cell(row, index);
00283 
00284         /* If the element has a border display it here */
00285         border[ index ] = cell->hasTopBorder();
00286         if( ! cell->hasTopBorder() )
00287             fullLine = false;
00288     }
00289 
00290     if(fullLine)
00291     {
00292         /* All column have a top border */
00293         writeIndent(out);
00294         out << "\\hline" << endl;
00295     }
00296     else
00297     {
00298         int index = 0;
00299         while(index < getMaxColumn())
00300         {
00301             if(border[index])
00302             {
00303                 int begin = index;
00304                 int end;
00305                 index = index + 1;
00306                 while(border[index] && index < getMaxColumn())
00307                 {
00308                     index = index + 1;
00309                 }
00310                 end = index - 1;
00311                 out << "\\cline{" << begin << "-" << end << "} " << endl;
00312             }
00313             index = index + 1;
00314         }
00315     }
00316     
00317     /*Row * row;
00318     row = searchRow(row);
00319     if(row != NULL)
00320         row->generate(out);*/
00321 }
00322 
00323 /*******************************************/
00324 /* generateBottomLineBorder                */
00325 /*******************************************/
00326 void Table::generateBottomLineBorder(QTextStream& out, int row)
00327 {
00328     Cell* cell = 0;
00329     QBitArray border( getMaxColumn() );
00330     bool fullLine = true;
00331 
00332     for(int index = 1; index <= getMaxColumn(); index++)
00333     {
00334         /* Search the cell in the list */
00335         cell = searchCell(index, row);
00336 
00337         if(cell == NULL)
00338             cell = new Cell(row, index);
00339 
00340         /* If the element has a border display it here */
00341         border[ index ] = cell->hasBottomBorder();
00342         if( ! cell->hasBottomBorder() )
00343             fullLine = false;
00344     }
00345 
00346     if(fullLine)
00347     {
00348         /* All column have a bottom border */
00349         writeIndent(out);
00350         out << "\\hline" << endl;
00351     }
00352     else
00353     {
00354         int index = 0;
00355         while(index < getMaxColumn())
00356         {
00357             if(border[index])
00358             {
00359                 int begin = index;
00360                 int end;
00361                 index = index + 1;
00362                 while(border[index] && index < getMaxColumn())
00363                 {
00364                     index = index + 1;
00365                 }
00366                 end = index - 1;
00367                 out << "\\cline{" << begin << "-" << end << "} " << endl;
00368             }
00369             index = index + 1;
00370         }
00371     }
00372 }
00373 
00374 /*******************************************/
00375 /* generateCell                            */
00376 /*******************************************/
00377 void Table::generateCell(QTextStream& out, int row, int col)
00378 {
00379     kdDebug(30522) << "GENERATE CELL : " << row << "," << col << endl;
00380 
00381     /* Search the cell in the list */
00382     Cell *cell = searchCell(col, row);
00383     if(cell != NULL)
00384     {   
00385         kdDebug(30522) << "generate cell with text: " << cell->getText() << endl;
00386         cell->generate(out, this);
00387     }
00388 
00389     kdDebug(30522) << "END OF A CELL" << endl;
00390 }
00391 
00392 /*******************************************/
00393 /* generateTableHeader                     */
00394 /*******************************************/
00395 void Table::generateTableHeader(QTextStream& out)
00396 {
00397     Column* column = 0;
00398 
00399     out << "{";
00400 
00401     for(int col = 1; col <= getMaxColumn(); col++)
00402     {
00403         column = searchColumn(col);
00404         if(column != NULL)
00405             column->generate(out);
00406         else
00407         {
00408             out << "m{20pt}";
00409         }
00410     }
00411     out << "}";
00412 
00413 }
00414 
KDE Home | KDE Accessibility Home | Description of Access Keys