File indexing completed on 2025-02-16 04:03:08
0001 /** 0002 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "zoomwidget.h" 0010 #include <KChartAbstractCoordinatePlane> 0011 #include <QWheelEvent> 0012 #include <QDebug> 0013 0014 ZoomWidget::ZoomWidget( QWidget* parent ) : 0015 KChart::Widget( parent ) 0016 { 0017 setFocusPolicy( Qt::WheelFocus ); 0018 } 0019 0020 0021 QPointF ZoomWidget::findNewZoomCenter( const QPoint & pos ) 0022 { 0023 if ( ! height() || ! width() ) return coordinatePlane()->zoomCenter(); 0024 0025 const qreal coordWidth = 1.0; 0026 const qreal coordHeight = 1.0; 0027 0028 // qDebug() << "pos = " << pos; 0029 const qreal resX = static_cast<qreal>( coordWidth /coordinatePlane()->zoomFactorX() )/ width(); 0030 const qreal resY = static_cast<qreal>( coordHeight/coordinatePlane()->zoomFactorY() )/ height(); 0031 // qDebug() << "resX = " << resX << " resY = " << resY; 0032 const qreal dX = (pos.x() - 0.5*width() ) * resX; 0033 const qreal dY = (pos.y() - 0.5*height()) * resY; 0034 // qDebug() << "dX = " << dX << " dY = " << dY; 0035 const qreal zoomCenterX = coordinatePlane()->zoomCenter().x() + dX; 0036 const qreal zoomCenterY = coordinatePlane()->zoomCenter().y() + dY; 0037 return QPointF( zoomCenterX, zoomCenterY ); 0038 } 0039 0040 0041 void ZoomWidget::mousePressEvent( QMouseEvent * e ) 0042 { 0043 const QPointF zoomCenter( findNewZoomCenter( e->pos() ) ); 0044 if ( zoomCenter != coordinatePlane()->zoomCenter() ) { 0045 // qDebug() << "zoom center = " << zoomCenter; 0046 coordinatePlane()->setZoomCenter( zoomCenter ); 0047 update(); 0048 } 0049 } 0050 0051 0052 void ZoomWidget::wheelEvent( QWheelEvent* e ) 0053 { 0054 qreal delta = static_cast<qreal>( e->angleDelta().y() ) / 120.0 / 10.0; 0055 coordinatePlane()->setZoomFactorX( coordinatePlane()->zoomFactorX() + delta ); 0056 coordinatePlane()->setZoomFactorY( coordinatePlane()->zoomFactorY() + delta ); 0057 /* new: 0058 const QPointF zoomCenter( findNewZoomCenter( e->pos() ) ); 0059 if ( zoomCenter != coordinatePlane()->zoomCenter() ) { 0060 qDebug() << "zoom center = " << zoomCenter; 0061 coordinatePlane()->setZoomCenter( zoomCenter ); 0062 } 0063 */ 0064 /* old: 0065 qreal zoomCenterX = static_cast<qreal>( e->pos().x() ) / static_cast<qreal>( width() ); 0066 qreal zoomCenterY = static_cast<qreal>( e->pos().y() ) / static_cast<qreal>( height() ); 0067 QPointF zoomCenter( zoomCenterX, zoomCenterY ); 0068 coordinatePlane()->setZoomCenter( zoomCenter ); 0069 */ 0070 update(); 0071 } 0072 0073 void ZoomWidget::keyPressEvent( QKeyEvent* e ) 0074 { 0075 int dZoom = 0; 0076 qreal dX = 0; 0077 qreal dY = 0; 0078 switch ( e->key() ) { 0079 case Qt::Key_PageDown: 0080 dZoom = 1; 0081 break; 0082 case Qt::Key_Down: 0083 dY = 0.1; 0084 break; 0085 case Qt::Key_Right: 0086 dX = 0.1; 0087 break; 0088 case Qt::Key_PageUp: 0089 dZoom = -1; 0090 break; 0091 case Qt::Key_Up: 0092 dY = -0.1; 0093 break; 0094 case Qt::Key_Left: 0095 dX = -0.1; 0096 break; 0097 default: 0098 e->ignore(); 0099 } 0100 if ( dZoom || dX || dY ) { 0101 const double factor = qMax( double(1.0), coordinatePlane()->zoomFactorX() + dZoom ); 0102 const qreal x = coordinatePlane()->zoomCenter().x() + dX; 0103 const qreal y = coordinatePlane()->zoomCenter().y() + dY; 0104 coordinatePlane()->setZoomFactorX( factor ); 0105 coordinatePlane()->setZoomFactorY( factor ); 0106 coordinatePlane()->setZoomCenter( QPointF(x,y) ); 0107 update(); 0108 } 0109 }