summaryrefslogtreecommitdiffstats
path: root/kugar/lib/mreportsection.cpp
blob: 5040d64c32c52b565fc90263f3773559837f9a4e (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
/***************************************************************************
             mreportsection.cpp  -  Kugar report section
             -------------------
   begin     : Mon Aug 23 1999
   copyright : (C) 1999 by Mutiny Bay Software
   email     : info@mutinybaysoftware.com
   copyright : (C) 2002 Alexander Dymo
   email     : cloudtemple@mksat.net
***************************************************************************/

#include "mreportsection.h"
#include "mutil.h"

namespace Kugar
{

/** Constructor */
MReportSection::MReportSection()
{
    // Set tqgeometry
    height = 50;

    // Set print frequency
    frequency = MReportSection::EveryPage;

    // Set special field data
    reportDate = TQDate::currentDate();
    pageNumber = 0;

    // Set the line list to AutoDelete
    lines.setAutoDelete( true );
    // Set the label list to AutoDelete
    labels.setAutoDelete( true );
    // Set the field list to AutoDelete
    fields.setAutoDelete( true );
    // Set the special field list to AutoDelete
    specialFields.setAutoDelete( true );
}

/** Copy constructor */
MReportSection::MReportSection( const MReportSection& mReportSection ) /*: TQObject((TQObject &) mReportSection)*/
{
    copy( &mReportSection );
}

/** Assignment operator */
MReportSection MReportSection::operator=( const MReportSection& mReportSection )
{
    if ( &mReportSection == this )
        return * this;

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

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

    return *this;
}

/** Destructor */
MReportSection::~MReportSection()
{
    clear();
}

/** Frees all resources allocated by the report section */
void MReportSection::clear()
{
    clearBase();
}

/** Frees base resources allocated by the report section */
void MReportSection::clearBase()
{
    // Clear the line collection
    lines.clear();
    // Clear the label collection
    labels.clear();
    // Clear the field collection
    fields.clear();
    // Clear the special field collection
    specialFields.clear();
    // Clear the calculated field collection
    calculatedFields.clear();
}

/** Adds a new line object to the section's line collection */
void MReportSection::addLine( MLineObject* line )
{
    lines.append( line );
}

/** Adds a new label object to the section's label collection */
void MReportSection::addLabel( MLabelObject* label )
{
    labels.append( label );
}

/** Adds a new field object to the section's label collection */
void MReportSection::addField( MFieldObject * field )
{
    fields.append( field );
}

/** Adds a new special field object to the section's special field collection */
void MReportSection::addSpecialField( MSpecialObject* special )
{
    specialFields.append( special );
}

/** Adds a new calculated field object to the section's calculated field collection */
void MReportSection::addCalculatedField( MCalcObject* calc )
{
    calculatedFields.append( calc );
}

/** Gets the height of the section */
int MReportSection::getHeight()
{
    return height;
}

/** Sets the height of the section */
void MReportSection::setHeight( int h )
{
    height = h;
}

/** Sets the level of the section */
void MReportSection::setLevel( int l )
{
    level = l;
}

/** Gets the level of the section */
int MReportSection::getLevel()
{
    return level;
}

/** Set the current page number - used by special fields */
void MReportSection::setPageNumber( int page )
{
    pageNumber = page;
}

/** Set the current date - used by special fields */
void MReportSection::setReportDate( TQDate date )
{
    reportDate = date;
}

/** Sets the print frequency of the section */
void MReportSection::setPrintFrequency( int printFrequency )
{
    frequency = printFrequency;
}

/** Returns the print frequency of the section */
int MReportSection::printFrequency()
{
    return frequency;
}

/** Returns the name of the bound field for the calculated field object at the given index */
TQString MReportSection::getCalcFieldName( int idx )
{
    MCalcObject * field = calculatedFields.at( idx );

    return field->getFieldName();
}

/** Sets the data for the specified calculated field */
void MReportSection::setCalcFieldData( int idx, TQString data )
{
    MCalcObject * field = calculatedFields.at( idx );

    field->setText( data );
}

/** Sets the data for the all calculated fields - list size must number of calculated fields */
void MReportSection::setCalcFieldData( TQPtrList<TQMemArray<double> >* values )
{
    MCalcObject * field;
    int i = 0;

    // Calculate and set the calculated field's data
    for ( field = calculatedFields.first(); field != 0; field = calculatedFields.next() )
    {
        switch ( field->getCalculationType() )
        {
        case MCalcObject::Count:
            field->setText( TQString::number( MUtil::count( values->at( i ) ) ) );
            break;
        case MCalcObject::Sum:
            field->setText( TQString::number( MUtil::sum( values->at( i ) ), 'f', 2 ) );
            break;
        case MCalcObject::Average:
            field->setText( TQString::number( MUtil::average( values->at( i ) ) ) );
            break;
        case MCalcObject::Variance:
            field->setText( TQString::number( MUtil::variance( values->at( i ) ) ) );
            break;
        case MCalcObject::StandardDeviation:
            field->setText( TQString::number( MUtil::stdDeviation( values->at( i ) ) ) );
            break;
        }
        i++;
    }
}

/** Returns the index of the calculated field object for the given bound field */
int MReportSection::getCalcFieldIndex( TQString field )
{
    MCalcObject * tmpField;

    // Find the field in the calculated field collection and return the index
    for ( tmpField = calculatedFields.first(); tmpField != 0; tmpField = calculatedFields.next() )
    {
        if ( tmpField->getFieldName() == field )
            break;
    }
    return calculatedFields.at();
}

/** Returns the number of calculated fields in the section */
int MReportSection::getCalcFieldCount()
{
    return calculatedFields.count();
}

/** Draws the section to the specified painter & x/y-offsets */
void MReportSection::draw( TQPainter* p, int xoffset, int yoffset )
{
    drawObjects( p, xoffset, yoffset );
}

/** Draws the section base objects to the specified painter & x/y offsets */
void MReportSection::drawObjects( TQPainter* p, int xoffset, int yoffset )
{
    MLineObject * line;
    MLabelObject* label;
    MSpecialObject* special;
    MCalcObject* cfield;
    MFieldObject* field;

    // Set the offsets
    int xcalc = xoffset;
    int ycalc = yoffset;

    // Draw the line collection
    for ( line = lines.first(); line != 0; line = lines.next() )
    {
        line->draw( p, xcalc, ycalc );
    }

    // Draw the label collection
    for ( label = labels.first(); label != 0; label = labels.next() )
    {
        label->draw( p, xcalc, ycalc );
    }

    // Draw the field collection
    for ( field = fields.first(); field != 0; field = fields.next() )
    {
        field->draw( p, xcalc, ycalc );
    }

    // Draw the calculated field collection
    for ( cfield = calculatedFields.first(); cfield != 0; cfield = calculatedFields.next() )
    {
        cfield->draw( p, xcalc, ycalc );
    }

    // Draw the special field collection
    for ( special = specialFields.first(); special != 0; special = specialFields.next() )
    {
        switch ( special->getType() )
        {
        case MSpecialObject::Date:
            special->setText( reportDate );
            break;
        case MSpecialObject::PageNumber:
            special->setText( pageNumber );
            break;
        }
        special->draw( p, xcalc, ycalc );
    }
}

/** Copies member data from one object to another.
      Used by the copy constructor and assignment operator */
void MReportSection::copy( const MReportSection* mReportSection )
{
    // Copy the section's tqgeometry
    height = mReportSection->height;

    // Copy the print frequency
    frequency = mReportSection->frequency;

    // Copy the line list
    lines = mReportSection->lines;
    // Copy the label list
    labels = mReportSection->labels;
    // Copy the field list
    fields = mReportSection->fields;
    // Copy the special field list
    specialFields = mReportSection->specialFields;
    // Copy the calculated field list
    calculatedFields = mReportSection->calculatedFields;
}

void MReportSection::setFieldData( TQString name, TQString data )
{
    MFieldObject * field;
    for ( field = fields.first(); field != 0; field = fields.next() )
    {
        qWarning( "    checking field %s", field->getFieldName().ascii() );
        if ( field->getFieldName() == name )
            field->setText( data );
    }
}

}