~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.0/src/qwt_plot_dict.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2
 
 * Qwt Widget Library
3
 
 * Copyright (C) 1997   Josef Wilgen
4
 
 * Copyright (C) 2002   Uwe Rathmann
5
 
 *
6
 
 * This library is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the Qwt License, Version 1.0
8
 
 *****************************************************************************/
9
 
 
10
 
// vim: expandtab
11
 
 
12
 
#include "qwt_plot_dict.h"
13
 
 
14
 
class QwtPlotDict::PrivateData
15
 
{
16
 
public:
17
 
 
18
 
#if QT_VERSION < 0x040000
19
 
    class ItemList: public QValueList<QwtPlotItem *>
20
 
#else
21
 
    class ItemList: public QList<QwtPlotItem *>
22
 
#endif
23
 
    {
24
 
    public:
25
 
        void insertItem(QwtPlotItem *item)
26
 
        {
27
 
            if ( item == NULL )
28
 
                return;
29
 
 
30
 
            // Unfortunately there is no inSort operation
31
 
            // for lists in Qt4. The implementation below
32
 
            // is slow, but there shouldn't be many plot items.
33
 
 
34
 
#ifdef __GNUC__
35
 
#endif
36
 
 
37
 
#if QT_VERSION < 0x040000
38
 
            QValueListIterator<QwtPlotItem *> it;
39
 
#else
40
 
            QList<QwtPlotItem *>::Iterator it;
41
 
#endif
42
 
            for ( it = begin(); it != end(); ++it )
43
 
            {
44
 
                if ( *it == item )
45
 
                    return;
46
 
 
47
 
                if ( (*it)->z() > item->z() )
48
 
                {
49
 
                    insert(it, item);
50
 
                    return;
51
 
                }
52
 
            }
53
 
            append(item);
54
 
        }
55
 
 
56
 
        void removeItem(QwtPlotItem *item)
57
 
        {
58
 
            if ( item == NULL )
59
 
                return;
60
 
 
61
 
            int i = 0;
62
 
 
63
 
#if QT_VERSION < 0x040000
64
 
            QValueListIterator<QwtPlotItem *> it;
65
 
#else
66
 
            QList<QwtPlotItem *>::Iterator it;
67
 
#endif
68
 
            for ( it = begin(); it != end(); ++it )
69
 
            {
70
 
                if ( item == *it )
71
 
                {
72
 
#if QT_VERSION < 0x040000
73
 
                    remove(it);
74
 
#else
75
 
                    removeAt(i);
76
 
#endif
77
 
                    return;
78
 
                }
79
 
                i++;
80
 
            }
81
 
        }
82
 
    };
83
 
 
84
 
    ItemList itemList;
85
 
    bool autoDelete;
86
 
};
87
 
 
88
 
/*! 
89
 
   Constructor 
90
 
 
91
 
   Auto deletion is enabled.
92
 
   \sa setAutoDelete, attachItem
93
 
*/
94
 
QwtPlotDict::QwtPlotDict()
95
 
{
96
 
    d_data = new QwtPlotDict::PrivateData;
97
 
    d_data->autoDelete = true;
98
 
}
99
 
 
100
 
/*! 
101
 
   Destructor
102
 
 
103
 
   If autoDelete is on, all attached items will be deleted
104
 
   \sa setAutoDelete, autoDelete, attachItem
105
 
*/
106
 
QwtPlotDict::~QwtPlotDict()
107
 
{
108
 
    detachItems(QwtPlotItem::Rtti_PlotItem, d_data->autoDelete);
109
 
    delete d_data;
110
 
}
111
 
 
112
 
/*!
113
 
   En/Disable Auto deletion
114
 
 
115
 
   If Auto deletion is on all attached plot items will be deleted
116
 
   in the destructor of QwtPlotDict. The default value is on.
117
 
 
118
 
   \sa autoDelete, attachItem
119
 
*/
120
 
void QwtPlotDict::setAutoDelete(bool autoDelete)
121
 
{
122
 
    d_data->autoDelete = autoDelete;
123
 
}
124
 
 
125
 
/*!
126
 
   \return true if auto deletion is enabled
127
 
   \sa setAutoDelete, attachItem
128
 
*/
129
 
bool QwtPlotDict::autoDelete() const
130
 
{
131
 
    return d_data->autoDelete;
132
 
}
133
 
 
134
 
/*!
135
 
   Attach/Detach a plot item
136
 
 
137
 
   Attached items will be deleted in the destructor,
138
 
   if auto deletion is enabled (default). Manually detached
139
 
   items are not deleted.
140
 
 
141
 
   \param item Plot item to attach/detach
142
 
   \ on If true attach, else detach the item
143
 
 
144
 
   \sa setAutoDelete, ~QwtPlotDict
145
 
*/
146
 
void QwtPlotDict::attachItem(QwtPlotItem *item, bool on)
147
 
{
148
 
    if ( on )
149
 
        d_data->itemList.insertItem(item);
150
 
    else
151
 
        d_data->itemList.removeItem(item);
152
 
}
153
 
 
154
 
/*!
155
 
   Detach items from the dictionary
156
 
 
157
 
   \param rtti In case of QwtPlotItem::Rtti_PlotItem detach all items 
158
 
               otherwise only those items of the type rtti.
159
 
   \param autoDelete If true, delete all detached items
160
 
*/
161
 
void QwtPlotDict::detachItems(int rtti, bool autoDelete)
162
 
{
163
 
    PrivateData::ItemList list = d_data->itemList;
164
 
    QwtPlotItemIterator it = list.begin();
165
 
    while ( it != list.end() )
166
 
    {
167
 
        QwtPlotItem *item = *it;
168
 
 
169
 
        ++it; // increment before removing item from the list
170
 
 
171
 
        if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
172
 
        {
173
 
            item->attach(NULL);
174
 
            if ( autoDelete )
175
 
                delete item;
176
 
        }
177
 
    }
178
 
}
179
 
 
180
 
//! \brief A QwtPlotItemList of all attached plot items.
181
 
///
182
 
/// Use caution when iterating these lists, as removing/detaching an item will
183
 
/// invalidate the iterator. Instead you can place pointers to objects to be
184
 
/// removed in a removal list, and traverse that list later.
185
 
//! \return List of all attached plot items.
186
 
const QwtPlotItemList &QwtPlotDict::itemList() const
187
 
{
188
 
    return d_data->itemList;
189
 
}