00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <iostream>
00022
#include <qpainter.h>
00023
00024
#include <kdebug.h>
00025
00026
#include "contextstyle.h"
00027
#include "formulacursor.h"
00028
#include "formulaelement.h"
00029
#include "kformulacontainer.h"
00030
#include "kformuladocument.h"
00031
00032 KFORMULA_NAMESPACE_BEGIN
00033
00034 FormulaElement::FormulaElement(
FormulaDocument* container)
00035 : document( container ), baseSize( 20 ), ownBaseSize( false )
00036 {
00037 }
00038
00039
00040
void FormulaElement::setBaseSize(
int size )
00041 {
00042
if ( size > 0 ) {
00043 baseSize = size;
00044 ownBaseSize =
true;
00045 }
00046
else {
00047 ownBaseSize =
false;
00048 }
00049 document->
baseSizeChanged( size, ownBaseSize );
00050 }
00051
00052
00056 BasicElement*
FormulaElement::goToPos(
FormulaCursor* cursor,
const LuPixelPoint& point )
00057 {
00058
bool handled =
false;
00059
BasicElement* element = inherited::goToPos(cursor, handled, point,
LuPixelPoint());
00060
if (element == 0) {
00061
00062 cursor->
setTo(
this,
countChildren());
00063
00064
return this;
00065 }
00066
return element;
00067 }
00068
00069 void FormulaElement::elementRemoval(
BasicElement* child)
00070 {
00071 document->
elementRemoval(child);
00072 }
00073
00074 void FormulaElement::changed()
00075 {
00076 document->
changed();
00077 }
00078
00079 void FormulaElement::cursorHasMoved(
FormulaCursor* cursor )
00080 {
00081 document->
cursorHasMoved( cursor );
00082 }
00083
00084
void FormulaElement::moveOutLeft(
FormulaCursor* cursor )
00085 {
00086 document->
moveOutLeft( cursor );
00087 }
00088
00089
void FormulaElement::moveOutRight(
FormulaCursor* cursor )
00090 {
00091 document->
moveOutRight( cursor );
00092 }
00093
00094
void FormulaElement::moveOutBelow(
FormulaCursor* cursor )
00095 {
00096 document->
moveOutBelow( cursor );
00097 }
00098
00099
void FormulaElement::moveOutAbove(
FormulaCursor* cursor )
00100 {
00101 document->
moveOutAbove( cursor );
00102 }
00103
00104 void FormulaElement::tell(
const QString& msg )
00105 {
00106 document->
tell( msg );
00107 }
00108
00109 void FormulaElement::removeFormula(
FormulaCursor* cursor )
00110 {
00111 document->
removeFormula( cursor );
00112 }
00113
00114
void FormulaElement::insertFormula(
FormulaCursor* cursor )
00115 {
00116 document->
insertFormula( cursor );
00117 }
00118
00119 void FormulaElement::calcSizes(
const ContextStyle& style,
00120 ContextStyle::TextStyle tstyle,
00121 ContextStyle::IndexStyle istyle )
00122 {
00123 inherited::calcSizes( style, tstyle, istyle );
00124 }
00125
00126
00127 void FormulaElement::draw(
QPainter& painter,
const LuPixelRect& r,
00128
const ContextStyle& context,
00129 ContextStyle::TextStyle tstyle,
00130 ContextStyle::IndexStyle istyle,
00131
const LuPixelPoint& parentOrigin )
00132 {
00133 inherited::draw( painter, r, context, tstyle, istyle, parentOrigin );
00134 }
00135
00136
00140 void FormulaElement::calcSizes(
ContextStyle& context )
00141 {
00142
00143
if ( ownBaseSize ) {
00144 context.
setSizeFactor( static_cast<double>( getBaseSize() )/context.
baseSize() );
00145 }
00146
else {
00147 context.
setSizeFactor( 1 );
00148 }
00149
calcSizes( context, context.
getBaseTextStyle(),
00150 ContextStyle::normal );
00151 }
00152
00156 void FormulaElement::draw(
QPainter& painter,
const LuPixelRect& r,
00157
ContextStyle& context )
00158 {
00159
00160
if ( ownBaseSize ) {
00161 context.
setSizeFactor( static_cast<double>( getBaseSize() )/context.
baseSize() );
00162 }
00163
else {
00164 context.
setSizeFactor( 1 );
00165 }
00166
draw( painter, r, context, context.
getBaseTextStyle(),
00167 ContextStyle::normal,
LuPixelPoint() );
00168 }
00169
00170 KCommand*
FormulaElement::buildCommand(
Container* container, Request* request )
00171 {
00172
switch ( *request ) {
00173
case req_compactExpression:
00174
return 0;
00175
default:
00176
break;
00177 }
00178
return inherited::buildCommand( container, request );
00179 }
00180
00181 const SymbolTable&
FormulaElement::getSymbolTable()
const
00182
{
00183
return document->
getSymbolTable();
00184 }
00185
00186
00187 QDomElement FormulaElement::emptyFormulaElement(
QDomDocument doc )
00188 {
00189
QDomElement element = doc.createElement(
getTagName() );
00190
00191
00192
00193
00194
00195
00196
return element;
00197 }
00198
00199 KCommand*
FormulaElement::input(
Container* container,
QKeyEvent* event )
00200 {
00201
QChar ch = event->text().at( 0 );
00202
if ( !ch.isPrint() ) {
00203
int action = event->key();
00204
00205
00206
00207
switch ( action ) {
00208
case Qt::Key_Return:
00209
case Qt::Key_Enter: {
00210
FormulaCursor* cursor = container->
activeCursor();
00211 insertFormula( cursor );
00212
return 0;
00213 }
00214 }
00215 }
00216
return inherited::input( container, event );
00217 }
00218
00222 void FormulaElement::writeDom(
QDomElement element)
00223 {
00224 inherited::writeDom(element);
00225 element.setAttribute(
"VERSION",
"6" );
00226
if ( ownBaseSize ) {
00227 element.setAttribute(
"BASESIZE", baseSize );
00228 }
00229 }
00230
00235 bool FormulaElement::readAttributesFromDom(
QDomElement element)
00236 {
00237
if (!inherited::readAttributesFromDom(element)) {
00238
return false;
00239 }
00240
int version = -1;
00241
QString versionStr = element.attribute(
"VERSION" );
00242
if ( !versionStr.isNull() ) {
00243 version = versionStr.toInt();
00244 }
00245
if ( version > -1 ) {
00246
00247
00248
if ( version < 4 ) {
00249 convertNames( element );
00250 }
00251 }
00252
QString baseSizeStr = element.attribute(
"BASESIZE" );
00253
if ( !baseSizeStr.isNull() ) {
00254 ownBaseSize =
true;
00255 baseSize = baseSizeStr.toInt();
00256 }
00257
else {
00258 ownBaseSize =
false;
00259 }
00260
return true;
00261 }
00262
00268 bool FormulaElement::readContentFromDom(
QDomNode& node)
00269 {
00270
return inherited::readContentFromDom(node);
00271 }
00272
00273
void FormulaElement::convertNames(
QDomNode node )
00274 {
00275
if ( node.isElement() && ( node.nodeName().upper() ==
"TEXT" ) ) {
00276
QDomNamedNodeMap attr = node.attributes();
00277
QDomAttr ch = attr.namedItem(
"CHAR" ).toAttr();
00278
if ( ch.value() ==
"\\" ) {
00279
QDomNode sequence = node.parentNode();
00280
QDomDocument doc = sequence.ownerDocument();
00281
QDomElement nameseq = doc.createElement(
"NAMESEQUENCE" );
00282 sequence.replaceChild( nameseq, node );
00283
00284
bool inName =
true;
00285
while ( inName ) {
00286 inName =
false;
00287
QDomNode n = nameseq.nextSibling();
00288
if ( n.isElement() && ( n.nodeName().upper() ==
"TEXT" ) ) {
00289 attr = n.attributes();
00290 ch = attr.namedItem(
"CHAR" ).toAttr();
00291
if ( ch.value().at( 0 ).isLetter() ) {
00292 nameseq.appendChild( sequence.removeChild( n ) );
00293 inName =
true;
00294 }
00295 }
00296 }
00297 }
00298 }
00299
if ( node.hasChildNodes() ) {
00300
QDomNode n = node.firstChild();
00301
while ( !n.isNull() ) {
00302 convertNames( n );
00303 n = n.nextSibling();
00304 }
00305 }
00306 }
00307
00308 QString FormulaElement::toLatex()
00309 {
00310
return inherited::toLatex();
00311 }
00312
00313 void FormulaElement::writeMathML(
QDomDocument doc,
QDomNode parent )
00314 {
00315
QDomElement de = doc.createElementNS(
"http://www.w3.org/1998/Math/MathML",
00316
"math" );
00317 inherited::writeMathML( doc, de );
00318 parent.appendChild( de );
00319 }
00320
00321 KFORMULA_NAMESPACE_END