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/qwtpolar/qwt_polar_plot.cpp

    r4272 r8127  
    1616#include <qwt_round_scale_draw.h>
    1717#include <qwt_legend.h>
    18 #include <qwt_legend_item.h>
    1918#include <qwt_dyngrid_layout.h>
    2019#include <qpointer.h>
     
    3534public:
    3635    ScaleData():
     36        isValid( false ),
    3737        scaleEngine( NULL )
    3838    {
     
    5353    int maxMinor;
    5454
     55    bool isValid;
     56
    5557    QwtScaleDiv scaleDiv;
    5658    QwtScaleEngine *scaleEngine;
     
    7072    QPointer<QwtTextLabel> titleLabel;
    7173    QPointer<QwtPolarCanvas> canvas;
    72     QPointer<QwtLegend> legend;
     74    QPointer<QwtAbstractLegend> legend;
    7375    double azimuthOrigin;
    7476
     
    183185      QwtPolarLayout::setLegendPosition()
    184186*/
    185 void QwtPolarPlot::insertLegend( QwtLegend *legend,
     187void QwtPolarPlot::insertLegend( QwtAbstractLegend *legend,
    186188    QwtPolarPlot::LegendPosition pos, double ratio )
    187189{
    188190    d_data->layout->setLegendPosition( pos, ratio );
     191
    189192    if ( legend != d_data->legend )
    190193    {
     
    196199        if ( d_data->legend )
    197200        {
    198             if ( pos != ExternalLegend )
     201            connect( this,
     202                SIGNAL( legendDataChanged(
     203                    const QVariant &, const QList<QwtLegendData> & ) ),
     204                d_data->legend,
     205                SLOT( updateLegend(
     206                    const QVariant &, const QList<QwtLegendData> & ) )
     207            );
     208
     209            if ( d_data->legend->parent() != this )
     210                d_data->legend->setParent( this );
     211
     212            updateLegend();
     213
     214            QwtLegend *lgd = qobject_cast<QwtLegend *>( legend );
     215            if ( lgd )
    199216            {
    200                 if ( d_data->legend->parent() != this )
    201                     d_data->legend->setParent( this );
    202             }
    203 
    204             const QwtPolarItemList& itmList = itemList();
    205             for ( QwtPolarItemIterator it = itmList.begin();
    206                     it != itmList.end(); ++it )
    207             {
    208                 ( *it )->updateLegend( d_data->legend );
    209             }
    210 
    211             QwtDynGridLayout *tl = qobject_cast<QwtDynGridLayout *>(
    212                 d_data->legend->contentsWidget()->layout() );
    213 
    214             if ( tl )
    215             {
    216                 switch( d_data->layout->legendPosition() )
     217                switch ( d_data->layout->legendPosition() )
    217218                {
    218219                    case LeftLegend:
    219220                    case RightLegend:
    220                         tl->setMaxCols( 1 ); // 1 column: align vertical
     221                    {
     222                        if ( lgd->maxColumns() == 0     )
     223                            lgd->setMaxColumns( 1 ); // 1 column: align vertical
    221224                        break;
    222 
     225                    }
    223226                    case TopLegend:
    224227                    case BottomLegend:
    225                         tl->setMaxCols( 0 ); // unlimited
     228                    {
     229                        lgd->setMaxColumns( 0 ); // unlimited
    226230                        break;
    227 
    228                     case ExternalLegend:
     231                    }
     232                    default:
    229233                        break;
    230234                }
    231235            }
    232         }
    233     }
     236
     237        }
     238    }
     239
    234240    updateLayout();
     241}
     242
     243/*!
     244  Emit legendDataChanged() for all plot item
     245
     246  \sa QwtPlotItem::legendData(), legendDataChanged()
     247 */
     248void QwtPolarPlot::updateLegend()
     249{
     250    const QwtPolarItemList& itmList = itemList();
     251    for ( QwtPolarItemIterator it = itmList.begin();
     252        it != itmList.end(); ++it )
     253    {
     254        updateLegend( *it );
     255    }
     256}
     257
     258/*!
     259  Emit legendDataChanged() for a plot item
     260
     261  \param plotItem Plot item
     262  \sa QwtPlotItem::legendData(), legendDataChanged()
     263 */
     264void QwtPolarPlot::updateLegend( const QwtPolarItem *plotItem )
     265{
     266    if ( plotItem == NULL )
     267        return;
     268
     269    QList<QwtLegendData> legendData;
     270
     271    if ( plotItem->testItemAttribute( QwtPolarItem::Legend ) )
     272        legendData = plotItem->legendData();
     273
     274    const QVariant itemInfo = itemToInfo( const_cast< QwtPolarItem *>( plotItem) );
     275    Q_EMIT legendDataChanged( itemInfo, legendData );
    235276}
    236277
     
    239280  \sa insertLegend()
    240281*/
    241 QwtLegend *QwtPolarPlot::legend()
     282QwtAbstractLegend *QwtPolarPlot::legend()
    242283{
    243284    return d_data->legend;
     
    248289  \sa insertLegend()
    249290*/
    250 const QwtLegend *QwtPolarPlot::legend() const
     291const QwtAbstractLegend *QwtPolarPlot::legend() const
    251292{
    252293    return d_data->legend;
    253 }
    254 
    255 /*!
    256   Called internally when the legend has been clicked on.
    257   Emits a legendClicked() signal.
    258 */
    259 void QwtPolarPlot::legendItemClicked()
    260 {
    261     if ( d_data->legend && sender()->isWidgetType() )
    262     {
    263         QwtPolarItem *plotItem = static_cast< QwtPolarItem* >(
    264             d_data->legend->find( qobject_cast<const QWidget *>( sender() ) ) );
    265         if ( plotItem )
    266             Q_EMIT legendClicked( plotItem );
    267     }
    268 }
    269 
    270 /*!
    271   Called internally when the legend has been checked
    272   Emits a legendClicked() signal.
    273 */
    274 void QwtPolarPlot::legendItemChecked( bool on )
    275 {
    276     if ( d_data->legend && sender()->isWidgetType() )
    277     {
    278         QwtPolarItem *plotItem = static_cast< QwtPolarItem* >(
    279             d_data->legend->find( qobject_cast<const QWidget *>( sender() ) ) );
    280         if ( plotItem )
    281             Q_EMIT legendChecked( plotItem, on );
    282     }
    283294}
    284295
     
    389400        return;
    390401
    391     if ( maxMinor < 0 )
    392         maxMinor = 0;
    393     if ( maxMinor > 100 )
    394         maxMinor = 100;
     402    maxMinor = qBound( 0, maxMinor, 100 );
    395403
    396404    ScaleData &scaleData = d_data->scaleData[scaleId];
     
    399407    {
    400408        scaleData.maxMinor = maxMinor;
    401         scaleData.scaleDiv.invalidate();
     409        scaleData.isValid = false;
    402410        autoRefresh();
    403411    }
     
    429437        return;
    430438
    431     if ( maxMajor < 1 )
    432         maxMajor = 1;
    433     if ( maxMajor > 1000 )
    434         maxMajor = 10000;
     439    maxMajor = qBound( 1, maxMajor, 10000 );
    435440
    436441    ScaleData &scaleData = d_data->scaleData[scaleId];
     
    438443    {
    439444        scaleData.maxMajor = maxMajor;
    440         scaleData.scaleDiv.invalidate();
     445        scaleData.isValid = false;
    441446        autoRefresh();
    442447    }
     
    477482    scaleData.scaleEngine = scaleEngine;
    478483
    479     scaleData.scaleDiv.invalidate();
     484    scaleData.isValid = false;
    480485
    481486    autoRefresh();
     
    527532    ScaleData &scaleData = d_data->scaleData[scaleId];
    528533
    529     scaleData.scaleDiv.invalidate();
     534    scaleData.isValid = false;
    530535
    531536    scaleData.minValue = min;
     
    551556
    552557    scaleData.scaleDiv = scaleDiv;
     558    scaleData.isValid = true;
    553559    scaleData.doAutoScale = false;
    554560
     
    732738    {
    733739        map.setPaintInterval( d_data->azimuthOrigin,
    734             d_data->azimuthOrigin + M_2PI );
     740            d_data->azimuthOrigin + 2 * M_PI );
    735741    }
    736742    else
     
    820826        scaleData.maxMajor = 8;
    821827
     828        scaleData.isValid = false;
     829
    822830        scaleData.scaleEngine = new QwtLinearScaleEngine;
    823         scaleData.scaleDiv.invalidate();
    824831    }
    825832    d_data->zoomFactor = 1.0;
     
    858865    }
    859866
    860     if ( d_data->legend &&
    861         d_data->layout->legendPosition() != ExternalLegend )
    862     {
    863         if ( d_data->legend->itemCount() > 0 )
    864         {
    865             d_data->legend->setGeometry( d_data->layout->legendRect().toRect() );
     867    if ( d_data->legend )
     868    {
     869        if ( d_data->legend->isEmpty() )
     870        {
     871            d_data->legend->hide();
     872        }
     873        else
     874        {
     875            const QRectF legendRect = d_data->layout->legendRect();
     876            d_data->legend->setGeometry( legendRect.toRect() );
    866877            d_data->legend->show();
    867878        }
    868         else
    869             d_data->legend->hide();
    870879    }
    871880
     
    10541063        d.scaleEngine->autoScale( d.maxMajor,
    10551064                                  minValue, maxValue, stepSize );
    1056         d.scaleDiv.invalidate();
    1057     }
    1058 
    1059     if ( !d.scaleDiv.isValid() )
     1065        d.isValid = false;
     1066    }
     1067
     1068    if ( !d.isValid )
    10601069    {
    10611070        d.scaleDiv = d.scaleEngine->divideScale(
    10621071            minValue, maxValue, d.maxMajor, d.maxMinor, stepSize );
     1072        d.isValid = true;
    10631073    }
    10641074
     
    12701280    return d_data->layout;
    12711281}
     1282
     1283/*!
     1284  \brief Attach/Detach a plot item
     1285
     1286  \param plotItem Plot item
     1287  \param on When true attach the item, otherwise detach it
     1288 */
     1289void QwtPolarPlot::attachItem( QwtPolarItem *plotItem, bool on )
     1290{
     1291    if ( on )
     1292        insertItem( plotItem );
     1293    else
     1294        removeItem( plotItem );
     1295
     1296    Q_EMIT itemAttached( plotItem, on );
     1297
     1298    if ( plotItem->testItemAttribute( QwtPolarItem::Legend ) )
     1299    {
     1300        // the item wants to be represented on the legend
     1301
     1302        if ( on )
     1303        {
     1304            updateLegend( plotItem );
     1305        }
     1306        else
     1307        {
     1308            const QVariant itemInfo = itemToInfo( plotItem );
     1309            Q_EMIT legendDataChanged( itemInfo, QList<QwtLegendData>() );
     1310        }
     1311    }
     1312
     1313    if ( autoReplot() )
     1314        update();
     1315}
     1316
     1317/*!
     1318  \brief Build an information, that can be used to identify
     1319         a plot item on the legend.
     1320
     1321  The default implementation simply wraps the plot item
     1322  into a QVariant object. When overloading itemToInfo()
     1323  usually infoToItem() needs to reimplemeted too.
     1324
     1325\code
     1326    QVariant itemInfo;
     1327    qVariantSetValue( itemInfo, plotItem );
     1328\endcode
     1329
     1330  \param plotItem Plot item
     1331  \sa infoToItem()
     1332 */
     1333QVariant QwtPolarPlot::itemToInfo( QwtPolarItem *plotItem ) const
     1334{
     1335    QVariant itemInfo;
     1336    qVariantSetValue( itemInfo, plotItem );
     1337
     1338    return itemInfo;
     1339}
     1340
     1341/*!
     1342  \brief Identify the plot item according to an item info object,
     1343         that has bee generated from itemToInfo().
     1344
     1345  The default implementation simply tries to unwrap a QwtPlotItem
     1346  pointer:
     1347
     1348\code
     1349    if ( itemInfo.canConvert<QwtPlotItem *>() )
     1350        return qvariant_cast<QwtPlotItem *>( itemInfo );
     1351\endcode
     1352  \param itemInfo Plot item
     1353  \return A plot item, when successful, otherwise a NULL pointer.
     1354  \sa itemToInfo()
     1355*/
     1356QwtPolarItem *QwtPolarPlot::infoToItem( const QVariant &itemInfo ) const
     1357{
     1358    if ( itemInfo.canConvert<QwtPolarItem *>() )
     1359        return qvariant_cast<QwtPolarItem *>( itemInfo );
     1360
     1361    return NULL;
     1362}
Note: See TracChangeset for help on using the changeset viewer.