summaryrefslogtreecommitdiffstats
path: root/kugar/lib/mreportengine.cpp
blob: 77f402ed79ce47cdf2acdca2895a5fe6f20c9861 (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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
/***************************************************************************
            mreportengine.cpp  -  Kugar report engine
            -------------------
  begin     : Sun Aug 15 1999
  copyright : (C) 1999 by Mutiny Bay Software
  email     : info@mutinybaysoftware.com
  copyright : (C) 2002 Alexander Dymo
  email     : cloudtemple@mksat.net
***************************************************************************/

#include <tqfile.h>
#include <tqprinter.h>

#include "mreportengine.h"
#include "mutil.h"

#include <kdebug.h>

#include <math.h>

namespace Kugar
{

/** Constructor */
MReportEngine::MReportEngine() : TQObject()
{
    // Set page params
    m_pageCollection = 0;
    m_needRegeneration = true;
    m_refCount = 1;
    pageSize = MReportEngine::Letter;
    pageOrientation = MReportEngine::Portrait;
    topMargin = 0;
    bottomMargin = 0;
    leftMargin = 0;
    rightMargin = 0;
    heightOfDetails = 0;
    widthDelta = 1;
    heightDelta = 1;

    // Set cancel flag
    cancelRender = false;

    // Set the grand total list to AutoDelete
    grandTotal.setAutoDelete( true );
    // Set the details lists to AutoDelete
    dHeaders.setAutoDelete( true );
    details.setAutoDelete( true );
    dFooters.setAutoDelete( true );

    rHeader.setPrintFrequency( MReportSection::FirstPage );
    pHeader.setPrintFrequency( MReportSection::EveryPage );
    pFooter.setPrintFrequency( MReportSection::EveryPage );
    rFooter.setPrintFrequency( MReportSection::LastPage );

    // Set the default page metrics
    TQSize ps = getPageMetrics( pageSize, pageOrientation );
    pageWidth = ps.width();
    pageHeight = ps.height();
}

/** Copy constructor */
MReportEngine::MReportEngine( const MReportEngine& mReportEngine )   /*: TQObject((TQObject &) mReportEngine)*/
{
    copy( &mReportEngine );
}

/** Assignment operator */
MReportEngine MReportEngine::operator=( const MReportEngine& mReportEngine )
{
    if ( &mReportEngine == this )
        return * this;

    // Copy the derived class's data
    copy( &mReportEngine );

    // Copy the base class's data
    //((TQObject &) *this) = mReportEngine;

    return *this;
}

/** Destructor */
MReportEngine::~MReportEngine()
{
    // Clear the grand total data
    grandTotal.clear();
    kdDebug( 31000 ) << k_funcinfo << endl;
}

void MReportEngine::addRef()
{
    m_refCount++;
}

void MReportEngine::removeRef()
{
    m_refCount--;
    if ( !m_refCount )
        deleteLater();
}

/** Clears report formatting */
void MReportEngine::clearFormatting()
{
    rHeader.clear();
    pHeader.clear();
    dHeaders.clear();
    details.clear();
    dFooters.clear();
    pFooter.clear();
    rFooter.clear();
}


//Set the report data from an existing TQDomDocument
bool MReportEngine::setReportData( const TQDomDocument &data )
{
    rd = data.cloneNode( true ).toDocument();
    initData();

    return true;
}


// Set the report's data from an inline string.  Return true if it was valid
// data.

bool MReportEngine::setReportData( const TQString &data )
{

    if ( !rd.setContent( data ) )
    {
        kdDebug() << "Unable to parse report data" << endl;
        return false;
    }
    initData();

    return true;
}


// Set the report's data from an i/o device.  Return true if it was valid data.

bool MReportEngine::setReportData( TQIODevice *dev )
{
    if ( !rd.setContent( dev ) )
    {
        kdDebug() << "Unable to parse report data" << endl;
        return false;
    }
    initData();

    return true;
}


// Initialise after reading a report's data.

void MReportEngine::initData()
{
    m_needRegeneration = true;
    // Get the record set (we assume there is only one).

    for ( TQDomNode n = rd.firstChild(); !n.isNull(); n = n.nextSibling() )
        if ( n.nodeName() == "KugarData" )
        {
            // Get the records.

            records = n.childNodes();

            // See if there is a prefered template given.

            TQDomNamedNodeMap attr = n.attributes();
            TQDomNode tempattr = attr.namedItem( "Template" );
            TQString tempname = tempattr.nodeValue();

            if ( !tempname.isNull() )
                emit preferedTemplate( tempname );

            break;
        }
}


// Set the report's template from an inline string.  Return true if it was a
// valid template.

bool MReportEngine::setReportTemplate( const TQString &tpl )
{
    clearFormatting();

    if ( !rt.setContent( tpl ) )
    {
        kdDebug() << "Unable to parse report template" << endl;
        return false;
    }

    initTemplate();

    return true;
}


// Set the report's template from an i/o device.  Return true if it was a valid
// template.

bool MReportEngine::setReportTemplate( TQIODevice *dev )
{
    clearFormatting();

    if ( !rt.setContent( dev ) )
    {
        kdDebug() << "Unable to parse report template" << endl;
        return false;
    }

    initTemplate();

    return true;
}


/** Cancels rendering of a report */
void MReportEngine::slotCancelRendering()
{
    cancelRender = true;
}

/** Finds the detail header object, which is apropriate for the given level */
MReportSection *MReportEngine::findDetailHeader( int level )
{
    MReportSection * sec;
    for ( sec = dHeaders.first(); sec; sec = dHeaders.next() )
    {
        if ( sec->getLevel() == level )
            return sec;
    }
    return NULL;
}

/** Finds the detail object, which is apropriate for the given level */
MReportDetail *MReportEngine::findDetail( int level )
{
    MReportDetail * sec;
    for ( sec = details.first(); sec; sec = details.next() )
    {
        if ( sec->getLevel() == level )
            return sec;
    }
    return NULL;
}

/** Finds the detail footer object, which is apropriate for the given level */
MReportSection *MReportEngine::findDetailFooter( int level )
{
    MReportSection * sec;
    for ( sec = dFooters.first(); sec; sec = dFooters.next() )
    {
        if ( sec->getLevel() == level )
            return sec;
    }
    return NULL;
}

/** Renders the report as a page collection - the caller
  * is responsible for de-allocating the returned
  * collection
  */
MPageCollection* MReportEngine::renderReport()
{
    if ( !m_needRegeneration )
        return m_pageCollection;
    if ( m_pageCollection )
    {
        m_pageCollection->deleteLater();
        m_pageCollection = 0;
    }

    unsigned int j;
    unsigned int i;

    // Set cancel flag
    cancelRender = false;

    // Create the page collection
    MPageCollection* pages = new MPageCollection( this );

    // Initialize the basic page data
    currHeight = pageHeight - ( topMargin + bottomMargin + pFooter.getHeight() );
    currPage = 0;
    currDate = TQDate::currentDate();

    // Initialise global report variables
    unsigned int rowCount = records.length();
    for ( j = 0; j < rowCount; j++ )
    {
        TQDomNode record = records.item( j );
        if ( ( record.nodeType() == TQDomNode::ElementNode )
                && ( record.nodeName() == "Var" ) )
        {
            // Process the variable ...
            TQDomNamedNodeMap fields = record.attributes();
            for ( int k = 0; k < fields.count(); ++k )
            {
                rHeader.setFieldData( fields.item( k ).nodeName(), fields.item( k ).nodeValue() );
                rFooter.setFieldData( fields.item( k ).nodeName(), fields.item( k ).nodeValue() );
                pHeader.setFieldData( fields.item( k ).nodeName(), fields.item( k ).nodeValue() );
                pFooter.setFieldData( fields.item( k ).nodeName(), fields.item( k ).nodeValue() );
            }
        }
    }

    // Initialize the grand total array
    grandTotal.clear();
    for ( int i = 0; i < rFooter.getCalcFieldCount(); i++ )
        grandTotal.append( new TQMemArray<double> );

    // Create the first page
    startPage( pages );

    // Draw the detail section by looping through all the records
    int chkRow = 0;
    int curDetailLevel = 0;
    int prevDetailLevel = -1;
    int curDetailHeader = -1;
    int curDetailFooter = -1;

    for ( j = 0; j < rowCount; j++ )
    {
        TQString detailValue;
        TQDomNode record = records.item( j );

        if ( ( record.nodeType() == TQDomNode::ElementNode )
                && ( record.nodeName() == "Row" ) )
        {
            // Update status event
            if ( ( chkRow = ( j / 2 ) % 20 ) == 0 )
                emit signalRenderStatus( j / 2 );

            // Check for cancel action
            if ( cancelRender )
            {
                p.end();
                delete pages;
                return 0;
            }

            // Process the record ...
            TQDomNamedNodeMap fields = record.attributes();

            // Find the detail object to process with rendering
            detailValue = fields.namedItem( "level" ).nodeValue();
            curDetailLevel = detailValue.toInt();

            // Reset the header/footer detail levels?
            curDetailHeader = curDetailLevel > curDetailHeader ? -1 : curDetailHeader;
            curDetailFooter = curDetailLevel > curDetailFooter ? -1 : curDetailFooter;

            // Draw detail footer if appropriate
            if ( curDetailLevel <= prevDetailLevel )
            {
                // Draw footer for previous detail levels
                //   from prevDetailLevel up to curDetailLevel
                // Draw footer for current detail level
                for ( int i = prevDetailLevel; i >= curDetailLevel; i-- )
                {
                    MReportSection *footer = findDetailFooter( i );
                    if ( footer )
                    {
                        footer->setPageNumber( currPage );
                        footer->setReportDate( currDate );
                        footer->setCalcFieldData( &grandTotal );
                        if ( ( currY + footer->getHeight() ) > currHeight )
                        {
                            newPage( pages );
                        }
                        footer->draw( &p, leftMargin, currY );
                        currY += footer->getHeight();
                    }
                }
            }

            MReportDetail *detail = findDetail( detailValue.toInt() );
            detail->setPageNumber( currPage );
            detail->setReportDate( currDate );

            int headerHeight = 0;
            MReportSection * header = findDetailHeader( curDetailLevel );
            if ( header && curDetailHeader != curDetailLevel )
                headerHeight = header->getHeight();

            bool repeating = ( currY + detail->getHeight() + headerHeight ) > currHeight ||
                             ( detail->getRepeat() && currY + heightOfDetails > currHeight );
            if ( repeating )
            {
                newPage( pages );

                MReportDetail *sec;
                for ( sec = details.first(); sec; sec = details.next() )
                {
                    if ( sec->getLevel() != curDetailLevel && sec->getRepeat() )
                    {
                        sec->draw( &p, leftMargin, currY );
                        currY += sec->getHeight();
                    }
                }
            }

            // Draw detail header for level curDetailLevel
            // if it hasn't already been drawn
            if ( curDetailHeader != curDetailLevel || repeating )
            {
                MReportSection * header = findDetailHeader( curDetailLevel );
                if ( header )
                {
                    header->setPageNumber( currPage );
                    header->setReportDate( currDate );
                    if ( ( currY + header->getHeight() ) > currHeight )
                    {
                        newPage( pages );
                    }
                    header->draw( &p, leftMargin, currY );
                    currY += header->getHeight();
                    curDetailHeader = curDetailLevel;
                }
            }

            unsigned int fieldCount = detail->getFieldCount();
            for ( i = 0; i < fieldCount; i++ )
            {
                // Get the field value
                detailValue = fields.namedItem( detail->getFieldName( i ) ).nodeValue();
                // Set the field
                detail->setFieldData( i, detailValue );
                // Set the grand total data
                int calcIdx = rFooter.getCalcFieldIndex( detail->getFieldName( i ) );
                if ( calcIdx != -1 )
                {
                    int vsize = grandTotal.at( calcIdx ) ->size();
                    grandTotal.at( calcIdx ) ->resize( vsize + 1 );
                    grandTotal.at( calcIdx ) ->at( vsize ) = detailValue.toDouble();
                }
            }

            detail->draw( &p, leftMargin, currY );
            currY += detail->getHeight();
            prevDetailLevel = curDetailLevel;
        }
    }

    // Draw detail footers that were not drawn before
    //   for details from curDetailLevel up to prevDetailLevel
    for ( int i = prevDetailLevel; i >= 0; i-- )
    {
        MReportSection *footer = findDetailFooter( i );
        if ( footer )
        {
            footer->setPageNumber( currPage );
            footer->setReportDate( currDate );
            footer->setCalcFieldData( &grandTotal );
            if ( ( currY + footer->getHeight() ) > currHeight )
            {
                newPage( pages );
            }

            footer->draw( &p, leftMargin, currY );
            currY += footer->getHeight();
        }
    }

    // Finish the last page of the report
    endPage( pages );

    // Destroy the page painter
    p.end();

    // Set the page collection attributes
    pages->setPageDimensions( TQSize( pageWidth, pageHeight ) );
    pages->setPageSize( pageSize );
    pages->setPageOrientation( pageOrientation );

    // Send final status
    emit signalRenderStatus( rowCount / 2 );
    m_needRegeneration = false;
    m_pageCollection = pages;
    return pages;
}

/** Starts a new page of the report */
void MReportEngine::startPage( MPageCollection* pages )
{
    // Set the current y pos
    currY = topMargin;

    // Create a new page
    pages->appendPage();
    // Increment the page count
    currPage++;

    // Set the page painter to the current page
    p.begin( pages->getCurrentPage() );

    // Draw the report header
    drawReportHeader( pages );
    // Draw the page header
    drawPageHeader( pages );
}

/** Finishes the current page of the report */
void MReportEngine::endPage( MPageCollection* pages )
{
    // Draw the report footer
    drawReportFooter( pages );
    // Draw the page footer
    drawPageFooter( pages );
}

/** Finishes the current page and adds a new page */
void MReportEngine::newPage( MPageCollection* pages )
{
    // Draw the page footer
    drawPageFooter( pages );

    // Stop the painter
    p.end();

    // Start a new page
    startPage( pages );
}

/** Draws the report header object to the current page */
void MReportEngine::drawReportHeader( MPageCollection* pages )
{
    if ( rHeader.getHeight() == 0 )
        return ;

    if ( ( rHeader.printFrequency() == MReportSection::FirstPage && currPage == 1 )
            || ( rHeader.printFrequency() == MReportSection::EveryPage ) )
    {

        rHeader.setPageNumber( currPage );
        rHeader.setReportDate( currDate );
        rHeader.draw( &p, leftMargin, currY );
        currY += rHeader.getHeight();
    }
}

/** Draws the page header to the current page */
void MReportEngine::drawPageHeader( MPageCollection* pages )
{
    if ( pHeader.getHeight() == 0 )
        return ;

    if ( ( currY + pHeader.getHeight() ) > currHeight )
        newPage( pages );

    if ( ( pHeader.printFrequency() == MReportSection::FirstPage && currPage == 1 )
            || ( pHeader.printFrequency() == MReportSection::EveryPage ) )
    {

        pHeader.setPageNumber( currPage );
        pHeader.setReportDate( currDate );
        pHeader.draw( &p, leftMargin, currY );
        currY += pHeader.getHeight();
    }
}

/** Draws the page footer to the current page */
void MReportEngine::drawPageFooter( MPageCollection* pages )
{
    if ( pFooter.getHeight() == 0 )
        return ;

    if ( ( pFooter.printFrequency() == MReportSection::FirstPage && currPage == 1 )
            || ( pFooter.printFrequency() == MReportSection::EveryPage ) )
    {

        pFooter.setPageNumber( currPage );
        pFooter.setReportDate( currDate );
        pFooter.draw( &p, leftMargin, ( pageHeight - bottomMargin ) - pFooter.getHeight() );
        currY += pFooter.getHeight();
    }
}

/** Draws the report footer to the current page */
void MReportEngine::drawReportFooter( MPageCollection* pages )
{
    if ( rFooter.getHeight() == 0 )
        return ;

    if ( ( currY + rFooter.getHeight() ) > currHeight )
        newPage( pages );

    if ( ( rFooter.printFrequency() == MReportSection::EveryPage )
            || ( rFooter.printFrequency() == MReportSection::LastPage ) )
    {

        rFooter.setCalcFieldData( &grandTotal );

        rFooter.setPageNumber( currPage );
        rFooter.setReportDate( currDate );
        rFooter.draw( &p, leftMargin, currY );
        currY += rFooter.getHeight();
    }
}

/** Gets the metrics for the selected page size & orientation */
TQSize MReportEngine::getPageMetrics( int size, int orientation )
{

    TQPrinter * printer;
    TQSize ps;

    // Set the page size
    printer = new TQPrinter();
    printer->setFullPage( true );
    printer->setPageSize( ( TQPrinter::PageSize ) size );
    printer->setOrientation( ( TQPrinter::Orientation ) orientation );

    // Get the page metrics
    TQPaintDeviceMetrics pdm( printer );

    // Display the first page of the report
    ps.setWidth( pdm.width() );
    ps.setHeight( pdm.height() );

    delete printer;

    return ps;
}


/**
This recalculates report dimensions to 96 dpi, standard on Windows
platform.

FIXME: TODO: get the proper screen resolution and not hardcode 96dpi
FIXME: TODO: make this function work on X11 too
*/
void MReportEngine::recalcDimensions()
{
    TQDomNode report;
    TQDomNode child;
    for ( report = rt.firstChild(); !report.isNull(); report = report.nextSibling() )
        if ( report.nodeName() == "KugarTemplate" )
            break;

    TQDomNamedNodeMap rattributes = report.attributes();
    recalcAttribute( "BottomMargin", rattributes );
    recalcAttribute( "TopMargin", rattributes );
    recalcAttribute( "LeftMargin", rattributes );
    recalcAttribute( "RightMargin", rattributes );

    TQDomNodeList tqchildren = report.childNodes();
    int childCount = tqchildren.length();

    for ( int j = 0; j < childCount; j++ )
    {
        child = tqchildren.item( j );
        TQDomNamedNodeMap attributes = child.attributes();

        TQDomNodeList tqchildren2 = child.childNodes();
        int childCount2 = tqchildren2.length();
        recalcAttribute( "Height", attributes );

        for ( int k = 0; k < childCount2; k++ )
        {
            TQDomNode child2 = tqchildren2.item( k );
            TQDomNamedNodeMap attributes = child2.attributes();
            recalcAttribute( "X", attributes );
            recalcAttribute( "Y", attributes );
            recalcAttribute( "Width", attributes );
            recalcAttribute( "Height", attributes );
            recalcAttribute( "X1", attributes );
            recalcAttribute( "X2", attributes );
            recalcAttribute( "Y1", attributes );
            recalcAttribute( "Y2", attributes );
        }
    }

}

void MReportEngine::recalcAttribute( const TQString& name, TQDomNamedNodeMap attributes )
{
    if ( !attributes.namedItem( name ).isNull() )
    {
        attributes.namedItem( name ).setNodeValue( TQString( "%1" ).tqarg( attributes.namedItem( name ).nodeValue().toInt() * 93 / 81 ) );
    }
}

/** Walks the document tree, setting the report tqlayout */
void MReportEngine::initTemplate()
{
    heightOfDetails = 0;

#ifdef TQ_WS_WIN

    recalcDimensions();
#endif

    TQDomNode report;
    TQDomNode child;

    m_needRegeneration = true;
    // Get the report - assume there is only one.

    for ( report = rt.firstChild(); !report.isNull(); report = report.nextSibling() )
        if ( report.nodeName() == "KugarTemplate" )
            break;

    setReportAttributes( &report );

    // Get all the child report elements
    TQDomNodeList tqchildren = report.childNodes();
    int childCount = tqchildren.length();

    for ( int j = 0; j < childCount; j++ )
    {
        child = tqchildren.item( j );

        if ( child.nodeType() == TQDomNode::ElementNode )
        {
            // Report Header
            if ( child.nodeName() == "ReportHeader" )
                setSectionAttributes( &rHeader, &child );
            else if ( child.nodeName() == "PageHeader" )
                setSectionAttributes( &pHeader, &child );
            else if ( child.nodeName() == "DetailHeader" )
            {
                MReportSection * dHeader = new MReportSection;
                dHeaders.append( dHeader );
                setDetMiscAttributes( dHeader, &child );
            }
            else if ( child.nodeName() == "Detail" )
                setDetailAttributes( &child );
            else if ( child.nodeName() == "DetailFooter" )
            {
                MReportSection * dFooter = new MReportSection;
                setDetMiscAttributes( dFooter, &child );
                dFooters.append( dFooter );
            }
            else if ( child.nodeName() == "PageFooter" )
                setSectionAttributes( &pFooter, &child );
            else if ( child.nodeName() == "ReportFooter" )
                setSectionAttributes( &rFooter, &child );
        }
    }
}

/** Sets the main tqlayout attributes for the report */
void MReportEngine::setReportAttributes( TQDomNode* report )
{
    // Get the attributes for the report
    TQDomNamedNodeMap attributes = report->attributes();

    pageSize = attributes.namedItem( "PageSize" ).nodeValue().toInt();
    pageOrientation = attributes.namedItem( "PageOrientation" ).nodeValue().toInt();
    int templateWidth = attributes.namedItem( "PageWidth" ).nodeValue().toInt();
    int templateheight = attributes.namedItem( "PageHeight" ).nodeValue().toInt();

    TQSize ps = getPageMetrics( pageSize, pageOrientation );
    pageWidth = ps.width();
    pageHeight = ps.height();

    widthDelta = ( float ) pageWidth / templateWidth;
    heightDelta = ( float ) pageHeight / templateheight;

    kdDebug( 30001 ) << "pagewidth: " << pageWidth
    << " pageheight: " << pageHeight << "\n"
    << "templateWidth: " << templateWidth
    << " templateheight: " << templateheight << "\n"
    << "widthDelta: " << widthDelta
    << " heightDelta: " << heightDelta
    << endl;

    topMargin = scaleDeltaHeight( attributes.namedItem( "TopMargin" ).nodeValue().toInt() );
    bottomMargin = scaleDeltaHeight( attributes.namedItem( "BottomMargin" ).nodeValue().toInt() );
    leftMargin = scaleDeltaWidth( attributes.namedItem( "LeftMargin" ).nodeValue().toInt() );
    rightMargin = scaleDeltaWidth( attributes.namedItem( "RightMargin" ).nodeValue().toInt() );
}

int MReportEngine::scaleDeltaWidth( int width ) const
{
    float f = width * widthDelta;
    return f > 1 ? int( f + 0.5) : ceil( f );
}

int MReportEngine::scaleDeltaHeight( int height ) const
{
    float f = height * heightDelta;
    return f > 1 ? int( f + 0.5 ) : ceil( f );
}

/** Sets the tqlayout attributes for the given report section */
void MReportEngine::setSectionAttributes( MReportSection* section, TQDomNode* report )
{
    // Get the attributes for the section
    TQDomNamedNodeMap attributes = report->attributes();

    // Get the section attributes
    section->setHeight( scaleDeltaHeight( attributes.namedItem( "Height" ).nodeValue().toInt() ) );
    section->setPrintFrequency( attributes.namedItem( "PrintFrequency" ).nodeValue().toInt() );

    // Process the sections labels
    TQDomNodeList tqchildren = report->childNodes();
    int childCount = tqchildren.length();

    // For each label, extract the attr list and add the new label
    // to the sections's label collection
    for ( int j = 0; j < childCount; j++ )
    {
        TQDomNode child = tqchildren.item( j );
        if ( child.nodeType() == TQDomNode::ElementNode )
        {
            if ( child.nodeName() == "Line" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MLineObject* line = new MLineObject();
                setLineAttributes( line, &attributes );
                section->addLine( line );
            }
            else if ( child.nodeName() == "Label" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MLabelObject* label = new MLabelObject();
                setLabelAttributes( label, &attributes );
                section->addLabel( label );
            }
            else if ( child.nodeName() == "Special" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MSpecialObject* field = new MSpecialObject();
                setSpecialAttributes( field, &attributes );
                section->addSpecialField( field );
            }
            else if ( child.nodeName() == "CalculatedField" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MCalcObject* field = new MCalcObject();
                setCalculatedFieldAttributes( field, &attributes );
                section->addCalculatedField( field );
            }
            else if ( child.nodeName() == "Field" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MFieldObject* field = new MFieldObject();
                setFieldAttributes( field, &attributes );
                section->addField( field );
            }
        }
    }
}

/** Sets the tqlayout attributes for the detail headers and footers */
void MReportEngine::setDetMiscAttributes( MReportSection* section, TQDomNode* report )
{
    // Get the attributes for the section
    TQDomNamedNodeMap attributes = report->attributes();

    // Get the section attributes
    section->setLevel( attributes.namedItem( "Level" ).nodeValue().toInt() );

    // Set other section attributes
    setSectionAttributes( section, report );
}


/** Sets the tqlayout attributes for the detail section */
void MReportEngine::setDetailAttributes( TQDomNode* report )
{
    // Get the attributes for the detail section
    TQDomNamedNodeMap attributes = report->attributes();

    // Get the report detail attributes
    MReportDetail *detail = new MReportDetail;
    int height = scaleDeltaHeight( attributes.namedItem( "Height" ).nodeValue().toInt() );
    heightOfDetails += height;
    detail->setHeight( height );
    detail->setLevel( attributes.namedItem( "Level" ).nodeValue().toInt() );
    detail->setRepeat( attributes.namedItem( "Repeat" ).nodeValue() == "true" );

    // Process the report detail labels
    TQDomNodeList tqchildren = report->childNodes();
    int childCount = tqchildren.length();

    for ( int j = 0; j < childCount; j++ )
    {
        TQDomNode child = tqchildren.item( j );
        if ( child.nodeType() == TQDomNode::ElementNode )
        {
            if ( child.nodeName() == "Line" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MLineObject* line = new MLineObject();
                setLineAttributes( line, &attributes );
                detail->addLine( line );
            }
            else if ( child.nodeName() == "Label" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MLabelObject* label = new MLabelObject();
                setLabelAttributes( label, &attributes );
                detail->addLabel( label );
            }
            else if ( child.nodeName() == "Special" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MSpecialObject* field = new MSpecialObject();
                setSpecialAttributes( field, &attributes );
                detail->addSpecialField( field );
            }
            else if ( child.nodeName() == "Field" )
            {
                TQDomNamedNodeMap attributes = child.attributes();
                MFieldObject* field = new MFieldObject();
                setFieldAttributes( field, &attributes );
                detail->addField( field );
            }
        }
    }
    // Append a newly created detail to the list
    details.append( detail );
}

/** Sets a line's tqlayout attributes */
void MReportEngine::setLineAttributes( MLineObject* line, TQDomNamedNodeMap* attr )
{
    line->setLine( scaleDeltaWidth( attr->namedItem( "X1" ).nodeValue().toInt() ),
                   scaleDeltaHeight( attr->namedItem( "Y1" ).nodeValue().toInt() ),
                   scaleDeltaWidth( attr->namedItem( "X2" ).nodeValue().toInt() ),
                   scaleDeltaHeight( attr->namedItem( "Y2" ).nodeValue().toInt() ) );

    TQString tmp = attr->namedItem( "Color" ).nodeValue();

    line->setColor( tmp.left( tmp.find( "," ) ).toInt(),
                    tmp.mid( tmp.find( "," ) + 1, ( tmp.findRev( "," ) - tmp.find( "," ) ) - 1 ).toInt(),
                    tmp.right( tmp.length() - tmp.findRev( "," ) - 1 ).toInt() );

    line->setWidth( attr->namedItem( "Width" ).nodeValue().toInt() );
    line->setStyle( attr->namedItem( "Style" ).nodeValue().toInt() );
}

/** Sets a label's tqlayout attributes */
void MReportEngine::setLabelAttributes( MLabelObject* label, TQDomNamedNodeMap* attr )
{
    TQString tmp;

    label->setText( attr->namedItem( "Text" ).nodeValue() );
    label->setGeometry( scaleDeltaWidth( attr->namedItem( "X" ).nodeValue().toInt() ),
                        scaleDeltaHeight( attr->namedItem( "Y" ).nodeValue().toInt() ),
                        scaleDeltaWidth( attr->namedItem( "Width" ).nodeValue().toInt() ),
                        scaleDeltaHeight( attr->namedItem( "Height" ).nodeValue().toInt() ) );

    tmp = attr->namedItem( "BackgroundColor" ).nodeValue();
    label->setBackgroundColor( tmp.left( tmp.find( "," ) ).toInt(),
                               tmp.mid( tmp.find( "," ) + 1, ( tmp.findRev( "," ) - tmp.find( "," ) ) - 1 ).toInt(),
                               tmp.right( tmp.length() - tmp.findRev( "," ) - 1 ).toInt() );

    tmp = attr->namedItem( "ForegroundColor" ).nodeValue();
    label->setForegroundColor( tmp.left( tmp.find( "," ) ).toInt(),
                               tmp.mid( tmp.find( "," ) + 1, ( tmp.findRev( "," ) - tmp.find( "," ) ) - 1 ).toInt(),
                               tmp.right( tmp.length() - tmp.findRev( "," ) - 1 ).toInt() );

    tmp = attr->namedItem( "BorderColor" ).nodeValue();
    label->setBorderColor( tmp.left( tmp.find( "," ) ).toInt(),
                           tmp.mid( tmp.find( "," ) + 1, ( tmp.findRev( "," ) - tmp.find( "," ) ) - 1 ).toInt(),
                           tmp.right( tmp.length() - tmp.findRev( "," ) - 1 ).toInt() );

    label->setBorderWidth( attr->namedItem( "BorderWidth" ).nodeValue().toInt() );
    label->setBorderStyle( attr->namedItem( "BorderStyle" ).nodeValue().toInt() );
    if ( !attr->namedItem( "DrawBottom" ).isNull() )
        label->setDrawBottom( attr->namedItem( "DrawBottom" ).nodeValue() == "true" );
    if ( !attr->namedItem( "DrawTop" ).isNull() )
        label->setDrawTop( attr->namedItem( "DrawTop" ).nodeValue() == "true" );
    if ( !attr->namedItem( "DrawLeft" ).isNull() )
        label->setDrawLeft( attr->namedItem( "DrawLeft" ).nodeValue() == "true" );
    if ( !attr->namedItem( "DrawRight" ).isNull() )
        label->setDrawRight( attr->namedItem( "DrawRight" ).nodeValue() == "true" );
    label->setFont( attr->namedItem( "FontFamily" ).nodeValue(),
                    attr->namedItem( "FontSize" ).nodeValue().toInt(),
                    attr->namedItem( "FontWeight" ).nodeValue().toInt(),
                    ( attr->namedItem( "FontItalic" ).nodeValue().toInt() == 0 ? false : true ) );
    label->setHorizontalAlignment( attr->namedItem( "HAlignment" ).nodeValue().toInt() );
    label->setVerticalAlignment( attr->namedItem( "VAlignment" ).nodeValue().toInt() );
    label->setWordWrap( attr->namedItem( "WordWrap" ).nodeValue().toInt() == 0 ? false : true );
}

/** Sets a special field's tqlayout attributes */
void MReportEngine::setSpecialAttributes( MSpecialObject* field, TQDomNamedNodeMap* attr )
{
    field->setType( attr->namedItem( "Type" ).nodeValue().toInt() );
    field->setDateFormat( attr->namedItem( "DateFormat" ).nodeValue().toInt() );

    setLabelAttributes( ( MLabelObject * ) field, attr );
}

/** Sets a field's tqlayout attributes */
void MReportEngine::setFieldAttributes( MFieldObject* field, TQDomNamedNodeMap* attr )
{
    field->setFieldName( attr->namedItem( "Field" ).nodeValue() );
    field->setDataType( attr->namedItem( "DataType" ).nodeValue().toInt() );
    field->setDateFormat( attr->namedItem( "DateFormat" ).nodeValue().toInt() );
    field->setPrecision( attr->namedItem( "Precision" ).nodeValue().toInt() );
    field->setCurrency( attr->namedItem( "Currency" ).nodeValue().toInt() );
    field->setCommaSeparator( attr->namedItem( "CommaSeparator" ).nodeValue().toInt() );
    field->setInputMask( attr->namedItem( "InputMask" ).nodeValue() );

    TQString tmp = attr->namedItem( "NegValueColor" ).nodeValue();

    field->setNegValueColor( tmp.left( tmp.find( "," ) ).toInt(),
                             tmp.mid( tmp.find( "," ) + 1, ( tmp.findRev( "," ) - tmp.find( "," ) ) - 1 ).toInt(),
                             tmp.right( tmp.length() - tmp.findRev( "," ) - 1 ).toInt() );

    setLabelAttributes( ( MLabelObject * ) field, attr );
}

/** Sets a calculated field's tqlayout attributes */
void MReportEngine::setCalculatedFieldAttributes( MCalcObject* field, TQDomNamedNodeMap* attr )
{
    field->setCalculationType( attr->namedItem( "CalculationType" ).nodeValue().toInt() );
    setFieldAttributes( ( MFieldObject * ) field, attr );
}

/** Copies member data from one object to another.
  Used by the copy constructor and assignment operator */
void MReportEngine::copy( const MReportEngine* mReportEngine )
{
    // Copy document data
    m_refCount = 1;
    rd = mReportEngine->rd;
    rt = mReportEngine->rt;

    // Copy page metrics
    pageSize = mReportEngine->pageSize;
    pageOrientation = mReportEngine->pageOrientation;
    topMargin = mReportEngine->topMargin;
    bottomMargin = mReportEngine->bottomMargin;
    leftMargin = mReportEngine->leftMargin;
    rightMargin = mReportEngine->rightMargin;
    pageWidth = mReportEngine->pageWidth;
    pageHeight = mReportEngine->pageHeight;
    heightOfDetails = mReportEngine->heightOfDetails;

    // Copy the report header
    rHeader = mReportEngine->rHeader;
    // Copy the page header
    pHeader = mReportEngine->pHeader;
    // Copy the detail sections
    MReportDetail *detail;
    TQPtrList<MReportDetail> temp = mReportEngine->details;
    temp.setAutoDelete( false );
    for ( detail = temp.first(); detail; detail = temp.next() )
    {
        MReportDetail * new_detail = new MReportDetail;
        *new_detail = *detail;
        details.append( new_detail );
    }
    // Copy the page footer
    pFooter = mReportEngine->pFooter;
    // Copy the report footer
    rFooter = mReportEngine->rFooter;

    // Copy the rendering state
    currY = mReportEngine->currY;
    currHeight = mReportEngine->currHeight;
    currPage = mReportEngine->currPage;
    currDate = mReportEngine->currDate;
    cancelRender = mReportEngine->cancelRender;

    // Copy grand totals list
    grandTotal = mReportEngine->grandTotal;
    m_pageCollection = mReportEngine->m_pageCollection;
    m_needRegeneration = mReportEngine->m_needRegeneration;
    if ( m_pageCollection )
        m_pageCollection->addRef();
}

}

#include "mreportengine.moc"