# 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 xScaleDiv;
    00035     QwtScaleDiv yScaleDiv;
    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
    00056 int QwtPlotGrid::rtti() const
    00057 {
    00058     return QwtPlotItem::Rtti_PlotGrid;
    00059 }
    00060
    00068 void QwtPlotGrid::enableX(bool tf)
    00069 {
    00070     if ( d_data->xEnabled != tf )
    00071     {
    00072         d_data->xEnabled = tf;
    00073         itemChanged();
    00074     }
    00075 }
    00076
    00082 void QwtPlotGrid::enableY(bool tf)
    00083 {
    00084     if ( d_data->yEnabled != tf )
    00085     {
    00086         d_data->yEnabled = tf;
    00087         itemChanged();
    00088     }
    00089 }
    00090
    00096 void QwtPlotGrid::enableXMin(bool tf)
    00097 {
    00098     if ( d_data->xMinEnabled != tf )
    00099     {
    00100         d_data->xMinEnabled = tf;
    00101         itemChanged();
    00102     }
    00103 }
    00104
    00110 void QwtPlotGrid::enableYMin(bool tf)
    00111 {
    00112     if ( d_data->yMinEnabled != tf )
    00113     {
    00114         d_data->yMinEnabled = tf;
    00115         itemChanged();
    00116     }
    00117 }
    00118
    00125 void QwtPlotGrid::setXDiv(const QwtScaleDiv &scaleDiv)
    00126 {
    00127     if ( d_data->xScaleDiv != scaleDiv )
    00128     {
    00129         d_data->xScaleDiv = scaleDiv;
    00130         itemChanged();
    00131     }
    00132 }
    00133
    00140 void QwtPlotGrid::setYDiv(const QwtScaleDiv &sy)
    00141 {
    00142     if ( d_data->yScaleDiv != sy )
    00143     {
    00144         d_data->yScaleDiv = sy;
    00145         itemChanged();
    00146     }
    00147 }
    00148
    00154 void QwtPlotGrid::setPen(const QPen &p)
    00155 {
    00156     if ( d_data->majPen != p || d_data->minPen != p )
    00157     {
    00158         d_data->majPen = p;
    00159         d_data->minPen = p;
    00160         itemChanged();
    00161     }
    00162 }
    00163
    00169 void QwtPlotGrid::setMajPen(const QPen &p)
    00170 {
    00171     if ( d_data->majPen != p )
    00172     {
    00173         d_data->majPen = p;
    00174         itemChanged();
    00175     }
    00176 }
    00177
    00182 void QwtPlotGrid::setMinPen(const QPen &p)
    00183 {
    00184     if ( d_data->minPen != p )
    00185     {
    00186         d_data->minPen = p;
    00187         itemChanged();
    00188     }
    00189 }
    00190
    00203 void QwtPlotGrid::draw(QPainter *painter,
    00204     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    00205     const QRect &canvasRect) const
    00206 {
    00207     //  draw minor gridlines
    00208     painter->setPen(d_data->minPen);
    00209
    00210     if (d_data->xEnabled && d_data->xMinEnabled)
    00211     {
    00212         drawLines(painter, canvasRect, Qt::Vertical, xMap,
    00213             d_data->xScaleDiv.ticks(QwtScaleDiv::MinorTick));
    00214         drawLines(painter, canvasRect, Qt::Vertical, xMap,
    00215             d_data->xScaleDiv.ticks(QwtScaleDiv::MediumTick));
    00216     }
    00217
    00218     if (d_data->yEnabled && d_data->yMinEnabled)
    00219     {
    00220         drawLines(painter, canvasRect, Qt::Horizontal, yMap,
    00221             d_data->yScaleDiv.ticks(QwtScaleDiv::MinorTick));
    00222         drawLines(painter, canvasRect, Qt::Horizontal, yMap,
    00223             d_data->yScaleDiv.ticks(QwtScaleDiv::MediumTick));
    00224     }
    00225
    00226     //  draw major gridlines
    00227     painter->setPen(d_data->majPen);
    00228
    00229     if (d_data->xEnabled)
    00230     {
    00231         drawLines(painter, canvasRect, Qt::Vertical, xMap,
    00232             d_data->xScaleDiv.ticks(QwtScaleDiv::MajorTick));
    00233     }
    00234
    00235     if (d_data->yEnabled)
    00236     {
    00237         drawLines(painter, canvasRect, Qt::Horizontal, yMap,
    00238             d_data->yScaleDiv.ticks(QwtScaleDiv::MajorTick));
    00239     }
    00240 }
    00241
    00242 void QwtPlotGrid::drawLines(QPainter *painter, const QRect &canvasRect,
    00243     Qt::Orientation orientation, const QwtScaleMap &scaleMap,
    00244     const QwtValueList &values) const
    00245 {
    00246     const int x1 = canvasRect.left();
    00247     const int x2 = canvasRect.right();
    00248     const int y1 = canvasRect.top();
    00249     const int y2 = canvasRect.bottom();
    00250
    00251     for (uint i = 0; i < (uint)values.count(); i++)
    00252     {
    00253         const int value = scaleMap.transform(values[i]);
    00254         if ( orientation == Qt::Horizontal )
    00255         {
    00256             if ((value >= y1) && (value <= y2))
    00257                 QwtPainter::drawLine(painter, x1, value, x2, value);
    00258         }
    00259         else
    00260         {
    00261             if ((value >= x1) && (value <= x2))
    00262                 QwtPainter::drawLine(painter, value, y1, value, y2);
    00263         }
    00264     }
    00265 }
    00266
    00271 const QPen &QwtPlotGrid::majPen() const
    00272 {
    00273     return d_data->majPen;
    00274 }
    00275
    00280 const QPen &QwtPlotGrid::minPen() const
    00281 {
    00282     return d_data->minPen;
    00283 }
    00284
    00289 bool QwtPlotGrid::xEnabled() const
    00290 {
    00291     return d_data->xEnabled;
    00292 }
    00293
    00298 bool QwtPlotGrid::xMinEnabled() const
    00299 {
    00300     return d_data->xMinEnabled;
    00301 }
    00302
    00307 bool QwtPlotGrid::yEnabled() const
    00308 {
    00309     return d_data->yEnabled;
    00310 }
    00311
    00316 bool QwtPlotGrid::yMinEnabled() const
    00317 {
    00318     return d_data->yMinEnabled;
    00319 }
    00320
    00321
    00323 const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const
    00324 {
    00325     return d_data->xScaleDiv;
    00326 }
    00327
    00329 const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const
    00330 {
    00331     return d_data->yScaleDiv;
    00332 }
    00333
    00334 void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
    00335     const QwtScaleDiv& yDiv)
    00336 {
    00337     setXDiv(xDiv);
    00338     setYDiv(yDiv);
    00339 }