Qwt User's Guide: qwt_plot_dict.cpp Source File

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
25 QList< QwtPlotItem* >::iterator it =
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
35 QList< QwtPlotItem* >::iterator it =
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
69QwtPlotDict::QwtPlotDict()
70{
71 m_data = new QwtPlotDict::PrivateData;
72 m_data->autoDelete = true;
73}
74
81QwtPlotDict::~QwtPlotDict()
82{
83 detachItems( QwtPlotItem::Rtti_PlotItem, m_data->autoDelete );
84 delete m_data;
85}
86
95 void QwtPlotDict::setAutoDelete( bool autoDelete )
96{
97 m_data->autoDelete = autoDelete;
98}
99
104 bool QwtPlotDict::autoDelete() const
105{
106 return m_data->autoDelete;
107}
108
115 void QwtPlotDict::insertItem( QwtPlotItem* item )
116{
117 m_data->itemList.insertItem( item );
118}
119
126 void QwtPlotDict::removeItem( QwtPlotItem* item )
127{
128 m_data->itemList.removeItem( item );
129}
130
138 void 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
166 const QwtPlotItemList& QwtPlotDict::itemList() const
167{
168 return m_data->itemList;
169}
170
176 QwtPlotItemList QwtPlotDict::itemList( int rtti ) const
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}
QList
Definition qwt_abstract_legend.h:17
QwtPlotDict::QwtPlotDict
QwtPlotDict()
Definition qwt_plot_dict.cpp:69
QwtPlotDict::setAutoDelete
void setAutoDelete(bool)
Definition qwt_plot_dict.cpp:95
QwtPlotDict::insertItem
void insertItem(QwtPlotItem *)
Definition qwt_plot_dict.cpp:115
QwtPlotDict::itemList
const QwtPlotItemList & itemList() const
A QwtPlotItemList of all attached plot items.
Definition qwt_plot_dict.cpp:166
QwtPlotDict::autoDelete
bool autoDelete() const
Definition qwt_plot_dict.cpp:104
QwtPlotDict::~QwtPlotDict
virtual ~QwtPlotDict()
Definition qwt_plot_dict.cpp:81
QwtPlotDict::removeItem
void removeItem(QwtPlotItem *)
Definition qwt_plot_dict.cpp:126
QwtPlotDict::detachItems
void detachItems(int rtti=QwtPlotItem::Rtti_PlotItem, bool autoDelete=true)
Definition qwt_plot_dict.cpp:138
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::z
double z() const
Definition qwt_plot_item.cpp:152
QwtPlotItem::rtti
virtual int rtti() const
Definition qwt_plot_item.cpp:136
QwtPlotItem::Rtti_PlotItem
@ Rtti_PlotItem
Unspecific value, that can be used, when it doesn't matter.
Definition qwt_plot_item.h:78
QwtPlotItem::attach
void attach(QwtPlot *plot)
Attach the item to a plot.
Definition qwt_plot_item.cpp:98