Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_plot_dict.cpp
1/******************************************************************************
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#include <algorithm>
12
13class QwtPlotDict::PrivateData
14{
15 public:
16
17 class ItemList : public QList< QwtPlotItem* >
18 {
19 public:
20 void insertItem( QwtPlotItem* item )
21 {
22 if ( item == NULL )
23 return;
24
26 std::upper_bound( begin(), end(), item, LessZThan() );
27 insert( it, item );
28 }
29
30 void removeItem( QwtPlotItem* item )
31 {
32 if ( item == NULL )
33 return;
34
36 std::lower_bound( begin(), end(), item, LessZThan() );
37
38 for ( ; it != end(); ++it )
39 {
40 if ( item == *it )
41 {
42 erase( it );
43 break;
44 }
45 }
46 }
47 private:
48 class LessZThan
49 {
50 public:
51 inline bool operator()( const QwtPlotItem* item1,
52 const QwtPlotItem* item2 ) const
53 {
54 return item1->z() < item2->z();
55 }
56 };
57 };
58
59 ItemList itemList;
60 bool autoDelete;
61};
62
70{
71 m_data = new QwtPlotDict::PrivateData;
72 m_data->autoDelete = true;
73}
74
82{
83 detachItems( QwtPlotItem::Rtti_PlotItem, m_data->autoDelete );
84 delete m_data;
85}
86
95void QwtPlotDict::setAutoDelete( bool autoDelete )
96{
97 m_data->autoDelete = autoDelete;
98}
99
105{
106 return m_data->autoDelete;
107}
108
116{
117 m_data->itemList.insertItem( item );
118}
119
127{
128 m_data->itemList.removeItem( item );
129}
130
138void QwtPlotDict::detachItems( int rtti, bool autoDelete )
139{
140 PrivateData::ItemList list = m_data->itemList;
141 QwtPlotItemIterator it = list.constBegin();
142 while ( it != list.constEnd() )
143 {
144 QwtPlotItem* item = *it;
145
146 ++it; // increment before removing item from the list
147
148 if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
149 {
150 item->attach( NULL );
151 if ( autoDelete )
152 delete item;
153 }
154 }
155}
156
167{
168 return m_data->itemList;
169}
170
177{
178 if ( rtti == QwtPlotItem::Rtti_PlotItem )
179 return m_data->itemList;
180
181 QwtPlotItemList items;
182
183 PrivateData::ItemList list = m_data->itemList;
184 for ( QwtPlotItemIterator it = list.constBegin(); it != list.constEnd(); ++it )
185 {
186 QwtPlotItem* item = *it;
187 if ( item->rtti() == rtti )
188 items += item;
189 }
190
191 return items;
192}
void setAutoDelete(bool)
void insertItem(QwtPlotItem *)
const QwtPlotItemList & itemList() const
A QwtPlotItemList of all attached plot items.
bool autoDelete() const
virtual ~QwtPlotDict()
void removeItem(QwtPlotItem *)
void detachItems(int rtti=QwtPlotItem::Rtti_PlotItem, bool autoDelete=true)
Base class for items on the plot canvas.
double z() const
virtual int rtti() const
@ Rtti_PlotItem
Unspecific value, that can be used, when it doesn't matter.
void attach(QwtPlot *plot)
Attach the item to a plot.