Add files from zip
This commit is contained in:
153
playground/rescaler/MainWindow.cpp
Normal file
153
playground/rescaler/MainWindow.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "MainWindow.h"
|
||||
#include "Plot.h"
|
||||
|
||||
#include <QwtPlot>
|
||||
#include <QwtPlotRescaler>
|
||||
#include <QwtInterval>
|
||||
|
||||
#include <QGroupBox>
|
||||
#include <QComboBox>
|
||||
#include <QLayout>
|
||||
#include <QStatusBar>
|
||||
#include <QLabel>
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
QFrame* w = new QFrame( this );
|
||||
|
||||
QWidget* panel = createPanel( w );
|
||||
panel->setFixedWidth( 2 * panel->sizeHint().width() );
|
||||
m_plot = createPlot( w );
|
||||
|
||||
QHBoxLayout* layout = new QHBoxLayout( w );
|
||||
layout->setContentsMargins( QMargins() );
|
||||
layout->addWidget( panel, 0 );
|
||||
layout->addWidget( m_plot, 10 );
|
||||
|
||||
setCentralWidget( w );
|
||||
|
||||
setRescaleMode( 0 );
|
||||
|
||||
( void )statusBar();
|
||||
}
|
||||
|
||||
QWidget* MainWindow::createPanel( QWidget* parent )
|
||||
{
|
||||
QGroupBox* panel = new QGroupBox( "Navigation Panel", parent );
|
||||
|
||||
QComboBox* rescaleBox = new QComboBox( panel );
|
||||
rescaleBox->setEditable( false );
|
||||
rescaleBox->insertItem( KeepScales, "None" );
|
||||
rescaleBox->insertItem( Fixed, "Fixed" );
|
||||
rescaleBox->insertItem( Expanding, "Expanding" );
|
||||
rescaleBox->insertItem( Fitting, "Fitting" );
|
||||
|
||||
connect( rescaleBox, SIGNAL(activated(int)), SLOT(setRescaleMode(int)) );
|
||||
|
||||
m_rescaleInfo = new QLabel( panel );
|
||||
m_rescaleInfo->setSizePolicy(
|
||||
QSizePolicy::Expanding, QSizePolicy::Expanding );
|
||||
m_rescaleInfo->setWordWrap( true );
|
||||
|
||||
QVBoxLayout* layout = new QVBoxLayout( panel );
|
||||
layout->addWidget( rescaleBox );
|
||||
layout->addWidget( m_rescaleInfo );
|
||||
layout->addStretch( 10 );
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
Plot* MainWindow::createPlot( QWidget* parent )
|
||||
{
|
||||
Plot* plot = new Plot( parent, QwtInterval( 0.0, 1000.0 ) );
|
||||
plot->replot();
|
||||
|
||||
m_rescaler = new QwtPlotRescaler( plot->canvas() );
|
||||
m_rescaler->setReferenceAxis( QwtAxis::XBottom );
|
||||
m_rescaler->setAspectRatio( QwtAxis::YLeft, 1.0 );
|
||||
m_rescaler->setAspectRatio( QwtAxis::YRight, 0.0 );
|
||||
m_rescaler->setAspectRatio( QwtAxis::XTop, 0.0 );
|
||||
|
||||
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
|
||||
m_rescaler->setIntervalHint( axisPos, QwtInterval( 0.0, 1000.0 ) );
|
||||
|
||||
connect( plot, SIGNAL(resized(double,double)),
|
||||
SLOT(showRatio(double,double)) );
|
||||
return plot;
|
||||
}
|
||||
|
||||
void MainWindow::setRescaleMode( int mode )
|
||||
{
|
||||
bool doEnable = true;
|
||||
QString info;
|
||||
QRectF rectOfInterest;
|
||||
QwtPlotRescaler::ExpandingDirection direction = QwtPlotRescaler::ExpandUp;
|
||||
|
||||
switch( mode )
|
||||
{
|
||||
case KeepScales:
|
||||
{
|
||||
doEnable = false;
|
||||
info = "All scales remain unchanged, when the plot is resized";
|
||||
break;
|
||||
}
|
||||
case Fixed:
|
||||
{
|
||||
m_rescaler->setRescalePolicy( QwtPlotRescaler::Fixed );
|
||||
info = "The scale of the bottom axis remains unchanged, "
|
||||
"when the plot is resized. All other scales are changed, "
|
||||
"so that a pixel on screen means the same distance for"
|
||||
"all scales.";
|
||||
break;
|
||||
}
|
||||
case Expanding:
|
||||
{
|
||||
m_rescaler->setRescalePolicy( QwtPlotRescaler::Expanding );
|
||||
info = "The scales of all axis are shrinked/expanded, when "
|
||||
"resizing the plot, keeping the distance that is represented "
|
||||
"by one pixel.";
|
||||
m_rescaleInfo->setText( "Expanding" );
|
||||
break;
|
||||
}
|
||||
case Fitting:
|
||||
{
|
||||
m_rescaler->setRescalePolicy( QwtPlotRescaler::Fitting );
|
||||
const QwtInterval xIntv =
|
||||
m_rescaler->intervalHint( QwtAxis::XBottom );
|
||||
const QwtInterval yIntv =
|
||||
m_rescaler->intervalHint( QwtAxis::YLeft );
|
||||
|
||||
rectOfInterest = QRectF( xIntv.minValue(), yIntv.minValue(),
|
||||
xIntv.width(), yIntv.width() );
|
||||
direction = QwtPlotRescaler::ExpandBoth;
|
||||
|
||||
info = "Fitting";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_plot->setRectOfInterest( rectOfInterest );
|
||||
|
||||
m_rescaleInfo->setText( info );
|
||||
m_rescaler->setEnabled( doEnable );
|
||||
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
|
||||
m_rescaler->setExpandingDirection( direction );
|
||||
|
||||
if ( doEnable )
|
||||
m_rescaler->rescale();
|
||||
else
|
||||
m_plot->replot();
|
||||
}
|
||||
|
||||
void MainWindow::showRatio( double xRatio, double yRatio )
|
||||
{
|
||||
const QString msg = QString( "%1, %2" ).arg( xRatio ).arg( yRatio );
|
||||
statusBar()->showMessage( msg );
|
||||
}
|
||||
|
||||
#include "moc_MainWindow.cpp"
|
||||
41
playground/rescaler/MainWindow.h
Normal file
41
playground/rescaler/MainWindow.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
class QwtPlotRescaler;
|
||||
class QLabel;
|
||||
class Plot;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum RescaleMode
|
||||
{
|
||||
KeepScales,
|
||||
Fixed,
|
||||
Expanding,
|
||||
Fitting
|
||||
};
|
||||
|
||||
MainWindow();
|
||||
|
||||
private Q_SLOTS:
|
||||
void setRescaleMode( int );
|
||||
void showRatio( double, double );
|
||||
|
||||
private:
|
||||
QWidget* createPanel( QWidget* );
|
||||
Plot* createPlot( QWidget* );
|
||||
|
||||
QwtPlotRescaler* m_rescaler;
|
||||
QLabel* m_rescaleInfo;
|
||||
|
||||
Plot* m_plot;
|
||||
};
|
||||
190
playground/rescaler/Plot.cpp
Normal file
190
playground/rescaler/Plot.cpp
Normal file
@@ -0,0 +1,190 @@
|
||||
/*****************************************************************************
|
||||
* 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 <QwtPlotGrid>
|
||||
#include <QwtPlotItem>
|
||||
#include <QwtScaleMap>
|
||||
#include <QwtPlotLayout>
|
||||
#include <QwtInterval>
|
||||
#include <QwtPainter>
|
||||
#include <QwtMath>
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
class TextItem : public QwtPlotItem
|
||||
{
|
||||
public:
|
||||
void setText( const QString& text )
|
||||
{
|
||||
m_text = text;
|
||||
}
|
||||
|
||||
virtual void draw( QPainter* painter,
|
||||
const QwtScaleMap&, const QwtScaleMap&,
|
||||
const QRectF& canvasRect ) const QWT_OVERRIDE
|
||||
{
|
||||
const int margin = 5;
|
||||
const QRectF textRect =
|
||||
canvasRect.adjusted( margin, margin, -margin, -margin );
|
||||
|
||||
painter->setPen( Qt::white );
|
||||
painter->drawText( textRect,
|
||||
Qt::AlignBottom | Qt::AlignRight, m_text );
|
||||
}
|
||||
|
||||
private:
|
||||
QString m_text;
|
||||
};
|
||||
|
||||
// RectItem shows how to implement a simple plot item,
|
||||
// what wouldn't be necessary as QwtPlotShapeItem
|
||||
// would do the same
|
||||
|
||||
class RectItem : public QwtPlotItem
|
||||
{
|
||||
public:
|
||||
enum Type
|
||||
{
|
||||
Rect,
|
||||
Ellipse
|
||||
};
|
||||
|
||||
explicit RectItem( Type type )
|
||||
: m_type( type )
|
||||
{
|
||||
}
|
||||
|
||||
void setPen( const QPen& pen )
|
||||
{
|
||||
if ( pen != m_pen )
|
||||
{
|
||||
m_pen = pen;
|
||||
itemChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void setBrush( const QBrush& brush )
|
||||
{
|
||||
if ( brush != m_brush )
|
||||
{
|
||||
m_brush = brush;
|
||||
itemChanged();
|
||||
}
|
||||
}
|
||||
void setRect( const QRectF& rect )
|
||||
{
|
||||
if ( m_rect != rect )
|
||||
{
|
||||
m_rect = rect;
|
||||
itemChanged();
|
||||
}
|
||||
}
|
||||
|
||||
virtual QRectF boundingRect() const QWT_OVERRIDE
|
||||
{
|
||||
return m_rect;
|
||||
}
|
||||
|
||||
virtual void draw( QPainter* painter,
|
||||
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
||||
const QRectF& ) const QWT_OVERRIDE
|
||||
{
|
||||
if ( m_rect.isValid() )
|
||||
{
|
||||
const QRectF rect = QwtScaleMap::transform(
|
||||
xMap, yMap, m_rect );
|
||||
|
||||
painter->setPen( m_pen );
|
||||
painter->setBrush( m_brush );
|
||||
|
||||
if ( m_type == Ellipse )
|
||||
QwtPainter::drawEllipse( painter, rect );
|
||||
else
|
||||
QwtPainter::drawRect( painter, rect );
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
QPen m_pen;
|
||||
QBrush m_brush;
|
||||
QRectF m_rect;
|
||||
Type m_type;
|
||||
};
|
||||
|
||||
Plot::Plot( QWidget* parent, const QwtInterval& interval )
|
||||
: QwtPlot( parent )
|
||||
{
|
||||
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
|
||||
setAxisScale( axisPos, interval.minValue(), interval.maxValue() );
|
||||
|
||||
setCanvasBackground( QColor( Qt::darkBlue ) );
|
||||
plotLayout()->setAlignCanvasToScales( true );
|
||||
|
||||
// grid
|
||||
QwtPlotGrid* grid = new QwtPlotGrid;
|
||||
//grid->enableXMin(true);
|
||||
grid->setMajorPen( Qt::white, 0, Qt::DotLine );
|
||||
grid->setMinorPen( Qt::gray, 0, Qt::DotLine );
|
||||
grid->attach( this );
|
||||
|
||||
const int numEllipses = 10;
|
||||
|
||||
for ( int i = 0; i < numEllipses; i++ )
|
||||
{
|
||||
const double x = interval.minValue() +
|
||||
qwtRand() % qRound( interval.width() );
|
||||
const double y = interval.minValue() +
|
||||
qwtRand() % qRound( interval.width() );
|
||||
const double r = interval.minValue() +
|
||||
qwtRand() % qRound( interval.width() / 6 );
|
||||
|
||||
const QRectF area( x - r, y - r, 2 * r, 2 * r );
|
||||
|
||||
RectItem* item = new RectItem( RectItem::Ellipse );
|
||||
item->setRenderHint( QwtPlotItem::RenderAntialiased, true );
|
||||
item->setRect( area );
|
||||
item->setPen( QPen( Qt::yellow ) );
|
||||
item->attach( this );
|
||||
}
|
||||
|
||||
TextItem* textItem = new TextItem();
|
||||
textItem->setText( "Navigation Example" );
|
||||
textItem->attach( this );
|
||||
|
||||
m_rectItem = new RectItem( RectItem::Rect );
|
||||
m_rectItem->setPen( Qt::NoPen );
|
||||
QColor c = Qt::gray;
|
||||
c.setAlpha( 100 );
|
||||
m_rectItem->setBrush( QBrush( c ) );
|
||||
m_rectItem->attach( this );
|
||||
}
|
||||
|
||||
void Plot::updateLayout()
|
||||
{
|
||||
QwtPlot::updateLayout();
|
||||
|
||||
const QwtScaleMap xMap = canvasMap( QwtAxis::XBottom );
|
||||
const QwtScaleMap yMap = canvasMap( QwtAxis::YLeft );
|
||||
|
||||
const QRect cr = canvas()->contentsRect();
|
||||
const double x1 = xMap.invTransform( cr.left() );
|
||||
const double x2 = xMap.invTransform( cr.right() );
|
||||
const double y1 = yMap.invTransform( cr.bottom() );
|
||||
const double y2 = yMap.invTransform( cr.top() );
|
||||
|
||||
const double xRatio = ( x2 - x1 ) / cr.width();
|
||||
const double yRatio = ( y2 - y1 ) / cr.height();
|
||||
|
||||
Q_EMIT resized( xRatio, yRatio );
|
||||
}
|
||||
|
||||
void Plot::setRectOfInterest( const QRectF& rect )
|
||||
{
|
||||
m_rectItem->setRect( rect );
|
||||
}
|
||||
|
||||
#include "moc_Plot.cpp"
|
||||
28
playground/rescaler/Plot.h
Normal file
28
playground/rescaler/Plot.h
Normal file
@@ -0,0 +1,28 @@
|
||||
/*****************************************************************************
|
||||
* 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 RectItem;
|
||||
class QwtInterval;
|
||||
|
||||
class Plot : public QwtPlot
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Plot( QWidget* parent, const QwtInterval& );
|
||||
virtual void updateLayout() QWT_OVERRIDE;
|
||||
|
||||
void setRectOfInterest( const QRectF& );
|
||||
|
||||
Q_SIGNALS:
|
||||
void resized( double xRatio, double yRatio );
|
||||
|
||||
private:
|
||||
RectItem* m_rectItem;
|
||||
};
|
||||
20
playground/rescaler/main.cpp
Normal file
20
playground/rescaler/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "MainWindow.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
QApplication app( argc, argv );
|
||||
|
||||
MainWindow window;
|
||||
|
||||
window.resize( 800, 600 );
|
||||
window.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
18
playground/rescaler/rescaler.pro
Normal file
18
playground/rescaler/rescaler.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 = rescaler
|
||||
|
||||
HEADERS = \
|
||||
MainWindow.h \
|
||||
Plot.h
|
||||
|
||||
SOURCES = \
|
||||
MainWindow.cpp \
|
||||
Plot.cpp \
|
||||
main.cpp
|
||||
|
||||
Reference in New Issue
Block a user