summaryrefslogtreecommitdiffstats
path: root/lib/kformula/symbolelement.cc
blob: fb2eb69de20cc1036e22f17aba408a67bf0618b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
	              Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqpainter.h>

#include <kdebug.h>

#include "elementvisitor.h"
#include "formulacursor.h"
#include "formulaelement.h"
#include "kformulacommand.h"
#include "sequenceelement.h"
#include "symbolelement.h"

KFORMULA_NAMESPACE_BEGIN


class SymbolSequenceElement : public SequenceElement {
    typedef SequenceElement inherited;
public:

    SymbolSequenceElement( BasicElement* tqparent = 0 ) : SequenceElement( tqparent ) {}

    /**
     * This is called by the container to get a command depending on
     * the current cursor position (this is how the element gets chosen)
     * and the request.
     *
     * @returns the command that performs the requested action with
     * the containers active cursor.
     */
    virtual KCommand* buildCommand( Container*, Request* );
};


KCommand* SymbolSequenceElement::buildCommand( Container* container, Request* request )
{
    FormulaCursor* cursor = container->activeCursor();
    if ( cursor->isReadOnly() ) {
        return 0;
    }

    switch ( *request ) {
    case req_addIndex: {
        FormulaCursor* cursor = container->activeCursor();
        if ( cursor->isSelection() ||
             ( cursor->getPos() > 0 && cursor->getPos() < countChildren() ) ) {
            break;
        }
        IndexRequest* ir = static_cast<IndexRequest*>( request );
        if ( ( ir->index() == upperMiddlePos ) || ( ir->index() == lowerMiddlePos ) ) {
            SymbolElement* element = static_cast<SymbolElement*>( getParent() );
            ElementIndexPtr index = element->getIndex( ir->index() );
            if ( !index->hasIndex() ) {
                KFCAddGenericIndex* command = new KFCAddGenericIndex( container, index );
                return command;
            }
            else {
                index->moveToIndex( cursor, afterCursor );
                cursor->setSelection( false );
                formula()->cursorHasMoved( cursor );
                return 0;
            }
        }
    }
    default:
        break;
    }
    return inherited::buildCommand( container, request );
}


SymbolElement::SymbolElement(SymbolType type, BasicElement* tqparent)
    : BasicElement(tqparent), symbol( 0 ), symbolType( type )
{
    content = new SymbolSequenceElement( this );
    upper = 0;
    lower = 0;
}

SymbolElement::~SymbolElement()
{
    delete lower;
    delete upper;
    delete content;
    delete symbol;
}


SymbolElement::SymbolElement( const SymbolElement& other )
    : BasicElement( other ), symbol( 0 ), symbolType( other.symbolType )
{
    content = new SymbolSequenceElement( *dynamic_cast<SymbolSequenceElement*>( other.content ) );
    content->setParent( this );

    if ( other.upper ) {
        upper = new SequenceElement( *( other.upper ) );
        upper->setParent( this );
    }
    else {
        upper = 0;
    }
    if ( other.lower ) {
        lower = new SequenceElement( *( other.lower ) );
        lower->setParent( this );
    }
    else {
        lower = 0;
    }
}


bool SymbolElement::accept( ElementVisitor* visitor )
{
    return visitor->visit( this );
}


BasicElement* SymbolElement::goToPos( FormulaCursor* cursor, bool& handled,
                                      const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
{
    BasicElement* e = BasicElement::goToPos(cursor, handled, point, parentOrigin);
    if (e != 0) {
        LuPixelPoint myPos(parentOrigin.x() + getX(),
                           parentOrigin.y() + getY());

        e = content->goToPos(cursor, handled, point, myPos);
        if (e != 0) {
            return e;
        }
        if (hasLower()) {
            e = lower->goToPos(cursor, handled, point, myPos);
            if (e != 0) {
                return e;
            }
        }
        if (hasUpper()) {
            e = upper->goToPos(cursor, handled, point, myPos);
            if (e != 0) {
                return e;
            }
        }

        // the positions after the indexes.
        luPixel dx = point.x() - myPos.x();
        luPixel dy = point.y() - myPos.y();
        if (dy < symbol->getY()) {
            if (hasUpper() && (dx > upper->getX())) {
                upper->moveLeft(cursor, this);
                handled = true;
                return upper;
            }
        }
        else if (dy > symbol->getY()+symbol->getHeight()) {
            if (hasLower() && (dx > lower->getX())) {
                lower->moveLeft(cursor, this);
                handled = true;
                return lower;
            }
        }

        // Have the cursor jump behind the integral.
        if ( ( dx < symbol->getX()+symbol->getWidth() ) &&
             ( dx > symbol->getX()+symbol->getWidth()/2 ) ) {
            content->moveRight( cursor, this );
            handled = true;
            return content;
        }

        return this;
    }
    return 0;
}


/**
 * Calculates our width and height and
 * our tqchildren's parentPosition.
 */
void SymbolElement::calcSizes( const ContextStyle& context,
                               ContextStyle::TextStyle tstyle,
                               ContextStyle::IndexStyle istyle,
                               StyleAttributes& style )
{
    double factor = style.sizeFactor();
    luPt mySize = context.getAdjustedSize( tstyle, factor );
    luPixel distX = context.ptToPixelX( context.getThinSpace( tstyle, factor ) );
    luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );

    //if ( symbol == 0 ) {
    delete symbol;
    symbol = context.fontStyle().createArtwork( symbolType );
    //}

    symbol->calcSizes(context, tstyle, factor, mySize);
    content->calcSizes( context, tstyle, istyle, style );

    //symbol->scale(((double)parentSize)/symbol->getHeight()*2);

    luPixel upperWidth = 0;
    luPixel upperHeight = 0;
    if (hasUpper()) {
        upper->calcSizes( context, context.convertTextStyleIndex( tstyle ),
                          context.convertIndexStyleUpper( istyle ), style );
        upperWidth = upper->getWidth();
        upperHeight = upper->getHeight() + distY;
    }

    luPixel lowerWidth = 0;
    luPixel lowerHeight = 0;
    if (hasLower()) {
        lower->calcSizes( context, context.convertTextStyleIndex( tstyle ),
                          context.convertIndexStyleLower( istyle ), style );
        lowerWidth = lower->getWidth();
        lowerHeight = lower->getHeight() + distY;
    }

    // widths
    luPixel xOffset = TQMAX(symbol->getWidth(), TQMAX(upperWidth, lowerWidth));
    if (context.getCenterSymbol()) {
        symbol->setX((xOffset - symbol->getWidth()) / 2);
    }
    else {
        symbol->setX(xOffset - symbol->getWidth());
    }
    // ???
    content->setX(xOffset +
                  static_cast<luPixel>( symbol->slant()*symbol->getHeight()/2 ) +
                  distX/2);

    setWidth(TQMAX(content->getX() + content->getWidth(),
                  TQMAX(upperWidth, lowerWidth)));

    // heights
    //int toMidline = TQMAX(content->getHeight() / 2,
    luPixel toMidline = TQMAX(content->axis( context, tstyle, factor ),
                             upperHeight + symbol->getHeight()/2);
    //int fromMidline = TQMAX(content->getHeight() / 2,
    luPixel fromMidline = TQMAX(content->getHeight() - content->axis( context, tstyle, factor ),
                               lowerHeight + symbol->getHeight()/2);
    setHeight(toMidline + fromMidline);
    //setMidline(toMidline);

    symbol->setY(toMidline - symbol->getHeight()/2);
    //content->setY(toMidline - content->getHeight()/2);
    content->setY(toMidline - content->axis( context, tstyle, factor ));

    if (hasUpper()) {
        luPixel slant =
            static_cast<luPixel>( symbol->slant()*( symbol->getHeight()+distY ) );
        if (context.getCenterSymbol()) {
            upper->setX((xOffset - upperWidth) / 2 + slant );
        }
        else {
            if (upperWidth < symbol->getWidth()) {
                upper->setX(symbol->getX() +
                            (symbol->getWidth() - upperWidth) / 2 + slant );
            }
            else {
                upper->setX(xOffset - upperWidth);
            }
        }
        upper->setY(toMidline - upperHeight - symbol->getHeight()/2);
    }
    if (hasLower()) {
        luPixel slant = static_cast<luPixel>( -symbol->slant()*distY );
        if (context.getCenterSymbol()) {
            lower->setX((xOffset - lowerWidth) / 2 + slant);
        }
        else {
            if (lowerWidth < symbol->getWidth()) {
                lower->setX(symbol->getX() +
                            (symbol->getWidth() - lowerWidth) / 2 + slant );
            }
            else {
                lower->setX(xOffset - lowerWidth);
            }
        }
        lower->setY(toMidline + symbol->getHeight()/2 + distY);
    }
    setBaseline(content->getBaseline() + content->getY());
}

/**
 * Draws the whole element including its tqchildren.
 * The `parentOrigin' is the point this element's tqparent starts.
 * We can use our parentPosition to get our own origin then.
 */
void SymbolElement::draw( TQPainter& painter, const LuPixelRect& r,
                          const ContextStyle& context,
                          ContextStyle::TextStyle tstyle,
                          ContextStyle::IndexStyle istyle,
                          StyleAttributes& style,
                          const LuPixelPoint& parentOrigin )
{
    LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
    //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
    //    return;

    luPt mySize = context.getAdjustedSize( tstyle, style.sizeFactor() );
    symbol->draw( painter, r, context, tstyle, style, mySize, myPos );
    content->draw( painter, r, context, tstyle, istyle, style, myPos );
    if ( hasUpper() ) {
        upper->draw( painter, r, context, context.convertTextStyleIndex( tstyle ),
                     context.convertIndexStyleUpper( istyle ), style, myPos );
    }
    if ( hasLower() ) {
        lower->draw( painter, r, context, context.convertTextStyleIndex( tstyle ),
                     context.convertIndexStyleLower( istyle ), style, myPos );
    }

    // Debug
#if 0
    painter.setBrush(TQt::NoBrush);
    painter.setPen(TQt::red);
//     painter.drawRect( context.layoutUnitToPixelX( myPos.x() ),
//                       context.layoutUnitToPixelY( myPos.y() ),
//                       context.layoutUnitToPixelX( getWidth() ),
//                       context.layoutUnitToPixelY( getHeight() ) );
    painter.drawRect( context.layoutUnitToPixelX( myPos.x()+symbol->getX() ),
                      context.layoutUnitToPixelY( myPos.y()+symbol->getY() ),
                      context.layoutUnitToPixelX( symbol->getWidth() ),
                      context.layoutUnitToPixelY( symbol->getHeight() ) );
    painter.setPen(TQt::green);
    painter.drawLine( context.layoutUnitToPixelX( myPos.x() ),
                      context.layoutUnitToPixelY( myPos.y()+axis(context, tstyle) ),
                      context.layoutUnitToPixelX( myPos.x()+getWidth() ),
                      context.layoutUnitToPixelY( myPos.y()+axis(context, tstyle) ) );
#endif
}


void SymbolElement::dispatchFontCommand( FontCommand* cmd )
{
    content->dispatchFontCommand( cmd );
    if ( hasUpper() ) {
        upper->dispatchFontCommand( cmd );
    }
    if ( hasLower() ) {
        lower->dispatchFontCommand( cmd );
    }
}

// navigation
//
// The elements are responsible to handle cursor movement themselves.
// To do this they need to know the direction the cursor moves and
// the element it comes from.
//
// The cursor might be in normal or in selection mode.

/**
 * Enters this element while moving to the left starting inside
 * the element `from'. Searches for a cursor position inside
 * this element or to the left of it.
 */
void SymbolElement::moveLeft(FormulaCursor* cursor, BasicElement* from)
{
    if (cursor->isSelectionMode()) {
        getParent()->moveLeft(cursor, this);
    }
    else {
        bool linear = cursor->getLinearMovement();
        if (from == getParent()) {
            content->moveLeft(cursor, this);
        }
        else if (from == content) {
            if (linear && hasLower()) {
                lower->moveLeft(cursor, this);
            }
            else if (linear && hasUpper()) {
                upper->moveLeft(cursor, this);
            }
            else {
                getParent()->moveLeft(cursor, this);
            }
        }
        else if (from == lower) {
            if (linear && hasUpper()) {
                upper->moveLeft(cursor, this);
            }
            else {
                getParent()->moveLeft(cursor, this);
            }
        }
        else if (from == upper) {
            getParent()->moveLeft(cursor, this);
        }
    }
}

/**
 * Enters this element while moving to the right starting inside
 * the element `from'. Searches for a cursor position inside
 * this element or to the right of it.
 */
void SymbolElement::moveRight(FormulaCursor* cursor, BasicElement* from)
{
    if (cursor->isSelectionMode()) {
        getParent()->moveRight(cursor, this);
    }
    else {
        bool linear = cursor->getLinearMovement();
        if (from == getParent()) {
            if (linear && hasUpper()) {
                upper->moveRight(cursor, this);
            }
            else if (linear && hasLower()) {
                lower->moveRight(cursor, this);
            }
            else {
                content->moveRight(cursor, this);
            }
        }
        else if (from == upper) {
            if (linear && hasLower()) {
                lower->moveRight(cursor, this);
            }
            else {
                content->moveRight(cursor, this);
            }
        }
        else if (from == lower) {
            content->moveRight(cursor, this);
        }
        else if (from == content) {
            getParent()->moveRight(cursor, this);
        }
    }
}

/**
 * Enters this element while moving up starting inside
 * the element `from'. Searches for a cursor position inside
 * this element or above it.
 */
void SymbolElement::moveUp(FormulaCursor* cursor, BasicElement* from)
{
    if (cursor->isSelectionMode()) {
        getParent()->moveUp(cursor, this);
    }
    else {
        if (from == content) {
            if (hasUpper()) {
                upper->moveLeft(cursor, this);
            }
            else {
                getParent()->moveUp(cursor, this);
            }
        }
        else if (from == upper) {
            getParent()->moveUp(cursor, this);
        }
        else if ((from == getParent()) || (from == lower)) {
            content->moveRight(cursor, this);
        }
    }
}

/**
 * Enters this element while moving down starting inside
 * the element `from'. Searches for a cursor position inside
 * this element or below it.
 */
void SymbolElement::moveDown(FormulaCursor* cursor, BasicElement* from)
{
    if (cursor->isSelectionMode()) {
        getParent()->moveDown(cursor, this);
    }
    else {
        if (from == content) {
            if (hasLower()) {
                lower->moveLeft(cursor, this);
            }
            else {
                getParent()->moveDown(cursor, this);
            }
        }
        else if (from == lower) {
            getParent()->moveDown(cursor, this);
        }
        else if ((from == getParent()) || (from == upper)) {
            content->moveRight(cursor, this);
        }
    }
}

// tqchildren

// main child
//
// If an element has tqchildren one has to become the main one.

// void SymbolElement::setMainChild(SequenceElement* child)
// {
//     formula()->elementRemoval(content);
//     content = child;
//     content->setParent(this);
//     formula()->changed();
// }


/**
 * Inserts all new tqchildren at the cursor position. Places the
 * cursor according to the direction.
 *
 * You only can insert one index at a time. So the list must contain
 * exactly on SequenceElement. And the index you want to insert
 * must not exist already.
 *
 * The list will be emptied but stays the property of the caller.
 */
void SymbolElement::insert(FormulaCursor* cursor,
                           TQPtrList<BasicElement>& newChildren,
                           Direction direction)
{
    SequenceElement* index = static_cast<SequenceElement*>(newChildren.take(0));
    index->setParent(this);

    switch (cursor->getPos()) {
    case upperMiddlePos:
        upper = index;
        break;
    case lowerMiddlePos:
        lower = index;
        break;
    default:
        // this is an error!
        return;
    }

    if (direction == beforeCursor) {
        index->moveLeft(cursor, this);
    }
    else {
        index->moveRight(cursor, this);
    }
    cursor->setSelection(false);
    formula()->changed();
}

/**
 * Removes all selected tqchildren and returns them. Places the
 * cursor to where the tqchildren have been.
 *
 * The cursor has to be inside one of our indexes which is supposed
 * to be empty. The index will be removed and the cursor will
 * be placed to the removed index so it can be inserted again.
 * This methode is called by SequenceElement::remove only.
 *
 * The ownership of the list is passed to the caller.
 */
void SymbolElement::remove(FormulaCursor* cursor,
                           TQPtrList<BasicElement>& removedChildren,
                           Direction direction)
{
    int pos = cursor->getPos();
    switch (pos) {
    case upperMiddlePos:
        removedChildren.append(upper);
        formula()->elementRemoval(upper);
        upper = 0;
        setToUpper(cursor);
        break;
    case lowerMiddlePos:
        removedChildren.append(lower);
        formula()->elementRemoval(lower);
        lower = 0;
        setToLower(cursor);
        break;
    case contentPos: {
        BasicElement* tqparent = getParent();
        tqparent->selectChild(cursor, this);
        tqparent->remove(cursor, removedChildren, direction);
        break;
    }
    }
    formula()->changed();
}

/**
 * Moves the cursor to a normal place where new elements
 * might be inserted.
 */
void SymbolElement::normalize(FormulaCursor* cursor, Direction direction)
{
    if (direction == beforeCursor) {
        content->moveLeft(cursor, this);
    }
    else {
        content->moveRight(cursor, this);
    }
}

/**
 * Returns the child at the cursor.
 */
BasicElement* SymbolElement::getChild(FormulaCursor* cursor, Direction)
{
    int pos = cursor->getPos();
    switch (pos) {
    case contentPos:
        return content;
    case upperMiddlePos:
        return upper;
    case lowerMiddlePos:
        return lower;
    }
    return 0;
}

/**
 * Sets the cursor to select the child. The mark is placed before,
 * the position behind it.
 */
void SymbolElement::selectChild(FormulaCursor* cursor, BasicElement* child)
{
    if (child == content) {
        setToContent(cursor);
    }
    else if (child == upper) {
        setToUpper(cursor);
    }
    else if (child == lower) {
        setToLower(cursor);
    }
}

void SymbolElement::setToUpper(FormulaCursor* cursor)
{
    cursor->setTo(this, upperMiddlePos);
}

void SymbolElement::setToLower(FormulaCursor* cursor)
{
    cursor->setTo(this, lowerMiddlePos);
}

/**
 * Sets the cursor to point to the place where the content is.
 * There always is a content so this is not a useful place.
 * No insertion or removal will succeed as long as the cursor is
 * there.
 */
void SymbolElement::setToContent(FormulaCursor* cursor)
{
    cursor->setTo(this, contentPos);
}


void SymbolElement::moveToUpper(FormulaCursor* cursor, Direction direction)
{
    if (hasUpper()) {
        if (direction == beforeCursor) {
            upper->moveLeft(cursor, this);
        }
        else {
            upper->moveRight(cursor, this);
        }
    }
}

void SymbolElement::moveToLower(FormulaCursor* cursor, Direction direction)
{
    if (hasLower()) {
        if (direction == beforeCursor) {
            lower->moveLeft(cursor, this);
        }
        else {
            lower->moveRight(cursor, this);
        }
    }
}


ElementIndexPtr SymbolElement::getIndex( int position )
{
    switch ( position ) {
	case lowerMiddlePos:
	    return getLowerIndex();
	case upperMiddlePos:
	    return getUpperIndex();
    }
    return getUpperIndex();
}


/**
 * Appends our attributes to the dom element.
 */
void SymbolElement::writeDom(TQDomElement element)
{
    BasicElement::writeDom(element);

    element.setAttribute("TYPE", symbolType);

    TQDomDocument doc = element.ownerDocument();

    TQDomElement con = doc.createElement("CONTENT");
    con.appendChild(content->getElementDom(doc));
    element.appendChild(con);

    if(hasLower()) {
        TQDomElement ind = doc.createElement("LOWER");
        ind.appendChild(lower->getElementDom(doc));
        element.appendChild(ind);
    }
    if(hasUpper()) {
        TQDomElement ind = doc.createElement("UPPER");
        ind.appendChild(upper->getElementDom(doc));
        element.appendChild(ind);
    }
}

/**
 * Reads our attributes from the element.
 * Returns false if it failed.
 */
bool SymbolElement::readAttributesFromDom(TQDomElement element)
{
    if (!BasicElement::readAttributesFromDom(element)) {
        return false;
    }

    TQString typeStr = element.attribute("TYPE");
    if(!typeStr.isNull()) {
        symbolType = static_cast<SymbolType>(typeStr.toInt());
    }

    return true;
}

/**
 * Reads our content from the node. Sets the node to the next node
 * that needs to be read.
 * Returns false if it failed.
 */
bool SymbolElement::readContentFromDom(TQDomNode& node)
{
    if (!BasicElement::readContentFromDom(node)) {
        return false;
    }

    if ( !buildChild( content, node, "CONTENT" ) ) {
        kdWarning( DEBUGID ) << "Empty content in SymbolElement." << endl;
        return false;
    }
    node = node.nextSibling();

    bool lowerRead = false;
    bool upperRead = false;

    while (!node.isNull() && !(upperRead && lowerRead)) {

        if (!lowerRead && (node.nodeName().upper() == "LOWER")) {
            lowerRead = buildChild( lower=new SequenceElement( this ), node, "LOWER" );
            if ( !lowerRead ) return false;
        }

        if (!upperRead && (node.nodeName().upper() == "UPPER")) {
            upperRead = buildChild( upper=new SequenceElement( this ), node, "UPPER" );
            if ( !upperRead ) return false;
        }

        node = node.nextSibling();
    }
    return true;
}

TQString SymbolElement::toLatex()
{
    TQString sym;

    switch(symbolType) {

	case 1001:
	 sym="\\int";
	break;
	case 1002:
	 sym="\\sum";
	break;
	case 1003:
	 sym="\\prod";
	break;

	default:
	 sym=" ";

    }


    if(hasLower()) {
        sym+="_{";
	sym+=lower->toLatex();
	sym+="}";
    }

    if(hasUpper()) {
        sym+="^{";
	sym+=upper->toLatex();
	sym+="} ";
    }

    sym += " ";

    sym+=content->toLatex();


    return sym;
}

TQString SymbolElement::formulaString()
{
    TQString sym;
    switch ( symbolType ) {
    case 1001:
        sym="int(";
	break;
    case 1002:
        sym="sum(";
	break;
    case 1003:
        sym="prod(";
	break;
    default:
        sym="(";
    }
    sym += content->formulaString();
    if ( hasLower() ) {
        sym += ", " + lower->formulaString();
    }
    if ( hasUpper() ) {
        sym += ", " + upper->formulaString();
    }
    return sym + ")";
}

void SymbolElement::writeMathML( TQDomDocument& doc, TQDomNode& tqparent, bool oasisFormat ) const
{
    TQDomElement de = doc.createElement( oasisFormat ? "math:mrow" : "mrow" );
    TQDomElement mo = doc.createElement( oasisFormat ? "math:mo" : "mo" );

    TQString value;

    switch( symbolType )
    {
    case EmptyBracket: break;
    case LeftLineBracket: case RightLineBracket:
        mo.appendChild( doc.createTextNode( "|" ) ); break;
    case Integral:
        mo.appendChild( doc.createEntityReference( "int" ) ); break;
    case Sum:
        mo.appendChild( doc.createEntityReference( "sum" ) ); break;
    case Product:
        mo.appendChild( doc.createEntityReference( "prod" ) ); break;
    default:
        mo.appendChild( doc.createTextNode( TQChar( symbolType ) ) );
    }

    TQDomElement between;
    if ( hasUpper() && hasLower() )
    {
        between = doc.createElement( oasisFormat ? "math:msubsup" : "msubsup" );
        between.appendChild( mo );
        lower->writeMathML( doc, between, oasisFormat );
        upper->writeMathML( doc, between, oasisFormat );
    }
    else if ( hasUpper() )
    {
        between = doc.createElement( oasisFormat ? "math:msup" : "msup" );
        between.appendChild( mo );
        upper->writeMathML( doc, between, oasisFormat );
    }
    else if ( hasLower() )
    {
        between = doc.createElement( oasisFormat ? "math:msub" : "msub" );
        between.appendChild( mo );
        lower->writeMathML( doc, between, oasisFormat );
    }
    else
        between = mo;

    de.appendChild( between );
    content->writeMathML( doc, de, oasisFormat );

    tqparent.appendChild( de );
}

KFORMULA_NAMESPACE_END