Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
aboutsummaryrefslogtreecommitdiffstats
blob: b4dcab944fd49cee9e7e834c15060bea6dd64479 (plain)
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
# Copyright (C) 2024 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from PySide6.QtCharts import QChart, QChartView
from PySide6.QtGui import QKeyEvent, QMouseEvent
from PySide6.QtCore import QEvent, Qt
from PySide6.QtWidgets import QGraphicsView


class ChartView(QChartView):
    def __init__(self, chart, parent=None):
        super().__init__(chart, parent)

        self.setRubberBand(QChartView.RectangleRubberBand)
        self._isTouching = False

    def viewPortEvent(self, event: QEvent):

        if event.type() == QMouseEvent.TouchBegin:
            self._isTouching = True

            self.chart().setAnimationOptions(QChart.NoAnimation)

        return super().viewPortEvent(event)

    def mousePressEvent(self, event: QMouseEvent):

        if self._isTouching:
            return

        return super().mousePressEvent(event)

    def mouseMoveEvent(self, event: QMouseEvent):

        if self._isTouching:
            return

        return super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event: QMouseEvent):

        if self._isTouching:
            self._isTouching = False

        self.chart().setAnimationOptions(QChart.SeriesAnimations)

        return super().mouseReleaseEvent(event)

    def keyPressEvent(self, event: QKeyEvent):

        key = event.key()
        if key == Qt.Key_Plus:
            self.chart().zoomIn()

        elif key == Qt.Key_Minus:
            self.chart().zoomOut()

        elif key == Qt.Key_Left:
            self.chart().scroll(-10, 0)

        elif key == Qt.Key_Right:
            self.chart().scroll(10, 0)

        elif key == Qt.Key_Up:
            self.chart().scroll(0, 10)

        elif key == Qt.Key_Down:
            self.chart().scroll(0, -10)

        else:
            QGraphicsView.keyPressEvent(event)