File: transformplot.cpp

package info (click to toggle)
qwt 6.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 23,808 kB
  • sloc: cpp: 57,687; xml: 182; makefile: 32
file content (108 lines) | stat: -rw-r--r-- 2,993 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "transformplot.h"
#include <qwt_curve_fitter.h>
#include <qwt_plot_curve.h>
#include <qwt_point_data.h>
#include <qwt_transform.h>
#include <qwt_legend.h>
#include <qwt_legend_label.h>

class TransformData: public QwtSyntheticPointData
{
public:
    TransformData( QwtTransform *transform ):
        QwtSyntheticPointData( 200 ),
        d_transform( transform )
    {
    }

    virtual ~TransformData()
    {
        delete d_transform;
    }

    const QwtTransform *transform() const
    {
        return d_transform;
    }

    virtual double y( double x ) const
    {
        const double min = 10.0;
        const double max = 1000.0;

        const double value = min + x * ( max - min );

        const double s1 = d_transform->transform( min );
        const double s2 = d_transform->transform( max );
        const double s = d_transform->transform( value );

        return ( s - s1 ) / ( s2 - s1 );
    }

private:
    QwtTransform *d_transform;
};

TransformPlot::TransformPlot( QWidget *parent ):
    QwtPlot( parent )
{
    setTitle( "Transformations" );
    setCanvasBackground( Qt::white );

    setAxisScale( QwtPlot::xBottom, 0.0, 1.0 );
    setAxisScale( QwtPlot::yLeft, 0.0, 1.0 );

    QwtLegend *legend = new QwtLegend();
    legend->setDefaultItemMode( QwtLegendData::Checkable );
    insertLegend( legend, QwtPlot::RightLegend );

    connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),
        this, SLOT( legendChecked( const QVariant &, bool ) ) );
}

void TransformPlot::insertTransformation(
    const QString &title, const QColor &color, QwtTransform *transform )
{
    QwtPlotCurve *curve = new QwtPlotCurve( title );
    curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
    curve->setPen( color, 2 );
    curve->setData( new TransformData( transform ) );
    curve->attach( this );
}

void TransformPlot::legendChecked( const QVariant &itemInfo, bool on )
{
    QwtPlotItem *plotItem = infoToItem( itemInfo );

    setLegendChecked( plotItem );

    if ( on && plotItem->rtti() == QwtPlotItem::Rtti_PlotCurve )
    {
        QwtPlotCurve *curve = static_cast<QwtPlotCurve *>( plotItem );
        TransformData *curveData = static_cast<TransformData *>( curve->data() );

        Q_EMIT selected( curveData->transform()->copy() );
    }
}

void TransformPlot::setLegendChecked( QwtPlotItem *plotItem )
{
    const QwtPlotItemList items = itemList();
    for ( int i = 0; i < items.size(); i++ )
    {
        QwtPlotItem *item = items[ i ];
        if ( item->testItemAttribute( QwtPlotItem::Legend ) )
        {
            QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );

            QwtLegendLabel *label = qobject_cast< QwtLegendLabel *>(
                lgd->legendWidget( itemToInfo( item ) ) );
            if ( label )
            {
                lgd->blockSignals( true );
                label->setChecked( item == plotItem );
                lgd->blockSignals( false );
            }
        }
    }
}