qwt_plot_dict.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 // vim: expandtab
00011 
00012 #include "qwt_plot_dict.h"
00013 
00014 class QwtPlotDict::PrivateData
00015 {
00016 public:
00017 
00018 #if QT_VERSION < 0x040000
00019     class ItemList: public QValueList<QwtPlotItem *>
00020 #else
00021     class ItemList: public QList<QwtPlotItem *>
00022 #endif
00023     {
00024     public:
00025         void insertItem(QwtPlotItem *item)
00026         {
00027             if ( item == NULL )
00028                 return;
00029 
00030             // Unfortunately there is no inSort operation
00031             // for lists in Qt4. The implementation below
00032             // is slow, but there shouldn't be many plot items.
00033 
00034 #ifdef __GNUC__
00035 #warning binary search missing
00036 #endif
00037 
00038 #if QT_VERSION < 0x040000
00039             QValueListIterator<QwtPlotItem *> it;
00040 #else
00041             QList<QwtPlotItem *>::Iterator it;
00042 #endif
00043             for ( it = begin(); it != end(); ++it )
00044             {
00045                 if ( *it == item )
00046                     return;
00047 
00048                 if ( (*it)->z() > item->z() )
00049                 {
00050                     insert(it, item);
00051                     return;
00052                 }
00053             }
00054             append(item);
00055         }
00056 
00057         void removeItem(QwtPlotItem *item)
00058         {
00059             if ( item == NULL )
00060                 return;
00061 
00062             int i = 0;
00063 
00064 #if QT_VERSION < 0x040000
00065             QValueListIterator<QwtPlotItem *> it;
00066 #else
00067             QList<QwtPlotItem *>::Iterator it;
00068 #endif
00069             for ( it = begin(); it != end(); ++it )
00070             {
00071                 if ( item == *it )
00072                 {
00073 #if QT_VERSION < 0x040000
00074                     remove(it);
00075 #else
00076                     removeAt(i);
00077 #endif
00078                     return;
00079                 }
00080                 i++;
00081             }
00082         }
00083     };
00084 
00085     ItemList itemList;
00086     bool autoDelete;
00087 };
00088 
00095 QwtPlotDict::QwtPlotDict()
00096 {
00097     d_data = new QwtPlotDict::PrivateData;
00098     d_data->autoDelete = true;
00099 }
00100 
00107 QwtPlotDict::~QwtPlotDict()
00108 {
00109     detachItems(QwtPlotItem::Rtti_PlotItem, d_data->autoDelete);
00110     delete d_data;
00111 }
00112 
00121 void QwtPlotDict::setAutoDelete(bool autoDelete)
00122 {
00123     d_data->autoDelete = autoDelete;
00124 }
00125 
00130 bool QwtPlotDict::autoDelete() const
00131 {
00132     return d_data->autoDelete;
00133 }
00134 
00147 void QwtPlotDict::attachItem(QwtPlotItem *item, bool on)
00148 {
00149     if ( on )
00150         d_data->itemList.insertItem(item);
00151     else
00152         d_data->itemList.removeItem(item);
00153 }
00154 
00162 void QwtPlotDict::detachItems(int rtti, bool autoDelete)
00163 {
00164     PrivateData::ItemList list = d_data->itemList;
00165     QwtPlotItemIterator it = list.begin();
00166     while ( it != list.end() )
00167     {
00168         QwtPlotItem *item = *it;
00169 
00170         ++it; // increment before removing item from the list
00171 
00172         if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
00173         {
00174             item->attach(NULL);
00175             if ( autoDelete )
00176                 delete item;
00177         }
00178     }
00179 }
00180 
00187 const QwtPlotItemList &QwtPlotDict::itemList() const
00188 {
00189     return d_data->itemList;
00190 }

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