Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

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_plot.h"
00011 #include "qwt_legend.h"
00012 #include "qwt_legend_item.h"
00013 #include "qwt_plot_item.h"
00014 
00015 class QwtPlotItem::PrivateData
00016 {
00017 public:
00018     PrivateData():
00019         plot(NULL),
00020         isVisible(true),
00021         attributes(0),
00022 #if QT_VERSION >= 0x040000
00023         renderHints(0),
00024 #endif
00025         z(0.0),
00026         xAxis(QwtPlot::xBottom),
00027         yAxis(QwtPlot::yLeft)
00028     {
00029     }
00030 
00031     mutable QwtPlot *plot;
00032 
00033     bool isVisible;
00034     int attributes;
00035 #if QT_VERSION >= 0x040000
00036     int renderHints;
00037 #endif
00038     double z;
00039 
00040     int xAxis;
00041     int yAxis;
00042 };
00043 
00045 QwtPlotItem::QwtPlotItem()
00046 {
00047     d_data = new PrivateData;
00048 }
00049 
00051 QwtPlotItem::~QwtPlotItem()
00052 {
00053     attach(NULL);
00054     delete d_data;
00055 }
00056 
00060 void QwtPlotItem::attach(QwtPlot *plot)
00061 {
00062     if ( plot == d_data->plot )
00063         return;
00064 
00065     // remove the item from the previous plot
00066 
00067     if ( d_data->plot )
00068     {
00069         if ( d_data->plot->legend() )
00070         {
00071             QWidget *legendItem = d_data->plot->legend()->find(this);
00072             if ( legendItem )
00073                 delete legendItem; 
00074         }
00075 
00076         d_data->plot->attachItem(this, false);
00077 
00078         if ( d_data->plot->autoReplot() )
00079             d_data->plot->update();
00080     }
00081 
00082     d_data->plot = plot;
00083 
00084     if ( d_data->plot )
00085     {
00086         // insert the item into the current plot
00087 
00088         d_data->plot->attachItem(this, true);
00089         itemChanged();
00090     }
00091 }
00092 
00093 int QwtPlotItem::rtti() const
00094 {
00095     return Rtti_PlotItem;
00096 }
00097 
00099 QwtPlot *QwtPlotItem::plot() const 
00100 { 
00101     return d_data->plot; 
00102 }
00103 
00104 double QwtPlotItem::z() const 
00105 { 
00106     return d_data->z; 
00107 }
00108 
00109 void QwtPlotItem::setZ(double z) 
00110 { 
00111     if ( d_data->z != z )
00112     {
00113         d_data->z = z; 
00114         itemChanged();
00115     }
00116 }
00117 
00118 void QwtPlotItem::setItemAttribute(ItemAttribute attribute, bool on)
00119 {
00120     if ( bool(d_data->attributes & attribute) != on )
00121     {
00122         if ( on )
00123             d_data->attributes |= attribute;
00124         else
00125             d_data->attributes &= ~attribute;
00126 
00127         itemChanged();
00128     }
00129 }
00130 
00131 bool QwtPlotItem::testItemAttribute(ItemAttribute attribute) const
00132 {
00133     return d_data->attributes & attribute;
00134 }
00135 
00136 #if QT_VERSION >= 0x040000
00137 
00138 void QwtPlotItem::setRenderHint(RenderHint hint, bool on)
00139 {
00140     if ( (d_data->renderHints & hint) != on )
00141     {
00142         if ( on )
00143             d_data->renderHints |= hint;
00144         else
00145             d_data->renderHints &= ~hint;
00146 
00147         itemChanged();
00148     }
00149 }
00150 
00151 bool QwtPlotItem::testRenderHint(RenderHint hint) const
00152 {
00153     return (d_data->renderHints & hint);
00154 }
00155 
00156 #endif
00157 
00158 void QwtPlotItem::show()
00159 {
00160     setVisible(true);
00161 }
00162 
00163 void QwtPlotItem::hide()
00164 {
00165     setVisible(false);
00166 }
00167 
00171 void QwtPlotItem::setVisible(bool on) 
00172 { 
00173     if ( on != d_data->isVisible )
00174     {
00175         d_data->isVisible = on; 
00176         itemChanged(); 
00177     }
00178 }
00179 
00184 bool QwtPlotItem::isVisible() const
00185 { 
00186     return d_data->isVisible; 
00187 }
00188 
00190 void QwtPlotItem::itemChanged()
00191 {
00192     if ( d_data->plot )
00193     {
00194         if ( d_data->plot->legend() )
00195             updateLegend(d_data->plot->legend());
00196 
00197         d_data->plot->autoRefresh();
00198     }
00199 }
00200 
00202 void QwtPlotItem::setAxis(int xAxis, int yAxis)
00203 {
00204     if (xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
00205        d_data->xAxis = xAxis;
00206 
00207     if (yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
00208        d_data->yAxis = yAxis;
00209 
00210     itemChanged();    
00211 }
00212 
00214 void QwtPlotItem::setXAxis(int axis)
00215 {
00216     if (axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
00217     {
00218        d_data->xAxis = axis;
00219        itemChanged();    
00220     }
00221 }
00222 
00224 void QwtPlotItem::setYAxis(int axis)
00225 {
00226     if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight )
00227     {
00228        d_data->yAxis = axis;
00229        itemChanged();   
00230     }
00231 }
00232 
00234 int QwtPlotItem::xAxis() const 
00235 { 
00236     return d_data->xAxis; 
00237 }
00238 
00240 int QwtPlotItem::yAxis() const 
00241 { 
00242     return d_data->yAxis; 
00243 }
00244 
00245 QwtDoubleRect QwtPlotItem::boundingRect() const
00246 {
00247     return QwtDoubleRect(1.0, 1.0, -2.0, -2.0); // invalid
00248 }
00249 
00250 QWidget *QwtPlotItem::legendItem() const
00251 {
00252     return new QwtLegendItem;
00253 }
00254 
00255 void QwtPlotItem::updateLegend(QwtLegend *legend) const
00256 {
00257     if ( !legend )
00258         return;
00259 
00260     QWidget *lgdItem = legend->find(this);
00261     if ( testItemAttribute(QwtPlotItem::Legend) )
00262     {
00263         if ( lgdItem == NULL )
00264         {
00265             lgdItem = legendItem();
00266             if ( lgdItem )
00267             {
00268                 if ( lgdItem->inherits("QwtLegendItem") )
00269                 {
00270                     QwtLegendItem *label = (QwtLegendItem *)lgdItem;
00271                     label->setItemMode(legend->itemMode());
00272 
00273                     if ( d_data->plot )
00274                     {
00275                         QObject::connect(label, SIGNAL(clicked()), 
00276                             d_data->plot, SLOT(legendItemClicked()));
00277                         QObject::connect(label, SIGNAL(checked(bool)), 
00278                             d_data->plot, SLOT(legendItemChecked(bool)));
00279                     }
00280                 }
00281                 legend->insert(this, lgdItem);
00282             }
00283         }
00284     }
00285     else
00286     {
00287         delete lgdItem;
00288     }
00289 }
00290 
00291 void QwtPlotItem::updateScaleDiv(const QwtScaleDiv &,
00292     const QwtScaleDiv &) 
00293 { 
00294 }
00295 

Generated on Mon Jan 30 22:16:25 2006 for Qwt User's Guide by  doxygen 1.4.4