27#include "elementvisitor.h"
28#include "formulacursor.h"
29#include "formulaelement.h"
30#include "kformulacommand.h"
31#include "rootelement.h"
32#include "sequenceelement.h"
34KFORMULA_NAMESPACE_BEGIN
38 typedef SequenceElement inherited;
41 RootSequenceElement( BasicElement* parent = 0 ) : SequenceElement( parent ) {}
42 virtual RootSequenceElement* clone() {
43 return new RootSequenceElement( *
this );
54 virtual KCommand* buildCommand( Container*, Request* );
58KCommand* RootSequenceElement::buildCommand(
Container* container, Request* request )
69 ( cursor->getPos() > 0 && cursor->getPos() <
countChildren() ) ) {
72 IndexRequest* ir =
static_cast<IndexRequest*
>( request );
73 if ( ir->index() == upperLeftPos ) {
74 RootElement* element =
static_cast<RootElement*
>( getParent() );
75 ElementIndexPtr index = element->getIndex();
76 if ( !index->hasIndex() ) {
77 KFCAddGenericIndex* command =
new KFCAddGenericIndex( container, index );
81 index->moveToIndex( cursor, afterCursor );
98 content =
new RootSequenceElement(
this );
102RootElement::~RootElement()
109RootElement::RootElement(
const RootElement& other )
112 content =
new RootSequenceElement( *
dynamic_cast<RootSequenceElement*
>( other.content ) );
115 index->setParent(
this );
125 return visitor->visit(
this );
131 if ( child == content ) {
141 const LuPixelPoint& point,
const LuPixelPoint& parentOrigin)
145 LuPixelPoint myPos(parentOrigin.x() + getX(),
146 parentOrigin.y() + getY());
148 e = content->
goToPos(cursor, handled, point, myPos);
153 e = index->
goToPos(cursor, handled, point, myPos);
160 luPixel dy = point.y() - myPos.y();
164 if (dy < index->getHeight()) {
165 index->moveLeft(cursor,
this);
183 ContextStyle::IndexStyle istyle,
184 StyleAttributes& style )
186 content->calcSizes( context, tstyle,
187 context.convertIndexStyleLower(istyle), style );
189 luPixel indexWidth = 0;
190 luPixel indexHeight = 0;
192 index->calcSizes( context,
193 context.convertTextStyleIndex(tstyle),
194 context.convertIndexStyleUpper(istyle),
196 indexWidth = index->getWidth();
197 indexHeight = index->getHeight();
200 double factor = style.sizeFactor();
201 luPixel distX = context.ptToPixelX( context.getThinSpace( tstyle, factor ) );
202 luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
203 luPixel unit = (content->getHeight() + distY)/ 3;
206 if (indexWidth > unit) {
208 rootOffset.setX( indexWidth - unit );
211 index->setX( ( unit - indexWidth )/2 );
214 if (indexHeight > unit) {
216 rootOffset.setY( indexHeight - unit );
219 index->setY( unit - indexHeight );
228 setWidth( content->getWidth() + unit+unit/3+ rootOffset.x() + distX/2 );
229 setHeight( content->getHeight() + distY*2 + rootOffset.y() );
231 content->setX( rootOffset.x() + unit+unit/3 );
232 content->setY( rootOffset.y() + distY );
233 setBaseline(content->getBaseline() + content->getY());
241void RootElement::draw( TQPainter& painter,
const LuPixelRect& r,
244 ContextStyle::IndexStyle istyle,
245 StyleAttributes& style,
246 const LuPixelPoint& parentOrigin )
248 LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
252 content->draw( painter, r, context, tstyle,
253 context.convertIndexStyleLower(istyle), style, myPos);
255 index->draw(painter, r, context,
256 context.convertTextStyleIndex(tstyle),
257 context.convertIndexStyleUpper(istyle), style, myPos);
260 luPixel x = myPos.x() + rootOffset.x();
261 luPixel y = myPos.y() + rootOffset.y();
263 double factor = style.sizeFactor();
264 luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
265 luPixel unit = (content->getHeight() + distY)/ 3;
267 painter.setPen( TQPen( style.color(),
270 context.layoutUnitToPixelY( y+unit+distY/3 ),
272 context.layoutUnitToPixelY( myPos.y()+getHeight() ) );
274 painter.setPen( TQPen( style.color(),
275 context.layoutUnitToPixelY( context.
getLineWidth( factor ) ) ) );
278 context.layoutUnitToPixelY( y+distY/3 ),
280 context.layoutUnitToPixelY( myPos.y()+getHeight() ) );
282 context.layoutUnitToPixelY( y+distY/3 ),
284 context.layoutUnitToPixelY( y+distY/3 ) );
286 context.layoutUnitToPixelY( y+unit+distY/2 ),
288 context.layoutUnitToPixelY( y+unit+unit/2 ) );
294 content->dispatchFontCommand( cmd );
296 index->dispatchFontCommand( cmd );
308 getParent()->moveLeft(cursor,
this);
311 bool linear = cursor->getLinearMovement();
312 if (from == getParent()) {
313 content->moveLeft(cursor,
this);
315 else if (from == content) {
316 if (linear && hasIndex()) {
317 index->moveLeft(cursor,
this);
320 getParent()->moveLeft(cursor,
this);
324 getParent()->moveLeft(cursor,
this);
337 getParent()->moveRight(cursor,
this);
340 bool linear = cursor->getLinearMovement();
341 if (from == getParent()) {
342 if (linear && hasIndex()) {
343 index->moveRight(cursor,
this);
346 content->moveRight(cursor,
this);
349 else if (from == index) {
350 content->moveRight(cursor,
this);
353 getParent()->moveRight(cursor,
this);
366 getParent()->moveUp(cursor,
this);
369 if (from == getParent()) {
370 content->moveRight(cursor,
this);
372 else if (from == content) {
374 index->moveRight(cursor,
this);
377 getParent()->moveUp(cursor,
this);
381 getParent()->moveUp(cursor,
this);
394 getParent()->moveDown(cursor,
this);
397 if (from == getParent()) {
399 index->moveRight(cursor,
this);
402 content->moveRight(cursor,
this);
405 else if (from == index) {
406 content->moveRight(cursor,
this);
409 getParent()->moveDown(cursor,
this);
418 TQPtrList<BasicElement>& newChildren,
421 if (cursor->getPos() == upperLeftPos) {
422 index =
static_cast<SequenceElement*
>(newChildren.take(0));
423 index->setParent(
this);
425 if (direction == beforeCursor) {
426 index->moveLeft(cursor,
this);
429 index->moveRight(cursor,
this);
443 TQPtrList<BasicElement>& removedChildren,
446 switch (cursor->getPos()) {
448 getParent()->selectChild(cursor,
this);
449 getParent()->remove(cursor, removedChildren, direction);
452 removedChildren.append(index);
455 cursor->
setTo(
this, upperLeftPos);
468 if (direction == beforeCursor) {
469 content->moveLeft(cursor,
this);
472 content->moveRight(cursor,
this);
501 if (child == content) {
502 cursor->
setTo(
this, contentPos);
504 else if (child == index) {
505 cursor->
setTo(
this, upperLeftPos);
513 if (direction == beforeCursor) {
514 index->moveLeft(cursor,
this);
517 index->moveRight(cursor,
this);
524 cursor->
setTo(
this, upperLeftPos);
535 TQDomDocument doc = element.ownerDocument();
537 TQDomElement con = doc.createElement(
"CONTENT");
538 con.appendChild(content->getElementDom(doc));
539 element.appendChild(con);
542 TQDomElement ind = doc.createElement(
"ROOTINDEX");
543 ind.appendChild(index->getElementDom(doc));
544 element.appendChild(ind);
568 if ( !
buildChild( content, node,
"CONTENT" ) ) {
569 kdWarning( DEBUGID ) <<
"Empty content in RootElement." << endl;
572 node = node.nextSibling();
574 if ( node.nodeName().upper() ==
"ROOTINDEX" ) {
575 if ( !
buildChild( index=
new SequenceElement(
this ), node,
"ROOTINDEX" ) ) {
580 else if ( node.nodeName().upper() ==
"INDEX" ) {
581 if ( !
buildChild( index=
new SequenceElement(
this ), node,
"INDEX" ) ) {
585 node = node.nextSibling();
597 if ( element.tagName().lower() ==
"mroot" )
618 if ( content->readContentFromMathMLDom( node ) == -1 ) {
619 kdWarning( DEBUGID ) <<
"Empty content in RootElement." << endl;
625 int contentNumber = content->buildMathMLChild( node );
626 if ( contentNumber == -1 ) {
627 kdWarning( DEBUGID ) <<
"Empty content in RootElement." << endl;
630 for (
int i = 0; i < contentNumber; i++ ) {
631 if ( node.isNull() ) {
634 node = node.nextSibling();
637 index =
new SequenceElement(
this );
638 if ( index->buildMathMLChild( node ) == -1 ) {
639 kdWarning( DEBUGID ) <<
"Empty index in RootElement." << endl;
653 root+=index->toLatex();
657 root+=content->toLatex();
663TQString RootElement::formulaString()
666 return "(" + content->formulaString() +
")**(1.0/(" + index->formulaString() +
"))";
668 return "sqrt(" + content->formulaString() +
")";
671void RootElement::writeMathMLContent( TQDomDocument& doc, TQDomElement& element,
bool oasisFormat )
const
673 content->writeMathML( doc, element, oasisFormat );
676 index->writeMathML( doc, element, oasisFormat );
680KFORMULA_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 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.
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
FormulaCursor * activeCursor()
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.
int layoutUnitToPixelX(int lupix) const
The "layout unit pixel" -> "[zoomed] view pixel" conversions.
virtual BasicElement * goToPos(FormulaCursor *, bool &handled, const LuPixelPoint &point, const LuPixelPoint &parentOrigin)
Sets the cursor and returns the element the point is in.
virtual void insert(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Enters this element while moving to the left starting inside the element `from'.
void moveToIndex(FormulaCursor *, Direction)
Moves the cursor away from the given child.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
virtual SequenceElement * getMainChild()
Removes the child.
virtual bool accept(ElementVisitor *visitor)
Visit this element.
virtual TQString toLatex()
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
virtual int readContentFromMathMLDom(TQDomNode &node)
Reads our content from the MathML node.
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
virtual void entered(SequenceElement *child)
The cursor has entered one of our child sequences.
virtual void dispatchFontCommand(FontCommand *cmd)
Draws the whole element including its children.
virtual void normalize(FormulaCursor *, Direction)
Moves the cursor to a normal place where new elements might be inserted.
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
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 void selectChild(FormulaCursor *cursor, BasicElement *child)
Sets the cursor to select the child.
virtual void remove(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Removes all selected children and returns them.
The element that contains a number of children.
uint countChildren() const
virtual KCommand * buildCommand(Container *, Request *)
This is called by the container to get a command depending on the current cursor position (this is ho...