summaryrefslogtreecommitdiffstats
path: root/kugar/kudesigner_lib/kugartemplate.cpp
blob: 28dc10d70921bdef4365ed23a1c943c41135a400 (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
/* This file is part of the KDE project
Copyright (C) 2003-2004 Alexander Dymo <adymo@mksat.net>

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 "kugartemplate.h"

#include <klocale.h>

#include <tqcanvas.h>
#include <tqpainter.h>
#include <tqprinter.h>
#include <tqpaintdevicemetrics.h>

#include <koproperty/property.h>

#include "propertyserializer.h"

#include "canvas.h"
#include "reportitem.h"

#include "reportheader.h"
#include "reportfooter.h"
#include "pageheader.h"
#include "pagefooter.h"
#include "detailheader.h"
#include "detailfooter.h"
#include "detail.h"

#include <kdebug.h>

namespace Kudesigner
{

KugarTemplate::KugarTemplate( int x, int y, int width, int height, Canvas *canvas )
        : Section( x, y, width, height, canvas )
{
    detailsCount = 0;

    setZ( 1 );
    m_canvas->setKugarTemplate( this );

    reportHeader = 0;
    reportFooter = 0;
    pageHeader = 0;
    pageFooter = 0;

    props.setGroupDescription( "DocumentSettings", i18n( "Document Settings" ) );

    TQMap<TQString, TQString> m;
    m[ "A4" ] = "0";
    m[ "B5" ] = "1";
    m[ "Letter" ] = "2";
    m[ "Legal" ] = "3";
    m[ "Executive" ] = "4";
    m[ "A0" ] = "5";
    m[ "A1" ] = "6";
    m[ "A2" ] = "7";
    m[ "A3" ] = "8";
    m[ "A5" ] = "9";
    m[ "A6" ] = "10";
    m[ "A7" ] = "11";
    m[ "A8" ] = "12";
    m[ "A9" ] = "13";
    m[ "B0" ] = "14";
    m[ "B1" ] = "15";
    m[ "B10" ] = "16";
    m[ "B2" ] = "17";
    m[ "B3" ] = "18";
    m[ "B4" ] = "19";
    m[ "B6" ] = "20";
    m[ "B7" ] = "21";
    m[ "B8" ] = "22";
    m[ "B9" ] = "23";
    m[ "C5E" ] = "24";
    m[ "Comm10E" ] = "25";
    m[ "DLE" ] = "26";
    m[ "Folio" ] = "27";
    m[ "Ledger" ] = "28";
    m[ "Tabloid" ] = "29";
    m[ "NPageSize" ] = "30";
    props.addProperty( new Property( "PageSize", m.values(), m.keys(), "0", i18n( "Page Size" ), i18n( "Page Size" ) ), "DocumentSettings" );
    m.clear();

    m[ i18n( "Portrait" ) ] = "0";
    m[ i18n( "Landscape" ) ] = "1";
    props.addProperty( new Property( "PageOrientation", m.values(), m.keys(), "0", i18n( "Page Orientation" ), i18n( "Page Orientation" ) ), "DocumentSettings" );
    m.clear();

    props.addProperty( new Property( "TopMargin", 0, i18n( "Top Margin" ), i18n( "Top Margin" ), KoProperty::Integer ), "DocumentSettings" );
    props.addProperty( new Property( "BottomMargin", 0, i18n( "Bottom Margin" ), i18n( "Bottom Margin" ), KoProperty::Integer ), "DocumentSettings" );
    props.addProperty( new Property( "LeftMargin", 0, i18n( "Left Margin" ), i18n( "Left Margin" ), KoProperty::Integer ), "DocumentSettings" );
    props.addProperty( new Property( "RightMargin", 0, i18n( "Right Margin" ), i18n( "Right Margin" ), KoProperty::Integer ), "DocumentSettings" );
}

KugarTemplate::~KugarTemplate()
{
    if ( reportHeader )
        delete reportHeader;
    if ( pageHeader )
        delete pageHeader;

    std::map<int, DetailBand>::const_iterator it;
    for ( it = details.begin(); it != details.end(); ++it )
    {
        if ( it->second.first.first )
            delete it->second.first.first;
        if ( it->second.second )
            delete it->second.second;
        if ( it->second.first.second )
            delete it->second.first.second;
    }
    if ( pageFooter )
        delete pageFooter;
    if ( reportFooter )
        delete reportFooter;
}

void KugarTemplate::draw( TQPainter &painter )
{
    updatePaperProps();
    painter.setPen( TQPen( TQColor( 160, 160, 160 ), 0, TQt::SolidLine ) );
    TQPoint p1( ( int ) ( x() + props[ "LeftMargin" ].value().toInt() ),
               ( int ) ( y() + props[ "TopMargin" ].value().toInt() ) );
    TQPoint p2( ( int ) ( x() + props[ "LeftMargin" ].value().toInt() ),
               ( int ) y() + height() - props[ "BottomMargin" ].value().toInt() );
    TQPoint p3( ( int ) x() + width() - props[ "RightMargin" ].value().toInt(),
               ( int ) y() + height() - props[ "BottomMargin" ].value().toInt() );
    TQPoint p4( ( int ) x() + width() - props[ "RightMargin" ].value().toInt(),
               ( int ) ( y() + props[ "TopMargin" ].value().toInt() ) );
    painter.moveTo( p1 );
    painter.lineTo( p2 );
    painter.lineTo( p3 );
    painter.lineTo( p4 );
    painter.lineTo( p1 );
    /*    painter.drawRect((int)(x()+props["LeftMargin"].first.toInt()),
            (int)(y()+props["TopMargin"].first.toInt()),
            width() - props["LeftMargin"].first.toInt() - props["RightMargin"].first.toInt(),
            height() - props["TopMargin"].first.toInt() - props["BottomMargin"].first.toInt());*/

    if ( Config::gridSize() > 1 )
    {
        int x = Config::gridSize();
        while ( x < width() )
        {
            int y = Config::gridSize();
            while ( y < height() )
            {
                painter.drawPoint( x, y );
                y += Config::gridSize();
            }
            x += Config::gridSize();
        }
    }

    Section::draw( painter );
}

void KugarTemplate::updatePaperProps()
{
    TQPrinter * printer;

    // Set the page size
    printer = new TQPrinter();
    printer->setFullPage( true );
    printer->setPageSize( ( TQPrinter::PageSize ) props[ "PageSize" ].value().toInt() );
    printer->setOrientation( ( TQPrinter::Orientation ) props[ "PageOrientation" ].value().toInt() );

    // Get the page metrics and set appropriate wigth and height
    TQPaintDeviceMetrics pdm( printer );
    canvas() ->resize( pdm.width(), pdm.height() );
    setSize( pdm.width(), pdm.height() );

    //this is not needed anymore
    delete printer;
}

/*arrange sections on page automatically*/
void KugarTemplate::arrangeSections( bool destructive )
{
    int base = props[ "TopMargin" ].value().toInt();
    if ( reportHeader )
    {
        reportHeader->arrange( base, destructive );
        base += reportHeader->props[ "Height" ].value().toInt();
        reportHeader->show();
    }
    if ( pageHeader )
    {
        pageHeader->arrange( base, destructive );
        base += pageHeader->props[ "Height" ].value().toInt();
        pageHeader->show();
    }

    std::map<int, DetailBand>::const_iterator it;
    for ( it = details.begin(); it != details.end(); ++it )
    {
        //arranging detail header
        if ( it->second.first.first )
        {
            it->second.first.first->arrange( base, destructive );
            base += it->second.first.first->props[ "Height" ].value().toInt();
            it->second.first.first->show();
        }
        //arranging detail
        if ( it->second.second )
        {
            it->second.second->arrange( base, destructive );
            base += it->second.second->props[ "Height" ].value().toInt();
            it->second.second->show();
        }
    }
    std::map<int, DetailBand>::reverse_iterator itr;
    for ( itr = details.rbegin(); itr != details.rend(); ++itr )
    {
        //arranging detail footer
        if ( itr->second.first.second )
        {
            itr->second.first.second->arrange( base, destructive );
            base += itr->second.first.second->props[ "Height" ].value().toInt();
            itr->second.first.second->show();
        }
    }

    if ( pageFooter )
    {
        pageFooter->arrange( base, destructive );
        base += pageFooter->props[ "Height" ].value().toInt();
        pageFooter->show();
    }
    if ( reportFooter )
    {
        reportFooter->arrange( base, destructive );
        base += reportFooter->props[ "Height" ].value().toInt();
        reportFooter->show();
    }
}

TQString KugarTemplate::getXml()
{
    TQString result = "";
    result += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n";
    result += "<!DOCTYPE KugarTemplate SYSTEM \"kugartemplate.dtd\">\n\n";
    result += "<KugarTemplate";

    for ( Set::Iterator it( props ); it.current(); ++it )
    {
        TQString attribute = it.currentKey();
        TQString value = PropertySerializer::toString( it.current() );
        if ( !attribute.isEmpty() && !value.isEmpty() )
            result += " " + attribute + "=" + "\"" + value + "\"";
    }

    result += " PageWidth=\"" + TQString::number( width() )
           + "\" PageHeight=\"" + TQString::number( height() ) + "\"";

    result += ">\n";

    if ( reportHeader )
        result += reportHeader->getXml();
    if ( pageHeader )
        result += pageHeader->getXml();

    std::map<int, DetailBand>::const_iterator it;
    for ( it = details.begin(); it != details.end(); ++it )
    {
        //getting xml from detail header
        if ( it->second.first.first )
            result += it->second.first.first->getXml();
        //getting xml from detail
        if ( it->second.second )
            result += it->second.second->getXml();
        //getting xml from detail footer
        if ( it->second.first.second )
            result += it->second.first.second->getXml();
    }
    if ( pageFooter )
        result += pageFooter->getXml();
    if ( reportFooter )
        result += reportFooter->getXml();

    result += "</KugarTemplate>\n";

    return result;
}

bool KugarTemplate::removeReportItem( TQCanvasItem *item )
{
    if ( item->rtti() > 2000 )
    {

        item->hide();
        ReportItem *ritem = dynamic_cast<ReportItem*>( item );
        if ( ritem != 0 )
        {
            ritem->section() ->items.remove( ritem );
            qWarning( "good" );
        }
        //delete item;
        //        section = 0;
        canvas() ->update();

        return true;
    }
    if ( item->rtti() > 1800 )
    {
        /*    if ((*it)->rtti() == Detail)
            {
                CanvasDetail *det = (CanvasDetail*)(*it);
                if ( det->props["Level"].first.toInt() <
                    ((MyCanvas*)(canvas()))->templ->detailsCount - 1)
                    return;
            }*/

        Band * section = dynamic_cast<Band *>( item );

        DetailHeader *header = 0;
        DetailFooter *footer = 0;
        removeSection( section, &header, &footer );
        section->hide();
        delete section;

        if ( header )
        {
            header->hide();
            delete header;
        }
        if ( footer )
        {
            footer->hide();
            delete footer;
        }
        arrangeSections();
        canvas() ->update();

        return true;
    }

    return false;
}

void KugarTemplate::removeSection( Band *section,
                                   DetailHeader **header, DetailFooter **footer )
{
    *header = 0;
    *footer = 0;
    if ( section == reportHeader )
        reportHeader = 0;
    if ( section == reportFooter )
        reportFooter = 0;
    if ( section == pageHeader )
        pageHeader = 0;
    if ( section == pageFooter )
        pageFooter = 0;
    for ( std::map<int, DetailBand>::iterator i = details.begin(); i != details.end(); ++i )
    {
        if ( i->second.second == section )
        {
            //delete not only detail but it's header and footer
            i->second.second = 0;
            *header = i->second.first.first;
            i->second.first.first = 0;
            *footer = i->second.first.second;
            i->second.first.second = 0;
            detailsCount--;
        }
        if ( i->second.first.first == section )
            i->second.first.first = 0;
        if ( i->second.first.second == section )
            i->second.first.second = 0;
    }
}

Band *KugarTemplate::band( Kudesigner::RttiValues type, int level )
{
    switch ( type )
    {
    case Rtti_ReportHeader:
        return reportHeader;
    case Rtti_PageHeader:
        return pageHeader;
    case Rtti_DetailHeader:
        return details[ level ].first.first;
    case Rtti_Detail:
        return details[ level ].second;
    case Rtti_DetailFooter:
        return details[ level ].first.second;
    case Rtti_PageFooter:
        return pageFooter;
    case Rtti_ReportFooter:
        return reportFooter;
    default:
        return 0;
    }
    return 0;
}

}