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

« back to all changes in this revision

Viewing changes to qwt-5.0.1/src/qwt_plot_item.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2007-10-05 15:20:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071005152041-qmybqh4fj9jejyo2
Tags: 5.0.2-2
* Handle nostrip build option. (Closes: #437877)
* Build libqwt5-doc package in binary-indep target. (Closes: #443110)

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 "qwt_text.h"
11
 
#include "qwt_plot.h"
12
 
#include "qwt_legend.h"
13
 
#include "qwt_legend_item.h"
14
 
#include "qwt_plot_item.h"
15
 
 
16
 
class QwtPlotItem::PrivateData
17
 
{
18
 
public:
19
 
    PrivateData():
20
 
        plot(NULL),
21
 
        isVisible(true),
22
 
        attributes(0),
23
 
#if QT_VERSION >= 0x040000
24
 
        renderHints(0),
25
 
#endif
26
 
        z(0.0),
27
 
        xAxis(QwtPlot::xBottom),
28
 
        yAxis(QwtPlot::yLeft)
29
 
    {
30
 
    }
31
 
 
32
 
    mutable QwtPlot *plot;
33
 
 
34
 
    bool isVisible;
35
 
    int attributes;
36
 
#if QT_VERSION >= 0x040000
37
 
    int renderHints;
38
 
#endif
39
 
    double z;
40
 
 
41
 
    int xAxis;
42
 
    int yAxis;
43
 
 
44
 
    QwtText title;
45
 
};
46
 
 
47
 
//! Constructor
48
 
QwtPlotItem::QwtPlotItem(const QwtText &title)
49
 
{
50
 
    d_data = new PrivateData;
51
 
    d_data->title = title;
52
 
}
53
 
 
54
 
//! Destroy the QwtPlotItem
55
 
QwtPlotItem::~QwtPlotItem()
56
 
{
57
 
    attach(NULL);
58
 
    delete d_data;
59
 
}
60
 
 
61
 
/*! 
62
 
  Attach the item to a plot
63
 
*/
64
 
void QwtPlotItem::attach(QwtPlot *plot)
65
 
{
66
 
    if ( plot == d_data->plot )
67
 
        return;
68
 
 
69
 
    // remove the item from the previous plot
70
 
 
71
 
    if ( d_data->plot )
72
 
    {
73
 
        if ( d_data->plot->legend() )
74
 
        {
75
 
            QWidget *legendItem = d_data->plot->legend()->find(this);
76
 
            if ( legendItem )
77
 
                delete legendItem; 
78
 
        }
79
 
 
80
 
        d_data->plot->attachItem(this, false);
81
 
 
82
 
        if ( d_data->plot->autoReplot() )
83
 
            d_data->plot->update();
84
 
    }
85
 
 
86
 
    d_data->plot = plot;
87
 
 
88
 
    if ( d_data->plot )
89
 
    {
90
 
        // insert the item into the current plot
91
 
 
92
 
        d_data->plot->attachItem(this, true);
93
 
        itemChanged();
94
 
    }
95
 
}
96
 
 
97
 
int QwtPlotItem::rtti() const
98
 
{
99
 
    return Rtti_PlotItem;
100
 
}
101
 
 
102
 
//! Return attached plot
103
 
QwtPlot *QwtPlotItem::plot() const 
104
 
105
 
    return d_data->plot; 
106
 
}
107
 
 
108
 
/*!
109
 
   Plot items are painted in increasing z-order.
110
 
 
111
 
   \return setZ(), QwtPlotDict::itemList()
112
 
*/
113
 
double QwtPlotItem::z() const 
114
 
115
 
    return d_data->z; 
116
 
}
117
 
 
118
 
/*!
119
 
   \brief Set the z value
120
 
 
121
 
   Plot items are painted in increasing z-order.
122
 
 
123
 
   \param z Z-value
124
 
   \sa z(), QwtPlotDict::itemList()
125
 
*/
126
 
void QwtPlotItem::setZ(double z) 
127
 
128
 
    if ( d_data->z != z )
129
 
    {
130
 
        d_data->z = z; 
131
 
        if ( d_data->plot )
132
 
        {
133
 
            // update the z order
134
 
            d_data->plot->attachItem(this, false);
135
 
            d_data->plot->attachItem(this, true);
136
 
        }
137
 
        itemChanged();
138
 
    }
139
 
}
140
 
 
141
 
/*! 
142
 
   Set a new title
143
 
 
144
 
   \param title Title
145
 
   \sa title() 
146
 
*/  
147
 
void QwtPlotItem::setTitle(const QString &title)
148
 
{
149
 
    setTitle(QwtText(title));
150
 
}
151
 
 
152
 
/*! 
153
 
   Set a new title
154
 
 
155
 
   \param title Title
156
 
   \sa title() 
157
 
*/  
158
 
void QwtPlotItem::setTitle(const QwtText &title)
159
 
{
160
 
    if ( d_data->title != title )
161
 
    {
162
 
        d_data->title = title; 
163
 
        itemChanged();
164
 
    }
165
 
}
166
 
 
167
 
/*!
168
 
   \return Title of the item
169
 
   \sa setTitle()
170
 
*/
171
 
const QwtText &QwtPlotItem::title() const
172
 
{
173
 
    return d_data->title;
174
 
}
175
 
 
176
 
/*!
177
 
   Toggle an item attribute
178
 
 
179
 
   \param attribute Attribute type
180
 
   \param on true/false
181
 
 
182
 
   \sa testItemAttribute(), ItemAttribute
183
 
*/
184
 
void QwtPlotItem::setItemAttribute(ItemAttribute attribute, bool on)
185
 
{
186
 
    if ( bool(d_data->attributes & attribute) != on )
187
 
    {
188
 
        if ( on )
189
 
            d_data->attributes |= attribute;
190
 
        else
191
 
            d_data->attributes &= ~attribute;
192
 
 
193
 
        itemChanged();
194
 
    }
195
 
}
196
 
 
197
 
/*!
198
 
   Test an item attribute
199
 
 
200
 
   \param ItemAttribute Attribute type
201
 
   \return true/false
202
 
   \sa setItemAttribute(), ItemAttribute
203
 
*/
204
 
bool QwtPlotItem::testItemAttribute(ItemAttribute attribute) const
205
 
{
206
 
    return d_data->attributes & attribute;
207
 
}
208
 
 
209
 
#if QT_VERSION >= 0x040000
210
 
 
211
 
/*!
212
 
   Toggle an render hint
213
 
 
214
 
   \param hint Render hint
215
 
   \param on true/false
216
 
 
217
 
   \sa testRenderHint(), RenderHint
218
 
*/
219
 
void QwtPlotItem::setRenderHint(RenderHint hint, bool on)
220
 
{
221
 
    if ( ((d_data->renderHints & hint) != 0) != on )
222
 
    {
223
 
        if ( on )
224
 
            d_data->renderHints |= hint;
225
 
        else
226
 
            d_data->renderHints &= ~hint;
227
 
 
228
 
        itemChanged();
229
 
    }
230
 
}
231
 
 
232
 
/*!
233
 
   Test a render hint
234
 
 
235
 
   \param hint Render hint
236
 
   \return true/false
237
 
   \sa setRenderHint(), RenderHint
238
 
*/
239
 
bool QwtPlotItem::testRenderHint(RenderHint hint) const
240
 
{
241
 
    return (d_data->renderHints & hint);
242
 
}
243
 
 
244
 
#endif
245
 
 
246
 
void QwtPlotItem::show()
247
 
{
248
 
    setVisible(true);
249
 
}
250
 
 
251
 
void QwtPlotItem::hide()
252
 
{
253
 
    setVisible(false);
254
 
}
255
 
 
256
 
/*! 
257
 
    Show/Hide the item
258
 
 
259
 
    \param on Show if true, otherwise hide
260
 
    \sa isVisible(), show(), hide()
261
 
*/
262
 
void QwtPlotItem::setVisible(bool on) 
263
 
264
 
    if ( on != d_data->isVisible )
265
 
    {
266
 
        d_data->isVisible = on; 
267
 
        itemChanged(); 
268
 
    }
269
 
}
270
 
 
271
 
/*! 
272
 
    \return true if visible
273
 
    \sa setVisible(), show(), hide()
274
 
*/
275
 
bool QwtPlotItem::isVisible() const
276
 
277
 
    return d_data->isVisible; 
278
 
}
279
 
 
280
 
/*! 
281
 
   Update the legend and call QwtPlot::autoRefresh for the 
282
 
   parent plot.
283
 
 
284
 
   \sa updateLegend()
285
 
*/
286
 
void QwtPlotItem::itemChanged()
287
 
{
288
 
    if ( d_data->plot )
289
 
    {
290
 
        if ( d_data->plot->legend() )
291
 
            updateLegend(d_data->plot->legend());
292
 
 
293
 
        d_data->plot->autoRefresh();
294
 
    }
295
 
}
296
 
 
297
 
/*!  
298
 
   Set X and Y axis
299
 
 
300
 
   The item will painted according to the coordinates its Axes.
301
 
 
302
 
   \param xAxis X Axis
303
 
   \param yAxis Y Axis
304
 
 
305
 
   \sa setXAxis(), setYAxis(), xAxis(), yAxis()
306
 
*/
307
 
void QwtPlotItem::setAxis(int xAxis, int yAxis)
308
 
{
309
 
    if (xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
310
 
       d_data->xAxis = xAxis;
311
 
 
312
 
    if (yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
313
 
       d_data->yAxis = yAxis;
314
 
 
315
 
    itemChanged();    
316
 
}
317
 
 
318
 
/*!  
319
 
   Set the X axis
320
 
 
321
 
   The item will painted according to the coordinates its Axes.
322
 
 
323
 
   \param axis X Axis
324
 
   \sa setAxis(), setYAxis(), xAxis()
325
 
*/
326
 
void QwtPlotItem::setXAxis(int axis)
327
 
{
328
 
    if (axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
329
 
    {
330
 
       d_data->xAxis = axis;
331
 
       itemChanged();    
332
 
    }
333
 
}
334
 
 
335
 
/*!  
336
 
   Set the Y axis
337
 
 
338
 
   The item will painted according to the coordinates its Axes.
339
 
 
340
 
   \param axis Y Axis
341
 
   \sa setAxis(), setXAxis(), yAxis()
342
 
*/
343
 
void QwtPlotItem::setYAxis(int axis)
344
 
{
345
 
    if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight )
346
 
    {
347
 
       d_data->yAxis = axis;
348
 
       itemChanged();   
349
 
    }
350
 
}
351
 
 
352
 
//! Return xAxis
353
 
int QwtPlotItem::xAxis() const 
354
 
355
 
    return d_data->xAxis; 
356
 
}
357
 
 
358
 
//! Return yAxis
359
 
int QwtPlotItem::yAxis() const 
360
 
361
 
    return d_data->yAxis; 
362
 
}
363
 
 
364
 
/*!
365
 
   \return An invalid bounding rect: QwtDoubleRect(1.0, 1.0, -2.0, -2.0)
366
 
*/
367
 
QwtDoubleRect QwtPlotItem::boundingRect() const
368
 
{
369
 
    return QwtDoubleRect(1.0, 1.0, -2.0, -2.0); // invalid
370
 
}
371
 
 
372
 
/*!
373
 
   \brief Allocate the widget that represents the item on the legend
374
 
 
375
 
   The default implementation is made for QwtPlotCurve and returns a 
376
 
   QwtLegendItem(), but an item could be represented by any type of widget,
377
 
   by overloading legendItem() and updateLegend().
378
 
 
379
 
   \return QwtLegendItem()
380
 
   \sa updateLegend() QwtLegend()
381
 
*/
382
 
QWidget *QwtPlotItem::legendItem() const
383
 
{
384
 
    return new QwtLegendItem;
385
 
}
386
 
 
387
 
/*!
388
 
   \brief Update the widget that represents the item on the legend
389
 
 
390
 
   updateLegend() is called from itemChanged() to adopt the widget
391
 
   representing the item on the legend to its new configuration.
392
 
   
393
 
   The default implementation is made for QwtPlotCurve and updates a 
394
 
   QwtLegendItem(), but an item could be represented by any type of widget,
395
 
   by overloading legendItem() and updateLegend().
396
 
 
397
 
   \sa legendItem(), itemChanged(), QwtLegend()
398
 
*/
399
 
void QwtPlotItem::updateLegend(QwtLegend *legend) const
400
 
{
401
 
    if ( !legend )
402
 
        return;
403
 
 
404
 
    QWidget *lgdItem = legend->find(this);
405
 
    if ( testItemAttribute(QwtPlotItem::Legend) )
406
 
    {
407
 
        if ( lgdItem == NULL )
408
 
        {
409
 
            lgdItem = legendItem();
410
 
            if ( lgdItem )
411
 
            {
412
 
                if ( lgdItem->inherits("QwtLegendItem") )
413
 
                {
414
 
                    QwtLegendItem *label = (QwtLegendItem *)lgdItem;
415
 
                    label->setItemMode(legend->itemMode());
416
 
 
417
 
                    if ( d_data->plot )
418
 
                    {
419
 
                        QObject::connect(label, SIGNAL(clicked()), 
420
 
                            d_data->plot, SLOT(legendItemClicked()));
421
 
                        QObject::connect(label, SIGNAL(checked(bool)), 
422
 
                            d_data->plot, SLOT(legendItemChecked(bool)));
423
 
                    }
424
 
                }
425
 
                legend->insert(this, lgdItem);
426
 
            }
427
 
        }
428
 
        if ( lgdItem && lgdItem->inherits("QwtLegendItem") )
429
 
        {
430
 
            QwtLegendItem* label = (QwtLegendItem*)lgdItem;
431
 
            if ( label )
432
 
                label->setText(d_data->title);
433
 
        }
434
 
    }
435
 
    else
436
 
    {
437
 
        delete lgdItem;
438
 
    }
439
 
}
440
 
 
441
 
/*!
442
 
   \brief Update the item to changes of the axes scale division
443
 
 
444
 
   Update the item, when the axes of plot have changed.
445
 
   The default implementation does nothing, but items that depend
446
 
   on the scale division (like QwtPlotGrid()) have to reimplement
447
 
   updateScaleDiv()
448
 
 
449
 
   \param xScaleDiv Scale division of the x-axis
450
 
   \param yScaleDiv Scale division of the y-axis
451
 
 
452
 
   \sa QwtPlot::updateAxes()
453
 
*/
454
 
void QwtPlotItem::updateScaleDiv(const QwtScaleDiv &,
455
 
    const QwtScaleDiv &) 
456
 
457
 
}
458
 
 
459
 
/*!
460
 
   \brief Calculate the bounding scale rect of 2 maps
461
 
 
462
 
   \param xMap X map
463
 
   \param yMap X map
464
 
 
465
 
   \return Bounding rect of the scale maps
466
 
*/
467
 
QwtDoubleRect QwtPlotItem::scaleRect(const QwtScaleMap &xMap, 
468
 
    const QwtScaleMap &yMap) const
469
 
{
470
 
    return QwtDoubleRect(xMap.s1(), yMap.s1(), 
471
 
        xMap.sDist(), yMap.sDist() );
472
 
}
473
 
 
474
 
/*!
475
 
   \brief Calculate the bounding paint rect of 2 maps
476
 
 
477
 
   \param xMap X map
478
 
   \param yMap X map
479
 
 
480
 
   \return Bounding rect of the scale maps
481
 
*/
482
 
QRect QwtPlotItem::paintRect(const QwtScaleMap &xMap, 
483
 
    const QwtScaleMap &yMap) const
484
 
{
485
 
    const QRect rect( qRound(xMap.p1()), qRound(yMap.p1()),
486
 
        qRound(xMap.pDist()), qRound(yMap.pDist()) );
487
 
 
488
 
    return rect;
489
 
}
490
 
 
491
 
/*!
492
 
   Transform a rectangle
493
 
 
494
 
   \param xMap X map
495
 
   \param yMap Y map
496
 
   \param rect Rectangle in scale coordinates
497
 
   \return Rectangle in paint coordinates
498
 
 
499
 
   \sa invTransform()
500
 
*/
501
 
QRect QwtPlotItem::transform(const QwtScaleMap &xMap, 
502
 
    const QwtScaleMap &yMap, const QwtDoubleRect& rect) const
503
 
{
504
 
    int x1 = qRound(xMap.transform(rect.left()));
505
 
    int x2 = qRound(xMap.transform(rect.right()));
506
 
    int y1 = qRound(yMap.transform(rect.top()));
507
 
    int y2 = qRound(yMap.transform(rect.bottom()));
508
 
 
509
 
    if ( x2 < x1 )
510
 
        qSwap(x1, x2);
511
 
    if ( y2 < y1 )
512
 
        qSwap(y1, y2);
513
 
 
514
 
    return QRect(x1, y1, x2 - x1 - 1, y2 - y1 - 1);
515
 
}
516
 
 
517
 
/*!
518
 
   Transform a rectangle from paint to scale coordinates
519
 
 
520
 
   \param xMap X map
521
 
   \param yMap Y map
522
 
   \param rect Rectangle in paint coordinates
523
 
   \return Rectangle in scale coordinates
524
 
   \sa transform()
525
 
*/
526
 
QwtDoubleRect QwtPlotItem::invTransform(const QwtScaleMap &xMap, 
527
 
    const QwtScaleMap &yMap, const QRect& rect) const
528
 
{
529
 
    const double x1 = xMap.invTransform(rect.x());
530
 
    const double x2 = xMap.invTransform(rect.x() + rect.width() + 1);
531
 
    const double y1 = yMap.invTransform(rect.y());
532
 
    const double y2 = yMap.invTransform(rect.y() + rect.height() + 1);
533
 
 
534
 
    const QwtDoubleRect r(x1, y1, x2 - x1, y2 - y1);
535
 
 
536
 
    return r.normalized();
537
 
}