00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <QKeyEvent>
00024
00025
00026 #include <kdebug.h>
00027
00028 #include <KoXmlWriter.h>
00029
00030 #include "contextstyle.h"
00031 #include "FormulaContainer.h"
00032 #include "FormulaCursor.h"
00033 #include "FormulaElement.h"
00034
00035 namespace KFormula {
00036
00037 FormulaElement::FormulaElement() : BasicElement( 0 ),
00038 baseSize( 20 ),
00039 ownBaseSize( false )
00040 {
00041 }
00042
00043 FormulaElement::~FormulaElement()
00044 {
00045 foreach( BasicElement* tmpElement, m_childElements )
00046 delete tmpElement;
00047 }
00048
00049 const QList<BasicElement*> FormulaElement::childElements()
00050 {
00051 return m_childElements;
00052 }
00053
00054 void FormulaElement::readMathML( const QDomElement& element )
00055 {
00056 readMathMLAttributes( element );
00057 }
00058
00059 void FormulaElement::writeMathML( KoXmlWriter* writer, bool oasisFormat )
00060 {
00061 if( oasisFormat )
00062 writer->startElement( "math:semantics" );
00063 else
00064 writer->startDocument( "math", "http://www.w3.org/1998/Math/MathML" );
00065
00066 foreach( BasicElement* tmpElement, m_childElements )
00067 tmpElement->writeMathML( writer, oasisFormat );
00068
00069 if( oasisFormat )
00070 writer->endElement();
00071 else
00072 writer->endDocument();
00073 }
00074
00075
00076
00077
00078
00079 void FormulaElement::setBaseSize( int size )
00080 {
00081 if ( size > 0 ) {
00082 baseSize = size;
00083 ownBaseSize = true;
00084 }
00085 else {
00086 ownBaseSize = false;
00087 }
00088
00089 }
00090
00091
00092
00093
00094 void FormulaElement::elementRemoval(BasicElement* child)
00095 {
00096
00097 }
00098
00099 void FormulaElement::changed()
00100 {
00101
00102 }
00103
00104 void FormulaElement::cursorHasMoved( FormulaCursor* cursor )
00105 {
00106
00107 }
00108
00109 void FormulaElement::moveOutLeft( FormulaCursor* cursor )
00110 {
00111
00112 }
00113
00114 void FormulaElement::moveOutRight( FormulaCursor* cursor )
00115 {
00116
00117 }
00118
00119 void FormulaElement::moveOutBelow( FormulaCursor* cursor )
00120 {
00121
00122 }
00123
00124 void FormulaElement::moveOutAbove( FormulaCursor* cursor )
00125 {
00126
00127 }
00128
00129 void FormulaElement::removeFormula( FormulaCursor* cursor )
00130 {
00131
00132 }
00133
00134 void FormulaElement::insertFormula( FormulaCursor* cursor )
00135 {
00136
00137 }
00138
00139 void FormulaElement::calcSizes( const ContextStyle& style,
00140 ContextStyle::TextStyle tstyle,
00141 ContextStyle::IndexStyle istyle,
00142 StyleAttributes& style )
00143 {
00144
00145 }
00146
00147
00148 void FormulaElement::draw( QPainter& painter, const LuPixelRect& r,
00149 const ContextStyle& context,
00150 ContextStyle::TextStyle tstyle,
00151 ContextStyle::IndexStyle istyle,
00152 StyleAttributes& style,
00153 const LuPixelPoint& parentOrigin )
00154 {
00155
00156 }
00157
00158
00162 void FormulaElement::calcSizes( ContextStyle& context )
00163 {
00164
00165 if ( ownBaseSize ) {
00166 context.setSizeFactor( static_cast<double>( getBaseSize() )/context.baseSize() );
00167 }
00168 else {
00169 context.setSizeFactor( 1 );
00170 }
00171 StyleAttributes style;
00172 calcSizes( context, context.getBaseTextStyle(),
00173 ContextStyle::normal, style );
00174 }
00175
00179 void FormulaElement::draw( QPainter& painter, const LuPixelRect& r,
00180 ContextStyle& context )
00181 {
00182
00183 if ( ownBaseSize ) {
00184 context.setSizeFactor( static_cast<double>( getBaseSize() )/context.baseSize() );
00185 }
00186 else {
00187 context.setSizeFactor( 1 );
00188 }
00189 draw( painter, r, context, context.getBaseTextStyle(),
00190 ContextStyle::normal, style, LuPixelPoint() );
00191 }
00192
00193 QDomElement FormulaElement::emptyFormulaElement( QDomDocument& doc )
00194 {
00195 QDomElement element = doc.createElement( getTagName() );
00196
00197
00198
00199
00200
00201
00202 return element;
00203 }
00207 void FormulaElement::writeDom(QDomElement element)
00208 {
00209 BasicElement::writeDom(element);
00210 element.setAttribute( "VERSION", "6" );
00211 if ( ownBaseSize ) {
00212 element.setAttribute( "BASESIZE", baseSize );
00213 }
00214 }
00215
00220 bool FormulaElement::readAttributesFromDom(QDomElement element)
00221 {
00222 if (!BasicElement::readAttributesFromDom(element)) {
00223 return false;
00224 }
00225 int version = -1;
00226 QString versionStr = element.attribute( "VERSION" );
00227 if ( !versionStr.isNull() ) {
00228 version = versionStr.toInt();
00229 }
00230 if ( version > -1 ) {
00231
00232
00233 if ( version < 4 ) {
00234 convertNames( element );
00235 }
00236 }
00237 QString baseSizeStr = element.attribute( "BASESIZE" );
00238 if ( !baseSizeStr.isNull() ) {
00239 ownBaseSize = true;
00240 baseSize = baseSizeStr.toInt();
00241 }
00242 else {
00243 ownBaseSize = false;
00244 }
00245 return true;
00246 }
00247
00253 bool FormulaElement::readContentFromDom(QDomNode& node)
00254 {
00255 return BasicElement::readContentFromDom(node);
00256 }
00257
00258 void FormulaElement::convertNames( QDomNode node )
00259 {
00260 if ( node.isElement() && ( node.nodeName().toUpper() == "TEXT" ) ) {
00261 QDomNamedNodeMap attr = node.attributes();
00262 QDomAttr ch = attr.namedItem( "CHAR" ).toAttr();
00263 if ( ch.value() == "\\" ) {
00264 QDomNode sequence = node.parentNode();
00265 QDomDocument doc = sequence.ownerDocument();
00266 QDomElement nameseq = doc.createElement( "NAMESEQUENCE" );
00267 sequence.replaceChild( nameseq, node );
00268
00269 bool inName = true;
00270 while ( inName ) {
00271 inName = false;
00272 QDomNode n = nameseq.nextSibling();
00273 if ( n.isElement() && ( n.nodeName().toUpper() == "TEXT" ) ) {
00274 attr = n.attributes();
00275 ch = attr.namedItem( "CHAR" ).toAttr();
00276 if ( ch.value().at( 0 ).isLetter() ) {
00277 nameseq.appendChild( sequence.removeChild( n ) );
00278 inName = true;
00279 }
00280 }
00281 }
00282 }
00283 }
00284 if ( node.hasChildNodes() ) {
00285 QDomNode n = node.firstChild();
00286 while ( !n.isNull() ) {
00287 convertNames( n );
00288 n = n.nextSibling();
00289 }
00290 }
00291 }
00292
00293
00294
00295 }