kchart

KDChartWidget.cpp

00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003    */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KDChart library.
00009  **
00010  ** This file may be distributed and/or modified under the terms of the
00011  ** GNU General Public License version 2 as published by the Free Software
00012  ** Foundation and appearing in the file LICENSE.GPL included in the
00013  ** packaging of this file.
00014  **
00015  ** Licensees holding valid commercial KDChart licenses may use this file in
00016  ** accordance with the KDChart Commercial License Agreement provided with
00017  ** the Software.
00018  **
00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021  **
00022  ** See http://www.klaralvdalens-datakonsult.se/?page=products for
00023  **   information about KDChart Commercial License Agreements.
00024  **
00025  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00026  ** licensing are not clear to you.
00027  **
00028  **********************************************************************/
00029 #include <KDChartWidget.h>
00030 #include <KDChart.h>
00031 #include <KDChartParams.h>
00032 #include <KDChartTableBase.h>
00033 #ifndef KDCHART_MASTER_CVS
00034 #include "KDChartWidget.moc"
00035 #endif
00036 
00037 #include <qpainter.h>
00038 
00072 KDChartWidget::KDChartWidget( QWidget* parent, const char* name ) :
00073 QWidget( parent, name ),
00074     _params( 0 ),
00075     _data( 0 ),
00076     _activeData( false ),
00077 _mousePressedOnRegion( 0 )
00078 {
00079     _dataRegions.setAutoDelete( true );
00080     setDoubleBuffered( true );
00081     setBackgroundMode( Qt::NoBackground );
00082 }
00083 
00084 
00094 KDChartWidget::KDChartWidget( KDChartParams* params,
00095         KDChartTableDataBase* data,
00096         QWidget* parent, const char* name ) :
00097 QWidget( parent, name ),
00098     _params( params ),
00099     _data( data ),
00100     _activeData( false ),
00101 _mousePressedOnRegion( 0 )
00102 {
00103     _dataRegions.setAutoDelete( true );
00104     setDoubleBuffered( true );
00105     setBackgroundMode( Qt::NoBackground );
00106 }
00107 
00108 
00112 KDChartWidget::~KDChartWidget()
00113 {
00114     // delete any regions that might still be registered
00115     _dataRegions.clear();
00116     KDChartAutoColor::freeInstance();
00117 }
00118 
00119 void KDChartWidget::paintTo( QPainter& painter,
00120         const QRect* rect )
00121 {
00122     KDChart::paint( &painter, _params, _data, &_dataRegions, rect );
00123 }
00124 
00125 void KDChartWidget::print( QPainter& painter,
00126         const QRect* rect )
00127 {
00128     bool oldOpt=true;
00129     if( _params ){
00130         oldOpt = _params->optimizeOutputForScreen();
00131         _params->setOptimizeOutputForScreen( false );
00132     }
00133     bool bOldBuf = _doubleBuffered;
00134     _doubleBuffered = false;
00135     paintTo( painter, rect );
00136     _doubleBuffered = bOldBuf;
00137     if( _params )
00138         _params->setOptimizeOutputForScreen( oldOpt );
00139 }
00140 
00141 void KDChartWidget::paintEvent( QPaintEvent* event )
00142 {
00143     if( _doubleBuffered ) {
00144         // if double-buffering, paint onto the pixmap and copy
00145         // afterwards
00146         _buffer.fill( backgroundColor() );
00147         QPainter painter( &_buffer );
00148         paintTo( painter );
00149         bitBlt( this, event->rect().topLeft(), &_buffer, event->rect() );
00150     } else {
00151         // if not double-buffering, paint directly into the window
00152         QPainter painter( this );
00153         paintTo( painter );
00154     }
00155 }
00156 
00157 
00161 void KDChartWidget::mousePressEvent( QMouseEvent* event )
00162 {
00163     if ( !_activeData )
00164         return ;
00165 
00166     _mousePressedOnRegion = 0;
00167     KDChartDataRegion* current = 0;
00168     //QPtrListIterator < KDChartDataRegion > it( _dataRegions );
00169     for( current = _dataRegions.last(); current; current = _dataRegions.prev() ){
00170     //while ( ( current = it.current() ) ) {
00171         if ( current->region().contains( event->pos() ) ) {
00172             _mousePressedOnRegion = current;
00173             if ( event->button() == LeftButton ){
00174                 emit dataLeftPressed( current->row, current->col );
00175                 emit dataLeftPressed( event->pos() );
00176             }else if ( event->button() == MidButton ){
00177                 emit dataMiddlePressed( current->row, current->col );
00178                 emit dataMiddlePressed( event->pos() );
00179             }else{
00180                 emit dataRightPressed( current->row, current->col );
00181                 emit dataRightPressed( event->pos() );
00182             }
00183             return;
00184         }
00185     }
00186 }
00187 
00188 
00192 void KDChartWidget::mouseReleaseEvent( QMouseEvent* event )
00193 {
00194     if ( !_activeData )
00195         return ;
00196 
00197     KDChartDataRegion* current = 0;
00198     QPtrListIterator < KDChartDataRegion > it( _dataRegions );
00199     while ( ( current = it.current() ) ) {
00200         ++it;
00201         if ( current->region().contains( event->pos() ) ) {
00202             if ( event->button() == LeftButton ) {
00203                 emit dataLeftReleased( current->row, current->col );
00204                 emit dataLeftReleased( event->pos() );
00205                 if ( _mousePressedOnRegion == current ){
00206                     emit dataLeftClicked( current->row, current->col );
00207                     emit dataLeftClicked( event->pos() );
00208                 }
00209             } else if ( event->button() == MidButton ) {
00210                 emit dataMiddleReleased( current->row, current->col );
00211                 emit dataMiddleReleased( event->pos() );
00212                 if ( _mousePressedOnRegion == current ){
00213                     emit dataMiddleClicked( current->row, current->col );
00214                     emit dataMiddleClicked( event->pos() );
00215                 }
00216             } else {
00217                 emit dataRightReleased( current->row, current->col );
00218                 emit dataRightReleased( event->pos() );
00219                 if ( _mousePressedOnRegion == current ){
00220                     emit dataRightClicked( current->row, current->col );
00221                     emit dataRightClicked( event->pos() );
00222                 }
00223             }
00224         }
00225     }
00226 }
00227 
00228 
00232 void KDChartWidget::resizeEvent( QResizeEvent* /*event*/ )
00233 {
00234     // if we use double-buffering, resize the buffer to the new size,
00235     // otherwise leave it alone
00236     if( _doubleBuffered )
00237         _buffer.resize( size() );
00238 }
00239 
00240 
00257 void KDChartWidget::setActiveData( bool active )
00258 {
00259     _activeData = active;
00260 }
00261 
00262 
00271 bool KDChartWidget::isActiveData() const
00272 {
00273     return _activeData;
00274 }
00275 
00276 
00290 void KDChartWidget::setDoubleBuffered( bool doublebuffered )
00291 {
00292     _doubleBuffered = doublebuffered;
00293     if( doublebuffered ) {
00294         // turn double-buffering on
00295         // resize the buffer to the size of the widget
00296         _buffer.resize( size() );
00297     } else {
00298         // turn double-buffering off
00299         // minimize the buffer so that it does not take any memory
00300         _buffer.resize( 0, 0 );
00301     }
00302 }
00303 
00304 
00311 bool KDChartWidget::isDoubleBuffered() const
00312 {
00313     return _doubleBuffered;
00314 }
00315 
00316 
00322 void KDChartWidget::setParams( KDChartParams* params )
00323 {
00324     _params = params;
00325 }
00326 
00330 void KDChartWidget::setData( KDChartTableDataBase* data )
00331 {
00332     _data = data;
00333 }
00334 
00338 KDChartParams* KDChartWidget::params() const
00339 {
00340     return _params;
00341 }
00342 
00346 KDChartTableDataBase* KDChartWidget::data() const
00347 {
00348     return _data;
00349 }
00350 
KDE Home | KDE Accessibility Home | Description of Access Keys