# 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_text.h"
    00014 #include "qwt_scale_map.h"
    00015 #include "qwt_scale_div.h"
    00016 #include "qwt_plot_grid.h"
    00017
    00018 class QwtPlotGrid::PrivateData
    00019 {
    00020 public:
    00021     PrivateData():
    00022         xEnabled(true),
    00023         yEnabled(true),
    00024         xMinEnabled(false),
    00025         yMinEnabled(false)
    00026     {
    00027     }
    00028
    00029     bool xEnabled;
    00030     bool yEnabled;
    00031     bool xMinEnabled;
    00032     bool yMinEnabled;
    00033
    00034     QwtScaleDiv sdx;
    00035     QwtScaleDiv sdy;
    00036
    00037     QPen majPen;
    00038     QPen minPen;
    00039 };
    00040
    00042 QwtPlotGrid::QwtPlotGrid():
    00043     QwtPlotItem(QwtText("Grid"))
    00044 {
    00045     d_data = new PrivateData;
    00046     setZ(10.0);
    00047 }
    00048
    00050 QwtPlotGrid::~QwtPlotGrid()
    00051 {
    00052     delete d_data;
    00053 }
    00054
    00055 int QwtPlotGrid::rtti() const
    00056 {
    00057     return QwtPlotItem::Rtti_PlotGrid;
    00058 }
    00059
    00067 void QwtPlotGrid::enableX(bool tf)
    00068 {
    00069     if ( d_data->xEnabled != tf )
    00070     {
    00071         d_data->xEnabled = tf;
    00072         itemChanged();
    00073     }
    00074 }
    00075
    00081 void QwtPlotGrid::enableY(bool tf)
    00082 {
    00083     if ( d_data->yEnabled != tf )
    00084     {
    00085         d_data->yEnabled = tf;
    00086         itemChanged();
    00087     }
    00088 }
    00089
    00095 void QwtPlotGrid::enableXMin(bool tf)
    00096 {
    00097     if ( d_data->xMinEnabled != tf )
    00098     {
    00099         d_data->xMinEnabled = tf;
    00100         itemChanged();
    00101     }
    00102 }
    00103
    00109 void QwtPlotGrid::enableYMin(bool tf)
    00110 {
    00111     if ( d_data->yMinEnabled != tf )
    00112     {
    00113         d_data->yMinEnabled = tf;
    00114         itemChanged();
    00115     }
    00116 }
    00117
    00124 void QwtPlotGrid::setXDiv(const QwtScaleDiv &sx)
    00125 {
    00126     if ( d_data->sdx != sx )
    00127     {
    00128         d_data->sdx = sx;
    00129         itemChanged();
    00130     }
    00131 }
    00132
    00139 void QwtPlotGrid::setYDiv(const QwtScaleDiv &sy)
    00140 {
    00141     if ( d_data->sdy != sy )
    00142     {
    00143         d_data->sdy = sy;
    00144         itemChanged();
    00145     }
    00146 }
    00147
    00153 void QwtPlotGrid::setPen(const QPen &p)
    00154 {
    00155     if ( d_data->majPen != p || d_data->minPen != p )
    00156     {
    00157         d_data->majPen = p;
    00158         d_data->minPen = p;
    00159         itemChanged();
    00160     }
    00161 }
    00162
    00168 void QwtPlotGrid::setMajPen(const QPen &p)
    00169 {
    00170     if ( d_data->majPen != p )
    00171     {
    00172         d_data->majPen = p;
    00173         itemChanged();
    00174     }
    00175 }
    00176
    00181 void QwtPlotGrid::setMinPen(const QPen &p)
    00182 {
    00183     if ( d_data->minPen != p )
    00184     {
    00185         d_data->minPen = p;
    00186         itemChanged();
    00187     }
    00188 }
    00189
    00202 void QwtPlotGrid::draw(QPainter *painter,
    00203     const QwtScaleMap &mx, const QwtScaleMap &my,
    00204     const QRect &r) const
    00205 {
    00206     //  draw minor gridlines
    00207     painter->setPen(d_data->minPen);
    00208
    00209     if (d_data->xEnabled && d_data->xMinEnabled)
    00210     {
    00211         drawLines(painter, r, Qt::Vertical, mx,
    00212             d_data->sdx.ticks(QwtScaleDiv::MinorTick));
    00213         drawLines(painter, r, Qt::Vertical, mx,
    00214             d_data->sdx.ticks(QwtScaleDiv::MediumTick));
    00215     }
    00216
    00217     if (d_data->yEnabled && d_data->yMinEnabled)
    00218     {
    00219         drawLines(painter, r, Qt::Horizontal, my,
    00220             d_data->sdy.ticks(QwtScaleDiv::MinorTick));
    00221         drawLines(painter, r, Qt::Horizontal, my,
    00222             d_data->sdy.ticks(QwtScaleDiv::MediumTick));
    00223     }
    00224
    00225     //  draw major gridlines
    00226     painter->setPen(d_data->majPen);
    00227
    00228     if (d_data->xEnabled)
    00229     {
    00230         drawLines(painter, r, Qt::Vertical, mx,
    00231             d_data->sdx.ticks(QwtScaleDiv::MajorTick));
    00232     }
    00233
    00234     if (d_data->yEnabled)
    00235     {
    00236         drawLines(painter, r, Qt::Horizontal, my,
    00237             d_data->sdy.ticks(QwtScaleDiv::MajorTick));
    00238     }
    00239 }
    00240
    00241 void QwtPlotGrid::drawLines(QPainter *painter, const QRect &rect,
    00242     Qt::Orientation orientation, const QwtScaleMap &map,
    00243     const QwtValueList &values) const
    00244 {
    00245     const int x1 = rect.left();
    00246     const int x2 = rect.right();
    00247     const int y1 = rect.top();
    00248     const int y2 = rect.bottom();
    00249
    00250     for (uint i = 0; i < (uint)values.count(); i++)
    00251     {
    00252         const int value = map.transform(values[i]);
    00253         if ( orientation == Qt::Horizontal )
    00254         {
    00255             if ((value >= y1) && (value <= y2))
    00256                 QwtPainter::drawLine(painter, x1, value, x2, value);
    00257         }
    00258         else
    00259         {
    00260             if ((value >= x1) && (value <= x2))
    00261                 QwtPainter::drawLine(painter, value, y1, value, y2);
    00262         }
    00263     }
    00264 }
    00265
    00270 const QPen &QwtPlotGrid::majPen() const
    00271 {
    00272     return d_data->majPen;
    00273 }
    00274
    00279 const QPen &QwtPlotGrid::minPen() const
    00280 {
    00281     return d_data->minPen;
    00282 }
    00283
    00288 bool QwtPlotGrid::xEnabled() const
    00289 {
    00290     return d_data->xEnabled;
    00291 }
    00292
    00297 bool QwtPlotGrid::xMinEnabled() const
    00298 {
    00299     return d_data->xMinEnabled;
    00300 }
    00301
    00306 bool QwtPlotGrid::yEnabled() const
    00307 {
    00308     return d_data->yEnabled;
    00309 }
    00310
    00315 bool QwtPlotGrid::yMinEnabled() const
    00316 {
    00317     return d_data->yMinEnabled;
    00318 }
    00319
    00320
    00322 const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const
    00323 {
    00324     return d_data->sdx;
    00325 }
    00326
    00328 const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const
    00329 {
    00330     return d_data->sdy;
    00331 }
    00332
    00333 void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
    00334     const QwtScaleDiv& yDiv)
    00335 {
    00336     setXDiv(xDiv);
    00337     setYDiv(yDiv);
    00338 }