Files
GoldenCheetah/qwt/examples/splineeditor/ScalePicker.cpp
Joachim Kohlhammer 49cf6340a4 Upgraded Qwt to 6.2 (branch: qwt-multiaxes) (#4427)
This commit is based on https://github.com/GoldenCheetah/GoldenCheetah/pull/3956
with the following additions / changes:
* Upgraded to the latest version of the multiaxes-branch, thus eliminating crashes of GoldenCheetah on startup
* Disabled the emitting of Layout Requests on geometry changes of QwtScaleWidget - without this, CPU utilization was up to 100% on one core
* Added the class SplineLookup, reusing small portions of code from Qwt 6.1
* Re-added the splines in WPrime and RideFile (resampling), using the new interface of QwtSpline
* Appveyor: qwt in cache-section now depends on qwt/qwtconfig.prin.in for refresh on version change
2024-01-06 18:59:55 -03:00

130 lines
3.6 KiB
C++

/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "ScalePicker.h"
#include <QwtPlot>
#include <QwtScaleWidget>
#include <QwtScaleMap>
#include <QMouseEvent>
#include <qmath.h>
ScalePicker::ScalePicker( QwtPlot* plot )
: QObject( plot )
{
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
{
QwtScaleWidget* scaleWidget = plot->axisWidget( axisPos );
if ( scaleWidget )
scaleWidget->installEventFilter( this );
}
}
bool ScalePicker::eventFilter( QObject* object, QEvent* event )
{
if ( event->type() == QEvent::MouseButtonPress )
{
QwtScaleWidget* scaleWidget = qobject_cast< QwtScaleWidget* >( object );
if ( scaleWidget )
{
QMouseEvent* mouseEvent = static_cast< QMouseEvent* >( event );
mouseClicked( scaleWidget, mouseEvent->pos() );
return true;
}
}
return QObject::eventFilter( object, event );
}
void ScalePicker::mouseClicked( const QwtScaleWidget* scale, const QPoint& pos )
{
QRect rect = scaleRect( scale );
int margin = 20; // 20 pixels tolerance
rect.setRect( rect.x() - margin, rect.y() - margin,
rect.width() + 2 * margin, rect.height() + 2 * margin );
if ( rect.contains( pos ) ) // No click on the title
{
// translate the position in a value on the scale
double value = 0.0;
int axis = -1;
const QwtScaleDraw* sd = scale->scaleDraw();
switch( scale->alignment() )
{
case QwtScaleDraw::LeftScale:
{
value = sd->scaleMap().invTransform( pos.y() );
axis = QwtAxis::YLeft;
break;
}
case QwtScaleDraw::RightScale:
{
value = sd->scaleMap().invTransform( pos.y() );
axis = QwtAxis::YRight;
break;
}
case QwtScaleDraw::BottomScale:
{
value = sd->scaleMap().invTransform( pos.x() );
axis = QwtAxis::XBottom;
break;
}
case QwtScaleDraw::TopScale:
{
value = sd->scaleMap().invTransform( pos.x() );
axis = QwtAxis::XTop;
break;
}
}
Q_EMIT clicked( axis, value );
}
}
// The rect of a scale without the title
QRect ScalePicker::scaleRect( const QwtScaleWidget* scale ) const
{
const int bld = scale->margin();
const int mjt = qCeil( scale->scaleDraw()->maxTickLength() );
const int sbd = scale->startBorderDist();
const int ebd = scale->endBorderDist();
QRect rect;
switch( scale->alignment() )
{
case QwtScaleDraw::LeftScale:
{
rect.setRect( scale->width() - bld - mjt, sbd,
mjt, scale->height() - sbd - ebd );
break;
}
case QwtScaleDraw::RightScale:
{
rect.setRect( bld, sbd,
mjt, scale->height() - sbd - ebd );
break;
}
case QwtScaleDraw::BottomScale:
{
rect.setRect( sbd, bld,
scale->width() - sbd - ebd, mjt );
break;
}
case QwtScaleDraw::TopScale:
{
rect.setRect( sbd, scale->height() - bld - mjt,
scale->width() - sbd - ebd, mjt );
break;
}
}
return rect;
}
#include "moc_ScalePicker.cpp"