Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

qwt_plot_grid.cpp

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 <qpainter.h>
00011 #include <qpen.h>
00012 #include "qwt_painter.h"
00013 #include "qwt_scale_map.h"
00014 #include "qwt_scale_div.h"
00015 #include "qwt_plot_grid.h"
00016 
00017 class QwtPlotGrid::PrivateData
00018 {
00019 public:
00020     PrivateData():
00021         xEnabled(true),
00022         yEnabled(true),
00023         xMinEnabled(false),
00024         yMinEnabled(false)
00025     {
00026     }
00027 
00028     bool xEnabled;
00029     bool yEnabled;
00030     bool xMinEnabled;
00031     bool yMinEnabled;
00032 
00033     QwtScaleDiv sdx;
00034     QwtScaleDiv sdy;
00035 
00036     QPen majPen;
00037     QPen minPen;
00038 };
00039 
00041 QwtPlotGrid::QwtPlotGrid()
00042 {
00043     d_data = new PrivateData;
00044     setZ(10.0);
00045 }
00046 
00048 QwtPlotGrid::~QwtPlotGrid()
00049 {
00050     delete d_data;
00051 }
00052 
00053 int QwtPlotGrid::rtti() const
00054 {
00055     return QwtPlotItem::Rtti_PlotGrid;
00056 }
00057 
00065 void QwtPlotGrid::enableX(bool tf)
00066 {
00067     if ( d_data->xEnabled != tf )
00068     {
00069         d_data->xEnabled = tf;
00070         gridChanged();
00071     }
00072 }
00073 
00079 void QwtPlotGrid::enableY(bool tf)
00080 {
00081     if ( d_data->yEnabled != tf )
00082     {
00083         d_data->yEnabled = tf;  
00084         gridChanged();
00085     }
00086 }
00087 
00093 void QwtPlotGrid::enableXMin(bool tf)
00094 {
00095     if ( d_data->xMinEnabled != tf )
00096     {
00097         d_data->xMinEnabled = tf;
00098         gridChanged();
00099     }
00100 }
00101 
00107 void QwtPlotGrid::enableYMin(bool tf)
00108 {
00109     if ( d_data->yMinEnabled != tf )
00110     {
00111         d_data->yMinEnabled = tf;
00112         gridChanged();
00113     }
00114 }
00115 
00123 void QwtPlotGrid::setXDiv(const QwtScaleDiv &sx)
00124 {
00125     if ( d_data->sdx != sx )
00126     {
00127         d_data->sdx = sx;
00128         gridChanged();
00129     }
00130 }
00131 
00139 void QwtPlotGrid::setYDiv(const QwtScaleDiv &sy)
00140 {
00141     if ( d_data->sdy != sy )
00142     {
00143         d_data->sdy = sy;    
00144         gridChanged();
00145     }
00146 }
00147 
00152 void QwtPlotGrid::setPen(const QPen &p)
00153 {
00154     if ( d_data->majPen != p || d_data->minPen != p )
00155     {
00156         d_data->majPen = p;
00157         d_data->minPen = p;
00158         gridChanged();
00159     }
00160 }
00161 
00166 void QwtPlotGrid::setMajPen(const QPen &p)
00167 {
00168     if ( d_data->majPen != p )
00169     {
00170         d_data->majPen = p;
00171         gridChanged();
00172     }
00173 }
00174 
00179 void QwtPlotGrid::setMinPen(const QPen &p)
00180 {
00181     if ( d_data->minPen != p )
00182     {
00183         d_data->minPen = p;  
00184         gridChanged();
00185     }
00186 }
00187 
00199 void QwtPlotGrid::draw(QPainter *painter, 
00200     const QwtScaleMap &mx, const QwtScaleMap &my,
00201     const QRect &r) const
00202 {
00203     //  draw minor gridlines
00204     painter->setPen(d_data->minPen);
00205     
00206     if (d_data->xEnabled && d_data->xMinEnabled)
00207     {
00208         drawLines(painter, r, Qt::Vertical, mx, 
00209             d_data->sdx.ticks(QwtScaleDiv::MinorTick));
00210         drawLines(painter, r, Qt::Vertical, mx, 
00211             d_data->sdx.ticks(QwtScaleDiv::MediumTick));
00212     }
00213 
00214     if (d_data->yEnabled && d_data->yMinEnabled)
00215     {
00216         drawLines(painter, r, Qt::Horizontal, my, 
00217             d_data->sdy.ticks(QwtScaleDiv::MinorTick));
00218         drawLines(painter, r, Qt::Horizontal, my, 
00219             d_data->sdy.ticks(QwtScaleDiv::MediumTick));
00220     }
00221 
00222     //  draw major gridlines
00223     painter->setPen(d_data->majPen);
00224     
00225     if (d_data->xEnabled)
00226     {
00227         drawLines(painter, r, Qt::Vertical, mx,
00228             d_data->sdx.ticks(QwtScaleDiv::MajorTick));
00229     }
00230 
00231     if (d_data->yEnabled)
00232     {
00233         drawLines(painter, r, Qt::Horizontal, my,
00234             d_data->sdy.ticks(QwtScaleDiv::MajorTick));
00235     }
00236 }
00237 
00238 void QwtPlotGrid::drawLines(QPainter *painter, const QRect &rect,
00239     Qt::Orientation orientation, const QwtScaleMap &map, 
00240     const QwtTickList &values) const
00241 {
00242     const int x1 = rect.left();
00243     const int x2 = rect.right();
00244     const int y1 = rect.top();
00245     const int y2 = rect.bottom();
00246 
00247     for (uint i = 0; i < (uint)values.count(); i++)
00248     {
00249         const int value = map.transform(values[i]);
00250         if ( orientation == Qt::Horizontal )
00251         {
00252             if ((value >= y1) && (value <= y2))
00253                 QwtPainter::drawLine(painter, x1, value, x2, value);
00254         }
00255         else
00256         {
00257             if ((value >= x1) && (value <= x2))
00258                 QwtPainter::drawLine(painter, value, y1, value, y2);
00259         }
00260     }
00261 }
00262 
00267 const QPen &QwtPlotGrid::majPen() const 
00268 { 
00269     return d_data->majPen; 
00270 }
00271 
00276 const QPen &QwtPlotGrid::minPen() const 
00277 { 
00278     return d_data->minPen; 
00279 }
00280   
00285 bool QwtPlotGrid::xEnabled() const
00286 { 
00287     return d_data->xEnabled; 
00288 }
00289 
00294 bool QwtPlotGrid::xMinEnabled() const 
00295 { 
00296     return d_data->xMinEnabled; 
00297 }
00298 
00303 bool QwtPlotGrid::yEnabled() const 
00304 { 
00305     return d_data->yEnabled; 
00306 }
00307 
00312 bool QwtPlotGrid::yMinEnabled() const 
00313 {
00314     return d_data->yMinEnabled; 
00315 }
00316 
00317   
00319 const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const 
00320 { 
00321     return d_data->sdx; 
00322 }
00323 
00325 const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const 
00326 { 
00327     return d_data->sdy; 
00328 }
00329  
00330 void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
00331     const QwtScaleDiv& yDiv)
00332 {
00333     setXDiv(xDiv);
00334     setYDiv(yDiv);
00335 }
00336 
00344 void QwtPlotGrid::gridChanged() 
00345 {
00346     itemChanged();
00347 }
00348 

Generated on Wed Aug 31 23:02:29 2005 for Qwt User's Guide by  doxygen 1.4.1