aboutsummaryrefslogtreecommitdiffstats
path: root/examples/graphs/3d/widgetgraphgallery/highlightseries.py
blob: be78521298f7afe421880bcc1e11142efe291782 (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
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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations

from PySide6.QtCore import QPoint, Qt, Slot
from PySide6.QtGui import QLinearGradient, QVector3D
from PySide6.QtGraphs import (QSurface3DSeries, QSurfaceDataItem,
                              QGraphsTheme)


DARK_RED_POS = 1.0
RED_POS = 0.8
YELLOW_POS = 0.6
GREEN_POS = 0.4
DARK_GREEN_POS = 0.2


class HighlightSeries(QSurface3DSeries):

    def __init__(self):
        super().__init__()
        self._width = 100
        self._height = 100
        self._srcWidth = 0
        self._srcHeight = 0
        self._position = QPoint()
        self._topographicSeries = None
        self._minHeight = 0.0
        self._height_adjustment = 5.0
        self.setDrawMode(QSurface3DSeries.DrawFlag.DrawSurface)
        self.setShading(QSurface3DSeries.Shading.Flat)
        self.setVisible(False)

    def setTopographicSeries(self, series):
        self._topographicSeries = series
        array = self._topographicSeries.dataArray()
        self._srcWidth = len(array[0])
        self._srcHeight = len(array)
        self._topographicSeries.selectedPointChanged.connect(self.handlePositionChange)

    def setMinHeight(self, height):
        self. m_minHeight = height

    @Slot(QPoint)
    def handlePositionChange(self, position):
        self._position = position

        if position == self.invalidSelectionPosition():
            self.setVisible(False)
            return

        halfWidth = self._width / 2
        halfHeight = self._height / 2

        startX = position.x() - halfWidth
        if startX < 0:
            startX = 0
        endX = position.x() + halfWidth
        if endX > (self._srcWidth - 1):
            endX = self._srcWidth - 1
        startZ = position.y() - halfHeight
        if startZ < 0:
            startZ = 0
        endZ = position.y() + halfHeight
        if endZ > (self._srcHeight - 1):
            endZ = self._srcHeight - 1

        srcArray = self._topographicSeries.dataArray()

        dataArray = []
        for i in range(int(startZ), int(endZ)):
            newRow = []
            srcRow = srcArray[i]
            for j in range(int(startX), int(endX)):
                pos = QVector3D(srcRow[j].position())
                pos.setY(pos.y() + self._height_adjustment)
                item = QSurfaceDataItem(pos)
                newRow.append(item)
            dataArray.append(newRow)
        self.dataProxy().resetArray(dataArray)
        self.setVisible(True)

    @Slot(float)
    def handleGradientChange(self, value):
        ratio = self._minHeight / value

        gr = QLinearGradient()
        gr.setColorAt(0.0, Qt.GlobalColor.black)
        gr.setColorAt(DARK_GREEN_POS * ratio, Qt.GlobalColor.darkGreen)
        gr.setColorAt(GREEN_POS * ratio, Qt.GlobalColor.green)
        gr.setColorAt(YELLOW_POS * ratio, Qt.GlobalColor.yellow)
        gr.setColorAt(RED_POS * ratio, Qt.GlobalColor.red)
        gr.setColorAt(DARK_RED_POS * ratio, Qt.GlobalColor.darkRed)

        self.setBaseGradient(gr)
        self.setColorStyle(QGraphsTheme.ColorStyle.RangeGradient)

        self.handle_zoom_change(ratio)

    def handle_zoom_change(self, zoom):
        self._height_adjustment = (1.2 - zoom) * 10.0