qwt_plot_dict.cpp 4.6 KB

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