qwt_plot_dict.cpp 4.37 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
/* -*- 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
 *****************************************************************************/

#include "qwt_plot_dict.h"

class QwtPlotDict::PrivateData
{
public:

    class ItemList: public QList<QwtPlotItem *>
    {
    public:
Bryant's avatar
Bryant committed
19 20
        void insertItem( QwtPlotItem *item )
        {
pixhawk's avatar
pixhawk committed
21 22 23
            if ( item == NULL )
                return;

Bryant's avatar
Bryant committed
24 25 26
            QList<QwtPlotItem *>::iterator it =
                qUpperBound( begin(), end(), item, LessZThan() );
            insert( it, item );
pixhawk's avatar
pixhawk committed
27 28
        }

Bryant's avatar
Bryant committed
29 30
        void removeItem( QwtPlotItem *item )
        {
pixhawk's avatar
pixhawk committed
31 32 33
            if ( item == NULL )
                return;

Bryant's avatar
Bryant committed
34 35 36 37 38 39 40 41 42
            QList<QwtPlotItem *>::iterator it =
                qLowerBound( begin(), end(), item, LessZThan() );

            for ( ; it != end(); ++it )
            {
                if ( item == *it )
                {
                    erase( it );
                    break;
pixhawk's avatar
pixhawk committed
43 44 45
                }
            }
        }
Bryant's avatar
Bryant committed
46 47 48 49 50 51 52 53 54 55
    private:
        class LessZThan
        {
        public:
            inline bool operator()( const QwtPlotItem *item1,
                const QwtPlotItem *item2 ) const
            {
                return item1->z() < item2->z();
            }
        };
pixhawk's avatar
pixhawk committed
56 57 58 59 60 61
    };

    ItemList itemList;
    bool autoDelete;
};

62 63
/*!
   Constructor
pixhawk's avatar
pixhawk committed
64 65

   Auto deletion is enabled.
Bryant's avatar
Bryant committed
66
   \sa setAutoDelete(), QwtPlotItem::attach()
pixhawk's avatar
pixhawk committed
67 68 69 70 71 72 73
*/
QwtPlotDict::QwtPlotDict()
{
    d_data = new QwtPlotDict::PrivateData;
    d_data->autoDelete = true;
}

74
/*!
pixhawk's avatar
pixhawk committed
75 76
   Destructor

Bryant's avatar
Bryant committed
77 78
   If autoDelete() is on, all attached items will be deleted
   \sa setAutoDelete(), autoDelete(), QwtPlotItem::attach()
pixhawk's avatar
pixhawk committed
79 80 81
*/
QwtPlotDict::~QwtPlotDict()
{
Bryant's avatar
Bryant committed
82
    detachItems( QwtPlotItem::Rtti_PlotItem, d_data->autoDelete );
pixhawk's avatar
pixhawk committed
83 84 85 86 87 88 89 90 91
    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.

Bryant's avatar
Bryant committed
92
   \sa autoDelete(), insertItem()
pixhawk's avatar
pixhawk committed
93
*/
Bryant's avatar
Bryant committed
94
void QwtPlotDict::setAutoDelete( bool autoDelete )
pixhawk's avatar
pixhawk committed
95 96 97 98 99 100
{
    d_data->autoDelete = autoDelete;
}

/*!
   \return true if auto deletion is enabled
Bryant's avatar
Bryant committed
101
   \sa setAutoDelete(), insertItem()
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108
*/
bool QwtPlotDict::autoDelete() const
{
    return d_data->autoDelete;
}

/*!
Bryant's avatar
Bryant committed
109
  Insert a plot item
pixhawk's avatar
pixhawk committed
110

Bryant's avatar
Bryant committed
111 112 113 114 115 116 117
  \param item PlotItem
  \sa removeItem()
 */
void QwtPlotDict::insertItem( QwtPlotItem *item )
{
    d_data->itemList.insertItem( item );
}
pixhawk's avatar
pixhawk committed
118

Bryant's avatar
Bryant committed
119 120
/*!
  Remove a plot item
pixhawk's avatar
pixhawk committed
121

Bryant's avatar
Bryant committed
122 123 124 125
  \param item PlotItem
  \sa insertItem()
 */
void QwtPlotDict::removeItem( QwtPlotItem *item )
pixhawk's avatar
pixhawk committed
126
{
Bryant's avatar
Bryant committed
127
    d_data->itemList.removeItem( item );
pixhawk's avatar
pixhawk committed
128 129 130 131 132
}

/*!
   Detach items from the dictionary

133
   \param rtti In case of QwtPlotItem::Rtti_PlotItem detach all items
pixhawk's avatar
pixhawk committed
134 135 136
               otherwise only those items of the type rtti.
   \param autoDelete If true, delete all detached items
*/
Bryant's avatar
Bryant committed
137
void QwtPlotDict::detachItems( int rtti, bool autoDelete )
pixhawk's avatar
pixhawk committed
138 139 140
{
    PrivateData::ItemList list = d_data->itemList;
    QwtPlotItemIterator it = list.begin();
Bryant's avatar
Bryant committed
141 142
    while ( it != list.end() )
    {
pixhawk's avatar
pixhawk committed
143 144 145 146
        QwtPlotItem *item = *it;

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

Bryant's avatar
Bryant committed
147 148 149
        if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
        {
            item->attach( NULL );
pixhawk's avatar
pixhawk committed
150 151 152 153 154 155
            if ( autoDelete )
                delete item;
        }
    }
}

Bryant's avatar
Bryant committed
156 157 158 159 160 161 162 163 164
/*!
  \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.
*/
pixhawk's avatar
pixhawk committed
165 166 167 168
const QwtPlotItemList &QwtPlotDict::itemList() const
{
    return d_data->itemList;
}
Bryant's avatar
Bryant committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

/*!
  \return List of all attached plot items of a specific type.
  \param rtti See QwtPlotItem::RttiValues
  \sa QwtPlotItem::rtti()
*/
QwtPlotItemList QwtPlotDict::itemList( int rtti ) const
{
    if ( rtti == QwtPlotItem::Rtti_PlotItem )
        return d_data->itemList;

    QwtPlotItemList items;

    PrivateData::ItemList list = d_data->itemList;
    for ( QwtPlotItemIterator it = list.begin(); it != list.end(); ++it )
    {
        QwtPlotItem *item = *it;
        if ( item->rtti() == rtti )
            items += item;
    }

    return items;
}