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 
00200 void QwtPlotGrid::draw(QPainter *painter, 
00201     const QwtScaleMap &mx, const QwtScaleMap &my,
00202     const QRect &r) const
00203 {
00204     //  draw minor gridlines
00205     painter->setPen(d_data->minPen);
00206     
00207     if (d_data->xEnabled && d_data->xMinEnabled)
00208     {
00209         drawLines(painter, r, Qt::Vertical, mx, 
00210             d_data->sdx.ticks(QwtScaleDiv::MinorTick));
00211         drawLines(painter, r, Qt::Vertical, mx, 
00212             d_data->sdx.ticks(QwtScaleDiv::MediumTick));
00213     }
00214 
00215     if (d_data->yEnabled && d_data->yMinEnabled)
00216     {
00217         drawLines(painter, r, Qt::Horizontal, my, 
00218             d_data->sdy.ticks(QwtScaleDiv::MinorTick));
00219         drawLines(painter, r, Qt::Horizontal, my, 
00220             d_data->sdy.ticks(QwtScaleDiv::MediumTick));
00221     }
00222 
00223     //  draw major gridlines
00224     painter->setPen(d_data->majPen);
00225     
00226     if (d_data->xEnabled)
00227     {
00228         drawLines(painter, r, Qt::Vertical, mx,
00229             d_data->sdx.ticks(QwtScaleDiv::MajorTick));
00230     }
00231 
00232     if (d_data->yEnabled)
00233     {
00234         drawLines(painter, r, Qt::Horizontal, my,
00235             d_data->sdy.ticks(QwtScaleDiv::MajorTick));
00236     }
00237 }
00238 
00239 void QwtPlotGrid::drawLines(QPainter *painter, const QRect &rect,
00240     Qt::Orientation orientation, const QwtScaleMap &map, 
00241     const QwtTickList &values) const
00242 {
00243     const int x1 = rect.left();
00244     const int x2 = rect.right();
00245     const int y1 = rect.top();
00246     const int y2 = rect.bottom();
00247 
00248     for (uint i = 0; i < (uint)values.count(); i++)
00249     {
00250         const int value = map.transform(values[i]);
00251         if ( orientation == Qt::Horizontal )
00252         {
00253             if ((value >= y1) && (value <= y2))
00254                 QwtPainter::drawLine(painter, x1, value, x2, value);
00255         }
00256         else
00257         {
00258             if ((value >= x1) && (value <= x2))
00259                 QwtPainter::drawLine(painter, value, y1, value, y2);
00260         }
00261     }
00262 }
00263 
00268 const QPen &QwtPlotGrid::majPen() const 
00269 { 
00270     return d_data->majPen; 
00271 }
00272 
00277 const QPen &QwtPlotGrid::minPen() const 
00278 { 
00279     return d_data->minPen; 
00280 }
00281   
00286 bool QwtPlotGrid::xEnabled() const
00287 { 
00288     return d_data->xEnabled; 
00289 }
00290 
00295 bool QwtPlotGrid::xMinEnabled() const 
00296 { 
00297     return d_data->xMinEnabled; 
00298 }
00299 
00304 bool QwtPlotGrid::yEnabled() const 
00305 { 
00306     return d_data->yEnabled; 
00307 }
00308 
00313 bool QwtPlotGrid::yMinEnabled() const 
00314 {
00315     return d_data->yMinEnabled; 
00316 }
00317 
00318   
00320 const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const 
00321 { 
00322     return d_data->sdx; 
00323 }
00324 
00326 const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const 
00327 { 
00328     return d_data->sdy; 
00329 }
00330  
00331 void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
00332     const QwtScaleDiv& yDiv)
00333 {
00334     setXDiv(xDiv);
00335     setYDiv(yDiv);
00336 }
00337 
00345 void QwtPlotGrid::gridChanged() 
00346 {
00347     itemChanged();
00348 }
00349 

Generated on Mon Jan 30 22:16:25 2006 for Qwt User's Guide by  doxygen 1.4.4