Loading...
Searching...
No Matches
qwt_plot_picker.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_picker.h"
#include "qwt_plot.h"
#include "qwt_text.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_picker_machine.h"
class QwtPlotPicker::PrivateData
{
public:
PrivateData():
xAxisId( -1 ),
yAxisId( -1 )
{
}
QwtAxisId xAxisId;
QwtAxisId yAxisId;
};
QwtPlotPicker::QwtPlotPicker( QWidget* canvas )
: QwtPicker( canvas )
{
m_data = new PrivateData;
if ( !canvas )
return;
const QwtPlot* plot = QwtPlotPicker::plot();
// attach axes
using namespace QwtAxis;
int xAxis = XBottom;
if ( !plot->isAxisVisible( XBottom ) && plot->isAxisVisible( XTop ) )
xAxis = XTop;
int yAxis = YLeft;
if ( !plot->isAxisVisible( YLeft ) && plot->isAxisVisible( YRight ) )
yAxis = YRight;
setAxes( xAxis, yAxis );
}
QwtPlotPicker::QwtPlotPicker( QwtAxisId xAxisId, QwtAxisId yAxisId, QWidget* canvas )
: QwtPicker( canvas )
{
m_data = new PrivateData;
m_data->xAxisId = xAxisId;
m_data->yAxisId = yAxisId;
}
QwtPlotPicker::QwtPlotPicker( QwtAxisId xAxisId, QwtAxisId yAxisId,
RubberBand rubberBand, DisplayMode trackerMode, QWidget* canvas )
: QwtPicker( rubberBand, trackerMode, canvas )
{
m_data = new PrivateData;
m_data->xAxisId = xAxisId;
m_data->yAxisId = yAxisId;
}
QwtPlotPicker::~QwtPlotPicker()
{
delete m_data;
}
QWidget* QwtPlotPicker::canvas()
{
return parentWidget();
}
const QWidget* QwtPlotPicker::canvas() const
{
return parentWidget();
}
QwtPlot* QwtPlotPicker::plot()
{
QWidget* w = canvas();
if ( w )
w = w->parentWidget();
return qobject_cast< QwtPlot* >( w );
}
const QwtPlot* QwtPlotPicker::plot() const
{
const QWidget* w = canvas();
if ( w )
w = w->parentWidget();
return qobject_cast< const QwtPlot* >( w );
}
QRectF QwtPlotPicker::scaleRect() const
{
QRectF rect;
if ( plot() )
{
const QwtScaleDiv& xs = plot()->axisScaleDiv( xAxis() );
const QwtScaleDiv& ys = plot()->axisScaleDiv( yAxis() );
rect = QRectF( xs.lowerBound(), ys.lowerBound(),
xs.range(), ys.range() );
rect = rect.normalized();
}
return rect;
}
void QwtPlotPicker::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
{
const QwtPlot* plt = plot();
if ( !plt )
return;
if ( xAxisId != m_data->xAxisId || yAxisId != m_data->yAxisId )
{
m_data->xAxisId = xAxisId;
m_data->yAxisId = yAxisId;
}
}
QwtAxisId QwtPlotPicker::xAxis() const
{
return m_data->xAxisId;
}
QwtAxisId QwtPlotPicker::yAxis() const
{
return m_data->yAxisId;
}
QwtText QwtPlotPicker::trackerText( const QPoint& pos ) const
{
if ( plot() == NULL )
return QwtText();
return trackerTextF( invTransform( pos ) );
}
QwtText QwtPlotPicker::trackerTextF( const QPointF& pos ) const
{
QString text;
switch ( rubberBand() )
{
case HLineRubberBand:
text = QString::number( pos.y(), 'f', 4 );
break;
case VLineRubberBand:
text = QString::number( pos.x(), 'f', 4 );
break;
default:
text = QString::number( pos.x(), 'f', 4 )
+ ", " + QString::number( pos.y(), 'f', 4 );
}
return QwtText( text );
}
void QwtPlotPicker::append( const QPoint& pos )
{
QwtPicker::append( pos );
Q_EMIT appended( invTransform( pos ) );
}
void QwtPlotPicker::move( const QPoint& pos )
{
QwtPicker::move( pos );
Q_EMIT moved( invTransform( pos ) );
}
bool QwtPlotPicker::end( bool ok )
{
ok = QwtPicker::end( ok );
if ( !ok )
return false;
QwtPlot* plot = QwtPlotPicker::plot();
if ( !plot )
return false;
const QPolygon points = selection();
if ( points.count() == 0 )
return false;
QwtPickerMachine::SelectionType selectionType =
QwtPickerMachine::NoSelection;
if ( stateMachine() )
selectionType = stateMachine()->selectionType();
switch ( selectionType )
{
case QwtPickerMachine::PointSelection:
{
const QPointF pos = invTransform( points.first() );
Q_EMIT selected( pos );
break;
}
case QwtPickerMachine::RectSelection:
{
if ( points.count() >= 2 )
{
const QPoint p1 = points.first();
const QPoint p2 = points.last();
const QRect rect = QRect( p1, p2 ).normalized();
Q_EMIT selected( invTransform( rect ) );
}
break;
}
case QwtPickerMachine::PolygonSelection:
{
QVector< QPointF > dpa( points.count() );
for ( int i = 0; i < points.count(); i++ )
dpa[i] = invTransform( points[i] );
Q_EMIT selected( dpa );
}
default:
break;
}
return true;
}
QRectF QwtPlotPicker::invTransform( const QRect& rect ) const
{
const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
return QwtScaleMap::invTransform( xMap, yMap, rect );
}
QRect QwtPlotPicker::transform( const QRectF& rect ) const
{
const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
}
QPointF QwtPlotPicker::invTransform( const QPoint& pos ) const
{
const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
return QPointF(
xMap.invTransform( pos.x() ),
yMap.invTransform( pos.y() )
);
}
QPoint QwtPlotPicker::transform( const QPointF& pos ) const
{
const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
const QPointF p( xMap.transform( pos.x() ), yMap.transform( pos.y() ) );
return p.toPoint();
}
#include "moc_qwt_plot_picker.cpp"
QVector
Definition qwt_clipper.h:23
QwtPicker
QwtPicker provides selections on a widget.
Definition qwt_picker.h:104
QwtPicker::DisplayMode
DisplayMode
Display mode.
Definition qwt_picker.h:162
QwtPicker::rubberBand
RubberBand rubberBand() const
Definition qwt_picker.cpp:299
QwtPicker::move
virtual void move(const QPoint &)
Definition qwt_picker.cpp:1357
QwtPicker::append
virtual void append(const QPoint &)
Definition qwt_picker.cpp:1339
QwtPicker::RubberBand
RubberBand
Definition qwt_picker.h:128
QwtPicker::VLineRubberBand
@ VLineRubberBand
A vertical line ( only for QwtPickerMachine::PointSelection )
Definition qwt_picker.h:136
QwtPicker::HLineRubberBand
@ HLineRubberBand
A horizontal line ( only for QwtPickerMachine::PointSelection )
Definition qwt_picker.h:133
QwtPicker::end
virtual bool end(bool ok=true)
Close a selection setting the state to inactive.
Definition qwt_picker.cpp:1291
QwtPicker::parentWidget
QWidget * parentWidget()
Return the parent widget, where the selection happens.
Definition qwt_picker.cpp:263
QwtPicker::stateMachine
const QwtPickerMachine * stateMachine() const
Definition qwt_picker.cpp:257
QwtPicker::selection
QPolygon selection() const
Definition qwt_picker.cpp:792
QwtPickerMachine::SelectionType
SelectionType
Definition qwt_picker_machine.h:36
QwtPickerMachine::NoSelection
@ NoSelection
The state machine not usable for any type of selection.
Definition qwt_picker_machine.h:38
QwtPickerMachine::RectSelection
@ RectSelection
The state machine is for selecting a rectangle (2 points).
Definition qwt_picker_machine.h:44
QwtPickerMachine::PolygonSelection
@ PolygonSelection
The state machine is for selecting a polygon (many points).
Definition qwt_picker_machine.h:47
QwtPickerMachine::PointSelection
@ PointSelection
The state machine is for selecting a single point.
Definition qwt_picker_machine.h:41
QwtPickerMachine::selectionType
SelectionType selectionType() const
Return the selection type.
Definition qwt_picker_machine.cpp:28
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79
QwtPlot::isAxisVisible
bool isAxisVisible(QwtAxisId) const
Definition qwt_plot_axis.cpp:225
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition qwt_plot_axis.cpp:283
QwtPlotPicker::transform
QRect transform(const QRectF &) const
Definition qwt_plot_picker.cpp:354
QwtPlotPicker::~QwtPlotPicker
virtual ~QwtPlotPicker()
Destructor.
Definition qwt_plot_picker.cpp:108
QwtPlotPicker::end
virtual bool end(bool ok=true) override
Definition qwt_plot_picker.cpp:280
QwtPlotPicker::selected
void selected(const QPointF &pos)
QwtPlotPicker::yAxis
QwtAxisId yAxis() const
Return y axis.
Definition qwt_plot_picker.cpp:192
QwtPlotPicker::moved
void moved(const QPointF &pos)
QwtPlotPicker::append
virtual void append(const QPoint &) override
Definition qwt_plot_picker.cpp:251
QwtPlotPicker::appended
void appended(const QPointF &pos)
QwtPlotPicker::xAxis
QwtAxisId xAxis() const
Return x axis.
Definition qwt_plot_picker.cpp:186
QwtPlotPicker::setAxes
virtual void setAxes(QwtAxisId xAxisId, QwtAxisId yAxisId)
Definition qwt_plot_picker.cpp:172
QwtPlotPicker::move
virtual void move(const QPoint &) override
Definition qwt_plot_picker.cpp:266
QwtPlotPicker::trackerText
virtual QwtText trackerText(const QPoint &) const override
Definition qwt_plot_picker.cpp:203
QwtPlotPicker::QwtPlotPicker
QwtPlotPicker(QWidget *canvas)
Create a plot picker.
Definition qwt_plot_picker.cpp:43
QwtPlotPicker::trackerTextF
virtual QwtText trackerTextF(const QPointF &) const
Translate a position into a position string.
Definition qwt_plot_picker.cpp:223
QwtPlotPicker::scaleRect
QRectF scaleRect() const
Definition qwt_plot_picker.cpp:149
QwtPlotPicker::invTransform
QRectF invTransform(const QRect &) const
Definition qwt_plot_picker.cpp:341
QwtPlotPicker::plot
QwtPlot * plot()
Definition qwt_plot_picker.cpp:126
QwtPlotPicker::canvas
QWidget * canvas()
Definition qwt_plot_picker.cpp:114
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::lowerBound
double lowerBound() const
Definition qwt_scale_div.cpp:131
QwtScaleDiv::range
double range() const
Definition qwt_scale_div.cpp:159
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtScaleMap::invTransform
double invTransform(double p) const
Definition qwt_scale_map.h:154
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtAxis
Definition qwt_axis.h:19