qwt_plot_grid.cpp 7.34 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qpainter.h>
#include <qpen.h>
#include "qwt_painter.h"
#include "qwt_text.h"
#include "qwt_scale_map.h"
#include "qwt_scale_div.h"
#include "qwt_plot_grid.h"

class QwtPlotGrid::PrivateData
{
public:
    PrivateData():
        xEnabled(true),
        yEnabled(true),
        xMinEnabled(false),
25
        yMinEnabled(false) {
pixhawk's avatar
pixhawk committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    }

    bool xEnabled;
    bool yEnabled;
    bool xMinEnabled;
    bool yMinEnabled;

    QwtScaleDiv xScaleDiv;
    QwtScaleDiv yScaleDiv;

    QPen majPen;
    QPen minPen;
};

//! Enables major grid, disables minor grid
QwtPlotGrid::QwtPlotGrid():
    QwtPlotItem(QwtText("Grid"))
{
    d_data = new PrivateData;
    setZ(10.0);
}

//! Destructor
QwtPlotGrid::~QwtPlotGrid()
{
    delete d_data;
}

//! \return QwtPlotItem::Rtti_PlotGrid
int QwtPlotGrid::rtti() const
{
    return QwtPlotItem::Rtti_PlotGrid;
}

/*!
  \brief Enable or disable vertical gridlines
  \param tf Enable (true) or disable

  \sa Minor gridlines can be enabled or disabled with
      enableXMin()
*/
void QwtPlotGrid::enableX(bool tf)
{
69
    if ( d_data->xEnabled != tf ) {
pixhawk's avatar
pixhawk committed
70 71 72 73 74 75 76 77 78 79 80 81
        d_data->xEnabled = tf;
        itemChanged();
    }
}

/*!
  \brief Enable or disable horizontal gridlines
  \param tf Enable (true) or disable
  \sa Minor gridlines can be enabled or disabled with enableYMin()
*/
void QwtPlotGrid::enableY(bool tf)
{
82 83
    if ( d_data->yEnabled != tf ) {
        d_data->yEnabled = tf;
pixhawk's avatar
pixhawk committed
84 85 86 87 88 89 90 91 92 93 94
        itemChanged();
    }
}

/*!
  \brief Enable or disable  minor vertical gridlines.
  \param tf Enable (true) or disable
  \sa enableX()
*/
void QwtPlotGrid::enableXMin(bool tf)
{
95
    if ( d_data->xMinEnabled != tf ) {
pixhawk's avatar
pixhawk committed
96 97 98 99 100 101 102 103 104 105 106 107
        d_data->xMinEnabled = tf;
        itemChanged();
    }
}

/*!
  \brief Enable or disable minor horizontal gridlines
  \param tf Enable (true) or disable
  \sa enableY()
*/
void QwtPlotGrid::enableYMin(bool tf)
{
108
    if ( d_data->yMinEnabled != tf ) {
pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115 116 117 118 119 120 121
        d_data->yMinEnabled = tf;
        itemChanged();
    }
}

/*!
  \brief Assign an x axis scale division
  \param scaleDiv Scale division
  \warning QwtPlotGrid uses implicit sharing (see Qt Manual) for
  the scale divisions.
*/
void QwtPlotGrid::setXDiv(const QwtScaleDiv &scaleDiv)
{
122
    if ( d_data->xScaleDiv != scaleDiv ) {
pixhawk's avatar
pixhawk committed
123 124 125 126 127 128 129 130 131 132 133 134 135
        d_data->xScaleDiv = scaleDiv;
        itemChanged();
    }
}

/*!
  \brief Assign a y axis division
  \param sy Scale division
  \warning QwtPlotGrid uses implicit sharing (see Qt Manual) for
  the scale divisions.
*/
void QwtPlotGrid::setYDiv(const QwtScaleDiv &sy)
{
136 137
    if ( d_data->yScaleDiv != sy ) {
        d_data->yScaleDiv = sy;
pixhawk's avatar
pixhawk committed
138 139 140 141 142 143 144 145 146 147 148
        itemChanged();
    }
}

/*!
  \brief Assign a pen for both major and minor gridlines
  \param p Pen
  \sa setMajPen(), setMinPen()
*/
void QwtPlotGrid::setPen(const QPen &p)
{
149
    if ( d_data->majPen != p || d_data->minPen != p ) {
pixhawk's avatar
pixhawk committed
150 151 152 153 154 155 156 157 158 159 160 161 162
        d_data->majPen = p;
        d_data->minPen = p;
        itemChanged();
    }
}

/*!
  \brief Assign a pen for the major gridlines
  \param p Pen
  \sa majPen(), setMinPen(), setPen()
*/
void QwtPlotGrid::setMajPen(const QPen &p)
{
163
    if ( d_data->majPen != p ) {
pixhawk's avatar
pixhawk committed
164 165 166 167 168 169 170 171 172 173 174
        d_data->majPen = p;
        itemChanged();
    }
}

/*!
  \brief Assign a pen for the minor gridlines
  \param p Pen
*/
void QwtPlotGrid::setMinPen(const QPen &p)
{
175 176
    if ( d_data->minPen != p ) {
        d_data->minPen = p;
pixhawk's avatar
pixhawk committed
177 178 179 180 181 182
        itemChanged();
    }
}

/*!
  \brief Draw the grid
183 184

  The grid is drawn into the bounding rectangle such that
pixhawk's avatar
pixhawk committed
185 186 187 188 189
  gridlines begin and end at the rectangle's borders. The X and Y
  maps are used to map the scale divisions into the drawing region
  screen.
  \param painter  Painter
  \param xMap X axis map
190
  \param yMap Y axis
pixhawk's avatar
pixhawk committed
191 192
  \param canvasRect Contents rect of the plot canvas
*/
193 194 195
void QwtPlotGrid::draw(QPainter *painter,
                       const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                       const QRect &canvasRect) const
pixhawk's avatar
pixhawk committed
196 197 198
{
    //  draw minor gridlines
    painter->setPen(d_data->minPen);
199 200 201 202 203 204

    if (d_data->xEnabled && d_data->xMinEnabled) {
        drawLines(painter, canvasRect, Qt::Vertical, xMap,
                  d_data->xScaleDiv.ticks(QwtScaleDiv::MinorTick));
        drawLines(painter, canvasRect, Qt::Vertical, xMap,
                  d_data->xScaleDiv.ticks(QwtScaleDiv::MediumTick));
pixhawk's avatar
pixhawk committed
205 206
    }

207 208 209 210 211
    if (d_data->yEnabled && d_data->yMinEnabled) {
        drawLines(painter, canvasRect, Qt::Horizontal, yMap,
                  d_data->yScaleDiv.ticks(QwtScaleDiv::MinorTick));
        drawLines(painter, canvasRect, Qt::Horizontal, yMap,
                  d_data->yScaleDiv.ticks(QwtScaleDiv::MediumTick));
pixhawk's avatar
pixhawk committed
212 213 214 215
    }

    //  draw major gridlines
    painter->setPen(d_data->majPen);
216 217

    if (d_data->xEnabled) {
pixhawk's avatar
pixhawk committed
218
        drawLines(painter, canvasRect, Qt::Vertical, xMap,
219
                  d_data->xScaleDiv.ticks(QwtScaleDiv::MajorTick));
pixhawk's avatar
pixhawk committed
220 221
    }

222
    if (d_data->yEnabled) {
pixhawk's avatar
pixhawk committed
223
        drawLines(painter, canvasRect, Qt::Horizontal, yMap,
224
                  d_data->yScaleDiv.ticks(QwtScaleDiv::MajorTick));
pixhawk's avatar
pixhawk committed
225 226 227 228
    }
}

void QwtPlotGrid::drawLines(QPainter *painter, const QRect &canvasRect,
229 230
                            Qt::Orientation orientation, const QwtScaleMap &scaleMap,
                            const QwtValueList &values) const
pixhawk's avatar
pixhawk committed
231 232 233 234 235 236
{
    const int x1 = canvasRect.left();
    const int x2 = canvasRect.right();
    const int y1 = canvasRect.top();
    const int y2 = canvasRect.bottom();

237
    for (uint i = 0; i < (uint)values.count(); i++) {
pixhawk's avatar
pixhawk committed
238
        const int value = scaleMap.transform(values[i]);
239
        if ( orientation == Qt::Horizontal ) {
pixhawk's avatar
pixhawk committed
240 241
            if ((value >= y1) && (value <= y2))
                QwtPainter::drawLine(painter, x1, value, x2, value);
242
        } else {
pixhawk's avatar
pixhawk committed
243 244 245 246 247 248 249 250 251 252
            if ((value >= x1) && (value <= x2))
                QwtPainter::drawLine(painter, value, y1, value, y2);
        }
    }
}

/*!
  \return the pen for the major gridlines
  \sa setMajPen(), setMinPen(), setPen()
*/
253 254 255
const QPen &QwtPlotGrid::majPen() const
{
    return d_data->majPen;
pixhawk's avatar
pixhawk committed
256 257 258 259 260 261
}

/*!
  \return the pen for the minor gridlines
  \sa setMinPen(), setMajPen(), setPen()
*/
262 263 264
const QPen &QwtPlotGrid::minPen() const
{
    return d_data->minPen;
pixhawk's avatar
pixhawk committed
265
}
266

pixhawk's avatar
pixhawk committed
267 268 269 270 271
/*!
  \return true if vertical gridlines are enabled
  \sa enableX()
*/
bool QwtPlotGrid::xEnabled() const
272 273
{
    return d_data->xEnabled;
pixhawk's avatar
pixhawk committed
274 275 276 277 278 279
}

/*!
  \return true if minor vertical gridlines are enabled
  \sa enableXMin()
*/
280 281 282
bool QwtPlotGrid::xMinEnabled() const
{
    return d_data->xMinEnabled;
pixhawk's avatar
pixhawk committed
283 284 285 286 287 288
}

/*!
  \return true if horizontal gridlines are enabled
  \sa enableY()
*/
289 290 291
bool QwtPlotGrid::yEnabled() const
{
    return d_data->yEnabled;
pixhawk's avatar
pixhawk committed
292 293 294 295 296 297
}

/*!
  \return true if minor horizontal gridlines are enabled
  \sa enableYMin()
*/
298
bool QwtPlotGrid::yMinEnabled() const
pixhawk's avatar
pixhawk committed
299
{
300
    return d_data->yMinEnabled;
pixhawk's avatar
pixhawk committed
301 302
}

303

pixhawk's avatar
pixhawk committed
304
/*! \return the scale division of the x axis */
305 306 307
const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const
{
    return d_data->xScaleDiv;
pixhawk's avatar
pixhawk committed
308 309 310
}

/*! \return the scale division of the y axis */
311 312 313
const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const
{
    return d_data->yScaleDiv;
pixhawk's avatar
pixhawk committed
314
}
315

pixhawk's avatar
pixhawk committed
316
void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
317
                                 const QwtScaleDiv& yDiv)
pixhawk's avatar
pixhawk committed
318 319 320 321
{
    setXDiv(xDiv);
    setYDiv(yDiv);
}