Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_text.h"
00011 #include "qwt_plot.h"
00012 #include "qwt_legend.h"
00013 #include "qwt_legend_item.h"
00014 #include "qwt_plot_item.h"
00015
00016 class QwtPlotItem::PrivateData
00017 {
00018 public:
00019 PrivateData():
00020 plot(NULL),
00021 isVisible(true),
00022 attributes(0),
00023 #if QT_VERSION >= 0x040000
00024 renderHints(0),
00025 #endif
00026 z(0.0),
00027 xAxis(QwtPlot::xBottom),
00028 yAxis(QwtPlot::yLeft)
00029 {
00030 }
00031
00032 mutable QwtPlot *plot;
00033
00034 bool isVisible;
00035 int attributes;
00036 #if QT_VERSION >= 0x040000
00037 int renderHints;
00038 #endif
00039 double z;
00040
00041 int xAxis;
00042 int yAxis;
00043
00044 QwtText title;
00045 };
00046
00051 QwtPlotItem::QwtPlotItem(const QwtText &title)
00052 {
00053 d_data = new PrivateData;
00054 d_data->title = title;
00055 }
00056
00058 QwtPlotItem::~QwtPlotItem()
00059 {
00060 attach(NULL);
00061 delete d_data;
00062 }
00063
00075 void QwtPlotItem::attach(QwtPlot *plot)
00076 {
00077 if ( plot == d_data->plot )
00078 return;
00079
00080
00081
00082 if ( d_data->plot )
00083 {
00084 if ( d_data->plot->legend() )
00085 {
00086 QWidget *legendItem = d_data->plot->legend()->find(this);
00087 if ( legendItem )
00088 delete legendItem;
00089 }
00090
00091 d_data->plot->attachItem(this, false);
00092
00093 if ( d_data->plot->autoReplot() )
00094 d_data->plot->update();
00095 }
00096
00097 d_data->plot = plot;
00098
00099 if ( d_data->plot )
00100 {
00101
00102
00103 d_data->plot->attachItem(this, true);
00104 itemChanged();
00105 }
00106 }
00107
00120 int QwtPlotItem::rtti() const
00121 {
00122 return Rtti_PlotItem;
00123 }
00124
00126 QwtPlot *QwtPlotItem::plot() const
00127 {
00128 return d_data->plot;
00129 }
00130
00136 double QwtPlotItem::z() const
00137 {
00138 return d_data->z;
00139 }
00140
00149 void QwtPlotItem::setZ(double z)
00150 {
00151 if ( d_data->z != z )
00152 {
00153 d_data->z = z;
00154 if ( d_data->plot )
00155 {
00156
00157 d_data->plot->attachItem(this, false);
00158 d_data->plot->attachItem(this, true);
00159 }
00160 itemChanged();
00161 }
00162 }
00163
00170 void QwtPlotItem::setTitle(const QString &title)
00171 {
00172 setTitle(QwtText(title));
00173 }
00174
00181 void QwtPlotItem::setTitle(const QwtText &title)
00182 {
00183 if ( d_data->title != title )
00184 {
00185 d_data->title = title;
00186 itemChanged();
00187 }
00188 }
00189
00194 const QwtText &QwtPlotItem::title() const
00195 {
00196 return d_data->title;
00197 }
00198
00207 void QwtPlotItem::setItemAttribute(ItemAttribute attribute, bool on)
00208 {
00209 if ( bool(d_data->attributes & attribute) != on )
00210 {
00211 if ( on )
00212 d_data->attributes |= attribute;
00213 else
00214 d_data->attributes &= ~attribute;
00215
00216 itemChanged();
00217 }
00218 }
00219
00227 bool QwtPlotItem::testItemAttribute(ItemAttribute attribute) const
00228 {
00229 return d_data->attributes & attribute;
00230 }
00231
00232 #if QT_VERSION >= 0x040000
00233
00242 void QwtPlotItem::setRenderHint(RenderHint hint, bool on)
00243 {
00244 if ( ((d_data->renderHints & hint) != 0) != on )
00245 {
00246 if ( on )
00247 d_data->renderHints |= hint;
00248 else
00249 d_data->renderHints &= ~hint;
00250
00251 itemChanged();
00252 }
00253 }
00254
00262 bool QwtPlotItem::testRenderHint(RenderHint hint) const
00263 {
00264 return (d_data->renderHints & hint);
00265 }
00266
00267 #endif
00268
00270 void QwtPlotItem::show()
00271 {
00272 setVisible(true);
00273 }
00274
00276 void QwtPlotItem::hide()
00277 {
00278 setVisible(false);
00279 }
00280
00287 void QwtPlotItem::setVisible(bool on)
00288 {
00289 if ( on != d_data->isVisible )
00290 {
00291 d_data->isVisible = on;
00292 itemChanged();
00293 }
00294 }
00295
00300 bool QwtPlotItem::isVisible() const
00301 {
00302 return d_data->isVisible;
00303 }
00304
00311 void QwtPlotItem::itemChanged()
00312 {
00313 if ( d_data->plot )
00314 {
00315 if ( d_data->plot->legend() )
00316 updateLegend(d_data->plot->legend());
00317
00318 d_data->plot->autoRefresh();
00319 }
00320 }
00321
00332 void QwtPlotItem::setAxis(int xAxis, int yAxis)
00333 {
00334 if (xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
00335 d_data->xAxis = xAxis;
00336
00337 if (yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
00338 d_data->yAxis = yAxis;
00339
00340 itemChanged();
00341 }
00342
00351 void QwtPlotItem::setXAxis(int axis)
00352 {
00353 if (axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
00354 {
00355 d_data->xAxis = axis;
00356 itemChanged();
00357 }
00358 }
00359
00368 void QwtPlotItem::setYAxis(int axis)
00369 {
00370 if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight )
00371 {
00372 d_data->yAxis = axis;
00373 itemChanged();
00374 }
00375 }
00376
00378 int QwtPlotItem::xAxis() const
00379 {
00380 return d_data->xAxis;
00381 }
00382
00384 int QwtPlotItem::yAxis() const
00385 {
00386 return d_data->yAxis;
00387 }
00388
00392 QwtDoubleRect QwtPlotItem::boundingRect() const
00393 {
00394 return QwtDoubleRect(1.0, 1.0, -2.0, -2.0);
00395 }
00396
00407 QWidget *QwtPlotItem::legendItem() const
00408 {
00409 return new QwtLegendItem;
00410 }
00411
00426 void QwtPlotItem::updateLegend(QwtLegend *legend) const
00427 {
00428 if ( !legend )
00429 return;
00430
00431 QWidget *lgdItem = legend->find(this);
00432 if ( testItemAttribute(QwtPlotItem::Legend) )
00433 {
00434 if ( lgdItem == NULL )
00435 {
00436 lgdItem = legendItem();
00437 if ( lgdItem )
00438 {
00439 if ( lgdItem->inherits("QwtLegendItem") )
00440 {
00441 QwtLegendItem *label = (QwtLegendItem *)lgdItem;
00442 label->setItemMode(legend->itemMode());
00443
00444 if ( d_data->plot )
00445 {
00446 QObject::connect(label, SIGNAL(clicked()),
00447 d_data->plot, SLOT(legendItemClicked()));
00448 QObject::connect(label, SIGNAL(checked(bool)),
00449 d_data->plot, SLOT(legendItemChecked(bool)));
00450 }
00451 }
00452 legend->insert(this, lgdItem);
00453 }
00454 }
00455 if ( lgdItem && lgdItem->inherits("QwtLegendItem") )
00456 {
00457 QwtLegendItem* label = (QwtLegendItem*)lgdItem;
00458 if ( label )
00459 label->setText(d_data->title);
00460 }
00461 }
00462 else
00463 {
00464 delete lgdItem;
00465 }
00466 }
00467
00481 void QwtPlotItem::updateScaleDiv(const QwtScaleDiv &,
00482 const QwtScaleDiv &)
00483 {
00484 }
00485
00494 QwtDoubleRect QwtPlotItem::scaleRect(const QwtScaleMap &xMap,
00495 const QwtScaleMap &yMap) const
00496 {
00497 return QwtDoubleRect(xMap.s1(), yMap.s1(),
00498 xMap.sDist(), yMap.sDist() );
00499 }
00500
00509 QRect QwtPlotItem::paintRect(const QwtScaleMap &xMap,
00510 const QwtScaleMap &yMap) const
00511 {
00512 const QRect rect( qRound(xMap.p1()), qRound(yMap.p1()),
00513 qRound(xMap.pDist()), qRound(yMap.pDist()) );
00514
00515 return rect;
00516 }
00517
00528 QRect QwtPlotItem::transform(const QwtScaleMap &xMap,
00529 const QwtScaleMap &yMap, const QwtDoubleRect& rect) const
00530 {
00531 int x1 = qRound(xMap.transform(rect.left()));
00532 int x2 = qRound(xMap.transform(rect.right()));
00533 int y1 = qRound(yMap.transform(rect.top()));
00534 int y2 = qRound(yMap.transform(rect.bottom()));
00535
00536 if ( x2 < x1 )
00537 qSwap(x1, x2);
00538 if ( y2 < y1 )
00539 qSwap(y1, y2);
00540
00541 return QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
00542 }
00543
00553 QwtDoubleRect QwtPlotItem::invTransform(const QwtScaleMap &xMap,
00554 const QwtScaleMap &yMap, const QRect& rect) const
00555 {
00556 const double x1 = xMap.invTransform(rect.left());
00557 const double x2 = xMap.invTransform(rect.right());
00558 const double y1 = yMap.invTransform(rect.top());
00559 const double y2 = yMap.invTransform(rect.bottom());
00560
00561 const QwtDoubleRect r(x1, y1, x2 - x1, y2 - y1);
00562
00563 return r.normalized();
00564 }