filters

formula.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 <stdlib.h>     /* for atoi function */
00023 #include <kdebug.h>     /* for kdDebug() stream */
00024 #include <qptrstack.h>      /* for getFormula() */
00025 #include <qdom.h>
00026 #include "formula.h"
00027 #include <kapplication.h>
00028 
00029 #include <kformuladocument.h>
00030 #include <kformulamimesource.h>
00031 
00032 /*******************************************/
00033 /* Constructor                             */
00034 /*******************************************/
00035 Formula::Formula()
00036 {
00037     _left              = 0;
00038     _right             = 0;
00039     _top               = 0;
00040     _bottom            = 0;
00041     _runaround         = TA_NONE;
00042     _runaroundGap      = 0;
00043     _autoCreate        = TC_EXTEND;
00044     _newFrameBehaviour = TF_RECONNECT;
00045 
00046 }
00047 
00048 /*******************************************/
00049 /* analyse                                 */
00050 /*******************************************/
00051 void Formula::analyse(const QDomNode balise)
00052 {
00053 
00054     /* MARKUP TYPE : FRAMESET INFO = TEXTE, ENTETE CONNUE */
00055 
00056     /* Parameters Analyse */
00057     Element::analyse(balise);
00058 
00059     kdDebug(30522) << "FRAME ANALYSE (Formula)" << endl;
00060 
00061     /* Chlidren markups Analyse */
00062     for(int index= 0; index < getNbChild(balise); index++)
00063     {
00064         if(getChildName(balise, index).compare("FRAME")== 0)
00065         {
00066             analyseParamFrame(balise);
00067         }
00068         else if(getChildName(balise, index).compare("FORMULA")== 0)
00069         {
00070             getFormula(getChild(getChild(balise, "FORMULA"), "FORMULA"), 0);
00071             kdDebug(30522) << _formula << endl;
00072         }
00073 
00074     }
00075     kdDebug(30522) << "END OF A FRAME" << endl;
00076 }
00077 
00078 /*******************************************/
00079 /* getFormula                              */
00080 /*******************************************/
00081 /* Get back the xml markup tree.           */
00082 /*******************************************/
00083 void Formula::getFormula(QDomNode p, int indent)
00084 {
00085 /*  while( p.)
00086     {*/
00087         switch( p.nodeType() )
00088         {
00089             case QDomNode::TextNode:
00090                 _formula = _formula + QString(p.toText().data()) + " ";
00091                 break;
00092         /*  case TT_Space:
00093                 _formula = _formula + p->zText;
00094                 //printf("%*s\"%s\"\n", indent, "", p->zText);
00095                 break;
00096             case TT_EOL:
00097                 _formula = _formula + "\n";
00098                 //printf("%*s\n", indent, "");
00099                 break;*/
00100             case QDomNode::ElementNode:
00101                 _formula = _formula + "<" + p.nodeName();
00102                 QDomNamedNodeMap attr = p.attributes();
00103                 for(unsigned int index = 0; index < attr.length(); index++)
00104                 { // The attributes
00105                     _formula = _formula + " " + attr.item(index).nodeName();
00106                     _formula = _formula + "=\"" + attr.item(index).nodeValue() + "\"";
00107                 }
00108                 if(p.childNodes().length() == 0)
00109                     _formula = _formula + "/>\n";
00110                 else
00111                 {
00112                     _formula = _formula + ">\n";
00113                     QDomNodeList child = p.childNodes();
00114                     for(unsigned int index = 0; index < child.length(); index++)
00115                     {
00116                         getFormula(child.item(index), indent+3); // The child elements
00117                     }
00118                     _formula = _formula + "</" + p.nodeName() + ">\n";
00119                 }
00120                 break;
00121             /*default:
00122                 kdError(30522) << "Can't happen" << endl;
00123                 break;*/
00124         }
00125     /*  p = p.nextSibling();
00126     }*/
00127 }
00128 
00129 /*******************************************/
00130 /* analyseParamFrame                       */
00131 /*******************************************/
00132 void Formula::analyseParamFrame(const QDomNode balise)
00133 {
00134     /*<FRAME left="28" top="42" right="566" bottom="798" runaround="1" />*/
00135 
00136     _left = getAttr(balise, "left").toInt();
00137     _top = getAttr(balise, "top").toInt();
00138     _right = getAttr(balise, "right").toInt();
00139     _bottom = getAttr(balise, "bottom").toInt();
00140     setRunAround(getAttr(balise, "runaround").toInt());
00141     setAroundGap(getAttr(balise, "runaroundGap").toInt());
00142     setAutoCreate(getAttr(balise, "autoCreateNewFrame").toInt());
00143     setNewFrame(getAttr(balise, "newFrameBehaviour").toInt());
00144     setSheetSide(getAttr(balise, "sheetside").toInt());
00145 }
00146 
00147 /*******************************************/
00148 /* generate                                */
00149 /*******************************************/
00150 void Formula::generate(QTextStream &out)
00151 {
00152     kdDebug(30522) << "FORMULA GENERATION" << endl;
00153     QDomDocument doc;
00154     doc.setContent(_formula);
00155 
00156     // a new KFormula::Document for every formula is not the best idea.
00157     // better to have only one such beast for the whole document.
00158     KFormula::Document formulaDoc( kapp->sessionConfig() );
00159 
00160     KFormula::Container* formula = new KFormula::Container( &formulaDoc );
00161     if ( !formula->load( doc ) ) {
00162         kdError(30522) << "Failed." << endl;
00163     }
00164 
00165     out << "$" << formula->texString() << "$";
00166         delete formula;
00167 }
00168 
KDE Home | KDE Accessibility Home | Description of Access Keys