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
*****************************************************************************/
10
#include "qwt_plot_item.h"
13
#include "qwt_legend.h"
14
#include "qwt_legend_item.h"
15
#include "qwt_scale_div.h"
18
class QwtPlotItem::PrivateData
27
xAxis( QwtPlot::xBottom ),
28
yAxis( QwtPlot::yLeft )
32
mutable QwtPlot *plot;
35
QwtPlotItem::ItemAttributes attributes;
36
QwtPlotItem::RenderHints renderHints;
47
\param title Title of the item
49
QwtPlotItem::QwtPlotItem( const QwtText &title )
51
d_data = new PrivateData;
52
d_data->title = title;
55
//! Destroy the QwtPlotItem
56
QwtPlotItem::~QwtPlotItem()
63
\brief Attach the item to a plot.
65
This method will attach a QwtPlotItem to the QwtPlot argument. It will first
66
detach the QwtPlotItem from any plot from a previous call to attach (if
67
necessary). If a NULL argument is passed, it will detach from any QwtPlot it
70
\param plot Plot widget
73
void QwtPlotItem::attach( QwtPlot *plot )
75
if ( plot == d_data->plot )
78
// remove the item from the previous plot
82
if ( d_data->plot->legend() )
83
d_data->plot->legend()->remove( this );
85
d_data->plot->attachItem( this, false );
87
if ( d_data->plot->autoReplot() )
88
d_data->plot->update();
95
// insert the item into the current plot
97
d_data->plot->attachItem( this, true );
103
\brief This method detaches a QwtPlotItem from any
104
QwtPlot it has been associated with.
106
detach() is equivalent to calling attach( NULL )
109
void QwtPlotItem::detach()
115
Return rtti for the specific class represented. QwtPlotItem is simply
116
a virtual interface class, and base classes will implement this method
117
with specific rtti values so a user can differentiate them.
119
The rtti value is useful for environments, where the
120
runtime type information is disabled and it is not possible
121
to do a dynamic_cast<...>.
126
int QwtPlotItem::rtti() const
128
return Rtti_PlotItem;
131
//! Return attached plot
132
QwtPlot *QwtPlotItem::plot() const
138
Plot items are painted in increasing z-order.
140
\return setZ(), QwtPlotDict::itemList()
142
double QwtPlotItem::z() const
148
\brief Set the z value
150
Plot items are painted in increasing z-order.
153
\sa z(), QwtPlotDict::itemList()
155
void QwtPlotItem::setZ( double z )
157
if ( d_data->z != z )
159
if ( d_data->plot ) // update the z order
160
d_data->plot->attachItem( this, false );
165
d_data->plot->attachItem( this, true );
177
void QwtPlotItem::setTitle( const QString &title )
179
setTitle( QwtText( title ) );
188
void QwtPlotItem::setTitle( const QwtText &title )
190
if ( d_data->title != title )
192
d_data->title = title;
198
\return Title of the item
201
const QwtText &QwtPlotItem::title() const
203
return d_data->title;
207
Toggle an item attribute
209
\param attribute Attribute type
212
\sa testItemAttribute(), ItemAttribute
214
void QwtPlotItem::setItemAttribute( ItemAttribute attribute, bool on )
216
if ( bool( d_data->attributes & attribute ) != on )
219
d_data->attributes |= attribute;
221
d_data->attributes &= ~attribute;
228
Test an item attribute
230
\param attribute Attribute type
232
\sa setItemAttribute(), ItemAttribute
234
bool QwtPlotItem::testItemAttribute( ItemAttribute attribute ) const
236
return ( d_data->attributes & attribute );
240
Toggle an render hint
242
\param hint Render hint
245
\sa testRenderHint(), RenderHint
247
void QwtPlotItem::setRenderHint( RenderHint hint, bool on )
249
if ( ( ( d_data->renderHints & hint ) != 0 ) != on )
252
d_data->renderHints |= hint;
254
d_data->renderHints &= ~hint;
263
\param hint Render hint
265
\sa setRenderHint(), RenderHint
267
bool QwtPlotItem::testRenderHint( RenderHint hint ) const
269
return ( d_data->renderHints & hint );
273
void QwtPlotItem::show()
279
void QwtPlotItem::hide()
287
\param on Show if true, otherwise hide
288
\sa isVisible(), show(), hide()
290
void QwtPlotItem::setVisible( bool on )
292
if ( on != d_data->isVisible )
294
d_data->isVisible = on;
300
\return true if visible
301
\sa setVisible(), show(), hide()
303
bool QwtPlotItem::isVisible() const
305
return d_data->isVisible;
309
Update the legend and call QwtPlot::autoRefresh for the
314
void QwtPlotItem::itemChanged()
318
if ( d_data->plot->legend() )
319
updateLegend( d_data->plot->legend() );
321
d_data->plot->autoRefresh();
328
The item will painted according to the coordinates its Axes.
333
\sa setXAxis(), setYAxis(), xAxis(), yAxis()
335
void QwtPlotItem::setAxes( int xAxis, int yAxis )
337
if ( xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
338
d_data->xAxis = xAxis;
340
if ( yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
341
d_data->yAxis = yAxis;
349
The item will painted according to the coordinates its Axes.
352
\sa setAxes(), setYAxis(), xAxis()
354
void QwtPlotItem::setXAxis( int axis )
356
if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
358
d_data->xAxis = axis;
366
The item will painted according to the coordinates its Axes.
369
\sa setAxes(), setXAxis(), yAxis()
371
void QwtPlotItem::setYAxis( int axis )
373
if ( axis == QwtPlot::yLeft || axis == QwtPlot::yRight )
375
d_data->yAxis = axis;
381
int QwtPlotItem::xAxis() const
383
return d_data->xAxis;
387
int QwtPlotItem::yAxis() const
389
return d_data->yAxis;
393
\return An invalid bounding rect: QRectF(1.0, 1.0, -2.0, -2.0)
395
QRectF QwtPlotItem::boundingRect() const
397
return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
401
\brief Allocate the widget that represents the item on the legend
403
The default implementation returns a QwtLegendItem(), but an item
404
could be represented by any type of widget,
405
by overloading legendItem() and updateLegend().
407
\return QwtLegendItem()
408
\sa updateLegend() QwtLegend()
410
QWidget *QwtPlotItem::legendItem() const
412
QwtLegendItem *item = new QwtLegendItem;
415
QObject::connect( item, SIGNAL( clicked() ),
416
d_data->plot, SLOT( legendItemClicked() ) );
417
QObject::connect( item, SIGNAL( checked( bool ) ),
418
d_data->plot, SLOT( legendItemChecked( bool ) ) );
424
\brief Update the widget that represents the item on the legend
426
updateLegend() is called from itemChanged() to adopt the widget
427
representing the item on the legend to its new configuration.
429
The default implementation updates a QwtLegendItem(),
430
but an item could be represented by any type of widget,
431
by overloading legendItem() and updateLegend().
435
\sa legendItem(), itemChanged(), QwtLegend()
437
void QwtPlotItem::updateLegend( QwtLegend *legend ) const
439
if ( legend == NULL )
442
QWidget *lgdItem = legend->find( this );
443
if ( testItemAttribute( QwtPlotItem::Legend ) )
445
if ( lgdItem == NULL )
447
lgdItem = legendItem();
449
legend->insert( this, lgdItem );
451
if ( lgdItem && lgdItem->inherits( "QwtLegendItem" ) )
453
QwtLegendItem* label = ( QwtLegendItem* )lgdItem;
456
// paint the identifier
457
const QSize sz = label->identifierSize();
459
QPixmap identifier( sz.width(), sz.height() );
460
identifier.fill( Qt::transparent );
462
QPainter painter( &identifier );
463
painter.setRenderHint( QPainter::Antialiasing,
464
testRenderHint( QwtPlotItem::RenderAntialiased ) );
465
drawLegendIdentifier( &painter,
466
QRect( 0, 0, sz.width(), sz.height() ) );
469
const bool doUpdate = label->updatesEnabled();
471
label->setUpdatesEnabled( false );
473
label->setText( title() );
474
label->setIdentifier( identifier );
475
label->setItemMode( legend->itemMode() );
478
label->setUpdatesEnabled( true );
489
lgdItem->deleteLater();
495
\brief Update the item to changes of the axes scale division
497
Update the item, when the axes of plot have changed.
498
The default implementation does nothing, but items that depend
499
on the scale division (like QwtPlotGrid()) have to reimplement
502
\param xScaleDiv Scale division of the x-axis
503
\param yScaleDiv Scale division of the y-axis
505
\sa QwtPlot::updateAxes()
507
void QwtPlotItem::updateScaleDiv( const QwtScaleDiv &xScaleDiv,
508
const QwtScaleDiv &yScaleDiv )
510
Q_UNUSED( xScaleDiv );
511
Q_UNUSED( yScaleDiv );
515
\brief Calculate the bounding scale rect of 2 maps
520
\return Bounding scale rect of the scale maps, normalized
522
QRectF QwtPlotItem::scaleRect( const QwtScaleMap &xMap,
523
const QwtScaleMap &yMap ) const
525
return QRectF( xMap.s1(), yMap.s1(),
526
xMap.sDist(), yMap.sDist() );
530
\brief Calculate the bounding paint rect of 2 maps
535
\return Bounding paint rect of the scale maps, normalized
537
QRectF QwtPlotItem::paintRect( const QwtScaleMap &xMap,
538
const QwtScaleMap &yMap ) const
540
const QRectF rect( xMap.p1(), yMap.p1(),
541
xMap.pDist(), yMap.pDist() );