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 #endif 00036 00037 #if QT_VERSION < 0x040000 00038 QValueListIterator<QwtPlotItem *> it; 00039 #else 00040 QList<QwtPlotItem *>::Iterator it; 00041 #endif 00042 for ( it = begin(); it != end(); ++it ) 00043 { 00044 if ( *it == item ) 00045 return; 00046 00047 if ( (*it)->z() > item->z() ) 00048 { 00049 insert(it, item); 00050 return; 00051 } 00052 } 00053 append(item); 00054 } 00055 00056 void removeItem(QwtPlotItem *item) 00057 { 00058 if ( item == NULL ) 00059 return; 00060 00061 int i = 0; 00062 00063 #if QT_VERSION < 0x040000 00064 QValueListIterator<QwtPlotItem *> it; 00065 #else 00066 QList<QwtPlotItem *>::Iterator it; 00067 #endif 00068 for ( it = begin(); it != end(); ++it ) 00069 { 00070 if ( item == *it ) 00071 { 00072 #if QT_VERSION < 0x040000 00073 remove(it); 00074 #else 00075 removeAt(i); 00076 #endif 00077 return; 00078 } 00079 i++; 00080 } 00081 } 00082 }; 00083 00084 ItemList itemList; 00085 bool autoDelete; 00086 }; 00087 00094 QwtPlotDict::QwtPlotDict() 00095 { 00096 d_data = new QwtPlotDict::PrivateData; 00097 d_data->autoDelete = true; 00098 } 00099 00106 QwtPlotDict::~QwtPlotDict() 00107 { 00108 detachItems(QwtPlotItem::Rtti_PlotItem, d_data->autoDelete); 00109 delete d_data; 00110 } 00111 00120 void QwtPlotDict::setAutoDelete(bool autoDelete) 00121 { 00122 d_data->autoDelete = autoDelete; 00123 } 00124 00129 bool QwtPlotDict::autoDelete() const 00130 { 00131 return d_data->autoDelete; 00132 } 00133 00146 void QwtPlotDict::attachItem(QwtPlotItem *item, bool on) 00147 { 00148 if ( on ) 00149 d_data->itemList.insertItem(item); 00150 else 00151 d_data->itemList.removeItem(item); 00152 } 00153 00161 void QwtPlotDict::detachItems(int rtti, bool autoDelete) 00162 { 00163 PrivateData::ItemList list = d_data->itemList; 00164 QwtPlotItemIterator it = list.begin(); 00165 while ( it != list.end() ) 00166 { 00167 QwtPlotItem *item = *it; 00168 00169 ++it; // increment before removing item from the list 00170 00171 if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti ) 00172 { 00173 item->attach(NULL); 00174 if ( autoDelete ) 00175 delete item; 00176 } 00177 } 00178 } 00179 00186 const QwtPlotItemList &QwtPlotDict::itemList() const 00187 { 00188 return d_data->itemList; 00189 }