Skip to content
Snippets Groups Projects
qwt_plot_dict.cpp 4.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • pixhawk's avatar
    pixhawk committed
    /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
     * Qwt Widget Library
     * Copyright (C) 1997   Josef Wilgen
     * Copyright (C) 2002   Uwe Rathmann
     *
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the Qwt License, Version 1.0
     *****************************************************************************/
    
    #include "qwt_plot_dict.h"
    
    class QwtPlotDict::PrivateData
    {
    public:
    
        class ItemList: public QList<QwtPlotItem *>
        {
        public:
    
    Bryant's avatar
    Bryant committed
            void insertItem( QwtPlotItem *item )
            {
    
    pixhawk's avatar
    pixhawk committed
                if ( item == NULL )
                    return;
    
    
    Bryant's avatar
    Bryant committed
                QList<QwtPlotItem *>::iterator it =
                    qUpperBound( begin(), end(), item, LessZThan() );
                insert( it, item );
    
    pixhawk's avatar
    pixhawk committed
            }
    
    
    Bryant's avatar
    Bryant committed
            void removeItem( QwtPlotItem *item )
            {
    
    pixhawk's avatar
    pixhawk committed
                if ( item == NULL )
                    return;
    
    
    Bryant's avatar
    Bryant committed
                QList<QwtPlotItem *>::iterator it =
                    qLowerBound( begin(), end(), item, LessZThan() );
    
                for ( ; it != end(); ++it )
                {
                    if ( item == *it )
                    {
                        erase( it );
                        break;
    
    pixhawk's avatar
    pixhawk committed
                    }
                }
            }
    
    Bryant's avatar
    Bryant committed
        private:
            class LessZThan
            {
            public:
                inline bool operator()( const QwtPlotItem *item1,
                    const QwtPlotItem *item2 ) const
                {
                    return item1->z() < item2->z();
                }
            };
    
    pixhawk's avatar
    pixhawk committed
        };
    
        ItemList itemList;
        bool autoDelete;
    };
    
    
    /*!
       Constructor
    
    pixhawk's avatar
    pixhawk committed
    
       Auto deletion is enabled.
    
    Bryant's avatar
    Bryant committed
       \sa setAutoDelete(), QwtPlotItem::attach()
    
    pixhawk's avatar
    pixhawk committed
    */
    QwtPlotDict::QwtPlotDict()
    {
        d_data = new QwtPlotDict::PrivateData;
        d_data->autoDelete = true;
    }
    
    
    pixhawk's avatar
    pixhawk committed
       Destructor
    
    
    Bryant's avatar
    Bryant committed
       If autoDelete() is on, all attached items will be deleted
       \sa setAutoDelete(), autoDelete(), QwtPlotItem::attach()
    
    pixhawk's avatar
    pixhawk committed
    */
    QwtPlotDict::~QwtPlotDict()
    {
    
    Bryant's avatar
    Bryant committed
        detachItems( QwtPlotItem::Rtti_PlotItem, d_data->autoDelete );
    
    pixhawk's avatar
    pixhawk committed
        delete d_data;
    }
    
    /*!
       En/Disable Auto deletion
    
       If Auto deletion is on all attached plot items will be deleted
       in the destructor of QwtPlotDict. The default value is on.
    
    
    Bryant's avatar
    Bryant committed
       \sa autoDelete(), insertItem()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    void QwtPlotDict::setAutoDelete( bool autoDelete )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->autoDelete = autoDelete;
    }
    
    /*!
       \return true if auto deletion is enabled
    
    Bryant's avatar
    Bryant committed
       \sa setAutoDelete(), insertItem()
    
    pixhawk's avatar
    pixhawk committed
    */
    bool QwtPlotDict::autoDelete() const
    {
        return d_data->autoDelete;
    }
    
    /*!
    
    Bryant's avatar
    Bryant committed
      Insert a plot item
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
      \param item PlotItem
      \sa removeItem()
     */
    void QwtPlotDict::insertItem( QwtPlotItem *item )
    {
        d_data->itemList.insertItem( item );
    }
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
    /*!
      Remove a plot item
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
      \param item PlotItem
      \sa insertItem()
     */
    void QwtPlotDict::removeItem( QwtPlotItem *item )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        d_data->itemList.removeItem( item );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    /*!
       Detach items from the dictionary
    
    
       \param rtti In case of QwtPlotItem::Rtti_PlotItem detach all items
    
    pixhawk's avatar
    pixhawk committed
                   otherwise only those items of the type rtti.
       \param autoDelete If true, delete all detached items
    */
    
    Bryant's avatar
    Bryant committed
    void QwtPlotDict::detachItems( int rtti, bool autoDelete )
    
    pixhawk's avatar
    pixhawk committed
    {
        PrivateData::ItemList list = d_data->itemList;
        QwtPlotItemIterator it = list.begin();
    
    Bryant's avatar
    Bryant committed
        while ( it != list.end() )
        {
    
    pixhawk's avatar
    pixhawk committed
            QwtPlotItem *item = *it;
    
            ++it; // increment before removing item from the list
    
    
    Bryant's avatar
    Bryant committed
            if ( rtti == QwtPlotItem::Rtti_PlotItem || item->rtti() == rtti )
            {
                item->attach( NULL );
    
    pixhawk's avatar
    pixhawk committed
                if ( autoDelete )
                    delete item;
            }
        }
    }
    
    
    Bryant's avatar
    Bryant committed
    /*!
      \brief A QwtPlotItemList of all attached plot items.
    
      Use caution when iterating these lists, as removing/detaching an item will
      invalidate the iterator. Instead you can place pointers to objects to be
      removed in a removal list, and traverse that list later.
    
      \return List of all attached plot items.
    */
    
    pixhawk's avatar
    pixhawk committed
    const QwtPlotItemList &QwtPlotDict::itemList() const
    {
        return d_data->itemList;
    }
    
    Bryant's avatar
    Bryant committed
    
    /*!
      \return List of all attached plot items of a specific type.
      \param rtti See QwtPlotItem::RttiValues
      \sa QwtPlotItem::rtti()
    */
    QwtPlotItemList QwtPlotDict::itemList( int rtti ) const
    {
        if ( rtti == QwtPlotItem::Rtti_PlotItem )
            return d_data->itemList;
    
        QwtPlotItemList items;
    
        PrivateData::ItemList list = d_data->itemList;
        for ( QwtPlotItemIterator it = list.begin(); it != list.end(); ++it )
        {
            QwtPlotItem *item = *it;
            if ( item->rtti() == rtti )
                items += item;
        }
    
        return items;
    }