26#include "elementvisitor.h"
27#include "formulaelement.h"
28#include "formulacursor.h"
29#include "fractionelement.h"
30#include "sequenceelement.h"
32KFORMULA_NAMESPACE_BEGIN
36 m_lineThicknessType( NoSize ),
37 m_numAlign( NoHorizontalAlign ),
38 m_denomAlign( NoHorizontalAlign ),
39 m_customBevelled( false )
45FractionElement::~FractionElement()
53 m_lineThicknessType( other.m_lineThicknessType ),
54 m_lineThickness( other.m_lineThickness ),
55 m_numAlign( other.m_numAlign ),
56 m_denomAlign( other.m_denomAlign ),
57 m_customBevelled( other.m_customBevelled ),
58 m_bevelled( other.m_bevelled )
62 numerator->setParent(
this );
63 denominator->setParent(
this );
69 return visitor->visit(
this );
74 if ( child == numerator ) {
84 const LuPixelPoint& point,
const LuPixelPoint& parentOrigin )
88 LuPixelPoint myPos(parentOrigin.x() + getX(),
89 parentOrigin.y() + getY());
90 e = numerator->
goToPos(cursor, handled, point, myPos);
94 e = denominator->
goToPos(cursor, handled, point, myPos);
99 luPixel dx = point.x() - myPos.x();
100 luPixel dy = point.y() - myPos.y();
103 if ((dx > numerator->getX()) &&
104 (dy < numerator->getHeight())) {
105 numerator->moveLeft(cursor,
this);
109 else if ((dx > denominator->getX()) &&
110 (dy > denominator->getY())) {
111 denominator->moveLeft(cursor,
this);
128 ContextStyle::IndexStyle istyle,
129 StyleAttributes& style )
132 ContextStyle::IndexStyle u_istyle = context.convertIndexStyleUpper( istyle );
133 ContextStyle::IndexStyle l_istyle = context.convertIndexStyleLower( istyle );
134 double factor = style.sizeFactor();
136 numerator->calcSizes( context, i_tstyle, u_istyle, style );
137 denominator->calcSizes( context, i_tstyle, l_istyle, style );
139 luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
141 double linethickness = lineThickness( context, factor );
143 setWidth( TQMAX( numerator->getWidth(), denominator->getWidth() ) );
144 setHeight( numerator->getHeight() + denominator->getHeight() +
145 2*distY + linethickness );
146 setBaseline( tqRound( numerator->getHeight() + distY + .5*linethickness
147 + context.axisHeight( tstyle, factor ) ) );
149 numerator->setX( ( getWidth() - numerator->getWidth() ) / 2 );
150 denominator->setX( ( getWidth() - denominator->getWidth() ) / 2 );
152 numerator->setY( 0 );
153 denominator->setY( getHeight() - denominator->getHeight() );
162void FractionElement::draw( TQPainter& painter,
const LuPixelRect& r,
165 ContextStyle::IndexStyle istyle,
166 StyleAttributes& style,
167 const LuPixelPoint& parentOrigin )
169 LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
173 numerator->draw( painter, r, context,
174 context.convertTextStyleFraction( tstyle ),
175 context.convertIndexStyleUpper( istyle ), style, myPos);
177 denominator->draw( painter, r, context,
178 context.convertTextStyleFraction( tstyle ),
179 context.convertIndexStyleLower( istyle ), style,
185 double factor = style.sizeFactor();
186 double linethickness = lineThickness( context, factor );
187 painter.setPen( TQPen( style.color(),
188 context.layoutUnitToPixelY( linethickness ) ) );
190 context.layoutUnitToPixelY( myPos.y() + axis( context, tstyle, factor ) ),
192 context.layoutUnitToPixelY( myPos.y() + axis( context, tstyle, factor ) ) );
200 denominator->dispatchFontCommand( cmd );
211 getParent()->moveLeft(cursor,
this);
214 bool linear = cursor->getLinearMovement();
215 if (from == getParent()) {
217 denominator->moveLeft(cursor,
this);
220 numerator->moveLeft(cursor,
this);
223 else if (from == denominator) {
224 numerator->moveLeft(cursor,
this);
227 getParent()->moveLeft(cursor,
this);
241 getParent()->moveRight(cursor,
this);
244 bool linear = cursor->getLinearMovement();
245 if (from == getParent()) {
246 numerator->moveRight(cursor,
this);
248 else if (from == numerator) {
250 denominator->moveRight(cursor,
this);
253 getParent()->moveRight(cursor,
this);
257 getParent()->moveRight(cursor,
this);
271 getParent()->moveUp(cursor,
this);
274 if (from == getParent()) {
275 denominator->moveRight(cursor,
this);
277 else if (from == denominator) {
278 numerator->moveRight(cursor,
this);
281 getParent()->moveUp(cursor,
this);
295 getParent()->moveDown(cursor,
this);
298 if (from == getParent()) {
299 numerator->moveRight(cursor,
this);
301 else if (from == numerator) {
302 denominator->moveRight(cursor,
this);
305 getParent()->moveDown(cursor,
this);
315 TQPtrList<BasicElement>& newChildren,
318 if (cursor->getPos() == denominatorPos) {
319 denominator =
static_cast<SequenceElement*
>(newChildren.take(0));
320 denominator->setParent(
this);
322 if (direction == beforeCursor) {
323 denominator->moveLeft(cursor,
this);
326 denominator->moveRight(cursor,
this);
344 TQPtrList<BasicElement>& removedChildren,
347 switch (cursor->getPos()) {
349 getParent()->selectChild(cursor,
this);
350 getParent()->remove(cursor, removedChildren, direction);
353 removedChildren.append(denominator);
356 cursor->
setTo(
this, denominatorPos);
370 return denominator == 0;
398 if (child == numerator) {
399 cursor->
setTo(
this, numeratorPos);
401 else if (child == denominator) {
402 cursor->
setTo(
this, denominatorPos);
414 TQDomDocument doc = element.ownerDocument();
415 if (!withLine()) element.setAttribute(
"NOLINE", 1);
417 TQDomElement num = doc.createElement(
"NUMERATOR");
418 num.appendChild(numerator->getElementDom(doc));
419 element.appendChild(num);
421 TQDomElement den = doc.createElement(
"DENOMINATOR");
422 den.appendChild(denominator->getElementDom(doc));
423 element.appendChild(den);
435 TQString lineStr = element.attribute(
"NOLINE");
436 if(!lineStr.isNull()) {
437 m_lineThicknessType = RelativeSize;
438 m_lineThickness = lineStr.toInt();
452 TQString linethicknessStr = element.attribute(
"linethickness" ).lower();
453 if ( ! linethicknessStr.isNull() ) {
454 if ( linethicknessStr ==
"thin" ) {
455 m_lineThicknessType = RelativeSize;
456 m_lineThickness = 0.5;
458 else if ( linethicknessStr ==
"medium" ) {
459 m_lineThicknessType = RelativeSize;
460 m_lineThickness = 1.0;
462 else if ( linethicknessStr ==
"thick" ) {
463 m_lineThicknessType = RelativeSize;
464 m_lineThickness = 2.0;
467 m_lineThickness =
getSize( linethicknessStr, &m_lineThicknessType );
470 TQString numalignStr = element.attribute(
"numalign" ).lower();
471 if ( ! numalignStr.isNull() ) {
472 if ( numalignStr ==
"left" ) {
473 m_numAlign = LeftHorizontalAlign;
475 else if ( numalignStr ==
"center" ) {
476 m_numAlign = CenterHorizontalAlign;
478 else if ( numalignStr ==
"right" ) {
479 m_numAlign = RightHorizontalAlign;
482 TQString denomalignStr = element.attribute(
"denomalign" ).lower();
483 if ( ! denomalignStr.isNull() ) {
484 if ( denomalignStr ==
"left" ) {
485 m_denomAlign = LeftHorizontalAlign;
487 else if ( denomalignStr ==
"center" ) {
488 m_denomAlign = CenterHorizontalAlign;
490 else if ( denomalignStr ==
"right" ) {
491 m_denomAlign = RightHorizontalAlign;
494 TQString bevelledStr = element.attribute(
"bevelled" ).lower();
495 if ( ! bevelledStr.isNull() ) {
496 m_customBevelled =
true;
497 if ( bevelledStr ==
"true" ) {
519 if ( !
buildChild( numerator, node,
"NUMERATOR" ) ) {
520 kdWarning( DEBUGID ) <<
"Empty numerator in FractionElement." << endl;
523 node = node.nextSibling();
525 if ( !
buildChild( denominator, node,
"DENOMINATOR" ) ) {
526 kdWarning( DEBUGID ) <<
"Empty denominator in FractionElement." << endl;
529 node = node.nextSibling();
545 int numeratorNumber = numerator->buildMathMLChild( node );
546 if ( numeratorNumber == -1 ) {
547 kdWarning( DEBUGID ) <<
"Empty numerator in FractionElement." << endl;
550 for (
int i = 0; i < numeratorNumber; i++ ) {
551 if ( node.isNull() ) {
554 node = node.nextSibling();
557 if ( denominator->buildMathMLChild( node ) == -1 ) {
558 kdWarning( DEBUGID ) <<
"Empty denominator in FractionElement." << endl;
568 return "\\frac{" + numerator->toLatex() +
"}{" + denominator->toLatex() +
"}";
571 return "{" + numerator->toLatex() +
"\\atop " + denominator->toLatex() +
"}";
575TQString FractionElement::formulaString()
577 return "(" + numerator->formulaString() +
")/(" + denominator->formulaString() +
")";
580void FractionElement::writeMathMLAttributes( TQDomElement& element )
const
582 switch ( m_lineThicknessType ) {
584 element.setAttribute(
"linethickness", TQString(
"%1pt" ).arg( m_lineThickness ) );
587 element.setAttribute(
"linethickness", TQString(
"%1%" ).arg( m_lineThickness * 100.0 ) );
590 element.setAttribute(
"linethickness", TQString(
"%1px" ).arg( m_lineThickness ) );
596 switch ( m_numAlign ) {
597 case LeftHorizontalAlign:
598 element.setAttribute(
"numalign",
"left" );
600 case CenterHorizontalAlign:
601 element.setAttribute(
"numalign",
"center" );
603 case RightHorizontalAlign:
604 element.setAttribute(
"numalign",
"right" );
610 switch ( m_denomAlign ) {
611 case LeftHorizontalAlign:
612 element.setAttribute(
"denomalign",
"left" );
614 case CenterHorizontalAlign:
615 element.setAttribute(
"denomalign",
"center" );
617 case RightHorizontalAlign:
618 element.setAttribute(
"denomalign",
"right" );
624 if ( m_customBevelled ) {
625 element.setAttribute(
"bevelled", m_bevelled ?
"true" :
"false" );
629void FractionElement::writeMathMLContent( TQDomDocument& doc,
630 TQDomElement& element,
631 bool oasisFormat )
const
633 numerator->writeMathML( doc, element, oasisFormat );
634 denominator->writeMathML( doc, element, oasisFormat );
637double FractionElement::lineThickness(
const ContextStyle& context,
double factor )
640 switch ( m_lineThicknessType ) {
645 linethickness *= m_lineThickness;
648 linethickness = m_lineThickness;
653 return linethickness;
657KFORMULA_NAMESPACE_END
Basis of every formula element.
virtual FormulaElement * formula()
Provide fast access to the rootElement for each child.
bool buildChild(SequenceElement *child, TQDomNode node, TQString name)
Returns if the SequenceElement could be constructed from the nodes first child.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
virtual int readContentFromMathMLDom(TQDomNode &node)
Reads our content from the MathML node.
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
virtual BasicElement * goToPos(FormulaCursor *, bool &handled, const LuPixelPoint &point, const LuPixelPoint &parentOrigin)
Sets the cursor and returns the element the point is in.
double getSize(const TQString &str, SizeType *st)
Utility function that sets the size type and returns the size value from a MathML attribute string wi...
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
Contains all the style information for the formela.
TextStyle
Textstyles like in TeX.
luPixel getLineWidth(double factor) const
All simple lines like the one that makes up a fraction.
Base for all font commands that act on the current selection.
virtual void remove(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Removes all selected children and returns them.
virtual void calcSizes(const ContextStyle &cstyle, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)
Calculates our width and height and our children's parentPosition.
virtual TQString toLatex()
virtual void dispatchFontCommand(FontCommand *cmd)
Draws the whole element including its children.
virtual int readContentFromMathMLDom(TQDomNode &node)
Reads our content from the MathML node.
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
virtual bool accept(ElementVisitor *visitor)
Visit this element.
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
virtual void insert(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Enters this element while moving to the left starting inside the element `from'.
virtual bool isSenseless()
Returns wether the element has no more useful children (except its main child) and should therefore b...
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
virtual BasicElement * goToPos(FormulaCursor *, bool &handled, const LuPixelPoint &point, const LuPixelPoint &parentOrigin)
Sets the cursor and returns the element the point is in.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
virtual void entered(SequenceElement *child)
The cursor has entered one of our child sequences.
virtual void selectChild(FormulaCursor *cursor, BasicElement *child)
Sets the cursor to select the child.
virtual SequenceElement * getMainChild()
Removes the child.
int ptToLayoutUnitPixX(double x_pt) const
The "document pt" -> "Layout Unit pixels" conversions, for convenience.
int layoutUnitToPixelX(int lupix) const
The "layout unit pixel" -> "[zoomed] view pixel" conversions.
The element that contains a number of children.
virtual void dispatchFontCommand(FontCommand *cmd)
Draws the whole element including its children.