123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- @文件 :my_chart_view.py
- @时间 :2022/03/14 12:31:02
- @作者 :None
- @版本 :1.0
- @说明 : 重写QChartView 方法,增加缩放功能
- '''
- from math import pow
- from utils. qt import Qt, QChartView, QPoint, QPointF
- class MyChartView(QChartView):
- m_bMiddleButtonPressed = 0
- m_oPrePos = QPoint()
- def __init__(self, parent=None):
- super(MyChartView, self).__init__(parent)
- def mouseMoveEvent(self, event):
- if self.m_bMiddleButtonPressed:
- oDeltaPos = event.pos() - self.m_oPrePos
- self.chart().scroll(-oDeltaPos.x(), 0)
- self.m_oPrePos = event.pos()
- super(MyChartView, self).mouseMoveEvent(event)
- def mousePressEvent(self, event):
- if event.button() == Qt.MiddleButton:
- self.m_bMiddleButtonPressed = True
- self.m_oPrePos = event.pos()
- self.setCursor(Qt.OpenHandCursor)
- super(MyChartView, self).mousePressEvent(event)
- def mouseReleaseEvent(self, event):
- if event.button() == Qt.MiddleButton:
- self.m_bMiddleButtonPressed = False
- self.setCursor(Qt.ArrowCursor)
- super(MyChartView, self).mouseReleaseEvent(event)
- def wheelEvent(self, event):
- # 设置比例
- rVal = pow(0.999, event.delta())
- # 1. 读取视图基本信息
- oPlotAreaRect = self.chart().plotArea()
- oCenterPoint = oPlotAreaRect.center()
- # 2. 水平调整
- oPlotAreaRect.setWidth(oPlotAreaRect.width() * rVal)
- # 3. 竖直调整
- oPlotAreaRect.setHeight(oPlotAreaRect.height() * rVal)
- # 4.1 计算视点,视点不变,围绕中心缩放
- # QPointF oNewCenterPoint(oCenterPoint);
- # 4.2 计算视点,让鼠标点击的位置移动到窗口中心
- # QPointF oNewCenterPoint(pEvent->pos());
- # 4.3 计算视点,让鼠标点击的位置尽量保持不动(等比换算,存在一点误差)
- oNewCenterPoint = QPointF(2 * oCenterPoint - event.pos() - (oCenterPoint - event.pos()) / rVal)
- # 5. 设置视点
- oPlotAreaRect.moveCenter(oNewCenterPoint)
- # 6. 提交缩放调整
- self.chart().zoomIn(oPlotAreaRect)
- super(MyChartView, self).wheelEvent(event)
|