1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
12
#include "qwt_legend.h"
13
#include "qwt_legend_item.h"
14
#include "qwt_plot_item.h"
16
class QwtPlotItem::PrivateData
23
#if QT_VERSION >= 0x040000
27
xAxis(QwtPlot::xBottom),
32
mutable QwtPlot *plot;
36
#if QT_VERSION >= 0x040000
48
QwtPlotItem::QwtPlotItem(const QwtText &title)
50
d_data = new PrivateData;
51
d_data->title = title;
54
//! Destroy the QwtPlotItem
55
QwtPlotItem::~QwtPlotItem()
62
Attach the item to a plot
64
void QwtPlotItem::attach(QwtPlot *plot)
66
if ( plot == d_data->plot )
69
// remove the item from the previous plot
73
if ( d_data->plot->legend() )
75
QWidget *legendItem = d_data->plot->legend()->find(this);
80
d_data->plot->attachItem(this, false);
82
if ( d_data->plot->autoReplot() )
83
d_data->plot->update();
90
// insert the item into the current plot
92
d_data->plot->attachItem(this, true);
97
int QwtPlotItem::rtti() const
102
//! Return attached plot
103
QwtPlot *QwtPlotItem::plot() const
109
Plot items are painted in increasing z-order.
111
\return setZ(), QwtPlotDict::itemList()
113
double QwtPlotItem::z() const
119
\brief Set the z value
121
Plot items are painted in increasing z-order.
124
\sa z(), QwtPlotDict::itemList()
126
void QwtPlotItem::setZ(double z)
128
if ( d_data->z != z )
133
// update the z order
134
d_data->plot->attachItem(this, false);
135
d_data->plot->attachItem(this, true);
147
void QwtPlotItem::setTitle(const QString &title)
149
setTitle(QwtText(title));
158
void QwtPlotItem::setTitle(const QwtText &title)
160
if ( d_data->title != title )
162
d_data->title = title;
168
\return Title of the item
171
const QwtText &QwtPlotItem::title() const
173
return d_data->title;
177
Toggle an item attribute
179
\param attribute Attribute type
182
\sa testItemAttribute(), ItemAttribute
184
void QwtPlotItem::setItemAttribute(ItemAttribute attribute, bool on)
186
if ( bool(d_data->attributes & attribute) != on )
189
d_data->attributes |= attribute;
191
d_data->attributes &= ~attribute;
198
Test an item attribute
200
\param ItemAttribute Attribute type
202
\sa setItemAttribute(), ItemAttribute
204
bool QwtPlotItem::testItemAttribute(ItemAttribute attribute) const
206
return d_data->attributes & attribute;
209
#if QT_VERSION >= 0x040000
212
Toggle an render hint
214
\param hint Render hint
217
\sa testRenderHint(), RenderHint
219
void QwtPlotItem::setRenderHint(RenderHint hint, bool on)
221
if ( ((d_data->renderHints & hint) != 0) != on )
224
d_data->renderHints |= hint;
226
d_data->renderHints &= ~hint;
235
\param hint Render hint
237
\sa setRenderHint(), RenderHint
239
bool QwtPlotItem::testRenderHint(RenderHint hint) const
241
return (d_data->renderHints & hint);
246
void QwtPlotItem::show()
251
void QwtPlotItem::hide()
259
\param on Show if true, otherwise hide
260
\sa isVisible(), show(), hide()
262
void QwtPlotItem::setVisible(bool on)
264
if ( on != d_data->isVisible )
266
d_data->isVisible = on;
272
\return true if visible
273
\sa setVisible(), show(), hide()
275
bool QwtPlotItem::isVisible() const
277
return d_data->isVisible;
281
Update the legend and call QwtPlot::autoRefresh for the
286
void QwtPlotItem::itemChanged()
290
if ( d_data->plot->legend() )
291
updateLegend(d_data->plot->legend());
293
d_data->plot->autoRefresh();
300
The item will painted according to the coordinates its Axes.
305
\sa setXAxis(), setYAxis(), xAxis(), yAxis()
307
void QwtPlotItem::setAxis(int xAxis, int yAxis)
309
if (xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
310
d_data->xAxis = xAxis;
312
if (yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
313
d_data->yAxis = yAxis;
321
The item will painted according to the coordinates its Axes.
324
\sa setAxis(), setYAxis(), xAxis()
326
void QwtPlotItem::setXAxis(int axis)
328
if (axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
330
d_data->xAxis = axis;
338
The item will painted according to the coordinates its Axes.
341
\sa setAxis(), setXAxis(), yAxis()
343
void QwtPlotItem::setYAxis(int axis)
345
if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight )
347
d_data->yAxis = axis;
353
int QwtPlotItem::xAxis() const
355
return d_data->xAxis;
359
int QwtPlotItem::yAxis() const
361
return d_data->yAxis;
365
\return An invalid bounding rect: QwtDoubleRect(1.0, 1.0, -2.0, -2.0)
367
QwtDoubleRect QwtPlotItem::boundingRect() const
369
return QwtDoubleRect(1.0, 1.0, -2.0, -2.0); // invalid
373
\brief Allocate the widget that represents the item on the legend
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().
379
\return QwtLegendItem()
380
\sa updateLegend() QwtLegend()
382
QWidget *QwtPlotItem::legendItem() const
384
return new QwtLegendItem;
388
\brief Update the widget that represents the item on the legend
390
updateLegend() is called from itemChanged() to adopt the widget
391
representing the item on the legend to its new configuration.
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().
397
\sa legendItem(), itemChanged(), QwtLegend()
399
void QwtPlotItem::updateLegend(QwtLegend *legend) const
404
QWidget *lgdItem = legend->find(this);
405
if ( testItemAttribute(QwtPlotItem::Legend) )
407
if ( lgdItem == NULL )
409
lgdItem = legendItem();
412
if ( lgdItem->inherits("QwtLegendItem") )
414
QwtLegendItem *label = (QwtLegendItem *)lgdItem;
415
label->setItemMode(legend->itemMode());
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)));
425
legend->insert(this, lgdItem);
428
if ( lgdItem && lgdItem->inherits("QwtLegendItem") )
430
QwtLegendItem* label = (QwtLegendItem*)lgdItem;
432
label->setText(d_data->title);
442
\brief Update the item to changes of the axes scale division
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
449
\param xScaleDiv Scale division of the x-axis
450
\param yScaleDiv Scale division of the y-axis
452
\sa QwtPlot::updateAxes()
454
void QwtPlotItem::updateScaleDiv(const QwtScaleDiv &,
460
\brief Calculate the bounding scale rect of 2 maps
465
\return Bounding rect of the scale maps
467
QwtDoubleRect QwtPlotItem::scaleRect(const QwtScaleMap &xMap,
468
const QwtScaleMap &yMap) const
470
return QwtDoubleRect(xMap.s1(), yMap.s1(),
471
xMap.sDist(), yMap.sDist() );
475
\brief Calculate the bounding paint rect of 2 maps
480
\return Bounding rect of the scale maps
482
QRect QwtPlotItem::paintRect(const QwtScaleMap &xMap,
483
const QwtScaleMap &yMap) const
485
const QRect rect( qRound(xMap.p1()), qRound(yMap.p1()),
486
qRound(xMap.pDist()), qRound(yMap.pDist()) );
492
Transform a rectangle
496
\param rect Rectangle in scale coordinates
497
\return Rectangle in paint coordinates
501
QRect QwtPlotItem::transform(const QwtScaleMap &xMap,
502
const QwtScaleMap &yMap, const QwtDoubleRect& rect) const
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()));
514
return QRect(x1, y1, x2 - x1 - 1, y2 - y1 - 1);
518
Transform a rectangle from paint to scale coordinates
522
\param rect Rectangle in paint coordinates
523
\return Rectangle in scale coordinates
526
QwtDoubleRect QwtPlotItem::invTransform(const QwtScaleMap &xMap,
527
const QwtScaleMap &yMap, const QRect& rect) const
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);
534
const QwtDoubleRect r(x1, y1, x2 - x1, y2 - y1);
536
return r.normalized();