qwt_plot_panner.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_plot_panner.h"
00011 #include "qwt_scale_div.h"
00012 #include "qwt_plot.h"
00013 #include "qwt_painter.h"
00014 #include <qbitmap.h>
00015 #include <qstyle.h>
00016 #include <qstyleoption.h>
00017 
00018 static QBitmap qwtBorderMask( const QWidget *canvas, const QSize &size )
00019 {
00020     const QRect r( 0, 0, size.width(), size.height() );
00021 
00022     QPainterPath borderPath;
00023 
00024     ( void )QMetaObject::invokeMethod( 
00025         const_cast< QWidget *>( canvas ), "borderPath", Qt::DirectConnection,
00026         Q_RETURN_ARG( QPainterPath, borderPath ), Q_ARG( QRect, r ) );
00027 
00028     if ( borderPath.isEmpty() )
00029     {
00030         if ( canvas->contentsRect() == canvas->rect() )
00031             return QBitmap();
00032 
00033         QBitmap mask( size );
00034         mask.fill( Qt::color0 );
00035 
00036         QPainter painter( &mask );
00037         painter.fillRect( canvas->contentsRect(), Qt::color1 );
00038 
00039         return mask;
00040     }
00041 
00042     QImage image( size, QImage::Format_ARGB32_Premultiplied );
00043     image.fill( Qt::color0 );
00044 
00045     QPainter painter( &image );
00046     painter.setClipPath( borderPath );
00047     painter.fillRect( r, Qt::color1 );
00048 
00049     // now erase the frame
00050 
00051     painter.setCompositionMode( QPainter::CompositionMode_DestinationOut );
00052 
00053     if ( canvas->testAttribute(Qt::WA_StyledBackground ) )
00054     {
00055         QStyleOptionFrame opt;
00056         opt.initFrom(canvas);
00057         opt.rect = r;
00058         canvas->style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter, canvas );
00059     }
00060     else
00061     {
00062         const QVariant borderRadius = canvas->property( "borderRadius" );
00063         const QVariant frameWidth = canvas->property( "frameWidth" );
00064 
00065         if ( borderRadius.type() == QVariant::Double 
00066             && frameWidth.type() == QVariant::Int )
00067         {
00068             const double br = borderRadius.toDouble();
00069             const int fw = frameWidth.toInt();
00070         
00071             if ( br > 0.0 && fw > 0 )
00072             {
00073                 painter.setPen( QPen( Qt::color1, fw ) );
00074                 painter.setBrush( Qt::NoBrush );
00075                 painter.setRenderHint( QPainter::Antialiasing, true );
00076 
00077                 painter.drawPath( borderPath );
00078             }
00079         }
00080     }
00081 
00082     painter.end();
00083 
00084     const QImage mask = image.createMaskFromColor(
00085         QColor( Qt::color1 ).rgb(), Qt::MaskOutColor );
00086 
00087     return QBitmap::fromImage( mask );
00088 }
00089 
00090 class QwtPlotPanner::PrivateData
00091 {
00092 public:
00093     PrivateData()
00094     {
00095         for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00096             isAxisEnabled[axis] = true;
00097     }
00098 
00099     bool isAxisEnabled[QwtPlot::axisCnt];
00100 };
00101 
00111 QwtPlotPanner::QwtPlotPanner( QWidget *canvas ):
00112     QwtPanner( canvas )
00113 {
00114     d_data = new PrivateData();
00115 
00116     connect( this, SIGNAL( panned( int, int ) ),
00117         SLOT( moveCanvas( int, int ) ) );
00118 }
00119 
00121 QwtPlotPanner::~QwtPlotPanner()
00122 {
00123     delete d_data;
00124 }
00125 
00137 void QwtPlotPanner::setAxisEnabled( int axis, bool on )
00138 {
00139     if ( axis >= 0 && axis < QwtPlot::axisCnt )
00140         d_data->isAxisEnabled[axis] = on;
00141 }
00142 
00151 bool QwtPlotPanner::isAxisEnabled( int axis ) const
00152 {
00153     if ( axis >= 0 && axis < QwtPlot::axisCnt )
00154         return d_data->isAxisEnabled[axis];
00155 
00156     return true;
00157 }
00158 
00160 QWidget *QwtPlotPanner::canvas()
00161 {
00162     return parentWidget();
00163 }
00164 
00166 const QWidget *QwtPlotPanner::canvas() const
00167 {
00168     return parentWidget();
00169 }
00170 
00172 QwtPlot *QwtPlotPanner::plot()
00173 {
00174     QWidget *w = canvas();
00175     if ( w )
00176         w = w->parentWidget();
00177 
00178     return qobject_cast<QwtPlot *>( w );
00179 }
00180 
00182 const QwtPlot *QwtPlotPanner::plot() const
00183 {
00184     const QWidget *w = canvas();
00185     if ( w )
00186         w = w->parentWidget();
00187 
00188     return qobject_cast<const QwtPlot *>( w );
00189 }
00190 
00199 void QwtPlotPanner::moveCanvas( int dx, int dy )
00200 {
00201     if ( dx == 0 && dy == 0 )
00202         return;
00203 
00204     QwtPlot *plot = this->plot();
00205     if ( plot == NULL )
00206         return;
00207 
00208     const bool doAutoReplot = plot->autoReplot();
00209     plot->setAutoReplot( false );
00210 
00211     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00212     {
00213         if ( !d_data->isAxisEnabled[axis] )
00214             continue;
00215 
00216         const QwtScaleMap map = plot->canvasMap( axis );
00217 
00218         const double p1 = map.transform( plot->axisScaleDiv( axis ).lowerBound() );
00219         const double p2 = map.transform( plot->axisScaleDiv( axis ).upperBound() );
00220 
00221         double d1, d2;
00222         if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
00223         {
00224             d1 = map.invTransform( p1 - dx );
00225             d2 = map.invTransform( p2 - dx );
00226         }
00227         else
00228         {
00229             d1 = map.invTransform( p1 - dy );
00230             d2 = map.invTransform( p2 - dy );
00231         }
00232 
00233         plot->setAxisScale( axis, d1, d2 );
00234     }
00235 
00236     plot->setAutoReplot( doAutoReplot );
00237     plot->replot();
00238 }
00239 
00246 QBitmap QwtPlotPanner::contentsMask() const
00247 {
00248     if ( canvas() )
00249         return qwtBorderMask( canvas(), size() );
00250 
00251     return QwtPanner::contentsMask();
00252 }
00253 
00257 QPixmap QwtPlotPanner::grab() const
00258 {   
00259     const QWidget *cv = canvas();
00260     if ( cv && cv->inherits( "QGLWidget" ) )
00261     {
00262         // we can't grab from a QGLWidget
00263 
00264         QPixmap pm( cv->size() );
00265         QwtPainter::fillPixmap( cv, pm );
00266 
00267         QPainter painter( &pm );
00268         const_cast<QwtPlot *>( plot() )->drawCanvas( &painter );
00269 
00270         return pm;
00271     }
00272 
00273     return QwtPanner::grab();
00274 }   
00275 


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56