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 | #include "qwt_plot_dict.h"
|
---|
11 |
|
---|
12 | class QwtPlotDict::PrivateData
|
---|
13 | {
|
---|
14 | public:
|
---|
15 |
|
---|
16 | class ItemList: public QList<QwtPlotItem *>
|
---|
17 | {
|
---|
18 | public:
|
---|
19 | void insertItem( QwtPlotItem *item )
|
---|
20 | {
|
---|
21 | if ( item == NULL )
|
---|
22 | return;
|
---|
23 |
|
---|
24 | QList<QwtPlotItem *>::iterator it =
|
---|
25 | qUpperBound( begin(), end(), item, LessZThan() );
|
---|
26 | insert( it, item );
|
---|
27 | }
|
---|
28 |
|
---|
29 | void removeItem( QwtPlotItem *item )
|
---|
30 | {
|
---|
31 | if ( item == NULL )
|
---|
32 | return;
|
---|
33 |
|
---|
34 | QList<QwtPlotItem *>::iterator it =
|
---|
35 | qLowerBound( begin(), end(), item, LessZThan() );
|
---|
36 |
|
---|
37 | for ( ; it != end(); ++it )
|
---|
38 | {
|
---|
39 | if ( item == *it )
|
---|
40 | {
|
---|
41 | erase( it );
|
---|
42 | break;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | private:
|
---|
47 | class LessZThan
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | inline bool operator()( const QwtPlotItem *item1,
|
---|
51 | const QwtPlotItem *item2 ) const
|
---|
52 | {
|
---|
53 | return item1->z() < item2->z();
|
---|
54 | }
|
---|
55 | };
|
---|
56 | };
|
---|
57 |
|
---|
58 | ItemList itemList;
|
---|
59 | bool autoDelete;
|
---|
60 | };
|
---|
61 |
|
---|
62 | /*!
|
---|
63 | Constructor
|
---|
64 |
|
---|
65 | Auto deletion is enabled.
|
---|
66 | \sa setAutoDelete(), attachItem()
|
---|
67 | */
|
---|
68 | QwtPlotDict::QwtPlotDict()
|
---|
69 | {
|
---|
70 | d_data = new QwtPlotDict::PrivateData;
|
---|
71 | d_data->autoDelete = true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*!
|
---|
75 | Destructor
|
---|
76 |
|
---|
77 | If autoDelete is on, all attached items will be deleted
|
---|
78 | \sa setAutoDelete(), autoDelete(), attachItem()
|
---|
79 | */
|
---|
80 | QwtPlotDict::~QwtPlotDict()
|
---|
81 | {
|
---|
82 | detachItems( QwtPlotItem::Rtti_PlotItem, d_data->autoDelete );
|
---|
83 | delete d_data;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*!
|
---|
87 | En/Disable Auto deletion
|
---|
88 |
|
---|
89 | If Auto deletion is on all attached plot items will be deleted
|
---|
90 | in the destructor of QwtPlotDict. The default value is on.
|
---|
91 |
|
---|
92 | \sa autoDelete(), attachItem()
|
---|
93 | */
|
---|
94 | void QwtPlotDict::setAutoDelete( bool autoDelete )
|
---|
95 | {
|
---|
96 | d_data->autoDelete = autoDelete;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*!
|
---|
100 | \return true if auto deletion is enabled
|
---|
101 | \sa setAutoDelete(), attachItem()
|
---|
102 | */
|
---|
103 | bool QwtPlotDict::autoDelete() const
|
---|
104 | {
|
---|
105 | return d_data->autoDelete;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*!
|
---|
109 | Attach/Detach a plot item
|
---|
110 |
|
---|
111 | Attached items will be deleted in the destructor,
|
---|
112 | if auto deletion is enabled (default). Manually detached
|
---|
113 | items are not deleted.
|
---|
114 |
|
---|
115 | \param item Plot item to attach/detach
|
---|
116 | \ on If true attach, else detach the item
|
---|
117 |
|
---|
118 | \sa setAutoDelete(), ~QwtPlotDict()
|
---|
119 | */
|
---|
120 | void QwtPlotDict::attachItem( QwtPlotItem *item, bool on )
|
---|
121 | {
|
---|
122 | if ( on )
|
---|
123 | d_data->itemList.insertItem( item );
|
---|
124 | else
|
---|
125 | d_data->itemList.removeItem( item );
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*!
|
---|
129 | Detach items from the dictionary
|
---|
130 |
|
---|
131 | \param rtti In case of QwtPlotItem::Rtti_PlotItem detach all items
|
---|
132 | otherwise only those items of the type rtti.
|
---|
133 | \param autoDelete If true, delete all detached items
|
---|
134 | */
|
---|
135 | void QwtPlotDict::detachItems( int rtti, bool autoDelete )
|
---|
136 | {
|
---|
137 | PrivateData::ItemList list = d_data->itemList;
|
---|
138 | QwtPlotItemIterator it = list.begin();
|
---|
139 | while ( it != list.end() )
|
---|
140 | {
|
---|
141 | QwtPlotItem *item = *it;
|
---|
142 |
|
---|
143 | ++it; // increment before removing item from the list
|
---|
144 |
|
---|
145 | if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
|
---|
146 | {
|
---|
147 | item->attach( NULL );
|
---|
148 | if ( autoDelete )
|
---|
149 | delete item;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*!
|
---|
155 | \brief A QwtPlotItemList of all attached plot items.
|
---|
156 |
|
---|
157 | Use caution when iterating these lists, as removing/detaching an item will
|
---|
158 | invalidate the iterator. Instead you can place pointers to objects to be
|
---|
159 | removed in a removal list, and traverse that list later.
|
---|
160 |
|
---|
161 | \return List of all attached plot items.
|
---|
162 | */
|
---|
163 | const QwtPlotItemList &QwtPlotDict::itemList() const
|
---|
164 | {
|
---|
165 | return d_data->itemList;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*!
|
---|
169 | \return List of all attached plot items of a specific type.
|
---|
170 | \sa QwtPlotItem::rtti()
|
---|
171 | */
|
---|
172 | QwtPlotItemList QwtPlotDict::itemList( int rtti ) const
|
---|
173 | {
|
---|
174 | if ( rtti == QwtPlotItem::Rtti_PlotItem )
|
---|
175 | return d_data->itemList;
|
---|
176 |
|
---|
177 | QwtPlotItemList items;
|
---|
178 |
|
---|
179 | PrivateData::ItemList list = d_data->itemList;
|
---|
180 | for ( QwtPlotItemIterator it = list.begin(); it != list.end(); ++it )
|
---|
181 | {
|
---|
182 | QwtPlotItem *item = *it;
|
---|
183 | if ( item->rtti() == rtti )
|
---|
184 | items += item;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return items;
|
---|
188 | }
|
---|