| 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 |
|
|---|
| 13 | class 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 |
|
|---|
| 63 | /*!
|
|---|
| 64 | Constructor
|
|---|
| 65 |
|
|---|
| 66 | Auto deletion is enabled.
|
|---|
| 67 | \sa setAutoDelete(), QwtPlotItem::attach()
|
|---|
| 68 | */
|
|---|
| 69 | QwtPlotDict::QwtPlotDict()
|
|---|
| 70 | {
|
|---|
| 71 | m_data = new QwtPlotDict::PrivateData;
|
|---|
| 72 | m_data->autoDelete = true;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /*!
|
|---|
| 76 | Destructor
|
|---|
| 77 |
|
|---|
| 78 | If autoDelete() is on, all attached items will be deleted
|
|---|
| 79 | \sa setAutoDelete(), autoDelete(), QwtPlotItem::attach()
|
|---|
| 80 | */
|
|---|
| 81 | QwtPlotDict::~QwtPlotDict()
|
|---|
| 82 | {
|
|---|
| 83 | detachItems( QwtPlotItem::Rtti_PlotItem, m_data->autoDelete );
|
|---|
| 84 | delete m_data;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*!
|
|---|
| 88 | En/Disable Auto deletion
|
|---|
| 89 |
|
|---|
| 90 | If Auto deletion is on all attached plot items will be deleted
|
|---|
| 91 | in the destructor of QwtPlotDict. The default value is on.
|
|---|
| 92 |
|
|---|
| 93 | \sa autoDelete(), insertItem()
|
|---|
| 94 | */
|
|---|
| 95 | void QwtPlotDict::setAutoDelete( bool autoDelete )
|
|---|
| 96 | {
|
|---|
| 97 | m_data->autoDelete = autoDelete;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /*!
|
|---|
| 101 | \return true if auto deletion is enabled
|
|---|
| 102 | \sa setAutoDelete(), insertItem()
|
|---|
| 103 | */
|
|---|
| 104 | bool QwtPlotDict::autoDelete() const
|
|---|
| 105 | {
|
|---|
| 106 | return m_data->autoDelete;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /*!
|
|---|
| 110 | Insert a plot item
|
|---|
| 111 |
|
|---|
| 112 | \param item PlotItem
|
|---|
| 113 | \sa removeItem()
|
|---|
| 114 | */
|
|---|
| 115 | void QwtPlotDict::insertItem( QwtPlotItem* item )
|
|---|
| 116 | {
|
|---|
| 117 | m_data->itemList.insertItem( item );
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /*!
|
|---|
| 121 | Remove a plot item
|
|---|
| 122 |
|
|---|
| 123 | \param item PlotItem
|
|---|
| 124 | \sa insertItem()
|
|---|
| 125 | */
|
|---|
| 126 | void QwtPlotDict::removeItem( QwtPlotItem* item )
|
|---|
| 127 | {
|
|---|
| 128 | m_data->itemList.removeItem( item );
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /*!
|
|---|
| 132 | Detach items from the dictionary
|
|---|
| 133 |
|
|---|
| 134 | \param rtti In case of QwtPlotItem::Rtti_PlotItem detach all items
|
|---|
| 135 | otherwise only those items of the type rtti.
|
|---|
| 136 | \param autoDelete If true, delete all detached items
|
|---|
| 137 | */
|
|---|
| 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 |
|
|---|
| 157 | /*!
|
|---|
| 158 | \brief A QwtPlotItemList of all attached plot items.
|
|---|
| 159 |
|
|---|
| 160 | Use caution when iterating these lists, as removing/detaching an item will
|
|---|
| 161 | invalidate the iterator. Instead you can place pointers to objects to be
|
|---|
| 162 | removed in a removal list, and traverse that list later.
|
|---|
| 163 |
|
|---|
| 164 | \return List of all attached plot items.
|
|---|
| 165 | */
|
|---|
| 166 | const QwtPlotItemList& QwtPlotDict::itemList() const
|
|---|
| 167 | {
|
|---|
| 168 | return m_data->itemList;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*!
|
|---|
| 172 | \return List of all attached plot items of a specific type.
|
|---|
| 173 | \param rtti See QwtPlotItem::RttiValues
|
|---|
| 174 | \sa QwtPlotItem::rtti()
|
|---|
| 175 | */
|
|---|
| 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 | }
|
|---|