Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_plot_glcanvas.h"
00011 #include "qwt_plot.h"
00012 #include "qwt_painter.h"
00013 #include <qevent.h>
00014 #include <qpainter.h>
00015 #include <qdrawutil.h>
00016 #include <qstyle.h>
00017 #include <qstyleoption.h>
00018
00019 #define FIX_GL_TRANSLATION 0
00020
00021 static QWidget *qwtBGWidget( QWidget *widget )
00022 {
00023 QWidget *w = widget;
00024
00025 for ( ; w->parentWidget() != NULL; w = w->parentWidget() )
00026 {
00027 if ( w->autoFillBackground() ||
00028 w->testAttribute( Qt::WA_StyledBackground ) )
00029 {
00030 return w;
00031 }
00032 }
00033
00034 return w;
00035 }
00036
00037 static void qwtUpdateContentsRect( QwtPlotGLCanvas *canvas )
00038 {
00039 const int fw = canvas->frameWidth();
00040 canvas->setContentsMargins( fw, fw, fw, fw );
00041 }
00042
00043 class QwtPlotGLCanvas::PrivateData
00044 {
00045 public:
00046 PrivateData():
00047 frameStyle( QFrame::Panel | QFrame::Sunken),
00048 lineWidth( 2 ),
00049 midLineWidth( 0 )
00050 {
00051 }
00052
00053 int frameStyle;
00054 int lineWidth;
00055 int midLineWidth;
00056 };
00057
00058 class QwtPlotGLCanvasFormat: public QGLFormat
00059 {
00060 public:
00061 QwtPlotGLCanvasFormat():
00062 QGLFormat( QGLFormat::defaultFormat() )
00063 {
00064 setSampleBuffers( true );
00065 }
00066 };
00067
00074 QwtPlotGLCanvas::QwtPlotGLCanvas( QwtPlot *plot ):
00075 QGLWidget( QwtPlotGLCanvasFormat(), plot )
00076 {
00077 d_data = new PrivateData;
00078
00079 #ifndef QT_NO_CURSOR
00080 setCursor( Qt::CrossCursor );
00081 #endif
00082
00083 setAutoFillBackground( true );
00084 qwtUpdateContentsRect( this );
00085 }
00086
00088 QwtPlotGLCanvas::~QwtPlotGLCanvas()
00089 {
00090 delete d_data;
00091 }
00092
00101 void QwtPlotGLCanvas::setFrameStyle( int style )
00102 {
00103 if ( style != d_data->frameStyle )
00104 {
00105 d_data->frameStyle = style;
00106 qwtUpdateContentsRect( this );
00107
00108 update();
00109 }
00110 }
00111
00116 int QwtPlotGLCanvas::frameStyle() const
00117 {
00118 return d_data->frameStyle;
00119 }
00120
00127 void QwtPlotGLCanvas::setFrameShadow( Shadow shadow )
00128 {
00129 setFrameStyle(( d_data->frameStyle & QFrame::Shape_Mask ) | shadow );
00130 }
00131
00136 QwtPlotGLCanvas::Shadow QwtPlotGLCanvas::frameShadow() const
00137 {
00138 return (Shadow) ( d_data->frameStyle & QFrame::Shadow_Mask );
00139 }
00140
00147 void QwtPlotGLCanvas::setFrameShape( Shape shape )
00148 {
00149 setFrameStyle( ( d_data->frameStyle & QFrame::Shadow_Mask ) | shape );
00150 }
00151
00156 QwtPlotGLCanvas::Shape QwtPlotGLCanvas::frameShape() const
00157 {
00158 return (Shape) ( d_data->frameStyle & QFrame::Shape_Mask );
00159 }
00160
00169 void QwtPlotGLCanvas::setLineWidth( int width )
00170 {
00171 width = qMax( width, 0 );
00172 if ( width != d_data->lineWidth )
00173 {
00174 d_data->lineWidth = qMax( width, 0 );
00175 qwtUpdateContentsRect( this );
00176 update();
00177 }
00178 }
00179
00184 int QwtPlotGLCanvas::lineWidth() const
00185 {
00186 return d_data->lineWidth;
00187 }
00188
00197 void QwtPlotGLCanvas::setMidLineWidth( int width )
00198 {
00199 width = qMax( width, 0 );
00200 if ( width != d_data->midLineWidth )
00201 {
00202 d_data->midLineWidth = width;
00203 qwtUpdateContentsRect( this );
00204 update();
00205 }
00206 }
00207
00212 int QwtPlotGLCanvas::midLineWidth() const
00213 {
00214 return d_data->midLineWidth;
00215 }
00216
00220 int QwtPlotGLCanvas::frameWidth() const
00221 {
00222 return ( frameStyle() != NoFrame ) ? d_data->lineWidth : 0;
00223 }
00224
00231 void QwtPlotGLCanvas::paintEvent( QPaintEvent *event )
00232 {
00233 Q_UNUSED( event );
00234
00235 QPainter painter( this );
00236
00237 #if FIX_GL_TRANSLATION
00238 if ( painter.paintEngine()->type() == QPaintEngine::OpenGL2 )
00239 {
00240
00241 painter.translate( 1, 1 );
00242 }
00243 #endif
00244
00245 drawBackground( &painter );
00246 drawItems( &painter );
00247
00248 if ( !testAttribute( Qt::WA_StyledBackground ) )
00249 {
00250 if ( frameWidth() > 0 )
00251 drawBorder( &painter );
00252 }
00253 }
00259 bool QwtPlotGLCanvas::event( QEvent *event )
00260 {
00261 const bool ok = QGLWidget::event( event );
00262
00263 if ( event->type() == QEvent::PolishRequest ||
00264 event->type() == QEvent::StyleChange )
00265 {
00266
00267
00268
00269 setAttribute( Qt::WA_StyledBackground,
00270 testAttribute( Qt::WA_StyleSheet ) );
00271 }
00272
00273 return ok;
00274 }
00275
00282 void QwtPlotGLCanvas::drawItems( QPainter *painter )
00283 {
00284 painter->save();
00285
00286 painter->setClipRect( contentsRect(), Qt::IntersectClip );
00287
00288 QwtPlot *plot = qobject_cast< QwtPlot *>( parent() );
00289 if ( plot )
00290 plot->drawCanvas( painter );
00291
00292 painter->restore();
00293 }
00294
00299 void QwtPlotGLCanvas::drawBackground( QPainter *painter )
00300 {
00301 painter->save();
00302
00303 QWidget *w = qwtBGWidget( this );
00304
00305 const QPoint off = mapTo( w, QPoint() );
00306 painter->translate( -off );
00307
00308 const QRect fillRect = rect().translated( off );
00309
00310 if ( w->testAttribute( Qt::WA_StyledBackground ) )
00311 {
00312 painter->setClipRect( fillRect );
00313
00314 QStyleOption opt;
00315 opt.initFrom( w );
00316 w->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, w);
00317 }
00318 else
00319 {
00320 painter->fillRect( fillRect,
00321 w->palette().brush( w->backgroundRole() ) );
00322 }
00323
00324 painter->restore();
00325 }
00326
00331 void QwtPlotGLCanvas::drawBorder( QPainter *painter )
00332 {
00333 const int fw = frameWidth();
00334 if ( fw <= 0 )
00335 return;
00336
00337 if ( frameShadow() == QwtPlotGLCanvas::Plain )
00338 {
00339 qDrawPlainRect( painter, frameRect(),
00340 palette().shadow().color(), lineWidth() );
00341 }
00342 else
00343 {
00344 if ( frameShape() == QwtPlotGLCanvas::Box )
00345 {
00346 qDrawShadeRect( painter, frameRect(), palette(),
00347 frameShadow() == Sunken, lineWidth(), midLineWidth() );
00348 }
00349 else
00350 {
00351 qDrawShadePanel( painter, frameRect(), palette(),
00352 frameShadow() == Sunken, lineWidth() );
00353 }
00354 }
00355 }
00356
00358 void QwtPlotGLCanvas::replot()
00359 {
00360 repaint();
00361 }
00362
00366 QPainterPath QwtPlotGLCanvas::borderPath( const QRect &rect ) const
00367 {
00368 Q_UNUSED( rect );
00369 return QPainterPath();
00370 }
00371
00373 QRect QwtPlotGLCanvas::frameRect() const
00374 {
00375 const int fw = frameWidth();
00376 return contentsRect().adjusted( -fw, -fw, fw, fw );
00377 }