summaryrefslogtreecommitdiffstats
path: root/libkdchart/KDChartWidget.cpp
blob: 96f1c6e50ec7926d1e0e6bcc297a684a6777ecf4 (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
/*
   KDChart - a multi-platform charting engine
   */

/****************************************************************************
 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
 **
 ** This file is part of the KDChart library.
 **
 ** This file may be distributed and/or modified under the terms of the
 ** GNU General Public License version 2 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.
 **
 ** Licensees holding valid commercial KDChart licenses may use this file in
 ** accordance with the KDChart Commercial License Agreement provided with
 ** the Software.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
 **   information about KDChart Commercial License Agreements.
 **
 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
 ** licensing are not clear to you.
 **
 **********************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <KDChartWidget.h>
#include <KDChart.h>
#include <KDChartParams.h>
#include <KDChartTableBase.h>
#ifndef KDCHART_MASTER_CVS
#include "KDChartWidget.moc"
#endif

#include <tqpainter.h>

/**
  \class KDChartWidget KDChartWidget.h

  \brief The entry point into the charting that most people will want
  to use.

  Simply create a KChartWidget in your application and put it where
  you want in your widget hierarchy and create a KChartParams object
  that specifies how the chart should be drawn.

  \note If for some reason you are NOT using the
  KDChartWidget class but calling the painting methods of KDChart directly,
  you probably will also use the KDChartDataRegionList class:
  This class is derived from TQPtrList, so all of the TQt documentation
  for this class is valid for KDChartDataRegionList too, e.g. freeing
  of the pointers stored can either be done automatically or
  manually - so PLEASE take the time to read the reference information for this class!

  \sa KDChart, KDChartDataRegionList
  */


/**
  Default Constructor.

  Sets params and data pointers to zero, you should call setParams
  and setData before using this chart otherwise only a simple
  default bar chart will be shown.

  \param parent the widget parent; passed on to TQWidget
  \param name the widget name; passed on to TQWidget
  */

KDChartWidget::KDChartWidget( TQWidget* parent, const char* name ) :
TQWidget( parent, name ),
    _params( 0 ),
    _data( 0 ),
    _activeData( false ),
_mousePressedOnRegion( 0 )
{
    _dataRegions.setAutoDelete( true );
    setDoubleBuffered( true );
    setBackgroundMode( TQt::NoBackground );
}


/**
  Constructor. Stores the chart parameters.

  \param params the specification of the chart
  \param data the data to be displayed as a chart
  \param parent the widget parent; passed on to TQWidget
  \param name the widget name; passed on to TQWidget
  */

KDChartWidget::KDChartWidget( KDChartParams* params,
        KDChartTableDataBase* data,
        TQWidget* parent, const char* name ) :
TQWidget( parent, name ),
    _params( params ),
    _data( data ),
    _activeData( false ),
_mousePressedOnRegion( 0 )
{
    _dataRegions.setAutoDelete( true );
    setDoubleBuffered( true );
    setBackgroundMode( TQt::NoBackground );
}


/**
  Destructor.
  */
KDChartWidget::~KDChartWidget()
{
    // delete any regions that might still be registered
    _dataRegions.clear();
    KDChartAutoColor::freeInstance();
}

void KDChartWidget::paintTo( TQPainter& painter,
        const TQRect* rect )
{
    KDChart::paint( &painter, _params, _data, &_dataRegions, rect );
}

void KDChartWidget::print( TQPainter& painter,
        const TQRect* rect )
{
    bool oldOpt=true;
    if( _params ){
        oldOpt = _params->optimizeOutputForScreen();
        _params->setOptimizeOutputForScreen( false );
    }
    bool bOldBuf = _doubleBuffered;
    _doubleBuffered = false;
    paintTo( painter, rect );
    _doubleBuffered = bOldBuf;
    if( _params )
        _params->setOptimizeOutputForScreen( oldOpt );
}

void KDChartWidget::paintEvent( TQPaintEvent* event )
{
    if( _doubleBuffered ) {
        // if double-buffering, paint onto the pixmap and copy
        // afterwards
        _buffer.fill( backgroundColor() );
        TQPainter painter( &_buffer );
        paintTo( painter );
        bitBlt( this, event->rect().topLeft(), &_buffer, event->rect() );
    } else {
        // if not double-buffering, paint directly into the window
        TQPainter painter( this );
        paintTo( painter );
    }
}


/**
  \internal
  */
void KDChartWidget::mousePressEvent( TQMouseEvent* event )
{
    if ( !_activeData )
        return ;

    _mousePressedOnRegion = 0;
    KDChartDataRegion* current = 0;
    //TQPtrListIterator < KDChartDataRegion > it( _dataRegions );
    for( current = _dataRegions.last(); current; current = _dataRegions.prev() ){
    //while ( ( current = it.current() ) ) {
        if ( current->contains( event->pos() ) ) {
            _mousePressedOnRegion = current;
            if ( event->button() == TQt::LeftButton ){
                emit dataLeftPressed( current->row, current->col );
                emit dataLeftPressed( event->pos() );
            }else if ( event->button() == TQt::MidButton ){
                emit dataMiddlePressed( current->row, current->col );
                emit dataMiddlePressed( event->pos() );
            }else{
                emit dataRightPressed( current->row, current->col );
                emit dataRightPressed( event->pos() );
            }
            return;
        }
    }
}


/**
  \internal
  */
void KDChartWidget::mouseReleaseEvent( TQMouseEvent* event )
{
    if ( !_activeData )
        return ;

    KDChartDataRegion* current = 0;
    TQPtrListIterator < KDChartDataRegion > it( _dataRegions );
    while ( ( current = it.current() ) ) {
        ++it;
        if ( current->contains( event->pos() ) ) {
            if ( event->button() == TQt::LeftButton ) {
                emit dataLeftReleased( current->row, current->col );
                emit dataLeftReleased( event->pos() );
                if ( _mousePressedOnRegion == current ){
                    emit dataLeftClicked( current->row, current->col );
                    emit dataLeftClicked( event->pos() );
                }
            } else if ( event->button() == TQt::MidButton ) {
                emit dataMiddleReleased( current->row, current->col );
                emit dataMiddleReleased( event->pos() );
                if ( _mousePressedOnRegion == current ){
                    emit dataMiddleClicked( current->row, current->col );
                    emit dataMiddleClicked( event->pos() );
                }
            } else {
                emit dataRightReleased( current->row, current->col );
                emit dataRightReleased( event->pos() );
                if ( _mousePressedOnRegion == current ){
                    emit dataRightClicked( current->row, current->col );
                    emit dataRightClicked( event->pos() );
                }
            }
        }
    }
}


/**
  \internal
  */
void KDChartWidget::resizeEvent( TQResizeEvent* /*event*/ )
{
    // if we use double-buffering, resize the buffer to the new size,
    // otherwise leave it alone
    if( _doubleBuffered )
        _buffer.resize( size() );
}


/**
  If \a active is true, this widget reports mouse presses, releases
  and clicks on the data segments it displays. This can slow down the
  display process, so this is turned off by default.

  If active data reporting is turned on when the widget is already
  shown, data will be reported after the next repaint(). Call
  repaint() explicitly if necessary.

  Active data is currently supported for bar, pie, and line charts
  (the latter only with markers, as trying to hit the line would be
  too difficult for the user anyway).

  \param active if true, the widget reports mouse events
  \sa isActiveData()
  */
void KDChartWidget::setActiveData( bool active )
{
    _activeData = active;
}


/**
  Returns true if the widget is configured to report mouse
  events. The default is not to report mouse events.

  \return true if the widget is configured to report mouse events,
  false otherwise
  \sa setActiveData()
  */
bool KDChartWidget::isActiveData() const
{
    return _activeData;
}


/**
  If \a doublebuffered is true, the widget will double-buffer
  everything while drawing which reduces flicker a lot, but requires
  more memory as an off-screen buffer of the same size as the widget
  needs to be kept around. However, in most cases, it is worth
  spending the extra memory. Double-buffering is on by
  default. Turning double-buffering on or off does not trigger a
  repaint.

  \param doublebuffered if true, turns double-buffering on, if false,
  turns double-buffering off
  \sa isDoubleBuffered
  */
void KDChartWidget::setDoubleBuffered( bool doublebuffered )
{
    _doubleBuffered = doublebuffered;
    if( doublebuffered ) {
        // turn double-buffering on
        // resize the buffer to the size of the widget
        _buffer.resize( size() );
    } else {
        // turn double-buffering off
        // minimize the buffer so that it does not take any memory
        _buffer.resize( 0, 0 );
    }
}


/**
  Returns whether the widget uses double-buffering for drawing. See
  \a setDoubleBuffered() for an explanation of double-buffering.

  \return true if double-buffering is turned on, false otherwise
  */
bool KDChartWidget::isDoubleBuffered() const
{
    return _doubleBuffered;
}


/**
  Set an entire new parameter set.
  (Normally you might prefer modifying the existing parameters
  rather than specifying a new set.)
  */
void KDChartWidget::setParams( KDChartParams* params )
{
    _params = params;
}

/**
  Set an entire new data table.
  */
void KDChartWidget::setData( KDChartTableDataBase* data )
{
    _data = data;
}

/**
  Returns a pointer to the current parameter set.
  */
KDChartParams* KDChartWidget::params() const
{
    return _params;
}

/**
  Returns a pointer to the current data table.
  */
KDChartTableDataBase* KDChartWidget::data() const
{
    return _data;
}

/**
  \fn void KDChartWidget::barsDisplayed( int barsDisplayed, int barsLeft )

  This signal is emitted when drawing of a bar chart is done.
  Use it to determine if all bars have been drawn: in case
  you specified both the bar width and the value block gap width
  it might be that KD Chart is not able to display all bars since
  they do not fit into the available horizontal space.

  The value of barsLeft indicates how many bars could not be
  drawn because the data area was not wide enough.

  \sa KDChartParams::numBarsDisplayed, KDChartParams::numBarsLeft
  */

/**
  \fn void KDChartWidget::dataLeftClicked( uint row, uint col )

  This signal is emitted when a data item was clicked onto with the left mouse button.

  The values of row / col indicate the respective dataset (row) and item (col).

  \note There is another signal sent simultaneously: reporting the screen coordinates clicked onto.

  \sa dataLeftReleased
  \sa dataRightClicked, dataMiddleClicked
  */
/**
  \fn void KDChartWidget::dataRightClicked( uint row, uint col )

  This signal is emitted when a data item was clicked onto with the right mouse button.

  The values of row / col indicate the respective dataset (row) and item (col).

  \note There is another signal sent simultaneously: reporting the screen coordinates clicked onto.

  \sa dataRightReleased
  \sa dataLeftClicked, dataMiddleClicked
  */
/**
  \fn void KDChartWidget::dataMiddleClicked( uint row, uint col )

  This signal is emitted when a data item was clicked onto with the middle mouse button.

  The values of row / col indicate the respective dataset (row) and item (col).

  \note There is another signal sent simultaneously: reporting the screen coordinates clicked onto.

  \sa dataMiddleReleased
  \sa dataLeftClicked, dataRightClicked
  */

/**
  \fn void KDChartWidget::dataLeftClicked( const TQPoint & pnt )

  This signal is emitted when a data item was clicked onto with the left mouse button.

  The value of pnt indicates the screen coordinates in relation to the origin of the data area.

  \note There is another signal sent simultaneously: reporting which data item was clicked onto.

  \sa dataLeftReleased
  \sa dataRightClicked, dataMiddleClicked
  */
/**
  \fn void KDChartWidget::dataRightClicked( const TQPoint & pnt )

  This signal is emitted when a data item was clicked onto with the right mouse button.

  The values of row / col indicate the screen coordinates in relation to the origin of the data area.

  \note There is another signal sent simultaneously: reporting which data item was clicked onto.

  \sa dataRightReleased
  \sa dataLeftClicked, dataMiddleClicked
  */
/**
  \fn void KDChartWidget::dataMiddleClicked( const TQPoint & pnt )

  This signal is emitted when a data item was clicked onto with the middle mouse button.

  The values of row / col indicate the screen coordinates in relation to the origin of the data area.

  \note There is another signal sent simultaneously: reporting which data item was clicked onto.

  \sa dataMiddleReleased
  \sa dataLeftClicked, dataRightClicked
  */