Changeset 8127 in ntrip for trunk/BNC/qwt/qwt_plot_item.cpp


Ignore:
Timestamp:
May 10, 2017, 3:20:54 PM (7 years ago)
Author:
stoecker
Message:

update qwt and qwtpolar, many QT5 fixes (unfinished)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/qwt/qwt_plot_item.cpp

    r4271 r8127  
    1111#include "qwt_text.h"
    1212#include "qwt_plot.h"
    13 #include "qwt_legend.h"
    14 #include "qwt_legend_item.h"
     13#include "qwt_legend_data.h"
    1514#include "qwt_scale_div.h"
     15#include "qwt_graphic.h"
    1616#include <qpainter.h>
    1717
     
    2323        isVisible( true ),
    2424        attributes( 0 ),
     25        interests( 0 ),
    2526        renderHints( 0 ),
     27        renderThreadCount( 1 ),
    2628        z( 0.0 ),
    2729        xAxis( QwtPlot::xBottom ),
    28         yAxis( QwtPlot::yLeft )
     30        yAxis( QwtPlot::yLeft ),
     31        legendIconSize( 8, 8 )
    2932    {
    3033    }
     
    3336
    3437    bool isVisible;
     38
    3539    QwtPlotItem::ItemAttributes attributes;
     40    QwtPlotItem::ItemInterests interests;
     41
    3642    QwtPlotItem::RenderHints renderHints;
     43    uint renderThreadCount;
     44
    3745    double z;
    3846
     
    4149
    4250    QwtText title;
     51    QSize legendIconSize;
    4352};
    4453
     
    7685        return;
    7786
    78     // remove the item from the previous plot
    79 
    8087    if ( d_data->plot )
    81     {
    82         if ( d_data->plot->legend() )
    83             d_data->plot->legend()->remove( this );
    84 
    8588        d_data->plot->attachItem( this, false );
    8689
    87         if ( d_data->plot->autoReplot() )
    88             d_data->plot->update();
    89     }
    90 
    9190    d_data->plot = plot;
    9291
    9392    if ( d_data->plot )
    94     {
    95         // insert the item into the current plot
    96 
    9793        d_data->plot->attachItem( this, true );
    98         itemChanged();
    99     }
    10094}
    10195
     
    191185    {
    192186        d_data->title = title;
    193         itemChanged();
     187
     188        legendChanged();
     189#if 0
     190        itemChanged();
     191#endif
    194192    }
    195193}
     
    210208   \param on true/false
    211209
    212    \sa testItemAttribute(), ItemAttribute
     210   \sa testItemAttribute(), ItemInterest
    213211*/
    214212void QwtPlotItem::setItemAttribute( ItemAttribute attribute, bool on )
    215213{
    216     if ( bool( d_data->attributes & attribute ) != on )
     214    if ( d_data->attributes.testFlag( attribute ) != on )
    217215    {
    218216        if ( on )
     
    221219            d_data->attributes &= ~attribute;
    222220
     221        if ( attribute == QwtPlotItem::Legend )
     222            legendChanged();
     223
    223224        itemChanged();
    224225    }
     
    230231   \param attribute Attribute type
    231232   \return true/false
    232    \sa setItemAttribute(), ItemAttribute
     233   \sa setItemAttribute(), ItemInterest
    233234*/
    234235bool QwtPlotItem::testItemAttribute( ItemAttribute attribute ) const
    235236{
    236     return ( d_data->attributes & attribute );
     237    return d_data->attributes.testFlag( attribute );
     238}
     239
     240/*!
     241   Toggle an item interest
     242
     243   \param interest Interest type
     244   \param on true/false
     245
     246   \sa testItemInterest(), ItemAttribute
     247*/
     248void QwtPlotItem::setItemInterest( ItemInterest interest, bool on )
     249{
     250    if ( d_data->interests.testFlag( interest ) != on )
     251    {
     252        if ( on )
     253            d_data->interests |= interest;
     254        else
     255            d_data->interests &= ~interest;
     256
     257        itemChanged();
     258    }
     259}
     260
     261/*!
     262   Test an item interest
     263
     264   \param interest Interest type
     265   \return true/false
     266   \sa setItemInterest(), ItemAttribute
     267*/
     268bool QwtPlotItem::testItemInterest( ItemInterest interest ) const
     269{
     270    return d_data->interests.testFlag( interest );
    237271}
    238272
     
    247281void QwtPlotItem::setRenderHint( RenderHint hint, bool on )
    248282{
    249     if ( ( ( d_data->renderHints & hint ) != 0 ) != on )
     283    if ( d_data->renderHints.testFlag( hint ) != on )
    250284    {
    251285        if ( on )
     
    267301bool QwtPlotItem::testRenderHint( RenderHint hint ) const
    268302{
    269     return ( d_data->renderHints & hint );
    270 }
     303    return d_data->renderHints.testFlag( hint );
     304}
     305
     306/*!
     307   On multi core systems rendering of certain plot item
     308   ( f.e QwtPlotRasterItem ) can be done in parallel in
     309   several threads.
     310
     311   The default setting is set to 1.
     312
     313   \param numThreads Number of threads to be used for rendering.
     314                     If numThreads is set to 0, the system specific
     315                     ideal thread count is used.
     316
     317   The default thread count is 1 ( = no additional threads )
     318*/
     319void QwtPlotItem::setRenderThreadCount( uint numThreads )
     320{
     321    d_data->renderThreadCount = numThreads;
     322}
     323
     324/*!
     325   \return Number of threads to be used for rendering.
     326           If numThreads() is set to 0, the system specific
     327           ideal thread count is used.
     328*/
     329uint QwtPlotItem::renderThreadCount() const
     330{
     331    return d_data->renderThreadCount;
     332}
     333
     334/*!
     335   Set the size of the legend icon
     336
     337   The default setting is 8x8 pixels
     338
     339   \param size Size
     340   \sa legendIconSize(), legendIcon()
     341*/
     342void QwtPlotItem::setLegendIconSize( const QSize &size )
     343{
     344    if ( d_data->legendIconSize != size )
     345    {
     346        d_data->legendIconSize = size;
     347        legendChanged();
     348    }
     349}
     350
     351/*!
     352   \return Legend icon size
     353   \sa setLegendIconSize(), legendIcon()
     354*/
     355QSize QwtPlotItem::legendIconSize() const
     356{
     357    return d_data->legendIconSize;
     358}
     359
     360/*!
     361   \return Icon representing the item on the legend
     362
     363   The default implementation returns an invalid icon
     364
     365   \param index Index of the legend entry
     366                ( usually there is only one )
     367   \param size Icon size
     368
     369   \sa setLegendIconSize(), legendData()
     370 */
     371QwtGraphic QwtPlotItem::legendIcon(
     372    int index, const QSizeF &size ) const
     373{
     374    Q_UNUSED( index )
     375    Q_UNUSED( size )
     376
     377    return QwtGraphic();
     378}
     379
     380/*!
     381   \brief Return a default icon from a brush
     382
     383   The default icon is a filled rectangle used
     384   in several derived classes as legendIcon().
     385
     386   \param brush Fill brush
     387   \param size Icon size
     388
     389   \return A filled rectangle
     390 */
     391QwtGraphic QwtPlotItem::defaultIcon(
     392    const QBrush &brush, const QSizeF &size ) const
     393{   
     394    QwtGraphic icon;
     395    if ( !size.isEmpty() )
     396    {
     397        icon.setDefaultSize( size );
     398       
     399        QRectF r( 0, 0, size.width(), size.height() );
     400       
     401        QPainter painter( &icon );
     402        painter.fillRect( r, brush );
     403    }   
     404   
     405    return icon;
     406}   
    271407
    272408//! Show the item
     
    307443
    308444/*!
    309    Update the legend and call QwtPlot::autoRefresh for the
     445   Update the legend and call QwtPlot::autoRefresh() for the
    310446   parent plot.
    311447
    312    \sa updateLegend()
     448   \sa QwtPlot::legendChanged(), QwtPlot::autoRefresh()
    313449*/
    314450void QwtPlotItem::itemChanged()
    315451{
    316452    if ( d_data->plot )
    317     {
    318         if ( d_data->plot->legend() )
    319             updateLegend( d_data->plot->legend() );
    320 
    321453        d_data->plot->autoRefresh();
    322     }
     454}
     455
     456/*!
     457   Update the legend of the parent plot.
     458   \sa QwtPlot::updateLegend(), itemChanged()
     459*/
     460void QwtPlotItem::legendChanged()
     461{
     462    if ( testItemAttribute( QwtPlotItem::Legend ) && d_data->plot )
     463        d_data->plot->updateLegend( this );
    323464}
    324465
     
    326467   Set X and Y axis
    327468
    328    The item will painted according to the coordinates its Axes.
    329 
    330    \param xAxis X Axis
    331    \param yAxis Y Axis
    332 
    333    \sa setXAxis(), setYAxis(), xAxis(), yAxis()
     469   The item will painted according to the coordinates of its Axes.
     470
     471   \param xAxis X Axis ( QwtPlot::xBottom or QwtPlot::xTop )
     472   \param yAxis Y Axis ( QwtPlot::yLeft or QwtPlot::yRight )
     473
     474   \sa setXAxis(), setYAxis(), xAxis(), yAxis(), QwtPlot::Axis
    334475*/
    335476void QwtPlotItem::setAxes( int xAxis, int yAxis )
     
    349490   The item will painted according to the coordinates its Axes.
    350491
    351    \param axis X Axis
    352    \sa setAxes(), setYAxis(), xAxis()
     492   \param axis X Axis ( QwtPlot::xBottom or QwtPlot::xTop )
     493   \sa setAxes(), setYAxis(), xAxis(), QwtPlot::Axis
    353494*/
    354495void QwtPlotItem::setXAxis( int axis )
     
    366507   The item will painted according to the coordinates its Axes.
    367508
    368    \param axis Y Axis
    369    \sa setAxes(), setXAxis(), yAxis()
     509   \param axis Y Axis ( QwtPlot::yLeft or QwtPlot::yRight )
     510   \sa setAxes(), setXAxis(), yAxis(), QwtPlot::Axis
    370511*/
    371512void QwtPlotItem::setYAxis( int axis )
     
    392533/*!
    393534   \return An invalid bounding rect: QRectF(1.0, 1.0, -2.0, -2.0)
     535   \note A width or height < 0.0 is ignored by the autoscaler
    394536*/
    395537QRectF QwtPlotItem::boundingRect() const
     
    399541
    400542/*!
    401    \brief Allocate the widget that represents the item on the legend
    402 
    403    The default implementation returns a QwtLegendItem(), but an item
    404    could be represented by any type of widget,
    405    by overloading legendItem() and updateLegend().
    406 
    407    \return QwtLegendItem()
    408    \sa updateLegend() QwtLegend()
    409 */
    410 QWidget *QwtPlotItem::legendItem() const
    411 {
    412     QwtLegendItem *item = new QwtLegendItem;
    413     if ( d_data->plot )
    414     {
    415         QObject::connect( item, SIGNAL( clicked() ),
    416             d_data->plot, SLOT( legendItemClicked() ) );
    417         QObject::connect( item, SIGNAL( checked( bool ) ),
    418             d_data->plot, SLOT( legendItemChecked( bool ) ) );
    419     }
    420     return item;
    421 }
    422 
    423 /*!
    424    \brief Update the widget that represents the item on the legend
    425 
    426    updateLegend() is called from itemChanged() to adopt the widget
    427    representing the item on the legend to its new configuration.
    428 
    429    The default implementation updates a QwtLegendItem(),
    430    but an item could be represented by any type of widget,
    431    by overloading legendItem() and updateLegend().
    432 
    433    \param legend Legend
    434 
    435    \sa legendItem(), itemChanged(), QwtLegend()
    436 */
    437 void QwtPlotItem::updateLegend( QwtLegend *legend ) const
    438 {
    439     if ( legend == NULL )
    440         return;
    441 
    442     QWidget *lgdItem = legend->find( this );
    443     if ( testItemAttribute( QwtPlotItem::Legend ) )
    444     {
    445         if ( lgdItem == NULL )
    446         {
    447             lgdItem = legendItem();
    448             if ( lgdItem )
    449                 legend->insert( this, lgdItem );
    450         }
    451 
    452         QwtLegendItem *label = qobject_cast<QwtLegendItem *>( lgdItem );
    453         if ( label )
    454         {
    455             // paint the identifier
    456             const QSize sz = label->identifierSize();
    457 
    458             QPixmap identifier( sz.width(), sz.height() );
    459             identifier.fill( Qt::transparent );
    460 
    461             QPainter painter( &identifier );
    462             painter.setRenderHint( QPainter::Antialiasing,
    463                 testRenderHint( QwtPlotItem::RenderAntialiased ) );
    464             drawLegendIdentifier( &painter,
    465                 QRect( 0, 0, sz.width(), sz.height() ) );
    466             painter.end();
    467 
    468             const bool doUpdate = label->updatesEnabled();
    469             if ( doUpdate )
    470                 label->setUpdatesEnabled( false );
    471 
    472             label->setText( title() );
    473             label->setIdentifier( identifier );
    474             label->setItemMode( legend->itemMode() );
    475 
    476             if ( doUpdate )
    477                 label->setUpdatesEnabled( true );
    478 
    479             label->update();
    480         }
    481     }
    482     else
    483     {
    484         if ( lgdItem )
    485         {
    486             lgdItem->hide();
    487             lgdItem->deleteLater();
    488         }
    489     }
     543   \brief Calculate a hint for the canvas margin
     544
     545   When the QwtPlotItem::Margins flag is enabled the plot item
     546   indicates, that it needs some margins at the borders of the canvas.
     547   This is f.e. used by bar charts to reserve space for displaying
     548   the bars.
     549
     550   The margins are in target device coordinates ( pixels on screen )
     551
     552   \param xMap Maps x-values into pixel coordinates.
     553   \param yMap Maps y-values into pixel coordinates.
     554   \param canvasRect Contents rectangle of the canvas in painter coordinates
     555   \param left Returns the left margin
     556   \param top Returns the top margin
     557   \param right Returns the right margin
     558   \param bottom Returns the bottom margin
     559
     560   \return The default implementation returns 0 for all margins
     561
     562   \sa QwtPlot::getCanvasMarginsHint(), QwtPlot::updateCanvasMargins()
     563 */
     564void QwtPlotItem::getCanvasMarginHint( const QwtScaleMap &xMap,
     565    const QwtScaleMap &yMap, const QRectF &canvasRect,
     566    double &left, double &top, double &right, double &bottom ) const
     567{
     568    Q_UNUSED( xMap );
     569    Q_UNUSED( yMap );
     570    Q_UNUSED( canvasRect );
     571
     572    // use QMargins, when we don't need to support Qt < 4.6 anymore
     573    left = top = right = bottom = 0.0;
     574}
     575
     576/*!
     577   \brief Return all information, that is needed to represent
     578          the item on the legend
     579
     580   Most items are represented by one entry on the legend
     581   showing an icon and a text, but f.e. QwtPlotMultiBarChart
     582   displays one entry for each bar.
     583
     584   QwtLegendData is basically a list of QVariants that makes it
     585   possible to overload and reimplement legendData() to
     586   return almost any type of information, that is understood
     587   by the receiver that acts as the legend.
     588
     589   The default implementation returns one entry with
     590   the title() of the item and the legendIcon().
     591
     592   \return Data, that is needed to represent the item on the legend
     593   \sa title(), legendIcon(), QwtLegend, QwtPlotLegendItem
     594 */
     595QList<QwtLegendData> QwtPlotItem::legendData() const
     596{
     597    QwtLegendData data;
     598
     599    QwtText label = title();
     600    label.setRenderFlags( label.renderFlags() & Qt::AlignLeft );
     601           
     602    QVariant titleValue;
     603    qVariantSetValue( titleValue, label );
     604    data.setValue( QwtLegendData::TitleRole, titleValue );
     605       
     606    const QwtGraphic graphic = legendIcon( 0, legendIconSize() );
     607    if ( !graphic.isNull() )
     608    {   
     609        QVariant iconValue;
     610        qVariantSetValue( iconValue, graphic );
     611        data.setValue( QwtLegendData::IconRole, iconValue );
     612    }   
     613       
     614    QList<QwtLegendData> list;
     615    list += data;
     616
     617    return list;
    490618}
    491619
     
    498626   updateScaleDiv()
    499627
     628   updateScaleDiv() is only called when the ScaleInterest interest
     629   is enabled. The default implementation does nothing.
     630
    500631   \param xScaleDiv Scale division of the x-axis
    501632   \param yScaleDiv Scale division of the y-axis
    502633
    503    \sa QwtPlot::updateAxes()
     634   \sa QwtPlot::updateAxes(), ScaleInterest
    504635*/
    505636void QwtPlotItem::updateScaleDiv( const QwtScaleDiv &xScaleDiv,
     
    511642
    512643/*!
    513    \brief Calculate the bounding scale rect of 2 maps
    514 
    515    \param xMap X map
    516    \param yMap X map
    517 
    518    \return Bounding scale rect of the scale maps, normalized
     644   \brief Update the item to changes of the legend info
     645
     646   Plot items that want to display a legend ( not those, that want to
     647   be displayed on a legend ! ) will have to implement updateLegend().
     648
     649   updateLegend() is only called when the LegendInterest interest
     650   is enabled. The default implementation does nothing.
     651
     652   \param item Plot item to be displayed on a legend
     653   \param data Attributes how to display item on the legend
     654
     655   \sa QwtPlotLegendItem
     656
     657   \note Plot items, that want to be displayed on a legend
     658         need to enable the QwtPlotItem::Legend flag and to implement
     659         legendData() and legendIcon()
     660 */
     661void QwtPlotItem::updateLegend( const QwtPlotItem *item,
     662    const QList<QwtLegendData> &data )
     663{
     664    Q_UNUSED( item );
     665    Q_UNUSED( data );
     666}
     667
     668/*!
     669   \brief Calculate the bounding scale rectangle of 2 maps
     670
     671   \param xMap Maps x-values into pixel coordinates.
     672   \param yMap Maps y-values into pixel coordinates.
     673
     674   \return Bounding scale rect of the scale maps, not normalized
    519675*/
    520676QRectF QwtPlotItem::scaleRect( const QwtScaleMap &xMap,
     
    526682
    527683/*!
    528    \brief Calculate the bounding paint rect of 2 maps
    529 
    530    \param xMap X map
    531    \param yMap X map
    532 
    533    \return Bounding paint rect of the scale maps, normalized
     684   \brief Calculate the bounding paint rectangle of 2 maps
     685
     686   \param xMap Maps x-values into pixel coordinates.
     687   \param yMap Maps y-values into pixel coordinates.
     688
     689   \return Bounding paint rectangle of the scale maps, not normalized
    534690*/
    535691QRectF QwtPlotItem::paintRect( const QwtScaleMap &xMap,
Note: See TracChangeset for help on using the changeset viewer.