summaryrefslogtreecommitdiffstats
path: root/kugar/lib/mreportviewer.cpp
blob: 3619d155151f8d42d88eeec7655888d0ba2b7d75 (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
/***************************************************************************
             mreportviewer.cpp  -  Kugar QT report viewer widget
             -------------------
   begin     : Fri Aug 13 1999
   copyright : (C) 1999 by Mutiny Bay Software
   email     : [email protected]
   copyright : (C) 2002 Alexander Dymo
   email     : [email protected]
***************************************************************************/

#include <tqapplication.h>
#include <tdemessagebox.h>
#include <kprinter.h>
#include <tdelocale.h>

#include "mreportviewer.h"

namespace Kugar
{

/** Constructor */
MReportViewer::MReportViewer( MReportEngine *engine, TQWidget *parent, const char *name ) : TQWidget( parent, name ), progress( 0 )
{
    rptEngine = engine;
    rptEngine->addRef();
    init();
}

MReportViewer::MReportViewer( TQWidget *parent, const char *name ) : TQWidget( parent, name ), progress( 0 )
{
    // Create the scrollview
    rptEngine = new MReportEngine();
    init();
}

void MReportViewer::init()
{
    scroller = new TQScrollView( this );

    // Connect the rendering update signal and slot
    connect( rptEngine, TQT_SIGNAL( signalRenderStatus( int ) ),
             TQT_SLOT( slotRenderProgress( int ) ) );

    connect( rptEngine, TQT_SIGNAL( preferedTemplate( const TQString & ) ),
             TQT_SIGNAL( preferedTemplate( const TQString & ) ) );

    // Get the current color palette
    TQPalette p = palette();
    TQColorGroup g = p.active();

    // Set the scroller's background color
    scroller->viewport() ->setBackgroundColor( g.mid() );

    // Create the report display widget
    display = new MPageDisplay( scroller->viewport() );

    // Set the display's default background color
    display->setBackgroundColor( white );

    // Add the display to the scrollview
    scroller->addChild( display );

    //Hide the display, we don't have a report yet ..
    display->hide();
}

/** Destructor */
MReportViewer::~MReportViewer()
{
    clearReport();
    rptEngine->removeRef();
}

/** Report viewer's paint event */
void MReportViewer::paintEvent( TQPaintEvent* event )
{}

/** Report viewer's resize event */
void MReportViewer::resizeEvent( TQResizeEvent* event )
{
    scroller->resize( event->size() );
}

// Set the report's data from an in-line string.

bool MReportViewer::setReportData( const TQString &data )
{
    return rptEngine -> setReportData( data );
}


// Set the report's data from an i/o device.

bool MReportViewer::setReportData( TQIODevice *dev )
{
    return rptEngine -> setReportData( dev );
}


// Set the report's template from an in-line string.

bool MReportViewer::setReportTemplate( const TQString &tpl )
{
    return rptEngine -> setReportTemplate( tpl );
}


// Set the report's template from an i/o device.

bool MReportViewer::setReportTemplate( TQIODevice *dev )
{
    return rptEngine -> setReportTemplate( dev );
}


/** Generates the report's page collection */
bool MReportViewer::renderReport()
{
    // Render the report
    report = rptEngine->renderReport();

    // Display the first page of the report
    if ( report && report->getFirstPage() != 0 )
    {
        display->setPageDimensions( report->pageDimensions() );
        display->setPage( report->getFirstPage() );
        display->show();

        return true;
    }

    return false;
}

/** Clears the report's page collection */
void MReportViewer::clearReport()
{
    // Hide the display
    display->hide();
}

/** Prints the rendered report to the selected printer - displays TDE print dialog */
void MReportViewer::printReport()
{
    // Check for a report
    if ( !report )
        return ;

    // Get the page count
    int cnt = report->pageCount();

    // Check if there is a report or any pages to print
    if ( cnt == 0 )
    {
        KMessageBox::error( this, i18n( "There are no pages in the\nreport to print." ) );
        return ;
    }

    // Set the printer dialog
    KPrinter printer;

    setupPrinter( printer );
    if ( printer.setup( this ) )
        printReport( printer );
}

/** Shows the first page in the report */
void MReportViewer::slotFirstPage()
{
    TQPicture * page;

    if ( !report )
        return ;

    if ( ( page = report->getFirstPage() ) != 0 )
    {
        display->setPage( page );
        display->repaint();
    }
}

/** Shows the next page in the report */
void MReportViewer::slotNextPage()
{
    TQPicture * page;

    if ( !report )
        return ;

    int index = report->getCurrentIndex();

    if ( ( page = report->getNextPage() ) != 0 )
    {
        display->setPage( page );
        display->repaint();
    }
    else
        report->setCurrentPage( index );
}

/** Shows the prevoius page in the report */
void MReportViewer::slotPrevPage()
{
    TQPicture * page;

    if ( !report )
        return ;

    int index = report->getCurrentIndex();

    if ( ( page = report->getPreviousPage() ) != 0 )
    {
        display->setPage( page );
        display->repaint();
    }
    else
        report->setCurrentPage( index );
}

/** Shows the last page in the report */
void MReportViewer::slotLastPage()
{
    TQPicture * page;

    if ( !report )
        return ;

    if ( ( page = report->getLastPage() ) != 0 )
    {
        display->setPage( page );
        display->repaint();
    }
}

/** Cancel printing of the report */
void MReportViewer::slotCancelPrinting()
{
    printer->abort();
}

/** Updates rendering progress */
void MReportViewer::slotRenderProgress( int p )
{

    // Check if the dialog was created
    if ( progress == 0 )
    {
        totalSteps = rptEngine->getRenderSteps();
        progress = new TQProgressDialog( i18n("Creating report..."), i18n("Cancel"),
                                        totalSteps, this, "progress", true );
        progress->setMinimumDuration( M_PROGRESS_DELAY );
    }

    // Update the dialog
    progress->setProgress( p );
    tqApp->processEvents();

    // Check if the action was canceled
    if ( progress->wasCancelled() )
    {
        progress->setProgress( totalSteps );
        rptEngine->slotCancelRendering();
    }

    // Cleanup dialog if necessary
    if ( progress->progress() == -1 )
    {
        delete progress;
        progress = 0;
    }
}


// Return the preferred size.

TQSize MReportViewer::sizeHint() const
{
    return scroller -> sizeHint();
}

void MReportViewer::printReport( KPrinter &printer )
{
    // Check for a report
    if ( !report )
        return ;

    // Get the page count
    int cnt = report->pageCount();

    // Check if there is a report or any pages to print
    if ( cnt == 0 )
    {
        KMessageBox::error( this, i18n( "There are no pages in the\nreport to print." ) );
        return ;
    }


    TQPicture* page;
    TQPainter painter;
    bool printRev;

    // Save the viewer's page index
    int viewIdx = report->getCurrentIndex();

    // Check the order we are printing the pages
    if ( printer.pageOrder() == KPrinter::FirstPageFirst )
        printRev = false;
    else
        printRev = true;

    // Get the count of pages and copies to print
    int printFrom = printer.fromPage() - 1;
    int printTo = printer.toPage();
    int printCnt = ( printTo - printFrom );
    int printCopies = printer.numCopies();
    int totalSteps = printCnt * printCopies;
    int currentStep = 1;

    // Set copies to 1, KPrinter copies does not appear to work ...
    printer.setNumCopies( 1 );

    // Setup the progress dialog
    TQProgressDialog progress( i18n( "Printing report..." ),
                              i18n( "Cancel" ),
                              totalSteps, this, "progress", true );
    progress.setMinimumDuration( M_PROGRESS_DELAY );
    TQObject::connect( &progress, TQT_SIGNAL( cancelled() ), this, TQT_SLOT( slotCancelPrinting() ) );
    progress.setProgress( 0 );
    tqApp->processEvents();

    // Start the printer
    painter.begin( &printer );

    // Print each copy
    for ( int j = 0; j < printCopies; j++ )
    {
        // Print each page in the collection
        for ( int i = printFrom ; i < printTo; i++, currentStep++ )
        {
            if ( !printer.aborted() )
            {
                progress.setProgress( currentStep );
                tqApp->processEvents();

                if ( printRev )
                    report->setCurrentPage( ( printCnt == 1 ) ? i : ( printCnt - 1 ) - i );
                else
                    report->setCurrentPage( i );

                page = report->getCurrentPage();
                page->play( &painter );
                if ( i < printCnt - 1 )
                    printer.newPage();
            }
            else
            {
                j = printCopies;
                break;
            }
        }
        if ( j < printCopies - 1 )
            printer.newPage();
    }

    // Cleanup printing
    setCursor( arrowCursor );
    painter.end();
    report->setCurrentPage( viewIdx );
}

void MReportViewer::setupPrinter( KPrinter &printer )
{
    int cnt = report->pageCount();

    printer.setPageSize( ( KPrinter::PageSize ) report->pageSize() );
    printer.setOrientation( ( KPrinter::Orientation ) report->pageOrientation() );
    printer.setMinMax( 1, cnt );
    printer.setFromTo( 1, cnt );
    printer.setFullPage( true );
}

void MReportViewer::printReportSilent( int printFrom, int printTo, int printCopies, TQString printerName )
{
    int cnt = report->pageCount();
    if ( printFrom == -1 )
        printFrom = 1;
    if ( printTo == -1 )
        printTo = cnt;
    if ( printCopies == -1 )
        printCopies = 1;


    printer = new KPrinter();

    printer->setPageSize( ( KPrinter::PageSize ) report->pageSize() );
    printer->setOrientation( ( KPrinter::Orientation ) report->pageOrientation() );
    printer->setMinMax( 1, cnt );
    printer->setFullPage( true );
    printer->setNumCopies( printCopies );
    printer->setFromTo( printFrom, printTo );
    if ( !printerName.isEmpty() )
        printer->setPrinterName( printerName );

    printReport( *printer );

    delete printer;
}

}

#include "mreportviewer.moc"