source: ntrip/trunk/BNC/qwtpolar/qwt_polar_itemdict.cpp@ 6984

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