kchart

KDChartWidget.cpp
1/*
2 KDChart - a multi-platform charting engine
3 */
4
5/****************************************************************************
6 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB. All rights reserved.
7 **
8 ** This file is part of the KDChart library.
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.GPL included in the
13 ** packaging of this file.
14 **
15 ** Licensees holding valid commercial KDChart licenses may use this file in
16 ** accordance with the KDChart Commercial License Agreement provided with
17 ** the Software.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 **
22 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
23 ** information about KDChart Commercial License Agreements.
24 **
25 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
26 ** licensing are not clear to you.
27 **
28 **********************************************************************/
29#include <KDChartWidget.h>
30#include <KDChart.h>
31#include <KDChartParams.h>
32#include <KDChartTableBase.h>
33#ifndef KDCHART_MASTER_CVS
34#include "KDChartWidget.moc"
35#endif
36
37#include <tqpainter.h>
38
59
60
71
72KDChartWidget::KDChartWidget( TQWidget* parent, const char* name ) :
73TQWidget( parent, name ),
74 _params( 0 ),
75 _data( 0 ),
76 _activeData( false ),
77_mousePressedOnRegion( 0 )
78{
79 _dataRegions.setAutoDelete( true );
80 setDoubleBuffered( true );
81 setBackgroundMode( TQt::NoBackground );
82}
83
84
93
96 TQWidget* parent, const char* name ) :
97TQWidget( parent, name ),
98 _params( params ),
99 _data( data ),
100 _activeData( false ),
101_mousePressedOnRegion( 0 )
102{
103 _dataRegions.setAutoDelete( true );
104 setDoubleBuffered( true );
105 setBackgroundMode( TQt::NoBackground );
106}
107
108
113{
114 // delete any regions that might still be registered
115 _dataRegions.clear();
116 KDChartAutoColor::freeInstance();
117}
118
119void KDChartWidget::paintTo( TQPainter& painter,
120 const TQRect* rect )
121{
122 KDChart::paint( &painter, _params, _data, &_dataRegions, rect );
123}
124
125void KDChartWidget::print( TQPainter& painter,
126 const TQRect* rect )
127{
128 bool oldOpt=true;
129 if( _params ){
130 oldOpt = _params->optimizeOutputForScreen();
131 _params->setOptimizeOutputForScreen( false );
132 }
133 bool bOldBuf = _doubleBuffered;
134 _doubleBuffered = false;
135 paintTo( painter, rect );
136 _doubleBuffered = bOldBuf;
137 if( _params )
138 _params->setOptimizeOutputForScreen( oldOpt );
139}
140
141void KDChartWidget::paintEvent( TQPaintEvent* event )
142{
143 if( _doubleBuffered ) {
144 // if double-buffering, paint onto the pixmap and copy
145 // afterwards
146 _buffer.fill( backgroundColor() );
147 TQPainter painter( &_buffer );
148 paintTo( painter );
149 bitBlt( this, event->rect().topLeft(), &_buffer, event->rect() );
150 } else {
151 // if not double-buffering, paint directly into the window
152 TQPainter painter( this );
153 paintTo( painter );
154 }
155}
156
157
161void KDChartWidget::mousePressEvent( TQMouseEvent* event )
162{
163 if ( !_activeData )
164 return ;
165
166 _mousePressedOnRegion = 0;
167 KDChartDataRegion* current = 0;
168 //TQPtrListIterator < KDChartDataRegion > it( _dataRegions );
169 for( current = _dataRegions.last(); current; current = _dataRegions.prev() ){
170 //while ( ( current = it.current() ) ) {
171 if ( current->contains( event->pos() ) ) {
172 _mousePressedOnRegion = current;
173 if ( event->button() == TQt::LeftButton ){
174 emit dataLeftPressed( current->row, current->col );
175 emit dataLeftPressed( event->pos() );
176 }else if ( event->button() == TQt::MidButton ){
177 emit dataMiddlePressed( current->row, current->col );
178 emit dataMiddlePressed( event->pos() );
179 }else{
180 emit dataRightPressed( current->row, current->col );
181 emit dataRightPressed( event->pos() );
182 }
183 return;
184 }
185 }
186}
187
188
192void KDChartWidget::mouseReleaseEvent( TQMouseEvent* event )
193{
194 if ( !_activeData )
195 return ;
196
197 KDChartDataRegion* current = 0;
198 TQPtrListIterator < KDChartDataRegion > it( _dataRegions );
199 while ( ( current = it.current() ) ) {
200 ++it;
201 if ( current->contains( event->pos() ) ) {
202 if ( event->button() == TQt::LeftButton ) {
203 emit dataLeftReleased( current->row, current->col );
204 emit dataLeftReleased( event->pos() );
205 if ( _mousePressedOnRegion == current ){
206 emit dataLeftClicked( current->row, current->col );
207 emit dataLeftClicked( event->pos() );
208 }
209 } else if ( event->button() == TQt::MidButton ) {
210 emit dataMiddleReleased( current->row, current->col );
211 emit dataMiddleReleased( event->pos() );
212 if ( _mousePressedOnRegion == current ){
213 emit dataMiddleClicked( current->row, current->col );
214 emit dataMiddleClicked( event->pos() );
215 }
216 } else {
217 emit dataRightReleased( current->row, current->col );
218 emit dataRightReleased( event->pos() );
219 if ( _mousePressedOnRegion == current ){
220 emit dataRightClicked( current->row, current->col );
221 emit dataRightClicked( event->pos() );
222 }
223 }
224 }
225 }
226}
227
228
232void KDChartWidget::resizeEvent( TQResizeEvent* /*event*/ )
233{
234 // if we use double-buffering, resize the buffer to the new size,
235 // otherwise leave it alone
236 if( _doubleBuffered )
237 _buffer.resize( size() );
238}
239
240
258{
259 _activeData = active;
260}
261
262
272{
273 return _activeData;
274}
275
276
290void KDChartWidget::setDoubleBuffered( bool doublebuffered )
291{
292 _doubleBuffered = doublebuffered;
293 if( doublebuffered ) {
294 // turn double-buffering on
295 // resize the buffer to the size of the widget
296 _buffer.resize( size() );
297 } else {
298 // turn double-buffering off
299 // minimize the buffer so that it does not take any memory
300 _buffer.resize( 0, 0 );
301 }
302}
303
304
312{
313 return _doubleBuffered;
314}
315
316
323{
324 _params = params;
325}
326
331{
332 _data = data;
333}
334
339{
340 return _params;
341}
342
347{
348 return _data;
349}
350
365
402
Header for all common chart parameters.
Provides a table class holding all data values that are to be used in a chart.
Provids the entry point into the charting that most people will want to use.
Definition of a single entry-point to the charting engine for applications that wish to provide their...
Bundles the display parameters of a chart.
KD Chart's build-in table data for an easy way of storing data values.
void dataMiddleClicked(uint row, uint col)
This signal is emitted when a data item was clicked onto with the middle mouse button.
KDChartWidget(TQWidget *parent=0, const char *name=0)
Default Constructor.
void dataLeftClicked(uint row, uint col)
This signal is emitted when a data item was clicked onto with the left mouse button.
bool isDoubleBuffered() const
Returns whether the widget uses double-buffering for drawing.
KDChartTableDataBase * data() const
Returns a pointer to the current data table.
~KDChartWidget()
Destructor.
void dataRightClicked(uint row, uint col)
This signal is emitted when a data item was clicked onto with the right mouse button.
void setData(KDChartTableDataBase *data)
Set an entire new data table.
void setParams(KDChartParams *params)
Set an entire new parameter set.
void setActiveData(bool active)
If active is true, this widget reports mouse presses, releases and clicks on the data segments it dis...
void setDoubleBuffered(bool doublebuffered)
If doublebuffered is true, the widget will double-buffer everything while drawing which reduces flick...
KDChartParams * params() const
Returns a pointer to the current parameter set.
bool isActiveData() const
Returns true if the widget is configured to report mouse events.
static void paint(TQPainter *, KDChartParams *, KDChartTableDataBase *, KDChartDataRegionList *regions=0, const TQRect *rect=0, bool mustCalculateGeometry=true)
Paints a chart with the specified parameters on the specified painter.
Definition KDChart.cpp:256