qwt_plot_item.cpp

00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
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 
00048 QwtPlotItem::QwtPlotItem(const QwtText &title)
00049 {
00050     d_data = new PrivateData;
00051     d_data->title = title;
00052 }
00053 
00055 QwtPlotItem::~QwtPlotItem()
00056 {
00057     attach(NULL);
00058     delete d_data;
00059 }
00060 
00071 void QwtPlotItem::attach(QwtPlot *plot)
00072 {
00073     if ( plot == d_data->plot )
00074         return;
00075 
00076     // remove the item from the previous plot
00077 
00078     if ( d_data->plot )
00079     {
00080         if ( d_data->plot->legend() )
00081         {
00082             QWidget *legendItem = d_data->plot->legend()->find(this);
00083             if ( legendItem )
00084                 delete legendItem; 
00085         }
00086 
00087         d_data->plot->attachItem(this, false);
00088 
00089         if ( d_data->plot->autoReplot() )
00090             d_data->plot->update();
00091     }
00092 
00093     d_data->plot = plot;
00094 
00095     if ( d_data->plot )
00096     {
00097         // insert the item into the current plot
00098 
00099         d_data->plot->attachItem(this, true);
00100         itemChanged();
00101     }
00102 }
00103 
00116 int QwtPlotItem::rtti() const
00117 {
00118     return Rtti_PlotItem;
00119 }
00120 
00122 QwtPlot *QwtPlotItem::plot() const 
00123 { 
00124     return d_data->plot; 
00125 }
00126 
00132 double QwtPlotItem::z() const 
00133 { 
00134     return d_data->z; 
00135 }
00136 
00145 void QwtPlotItem::setZ(double z) 
00146 { 
00147     if ( d_data->z != z )
00148     {
00149         d_data->z = z; 
00150         if ( d_data->plot )
00151         {
00152             // update the z order
00153             d_data->plot->attachItem(this, false);
00154             d_data->plot->attachItem(this, true);
00155         }
00156         itemChanged();
00157     }
00158 }
00159 
00166 void QwtPlotItem::setTitle(const QString &title)
00167 {
00168     setTitle(QwtText(title));
00169 }
00170 
00177 void QwtPlotItem::setTitle(const QwtText &title)
00178 {
00179     if ( d_data->title != title )
00180     {
00181         d_data->title = title; 
00182         itemChanged();
00183     }
00184 }
00185 
00190 const QwtText &QwtPlotItem::title() const
00191 {
00192     return d_data->title;
00193 }
00194 
00203 void QwtPlotItem::setItemAttribute(ItemAttribute attribute, bool on)
00204 {
00205     if ( bool(d_data->attributes & attribute) != on )
00206     {
00207         if ( on )
00208             d_data->attributes |= attribute;
00209         else
00210             d_data->attributes &= ~attribute;
00211 
00212         itemChanged();
00213     }
00214 }
00215 
00223 bool QwtPlotItem::testItemAttribute(ItemAttribute attribute) const
00224 {
00225     return d_data->attributes & attribute;
00226 }
00227 
00228 #if QT_VERSION >= 0x040000
00229 
00238 void QwtPlotItem::setRenderHint(RenderHint hint, bool on)
00239 {
00240     if ( ((d_data->renderHints & hint) != 0) != on )
00241     {
00242         if ( on )
00243             d_data->renderHints |= hint;
00244         else
00245             d_data->renderHints &= ~hint;
00246 
00247         itemChanged();
00248     }
00249 }
00250 
00258 bool QwtPlotItem::testRenderHint(RenderHint hint) const
00259 {
00260     return (d_data->renderHints & hint);
00261 }
00262 
00263 #endif
00264 
00265 void QwtPlotItem::show()
00266 {
00267     setVisible(true);
00268 }
00269 
00270 void QwtPlotItem::hide()
00271 {
00272     setVisible(false);
00273 }
00274 
00281 void QwtPlotItem::setVisible(bool on) 
00282 { 
00283     if ( on != d_data->isVisible )
00284     {
00285         d_data->isVisible = on; 
00286         itemChanged(); 
00287     }
00288 }
00289 
00294 bool QwtPlotItem::isVisible() const
00295 { 
00296     return d_data->isVisible; 
00297 }
00298 
00305 void QwtPlotItem::itemChanged()
00306 {
00307     if ( d_data->plot )
00308     {
00309         if ( d_data->plot->legend() )
00310             updateLegend(d_data->plot->legend());
00311 
00312         d_data->plot->autoRefresh();
00313     }
00314 }
00315 
00326 void QwtPlotItem::setAxis(int xAxis, int yAxis)
00327 {
00328     if (xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
00329        d_data->xAxis = xAxis;
00330 
00331     if (yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
00332        d_data->yAxis = yAxis;
00333 
00334     itemChanged();    
00335 }
00336 
00345 void QwtPlotItem::setXAxis(int axis)
00346 {
00347     if (axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
00348     {
00349        d_data->xAxis = axis;
00350        itemChanged();    
00351     }
00352 }
00353 
00362 void QwtPlotItem::setYAxis(int axis)
00363 {
00364     if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight )
00365     {
00366        d_data->yAxis = axis;
00367        itemChanged();   
00368     }
00369 }
00370 
00372 int QwtPlotItem::xAxis() const 
00373 { 
00374     return d_data->xAxis; 
00375 }
00376 
00378 int QwtPlotItem::yAxis() const 
00379 { 
00380     return d_data->yAxis; 
00381 }
00382 
00386 QwtDoubleRect QwtPlotItem::boundingRect() const
00387 {
00388     return QwtDoubleRect(1.0, 1.0, -2.0, -2.0); // invalid
00389 }
00390 
00401 QWidget *QwtPlotItem::legendItem() const
00402 {
00403     return new QwtLegendItem;
00404 }
00405 
00418 void QwtPlotItem::updateLegend(QwtLegend *legend) const
00419 {
00420     if ( !legend )
00421         return;
00422 
00423     QWidget *lgdItem = legend->find(this);
00424     if ( testItemAttribute(QwtPlotItem::Legend) )
00425     {
00426         if ( lgdItem == NULL )
00427         {
00428             lgdItem = legendItem();
00429             if ( lgdItem )
00430             {
00431                 if ( lgdItem->inherits("QwtLegendItem") )
00432                 {
00433                     QwtLegendItem *label = (QwtLegendItem *)lgdItem;
00434                     label->setItemMode(legend->itemMode());
00435 
00436                     if ( d_data->plot )
00437                     {
00438                         QObject::connect(label, SIGNAL(clicked()), 
00439                             d_data->plot, SLOT(legendItemClicked()));
00440                         QObject::connect(label, SIGNAL(checked(bool)), 
00441                             d_data->plot, SLOT(legendItemChecked(bool)));
00442                     }
00443                 }
00444                 legend->insert(this, lgdItem);
00445             }
00446         }
00447         if ( lgdItem && lgdItem->inherits("QwtLegendItem") )
00448         {
00449             QwtLegendItem* label = (QwtLegendItem*)lgdItem;
00450             if ( label )
00451                 label->setText(d_data->title);
00452         }
00453     }
00454     else
00455     {
00456         delete lgdItem;
00457     }
00458 }
00459 
00473 void QwtPlotItem::updateScaleDiv(const QwtScaleDiv &,
00474     const QwtScaleDiv &) 
00475 { 
00476 }
00477 
00486 QwtDoubleRect QwtPlotItem::scaleRect(const QwtScaleMap &xMap, 
00487     const QwtScaleMap &yMap) const
00488 {
00489     return QwtDoubleRect(xMap.s1(), yMap.s1(), 
00490         xMap.sDist(), yMap.sDist() );
00491 }
00492 
00501 QRect QwtPlotItem::paintRect(const QwtScaleMap &xMap, 
00502     const QwtScaleMap &yMap) const
00503 {
00504     const QRect rect( qRound(xMap.p1()), qRound(yMap.p1()),
00505         qRound(xMap.pDist()), qRound(yMap.pDist()) );
00506 
00507     return rect;
00508 }
00509 
00520 QRect QwtPlotItem::transform(const QwtScaleMap &xMap, 
00521     const QwtScaleMap &yMap, const QwtDoubleRect& rect) const
00522 {
00523     int x1 = qRound(xMap.transform(rect.left()));
00524     int x2 = qRound(xMap.transform(rect.right()));
00525     int y1 = qRound(yMap.transform(rect.top()));
00526     int y2 = qRound(yMap.transform(rect.bottom()));
00527 
00528     if ( x2 < x1 )
00529         qSwap(x1, x2);
00530     if ( y2 < y1 )
00531         qSwap(y1, y2);
00532 
00533     return QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
00534 }
00535 
00545 QwtDoubleRect QwtPlotItem::invTransform(const QwtScaleMap &xMap, 
00546     const QwtScaleMap &yMap, const QRect& rect) const
00547 {
00548     const double x1 = xMap.invTransform(rect.left());
00549     const double x2 = xMap.invTransform(rect.right());
00550     const double y1 = yMap.invTransform(rect.top());
00551     const double y2 = yMap.invTransform(rect.bottom());
00552         
00553     const QwtDoubleRect r(x1, y1, x2 - x1, y2 - y1);
00554 
00555     return r.normalized();
00556 }

Generated on Sun Jul 22 11:26:54 2007 for Qwt User's Guide by  doxygen 1.5.2