#include "Widget.h"
#include "ui_Widget.h"
#include <QTime>
#include <QSizeGrip>
#include <QResizeEvent>
#include <qmath.h>
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_rescaler.h>
#include <qwt_scale_draw.h>
{
public:
void setZoomBase
( const QRectF &base
);
};
{
}
void PlotZoomer
::setZoomBase(const QRectF &base
) {
if ( !plot() )
return;
int currentIndex = zoomRectIndex();
QStack<QRectF> stack = zoomStack();
stack.replace(0, base);
setZoomStack(stack, 0);
zoom(currentIndex);
}
ui(new Ui::Widget),
curX( 0 ),
stepSize( 50 ),
{
ui->setupUi(this);
m_pPlot
->canvas
()->setPaintAttribute
(QwtPlotCanvas::BackingStore,
false);
m_pPlot->setCanvasBackground(Qt::black);
m_pPlot->setWindowFlags( m_pPlot->windowFlags() | Qt::SubWindow );
m_pPlot->plotLayout()->setAlignCanvasToScales(true);
m_pRescaler = new QwtPlotRescaler(m_pPlot->canvas());
m_pRescaler
->setAspectRatio
(QwtPlot::yLeft,
0.0);
m_pSizeGrip->resize(12,12);
m_pSizeGrip->move(width()-12,height()-12);
m_pCurve
->setPen
(QColor(Qt
::green));
m_pCurve
->setRenderHint
(QwtPlotItem::RenderAntialiased);
m_pCurve->attach(m_pPlot);
m_pGrid
->setPen
(QColor(Qt
::white));
m_pGrid->attach(m_pPlot);
m_pZoomer = new PlotZoomer( m_pPlot->canvas() );
m_pZoomer
->setRubberBandPen
(QColor(Qt
::red));
m_pZoomer
->setTrackerPen
( QColor( Qt
::red ) );
m_pZoomer
->setTrackerFont
( QFont( "Helvetica [Cronyx]",
12,
QFont::Bold ) );
m_pZoomer->setTrackerMode( PlotZoomer::AlwaysOn );
m_pZoomer
->setMousePattern
( QwtEventPattern::MouseSelect2, Qt
::RightButton, Qt
::ControlModifier );
m_pZoomer
->setMousePattern
( QwtEventPattern::MouseSelect3, Qt
::RightButton );
m_pMagnifier->setMouseButton( Qt::MidButton );
m_pPanner->setMouseButton( Qt::LeftButton, Qt::ControlModifier );
ui->verticalLayout->addWidget( m_pPlot );
connect( &timer, SIGNAL( timeout() ), SLOT( newValue() ) );
connect( ui->reset, SIGNAL( clicked() ), SLOT( playPause() ) );
qsrand
( QTime::currentTime().
msec() );
playPause();
}
Widget::~Widget()
{
delete ui;
}
void Widget::newValue()
{
points <<
QPointF(curX
++, qSin
(curX
/30)*100);
//qrand()%201 - 100);
if (points.size() > m_pPlot->canvas()->width()) {
m_pRescaler->setExpandingDirection(QwtPlotRescaler::ExpandDown);
points.remove(0);
}
int left (curX > points.size() ? curX - points.size() : 0);
m_pPlot
->setAxisScale
(QwtPlot::xBottom, left, left
+ m_pPlot
->canvas
()->width
(), stepSize
);
m_pCurve->setSamples(points);
m_pPlot->replot();
}
void Widget::playPause() {
bool wasActive(timer.isActive());
m_pZoomer->setEnabled(wasActive);
m_pMagnifier->setEnabled(wasActive);
m_pPanner->setEnabled(wasActive);
if (!wasActive) {
m_pZoomer->zoom(0);
ui->reset->setText("Stop");
points.clear();
curX = 0;
m_pPlot
->setAxisScale
(QwtPlot::xBottom,
0, m_pPlot
->canvas
()->width
(), stepSize
);
m_pPlot
->setAxisScale
(QwtPlot::yLeft,
0,
0,
0);
m_pPlot
->setAxisAutoScale
(QwtPlot::yLeft);
m_pPlot->setContentsMargins(0,0,0,15);
timer.start(20);
} else {
timer.stop();
ui->reset->setText("Start");
m_pZoomer
->setZoomBase
(QRectF(m_pPlot
->axisScaleDiv
(QwtPlot::xBottom)->lowerBound
(), m_pPlot
->axisScaleDiv
(QwtPlot::yLeft)->lowerBound
(), m_pPlot
->axisScaleDiv
(QwtPlot::xBottom)->range
(), m_pPlot
->axisScaleDiv
(QwtPlot::yLeft)->range
()));
m_pPlot->setContentsMargins(0,0,0,0);
m_pPlot->replot();
}
}
if (m_pPlot->isVisible()) {
QSize oldSize
(e
->oldSize
());
if (points.size() >= m_pPlot->canvas()->width() && size.width() < oldSize.width()) {
m_pRescaler->setExpandingDirection(QwtPlotRescaler::ExpandDown);
points.remove(0, oldSize.width() - size.width());
} else {
m_pRescaler->setExpandingDirection(QwtPlotRescaler::ExpandUp);
}
}
m_pSizeGrip->move(width()-12, height()-12);
}
#include "Widget.h"
#include "ui_Widget.h"
#include <QTime>
#include <QSizeGrip>
#include <QResizeEvent>
#include <qmath.h>
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_rescaler.h>
#include <qwt_scale_draw.h>
class PlotZoomer : public QwtPlotZoomer
{
public:
explicit PlotZoomer(QwtPlotCanvas *canvas);
void setZoomBase( const QRectF &base );
};
PlotZoomer::PlotZoomer(QwtPlotCanvas *canvas) :
QwtPlotZoomer(canvas)
{
}
void PlotZoomer::setZoomBase(const QRectF &base)
{
if ( !plot() )
return;
int currentIndex = zoomRectIndex();
QStack<QRectF> stack = zoomStack();
stack.replace(0, base);
setZoomStack(stack, 0);
zoom(currentIndex);
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),
m_pSizeGrip(new QSizeGrip(this)),
curX( 0 ),
stepSize( 50 ),
m_pPlot( new QwtPlot( this ) ),
m_pCurve( new QwtPlotCurve ),
m_pGrid( new QwtPlotGrid )
{
ui->setupUi(this);
m_pPlot->canvas()->setPaintAttribute(QwtPlotCanvas::BackingStore, false);
m_pPlot->setCanvasBackground(Qt::black);
m_pPlot->setWindowFlags( m_pPlot->windowFlags() | Qt::SubWindow );
m_pPlot->plotLayout()->setAlignCanvasToScales(true);
m_pRescaler = new QwtPlotRescaler(m_pPlot->canvas());
m_pRescaler->setAspectRatio(QwtPlot::yLeft, 0.0);
m_pSizeGrip->resize(12,12);
m_pSizeGrip->move(width()-12,height()-12);
m_pCurve->setPen(QColor(Qt::green));
m_pCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
m_pCurve->attach(m_pPlot);
m_pGrid->setPen(QColor(Qt::white));
m_pGrid->attach(m_pPlot);
m_pZoomer = new PlotZoomer( m_pPlot->canvas() );
m_pZoomer->setRubberBandPen(QColor(Qt::red));
m_pZoomer->setTrackerPen( QColor( Qt::red ) );
m_pZoomer->setTrackerFont( QFont( "Helvetica [Cronyx]", 12, QFont::Bold ) );
m_pZoomer->setTrackerMode( PlotZoomer::AlwaysOn );
m_pZoomer->setMousePattern( QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier );
m_pZoomer->setMousePattern( QwtEventPattern::MouseSelect3, Qt::RightButton );
m_pMagnifier = new QwtPlotMagnifier( m_pPlot->canvas() );
m_pMagnifier->setMouseButton( Qt::MidButton );
m_pPanner = new QwtPlotPanner( m_pPlot->canvas() );
m_pPanner->setMouseButton( Qt::LeftButton, Qt::ControlModifier );
ui->verticalLayout->addWidget( m_pPlot );
connect( &timer, SIGNAL( timeout() ), SLOT( newValue() ) );
connect( ui->reset, SIGNAL( clicked() ), SLOT( playPause() ) );
qsrand( QTime::currentTime().msec() );
playPause();
}
Widget::~Widget()
{
delete ui;
}
void Widget::newValue()
{
points << QPointF(curX++, qSin(curX/30)*100);//qrand()%201 - 100);
if (points.size() > m_pPlot->canvas()->width()) {
m_pRescaler->setExpandingDirection(QwtPlotRescaler::ExpandDown);
points.remove(0);
}
int left (curX > points.size() ? curX - points.size() : 0);
m_pPlot->setAxisScale(QwtPlot::xBottom, left, left + m_pPlot->canvas()->width(), stepSize);
m_pCurve->setSamples(points);
m_pPlot->replot();
}
void Widget::playPause() {
bool wasActive(timer.isActive());
m_pZoomer->setEnabled(wasActive);
m_pMagnifier->setEnabled(wasActive);
m_pPanner->setEnabled(wasActive);
m_pPlot->axisScaleDraw(QwtPlot::xBottom)->enableComponent(QwtAbstractScaleDraw::Labels, wasActive);
if (!wasActive) {
m_pZoomer->zoom(0);
ui->reset->setText("Stop");
points.clear();
curX = 0;
m_pPlot->setAxisScale(QwtPlot::xBottom, 0, m_pPlot->canvas()->width(), stepSize);
m_pPlot->setAxisScale(QwtPlot::yLeft, 0, 0, 0);
m_pPlot->setAxisAutoScale(QwtPlot::yLeft);
m_pPlot->setContentsMargins(0,0,0,15);
timer.start(20);
} else {
timer.stop();
ui->reset->setText("Start");
m_pZoomer->setZoomBase(QRectF(m_pPlot->axisScaleDiv(QwtPlot::xBottom)->lowerBound(), m_pPlot->axisScaleDiv(QwtPlot::yLeft)->lowerBound(), m_pPlot->axisScaleDiv(QwtPlot::xBottom)->range(), m_pPlot->axisScaleDiv(QwtPlot::yLeft)->range()));
m_pPlot->setContentsMargins(0,0,0,0);
m_pPlot->replot();
}
}
void Widget::resizeEvent(QResizeEvent *e) {
QWidget::resizeEvent(e);
if (m_pPlot->isVisible()) {
QSize oldSize(e->oldSize());
QSize size(e->size());
if (points.size() >= m_pPlot->canvas()->width() && size.width() < oldSize.width()) {
m_pRescaler->setExpandingDirection(QwtPlotRescaler::ExpandDown);
points.remove(0, oldSize.width() - size.width());
} else {
m_pRescaler->setExpandingDirection(QwtPlotRescaler::ExpandUp);
}
}
m_pSizeGrip->move(width()-12, height()-12);
}
To copy to clipboard, switch view to plain text mode
Bookmarks