1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/**
* SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
*
* This file is part of the KD Chart library.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "zoomwidget.h"
#include <KChartAbstractCoordinatePlane>
#include <QWheelEvent>
#include <QDebug>
ZoomWidget::ZoomWidget( QWidget* parent ) :
KChart::Widget( parent )
{
setFocusPolicy( Qt::WheelFocus );
}
QPointF ZoomWidget::findNewZoomCenter( const QPoint & pos )
{
if ( ! height() || ! width() ) return coordinatePlane()->zoomCenter();
const qreal coordWidth = 1.0;
const qreal coordHeight = 1.0;
// qDebug() << "pos = " << pos;
const qreal resX = static_cast<qreal>( coordWidth /coordinatePlane()->zoomFactorX() )/ width();
const qreal resY = static_cast<qreal>( coordHeight/coordinatePlane()->zoomFactorY() )/ height();
// qDebug() << "resX = " << resX << " resY = " << resY;
const qreal dX = (pos.x() - 0.5*width() ) * resX;
const qreal dY = (pos.y() - 0.5*height()) * resY;
// qDebug() << "dX = " << dX << " dY = " << dY;
const qreal zoomCenterX = coordinatePlane()->zoomCenter().x() + dX;
const qreal zoomCenterY = coordinatePlane()->zoomCenter().y() + dY;
return QPointF( zoomCenterX, zoomCenterY );
}
void ZoomWidget::mousePressEvent( QMouseEvent * e )
{
const QPointF zoomCenter( findNewZoomCenter( e->pos() ) );
if ( zoomCenter != coordinatePlane()->zoomCenter() ) {
// qDebug() << "zoom center = " << zoomCenter;
coordinatePlane()->setZoomCenter( zoomCenter );
update();
}
}
void ZoomWidget::wheelEvent( QWheelEvent* e )
{
qreal delta = static_cast<qreal>( e->angleDelta().y() ) / 120.0 / 10.0;
coordinatePlane()->setZoomFactorX( coordinatePlane()->zoomFactorX() + delta );
coordinatePlane()->setZoomFactorY( coordinatePlane()->zoomFactorY() + delta );
/* new:
const QPointF zoomCenter( findNewZoomCenter( e->pos() ) );
if ( zoomCenter != coordinatePlane()->zoomCenter() ) {
qDebug() << "zoom center = " << zoomCenter;
coordinatePlane()->setZoomCenter( zoomCenter );
}
*/
/* old:
qreal zoomCenterX = static_cast<qreal>( e->pos().x() ) / static_cast<qreal>( width() );
qreal zoomCenterY = static_cast<qreal>( e->pos().y() ) / static_cast<qreal>( height() );
QPointF zoomCenter( zoomCenterX, zoomCenterY );
coordinatePlane()->setZoomCenter( zoomCenter );
*/
update();
}
void ZoomWidget::keyPressEvent( QKeyEvent* e )
{
int dZoom = 0;
qreal dX = 0;
qreal dY = 0;
switch ( e->key() ) {
case Qt::Key_PageDown:
dZoom = 1;
break;
case Qt::Key_Down:
dY = 0.1;
break;
case Qt::Key_Right:
dX = 0.1;
break;
case Qt::Key_PageUp:
dZoom = -1;
break;
case Qt::Key_Up:
dY = -0.1;
break;
case Qt::Key_Left:
dX = -0.1;
break;
default:
e->ignore();
}
if ( dZoom || dX || dY ) {
const double factor = qMax( double(1.0), coordinatePlane()->zoomFactorX() + dZoom );
const qreal x = coordinatePlane()->zoomCenter().x() + dX;
const qreal y = coordinatePlane()->zoomCenter().y() + dY;
coordinatePlane()->setZoomFactorX( factor );
coordinatePlane()->setZoomFactorY( factor );
coordinatePlane()->setZoomCenter( QPointF(x,y) );
update();
}
}
|