filters

table.cc

00001 /*
00002 ** A program to convert the XML rendered by KWord into LATEX.
00003 **
00004 ** Copyright (C) 2000,2002 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 "listtable.h"
00025 #include "textFrame.h"
00026 
00027 /*******************************************/
00028 /* Constructor                             */
00029 /*******************************************/
00030 Table::Table()
00031 {
00032     setMaxCol(0);
00033     setMaxRow(0);
00034 }
00035 
00036 Table::Table(QString grpMgr)
00037 {
00038     setGrpMgr(grpMgr);
00039     setMaxCol(0);
00040     setMaxRow(0);
00041 }
00042 
00043 /*******************************************/
00044 /* Destructor                              */
00045 /*******************************************/
00046 Table::~Table()
00047 {
00048     kdDebug(30522) << "Destruction of a list of frames" << endl;
00049 }
00050 
00051 /*******************************************/
00052 /* getCellFlow                             */
00053 /*******************************************/
00054 EEnv Table::getCellFlow(int col)
00055 {
00056     for(int row = 0; row<= getMaxRow(); row++)
00057     {
00058         Element* elt = at(row * getMaxRow() + col);
00059         if(elt->getType() == ST_TEXT)
00060         {
00061             kdDebug(30522) << ((TextFrame*) elt)->getFirstPara()->getEnv() << endl;
00062             return ((TextFrame*) elt)->getFirstPara()->getEnv();
00063         }
00064     }
00065     kdDebug(30522) << "Default flow for cell" << endl;
00066     return ENV_JUSTIFY;
00067 }
00068 
00069 /*******************************************/
00070 /* getCellFlow                             */
00071 /*******************************************/
00072 double Table::getCellSize(int col)
00073 {
00074 
00075     for(int row = 0; row<= getMaxRow(); row++)
00076     {
00077         Element* elt = at(row * getMaxRow() + col);
00078         if(elt->getType() == ST_TEXT)
00079         {
00080             kdDebug(30522) << "size : " << ((TextFrame*) elt)->getLeft() << endl;
00081             return ((TextFrame*) elt)->getRight() - ((TextFrame*) elt)->getLeft();
00082         }
00083     }
00084     kdDebug(30522) << "Default size for cell" << endl;
00085     return 3;
00086 }
00087 
00088 /*******************************************/
00089 /* searchCell                              */
00090 /*******************************************/
00091 Element* Table::searchCell(int row, int col)
00092 {
00093     Element* current = 0;
00094 
00095     /* Parcourir les tables et tester chaque nom de table */
00096     for(current = first(); current != 0; current = next())
00097     {
00098         kdDebug(30522) << "+" << current->getRow() << "," << current->getCol() << endl;
00099         if(current->getRow() == row && current->getCol() == col)
00100             return current;
00101     }
00102     return 0;
00103 }
00104 
00105 /*******************************************/
00106 /* append                                  */
00107 /*******************************************/
00108 void Table::append(Element* elt)
00109 {
00110     if(elt->getRow() > getMaxRow())
00111         setMaxRow(elt->getRow());
00112 
00113     if(elt->getCol() > getMaxCol())
00114         setMaxCol(elt->getCol());
00115 
00116     QPtrList<Element>::append(elt);
00117 }
00118 
00119 /*******************************************/
00120 /* generate                                */
00121 /*******************************************/
00122 void Table::generate(QTextStream& out)
00123 {
00124     Element* elt = 0;
00125     kdDebug(30522) << "GENERATION OF A TABLE " << count() << endl;
00126     out << endl << "\\begin{tabular}";
00127     generateTableHeader(out);
00128     out << endl;
00129     Config::instance()->indent();
00130 
00131     int row= 0;
00132     while(row <= getMaxRow())
00133     {
00134         generateTopLineBorder(out, row);
00135         for(int col= 0; col <= getMaxCol(); col++)
00136         {
00137             Config::instance()->writeIndent(out);
00138     
00139             /* Search the cell in the list */
00140             elt = searchCell(row, col);
00141 
00142             out << "\\multicolumn{1}{";
00143             if(elt->hasLeftBorder())
00144                 out << "|";
00145             out << "m{" << getCellSize(col) << "pt}";
00146             
00147             if(elt->hasRightBorder())
00148                 out << "|";
00149             out << "}{" << endl;
00150 
00151             generateCell(out, row, col);
00152             out << "}" << endl;
00153             if(col < getMaxCol())
00154                 out << "&" << endl;
00155         }
00156         out << "\\\\" << endl;
00157         Config::instance()->writeIndent(out);
00158         row = row + 1;
00159     }
00160     generateBottomLineBorder(out, row - 1);
00161     out << "\\end{tabular}" << endl << endl;
00162     Config::instance()->desindent();
00163     kdDebug(30522) << "END OF GENERATINO OF A TABLE" << endl;
00164 }
00165 
00166 /*******************************************/
00167 /* generateTopLineBorder                   */
00168 /*******************************************/
00169 void Table::generateTopLineBorder(QTextStream& out, int row)
00170 {
00171     Element* elt = 0;
00172     QBitArray border(getMaxCol());
00173     bool fullLine = true;
00174     
00175     for(int index = 0; index <= getMaxCol(); index++)
00176     {
00177         /* Search the cell in the list */
00178         elt = searchCell(row, index);
00179         kdDebug(30522) << endl << "name (" << row << ", " << index << ") = " << elt->getName() << endl << endl;
00180 
00181         /* If the element has a border display it here */
00182         if(elt->hasTopBorder())
00183         {
00184             border[index] = 1;
00185         }
00186         else
00187         {
00188             border[index] = 0;
00189             fullLine = false;
00190         }
00191     }
00192 
00193     if(fullLine)
00194     {
00195         /* All column have a top border */
00196         Config::instance()->writeIndent(out);
00197         out << "\\hline" << endl;
00198     }
00199     else
00200     {
00201         int index = 0;
00202         while(index <= getMaxCol())
00203         {
00204             if(border[index])
00205             {
00206                 int begin = index;
00207                 int end = index;
00208                 while(border[index] && index < getMaxCol())
00209                 {
00210                     index = index + 1;
00211                 }
00212                 end = index - 1;
00213                 out << "\\cline{" << (begin + 1) << "-" << (end + 1) << "} " << endl;
00214             }
00215             index = index + 1;
00216         }
00217     }
00218 }
00219 
00220 /*******************************************/
00221 /* generateBottomLineBorder                */
00222 /*******************************************/
00223 void Table::generateBottomLineBorder(QTextStream& out, int row)
00224 {
00225     Element* elt = 0;
00226     QBitArray border(getMaxCol());
00227     bool fullLine = true;
00228 
00229     for(int index = 0; index <= getMaxCol(); index++)
00230     {
00231         /* Search the cell in the list */
00232         elt = searchCell(row, index);
00233 
00234         /* If the element has a border display it here */
00235         if(elt->hasBottomBorder())
00236         {
00237             border[index] = 1;
00238         }
00239         else
00240         {
00241             border[index] = 0;
00242             fullLine = false;
00243         }
00244     }
00245 
00246     if(fullLine)
00247     {
00248         /* All column have a top border */
00249         Config::instance()->writeIndent(out);
00250         out << "\\hline" << endl;
00251     }
00252     else
00253     {
00254         int index = 0;
00255         while(index <= getMaxCol())
00256         {
00257             if(border[index])
00258             {
00259                 int begin = index;
00260                 int end = index;
00261                 while(border[index] && index <= getMaxCol())
00262                 {
00263                     index = index + 1;
00264                 }
00265                 end = index - 1;
00266                 out << "\\cline{" << (begin + 1) << "-" << (end + 1) << "} " << endl;
00267             }
00268             index = index + 1;
00269         }
00270     }
00271 }
00272 
00273 /*******************************************/
00274 /* generateCell                            */
00275 /*******************************************/
00276 void Table::generateCell(QTextStream& out, int row, int col)
00277 {
00278     Element* elt = 0;
00279 
00280     kdDebug(30522) << "NEW CELL : " << row << "," << col << endl;
00281 
00282     /* Search the cell in the list */
00283     elt = searchCell(row, col);
00284 
00285     /* Generate it */
00286     if(elt != 0)
00287         elt->generate(out);
00288     kdDebug(30522) << "END OF A CELL" << endl;
00289 }
00290 
00291 /*******************************************/
00292 /* generateTableHeader                     */
00293 /*******************************************/
00294 void Table::generateTableHeader(QTextStream& out)
00295 {
00296     Element* elt = 0;
00297     bool fullRightBorder = true;
00298     bool fullLeftBorder = true;
00299 
00300     out << "{";
00301 
00302     for(int col = 0; col <= getMaxCol(); col++)
00303     {
00304         for(int row = 0; row < getMaxRow(); row++)
00305         {
00306             /* Search the cell in the list */
00307             elt = searchCell(row, col);
00308 
00309             /* If the element has a border display it here */
00310             if(!elt->hasRightBorder())
00311                 fullRightBorder = false;
00312             if(!elt->hasLeftBorder())
00313                 fullLeftBorder = false;
00314         }
00315         if(fullLeftBorder)
00316             out << "|";
00317         out << "m{" << getCellSize(col) << "pt}";
00318         if(fullRightBorder)
00319             out << "|";
00320     }
00321     out << "}";
00322 }
00323 
KDE Home | KDE Accessibility Home | Description of Access Keys