Add files from zip
This commit is contained in:
137
playground/curvetracker/CurveTracker.cpp
Normal file
137
playground/curvetracker/CurveTracker.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "CurveTracker.h"
|
||||
|
||||
#include <QwtPickerMachine>
|
||||
#include <QwtPlot>
|
||||
#include <QwtPlotCurve>
|
||||
#include <QwtText>
|
||||
|
||||
#include <QPen>
|
||||
|
||||
struct compareX
|
||||
{
|
||||
inline bool operator()( const double x, const QPointF& pos ) const
|
||||
{
|
||||
return ( x < pos.x() );
|
||||
}
|
||||
};
|
||||
|
||||
CurveTracker::CurveTracker( QWidget* canvas )
|
||||
: QwtPlotPicker( canvas )
|
||||
{
|
||||
setTrackerMode( QwtPlotPicker::ActiveOnly );
|
||||
setRubberBand( VLineRubberBand );
|
||||
|
||||
setStateMachine( new QwtPickerDragPointMachine() );
|
||||
}
|
||||
|
||||
QRect CurveTracker::trackerRect( const QFont& font ) const
|
||||
{
|
||||
QRect r = QwtPlotPicker::trackerRect( font );
|
||||
|
||||
// align r to the first curve
|
||||
|
||||
const QwtPlotItemList curves = plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
|
||||
if ( curves.size() > 0 )
|
||||
{
|
||||
QPointF pos = invTransform( trackerPosition() );
|
||||
|
||||
const QLineF line = curveLineAt(
|
||||
static_cast< const QwtPlotCurve* >( curves[0] ), pos.x() );
|
||||
if ( !line.isNull() )
|
||||
{
|
||||
const double curveY = line.pointAt(
|
||||
( pos.x() - line.p1().x() ) / line.dx() ).y();
|
||||
|
||||
pos.setY( curveY );
|
||||
pos = transform( pos );
|
||||
|
||||
r.moveBottom( pos.y() );
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
QwtText CurveTracker::trackerTextF( const QPointF& pos ) const
|
||||
{
|
||||
QwtText trackerText;
|
||||
|
||||
trackerText.setColor( Qt::black );
|
||||
|
||||
QColor c( "#333333" );
|
||||
trackerText.setBorderPen( QPen( c, 2 ) );
|
||||
c.setAlpha( 200 );
|
||||
trackerText.setBackgroundBrush( c );
|
||||
|
||||
QString info;
|
||||
|
||||
const QwtPlotItemList curves =
|
||||
plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
|
||||
|
||||
for ( int i = 0; i < curves.size(); i++ )
|
||||
{
|
||||
const QString curveInfo = curveInfoAt(
|
||||
static_cast< const QwtPlotCurve* >( curves[i] ), pos );
|
||||
|
||||
if ( !curveInfo.isEmpty() )
|
||||
{
|
||||
if ( !info.isEmpty() )
|
||||
info += "<br>";
|
||||
|
||||
info += curveInfo;
|
||||
}
|
||||
}
|
||||
|
||||
trackerText.setText( info );
|
||||
return trackerText;
|
||||
}
|
||||
|
||||
QString CurveTracker::curveInfoAt(
|
||||
const QwtPlotCurve* curve, const QPointF& pos ) const
|
||||
{
|
||||
const QLineF line = curveLineAt( curve, pos.x() );
|
||||
if ( line.isNull() )
|
||||
return QString();
|
||||
|
||||
const double y = line.pointAt(
|
||||
( pos.x() - line.p1().x() ) / line.dx() ).y();
|
||||
|
||||
QString info( "<font color=" "%1" ">%2</font>" );
|
||||
return info.arg( curve->pen().color().name() ).arg( y );
|
||||
}
|
||||
|
||||
QLineF CurveTracker::curveLineAt(
|
||||
const QwtPlotCurve* curve, double x ) const
|
||||
{
|
||||
QLineF line;
|
||||
|
||||
if ( curve->dataSize() >= 2 )
|
||||
{
|
||||
const QRectF br = curve->boundingRect();
|
||||
if ( ( br.width() > 0 ) && ( x >= br.left() ) && ( x <= br.right() ) )
|
||||
{
|
||||
int index = qwtUpperSampleIndex< QPointF >(
|
||||
*curve->data(), x, compareX() );
|
||||
|
||||
if ( index == -1 &&
|
||||
x == curve->sample( curve->dataSize() - 1 ).x() )
|
||||
{
|
||||
// the last sample is excluded from qwtUpperSampleIndex
|
||||
index = curve->dataSize() - 1;
|
||||
}
|
||||
|
||||
if ( index > 0 )
|
||||
{
|
||||
line.setP1( curve->sample( index - 1 ) );
|
||||
line.setP2( curve->sample( index ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
25
playground/curvetracker/CurveTracker.h
Normal file
25
playground/curvetracker/CurveTracker.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QwtPlotPicker>
|
||||
|
||||
class QwtPlotCurve;
|
||||
class QLineF;
|
||||
|
||||
class CurveTracker : public QwtPlotPicker
|
||||
{
|
||||
public:
|
||||
CurveTracker( QWidget* );
|
||||
|
||||
protected:
|
||||
virtual QwtText trackerTextF( const QPointF& ) const QWT_OVERRIDE;
|
||||
virtual QRect trackerRect( const QFont& ) const QWT_OVERRIDE;
|
||||
|
||||
private:
|
||||
QString curveInfoAt( const QwtPlotCurve*, const QPointF& ) const;
|
||||
QLineF curveLineAt( const QwtPlotCurve*, double x ) const;
|
||||
};
|
||||
112
playground/curvetracker/Plot.cpp
Normal file
112
playground/curvetracker/Plot.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Plot.h"
|
||||
#include "CurveTracker.h"
|
||||
|
||||
#include <QwtPickerMachine>
|
||||
#include <QwtPlotCanvas>
|
||||
#include <QwtPlotGrid>
|
||||
#include <QwtPlotTextLabel>
|
||||
#include <QwtPlotZoneItem>
|
||||
#include <QwtPlotCurve>
|
||||
#include <QwtPlotLayout>
|
||||
#include <QwtScaleWidget>
|
||||
#include <QwtSymbol>
|
||||
|
||||
Plot::Plot( QWidget* parent )
|
||||
: QwtPlot( parent)
|
||||
{
|
||||
setPalette( Qt::black );
|
||||
|
||||
// we want to have the axis scales like a frame around the
|
||||
// canvas
|
||||
plotLayout()->setAlignCanvasToScales( true );
|
||||
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
|
||||
axisWidget( axisPos )->setMargin( 0 );
|
||||
|
||||
QwtPlotCanvas* canvas = new QwtPlotCanvas();
|
||||
canvas->setAutoFillBackground( false );
|
||||
canvas->setFrameStyle( QFrame::NoFrame );
|
||||
setCanvas( canvas );
|
||||
|
||||
setAxisScale( QwtAxis::YLeft, 0.0, 10.0 );
|
||||
|
||||
// a title
|
||||
QwtText title( "Picker Demo" );
|
||||
title.setColor( Qt::white );
|
||||
title.setRenderFlags( Qt::AlignHCenter | Qt::AlignTop );
|
||||
|
||||
QFont font;
|
||||
font.setBold( true );
|
||||
title.setFont( font );
|
||||
|
||||
QwtPlotTextLabel* titleItem = new QwtPlotTextLabel();
|
||||
titleItem->setText( title );
|
||||
titleItem->attach( this );
|
||||
|
||||
#if 1
|
||||
// section
|
||||
|
||||
//QColor c( "PaleVioletRed" );
|
||||
|
||||
QwtPlotZoneItem* zone = new QwtPlotZoneItem();
|
||||
zone->setPen( Qt::darkGray );
|
||||
zone->setBrush( QColor( "#834358" ) );
|
||||
zone->setOrientation( Qt::Horizontal );
|
||||
zone->setInterval( 3.8, 5.7 );
|
||||
zone->attach( this );
|
||||
|
||||
#else
|
||||
// grid
|
||||
|
||||
QwtPlotGrid* grid = new QwtPlotGrid();
|
||||
grid->setMajorPen( Qt::white, 0, Qt::DotLine );
|
||||
grid->setMinorPen( Qt::gray, 0, Qt::DotLine );
|
||||
grid->attach( this );
|
||||
#endif
|
||||
|
||||
// curves
|
||||
|
||||
QPolygonF points1;
|
||||
points1 << QPointF( 0.2, 4.4 ) << QPointF( 1.2, 3.0 )
|
||||
<< QPointF( 2.7, 4.5 ) << QPointF( 3.5, 6.8 )
|
||||
<< QPointF( 4.7, 7.9 ) << QPointF( 5.8, 7.1 );
|
||||
|
||||
insertCurve( "Curve 1", "DarkOrange", points1 );
|
||||
|
||||
QPolygonF points2;
|
||||
points2 << QPointF( 0.4, 8.7 ) << QPointF( 1.4, 7.8 )
|
||||
<< QPointF( 2.3, 5.5 ) << QPointF( 3.3, 4.1 )
|
||||
<< QPointF( 4.4, 5.2 ) << QPointF( 5.6, 5.7 );
|
||||
|
||||
insertCurve( "Curve 2", "DodgerBlue", points2 );
|
||||
|
||||
CurveTracker* tracker = new CurveTracker( this->canvas() );
|
||||
|
||||
// for the demo we want the tracker to be active without
|
||||
// having to click on the canvas
|
||||
tracker->setStateMachine( new QwtPickerTrackerMachine() );
|
||||
tracker->setRubberBandPen( QPen( "MediumOrchid" ) );
|
||||
}
|
||||
|
||||
void Plot::insertCurve( const QString& title,
|
||||
const QColor& color, const QPolygonF& points )
|
||||
{
|
||||
QwtPlotCurve* curve = new QwtPlotCurve();
|
||||
curve->setTitle( title );
|
||||
curve->setPen( color, 2 ),
|
||||
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
|
||||
|
||||
QwtSymbol* symbol = new QwtSymbol( QwtSymbol::Ellipse,
|
||||
QBrush( Qt::white ), QPen( color, 2 ), QSize( 8, 8 ) );
|
||||
curve->setSymbol( symbol );
|
||||
|
||||
curve->setSamples( points );
|
||||
|
||||
curve->attach( this );
|
||||
}
|
||||
|
||||
#include "moc_Plot.cpp"
|
||||
22
playground/curvetracker/Plot.h
Normal file
22
playground/curvetracker/Plot.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QwtPlot>
|
||||
|
||||
class QPolygonF;
|
||||
|
||||
class Plot : public QwtPlot
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Plot( QWidget* = NULL );
|
||||
|
||||
private:
|
||||
void insertCurve( const QString& title,
|
||||
const QColor&, const QPolygonF& );
|
||||
};
|
||||
18
playground/curvetracker/curvetracker.pro
Normal file
18
playground/curvetracker/curvetracker.pro
Normal file
@@ -0,0 +1,18 @@
|
||||
######################################################################
|
||||
# Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
# This file may be used under the terms of the 3-clause BSD License
|
||||
######################################################################
|
||||
|
||||
include( $${PWD}/../playground.pri )
|
||||
|
||||
TARGET = curvetracker
|
||||
|
||||
HEADERS = \
|
||||
CurveTracker.h \
|
||||
Plot.h
|
||||
|
||||
SOURCES = \
|
||||
CurveTracker.cpp \
|
||||
Plot.cpp \
|
||||
main.cpp
|
||||
|
||||
18
playground/curvetracker/main.cpp
Normal file
18
playground/curvetracker/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Plot.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
QApplication app( argc, argv );
|
||||
|
||||
Plot plot;
|
||||
plot.resize( 600, 400 );
|
||||
plot.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user