qwt_plot_dict.cpp 4.33 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

// vim: expandtab

#include "qwt_plot_dict.h"

class QwtPlotDict::PrivateData
{
public:

#if QT_VERSION < 0x040000
    class ItemList: public QValueList<QwtPlotItem *>
#else
    class ItemList: public QList<QwtPlotItem *>
#endif
    {
    public:
25
        void insertItem(QwtPlotItem *item) {
pixhawk's avatar
pixhawk committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
            if ( item == NULL )
                return;

            // Unfortunately there is no inSort operation
            // for lists in Qt4. The implementation below
            // is slow, but there shouldn't be many plot items.

#ifdef __GNUC__
#endif

#if QT_VERSION < 0x040000
            QValueListIterator<QwtPlotItem *> it;
#else
            QList<QwtPlotItem *>::Iterator it;
#endif
41
            for ( it = begin(); it != end(); ++it ) {
pixhawk's avatar
pixhawk committed
42 43 44
                if ( *it == item )
                    return;

45
                if ( (*it)->z() > item->z() ) {
pixhawk's avatar
pixhawk committed
46 47 48 49 50 51 52
                    insert(it, item);
                    return;
                }
            }
            append(item);
        }

53
        void removeItem(QwtPlotItem *item) {
pixhawk's avatar
pixhawk committed
54 55 56 57 58 59 60 61 62 63
            if ( item == NULL )
                return;

            int i = 0;

#if QT_VERSION < 0x040000
            QValueListIterator<QwtPlotItem *> it;
#else
            QList<QwtPlotItem *>::Iterator it;
#endif
64 65
            for ( it = begin(); it != end(); ++it ) {
                if ( item == *it ) {
pixhawk's avatar
pixhawk committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#if QT_VERSION < 0x040000
                    remove(it);
#else
                    removeAt(i);
#endif
                    return;
                }
                i++;
            }
        }
    };

    ItemList itemList;
    bool autoDelete;
};

82 83
/*!
   Constructor
pixhawk's avatar
pixhawk committed
84 85 86 87 88 89 90 91 92 93

   Auto deletion is enabled.
   \sa setAutoDelete, attachItem
*/
QwtPlotDict::QwtPlotDict()
{
    d_data = new QwtPlotDict::PrivateData;
    d_data->autoDelete = true;
}

94
/*!
pixhawk's avatar
pixhawk committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
   Destructor

   If autoDelete is on, all attached items will be deleted
   \sa setAutoDelete, autoDelete, attachItem
*/
QwtPlotDict::~QwtPlotDict()
{
    detachItems(QwtPlotItem::Rtti_PlotItem, d_data->autoDelete);
    delete d_data;
}

/*!
   En/Disable Auto deletion

   If Auto deletion is on all attached plot items will be deleted
   in the destructor of QwtPlotDict. The default value is on.

   \sa autoDelete, attachItem
*/
void QwtPlotDict::setAutoDelete(bool autoDelete)
{
    d_data->autoDelete = autoDelete;
}

/*!
   \return true if auto deletion is enabled
   \sa setAutoDelete, attachItem
*/
bool QwtPlotDict::autoDelete() const
{
    return d_data->autoDelete;
}

/*!
   Attach/Detach a plot item

   Attached items will be deleted in the destructor,
   if auto deletion is enabled (default). Manually detached
   items are not deleted.

   \param item Plot item to attach/detach
   \ on If true attach, else detach the item

   \sa setAutoDelete, ~QwtPlotDict
*/
void QwtPlotDict::attachItem(QwtPlotItem *item, bool on)
{
    if ( on )
        d_data->itemList.insertItem(item);
    else
        d_data->itemList.removeItem(item);
}

/*!
   Detach items from the dictionary

151
   \param rtti In case of QwtPlotItem::Rtti_PlotItem detach all items
pixhawk's avatar
pixhawk committed
152 153 154 155 156 157 158
               otherwise only those items of the type rtti.
   \param autoDelete If true, delete all detached items
*/
void QwtPlotDict::detachItems(int rtti, bool autoDelete)
{
    PrivateData::ItemList list = d_data->itemList;
    QwtPlotItemIterator it = list.begin();
159
    while ( it != list.end() ) {
pixhawk's avatar
pixhawk committed
160 161 162 163
        QwtPlotItem *item = *it;

        ++it; // increment before removing item from the list

164
        if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti ) {
pixhawk's avatar
pixhawk committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            item->attach(NULL);
            if ( autoDelete )
                delete item;
        }
    }
}

//! \brief A QwtPlotItemList of all attached plot items.
///
/// Use caution when iterating these lists, as removing/detaching an item will
/// invalidate the iterator. Instead you can place pointers to objects to be
/// removed in a removal list, and traverse that list later.
//! \return List of all attached plot items.
const QwtPlotItemList &QwtPlotDict::itemList() const
{
    return d_data->itemList;
}