~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.2/src/qwt_plot_grid.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-04-12 23:25:58 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090412232558-3bl06x785yr8xm8u
Tags: 5.1.2-1
* New upstream release.
* Bump compat/debhelper to 7.
* Bump Standards-Version to 3.8.1. No changes needed.
* Invert Maintainers and Uploaders field.
* Fix lintian warnings:
  - dh_clean _k deprecated.
  - missing dependency on libc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 * 
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
#include <qpainter.h>
 
11
#include <qpen.h>
 
12
#include "qwt_painter.h"
 
13
#include "qwt_text.h"
 
14
#include "qwt_scale_map.h"
 
15
#include "qwt_scale_div.h"
 
16
#include "qwt_plot_grid.h"
 
17
 
 
18
class QwtPlotGrid::PrivateData
 
19
{
 
20
public:
 
21
    PrivateData():
 
22
        xEnabled(true),
 
23
        yEnabled(true),
 
24
        xMinEnabled(false),
 
25
        yMinEnabled(false)
 
26
    {
 
27
    }
 
28
 
 
29
    bool xEnabled;
 
30
    bool yEnabled;
 
31
    bool xMinEnabled;
 
32
    bool yMinEnabled;
 
33
 
 
34
    QwtScaleDiv xScaleDiv;
 
35
    QwtScaleDiv yScaleDiv;
 
36
 
 
37
    QPen majPen;
 
38
    QPen minPen;
 
39
};
 
40
 
 
41
//! Enables major grid, disables minor grid
 
42
QwtPlotGrid::QwtPlotGrid():
 
43
    QwtPlotItem(QwtText("Grid"))
 
44
{
 
45
    d_data = new PrivateData;
 
46
    setZ(10.0);
 
47
}
 
48
 
 
49
//! Destructor
 
50
QwtPlotGrid::~QwtPlotGrid()
 
51
{
 
52
    delete d_data;
 
53
}
 
54
 
 
55
//! \return QwtPlotItem::Rtti_PlotGrid
 
56
int QwtPlotGrid::rtti() const
 
57
{
 
58
    return QwtPlotItem::Rtti_PlotGrid;
 
59
}
 
60
 
 
61
/*!
 
62
  \brief Enable or disable vertical gridlines
 
63
  \param tf Enable (true) or disable
 
64
 
 
65
  \sa Minor gridlines can be enabled or disabled with
 
66
      enableXMin()
 
67
*/
 
68
void QwtPlotGrid::enableX(bool tf)
 
69
{
 
70
    if ( d_data->xEnabled != tf )
 
71
    {
 
72
        d_data->xEnabled = tf;
 
73
        itemChanged();
 
74
    }
 
75
}
 
76
 
 
77
/*!
 
78
  \brief Enable or disable horizontal gridlines
 
79
  \param tf Enable (true) or disable
 
80
  \sa Minor gridlines can be enabled or disabled with enableYMin()
 
81
*/
 
82
void QwtPlotGrid::enableY(bool tf)
 
83
{
 
84
    if ( d_data->yEnabled != tf )
 
85
    {
 
86
        d_data->yEnabled = tf;  
 
87
        itemChanged();
 
88
    }
 
89
}
 
90
 
 
91
/*!
 
92
  \brief Enable or disable  minor vertical gridlines.
 
93
  \param tf Enable (true) or disable
 
94
  \sa enableX()
 
95
*/
 
96
void QwtPlotGrid::enableXMin(bool tf)
 
97
{
 
98
    if ( d_data->xMinEnabled != tf )
 
99
    {
 
100
        d_data->xMinEnabled = tf;
 
101
        itemChanged();
 
102
    }
 
103
}
 
104
 
 
105
/*!
 
106
  \brief Enable or disable minor horizontal gridlines
 
107
  \param tf Enable (true) or disable
 
108
  \sa enableY()
 
109
*/
 
110
void QwtPlotGrid::enableYMin(bool tf)
 
111
{
 
112
    if ( d_data->yMinEnabled != tf )
 
113
    {
 
114
        d_data->yMinEnabled = tf;
 
115
        itemChanged();
 
116
    }
 
117
}
 
118
 
 
119
/*!
 
120
  \brief Assign an x axis scale division
 
121
  \param scaleDiv Scale division
 
122
  \warning QwtPlotGrid uses implicit sharing (see Qt Manual) for
 
123
  the scale divisions.
 
124
*/
 
125
void QwtPlotGrid::setXDiv(const QwtScaleDiv &scaleDiv)
 
126
{
 
127
    if ( d_data->xScaleDiv != scaleDiv )
 
128
    {
 
129
        d_data->xScaleDiv = scaleDiv;
 
130
        itemChanged();
 
131
    }
 
132
}
 
133
 
 
134
/*!
 
135
  \brief Assign a y axis division
 
136
  \param sy Scale division
 
137
  \warning QwtPlotGrid uses implicit sharing (see Qt Manual) for
 
138
  the scale divisions.
 
139
*/
 
140
void QwtPlotGrid::setYDiv(const QwtScaleDiv &sy)
 
141
{
 
142
    if ( d_data->yScaleDiv != sy )
 
143
    {
 
144
        d_data->yScaleDiv = sy;    
 
145
        itemChanged();
 
146
    }
 
147
}
 
148
 
 
149
/*!
 
150
  \brief Assign a pen for both major and minor gridlines
 
151
  \param p Pen
 
152
  \sa setMajPen(), setMinPen()
 
153
*/
 
154
void QwtPlotGrid::setPen(const QPen &p)
 
155
{
 
156
    if ( d_data->majPen != p || d_data->minPen != p )
 
157
    {
 
158
        d_data->majPen = p;
 
159
        d_data->minPen = p;
 
160
        itemChanged();
 
161
    }
 
162
}
 
163
 
 
164
/*!
 
165
  \brief Assign a pen for the major gridlines
 
166
  \param p Pen
 
167
  \sa majPen(), setMinPen(), setPen()
 
168
*/
 
169
void QwtPlotGrid::setMajPen(const QPen &p)
 
170
{
 
171
    if ( d_data->majPen != p )
 
172
    {
 
173
        d_data->majPen = p;
 
174
        itemChanged();
 
175
    }
 
176
}
 
177
 
 
178
/*!
 
179
  \brief Assign a pen for the minor gridlines
 
180
  \param p Pen
 
181
*/
 
182
void QwtPlotGrid::setMinPen(const QPen &p)
 
183
{
 
184
    if ( d_data->minPen != p )
 
185
    {
 
186
        d_data->minPen = p;  
 
187
        itemChanged();
 
188
    }
 
189
}
 
190
 
 
191
/*!
 
192
  \brief Draw the grid
 
193
  
 
194
  The grid is drawn into the bounding rectangle such that 
 
195
  gridlines begin and end at the rectangle's borders. The X and Y
 
196
  maps are used to map the scale divisions into the drawing region
 
197
  screen.
 
198
  \param painter  Painter
 
199
  \param xMap X axis map
 
200
  \param yMap Y axis 
 
201
  \param canvasRect Contents rect of the plot canvas
 
202
*/
 
203
void QwtPlotGrid::draw(QPainter *painter, 
 
204
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
 
205
    const QRect &canvasRect) const
 
206
{
 
207
    //  draw minor gridlines
 
208
    painter->setPen(d_data->minPen);
 
209
    
 
210
    if (d_data->xEnabled && d_data->xMinEnabled)
 
211
    {
 
212
        drawLines(painter, canvasRect, Qt::Vertical, xMap, 
 
213
            d_data->xScaleDiv.ticks(QwtScaleDiv::MinorTick));
 
214
        drawLines(painter, canvasRect, Qt::Vertical, xMap, 
 
215
            d_data->xScaleDiv.ticks(QwtScaleDiv::MediumTick));
 
216
    }
 
217
 
 
218
    if (d_data->yEnabled && d_data->yMinEnabled)
 
219
    {
 
220
        drawLines(painter, canvasRect, Qt::Horizontal, yMap, 
 
221
            d_data->yScaleDiv.ticks(QwtScaleDiv::MinorTick));
 
222
        drawLines(painter, canvasRect, Qt::Horizontal, yMap, 
 
223
            d_data->yScaleDiv.ticks(QwtScaleDiv::MediumTick));
 
224
    }
 
225
 
 
226
    //  draw major gridlines
 
227
    painter->setPen(d_data->majPen);
 
228
    
 
229
    if (d_data->xEnabled)
 
230
    {
 
231
        drawLines(painter, canvasRect, Qt::Vertical, xMap,
 
232
            d_data->xScaleDiv.ticks(QwtScaleDiv::MajorTick));
 
233
    }
 
234
 
 
235
    if (d_data->yEnabled)
 
236
    {
 
237
        drawLines(painter, canvasRect, Qt::Horizontal, yMap,
 
238
            d_data->yScaleDiv.ticks(QwtScaleDiv::MajorTick));
 
239
    }
 
240
}
 
241
 
 
242
void QwtPlotGrid::drawLines(QPainter *painter, const QRect &canvasRect,
 
243
    Qt::Orientation orientation, const QwtScaleMap &scaleMap, 
 
244
    const QwtValueList &values) const
 
245
{
 
246
    const int x1 = canvasRect.left();
 
247
    const int x2 = canvasRect.right();
 
248
    const int y1 = canvasRect.top();
 
249
    const int y2 = canvasRect.bottom();
 
250
 
 
251
    for (uint i = 0; i < (uint)values.count(); i++)
 
252
    {
 
253
        const int value = scaleMap.transform(values[i]);
 
254
        if ( orientation == Qt::Horizontal )
 
255
        {
 
256
            if ((value >= y1) && (value <= y2))
 
257
                QwtPainter::drawLine(painter, x1, value, x2, value);
 
258
        }
 
259
        else
 
260
        {
 
261
            if ((value >= x1) && (value <= x2))
 
262
                QwtPainter::drawLine(painter, value, y1, value, y2);
 
263
        }
 
264
    }
 
265
}
 
266
 
 
267
/*!
 
268
  \return the pen for the major gridlines
 
269
  \sa setMajPen(), setMinPen(), setPen()
 
270
*/
 
271
const QPen &QwtPlotGrid::majPen() const 
 
272
 
273
    return d_data->majPen; 
 
274
}
 
275
 
 
276
/*!
 
277
  \return the pen for the minor gridlines
 
278
  \sa setMinPen(), setMajPen(), setPen()
 
279
*/
 
280
const QPen &QwtPlotGrid::minPen() const 
 
281
 
282
    return d_data->minPen; 
 
283
}
 
284
  
 
285
/*!
 
286
  \return true if vertical gridlines are enabled
 
287
  \sa enableX()
 
288
*/
 
289
bool QwtPlotGrid::xEnabled() const
 
290
 
291
    return d_data->xEnabled; 
 
292
}
 
293
 
 
294
/*!
 
295
  \return true if minor vertical gridlines are enabled
 
296
  \sa enableXMin()
 
297
*/
 
298
bool QwtPlotGrid::xMinEnabled() const 
 
299
 
300
    return d_data->xMinEnabled; 
 
301
}
 
302
 
 
303
/*!
 
304
  \return true if horizontal gridlines are enabled
 
305
  \sa enableY()
 
306
*/
 
307
bool QwtPlotGrid::yEnabled() const 
 
308
 
309
    return d_data->yEnabled; 
 
310
}
 
311
 
 
312
/*!
 
313
  \return true if minor horizontal gridlines are enabled
 
314
  \sa enableYMin()
 
315
*/
 
316
bool QwtPlotGrid::yMinEnabled() const 
 
317
{
 
318
    return d_data->yMinEnabled; 
 
319
}
 
320
 
 
321
  
 
322
/*! \return the scale division of the x axis */
 
323
const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const 
 
324
 
325
    return d_data->xScaleDiv; 
 
326
}
 
327
 
 
328
/*! \return the scale division of the y axis */
 
329
const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const 
 
330
 
331
    return d_data->yScaleDiv; 
 
332
}
 
333
 
 
334
void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
 
335
    const QwtScaleDiv& yDiv)
 
336
{
 
337
    setXDiv(xDiv);
 
338
    setYDiv(yDiv);
 
339
}