Advertisement
Ilya_Bykonya

Untitled

Dec 5th, 2023
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 KB | Source Code | 0 0
  1. # This Python file uses the following encoding: utf-8
  2. from PySide6.QtWidgets import QApplication, QLabel, QWidget, QGridLayout, QHBoxLayout, QVBoxLayout, QSpinBox, QSizePolicy, QStackedLayout
  3. from PySide6.QtGui import QPen, QPainter, QColor, QFont, QRadialGradient, QPolygon, QPaintEvent, QConicalGradient, QGradient, QBrush, QRegion, QResizeEvent
  4. from PySide6.QtCore import Qt, Signal, QRect, QPoint, QSize
  5. from typing import List, Tuple
  6. import zipfile
  7. import pandas
  8. import numpy
  9. import math
  10. import sys
  11.  
  12. class ArrowPointerIndicator(QWidget):
  13.     minimumValueChange: Signal = Signal(float)
  14.     maximumValueChange: Signal = Signal(float)
  15.     valueChanged: Signal = Signal(float)
  16.     def __init__(self, chunks: List[Tuple[float, QColor]], lineWidth: int = 5, parent: QWidget = None) ->None:
  17.         super().__init__(parent)
  18.         self.__arrowColor: QColor = QColor(255, 0, 0)
  19.         self.__gradient: QConicalGradient = None
  20.         self.setChunks(chunks)
  21.         self.__arrow = QPolygon([
  22.             QPoint(-2, -80),
  23.             QPoint(0, -100),
  24.             QPoint(2, -80),
  25.             QPoint(2, 10),
  26.             QPoint(-2, 10),
  27.             QPoint(-2, -80)
  28.         ])
  29.  
  30.         self.__minimum = -90.0
  31.         self.__maximum = 90.0
  32.         self.__lineWidth = lineWidth
  33.         self.__value: float = 0.0
  34.  
  35.  
  36.     def valueRange(self) ->(float, float):
  37.         return (self.__minimum, self.__maximum)
  38.     def setValueRange(self, minimum: float, maximum: float) ->None:
  39.         if self.__minimum != minimum:
  40.             self.__minimum = minimum
  41.             self.update()
  42.         if self.__maximum != maximum:
  43.             self.__maximum = maximum
  44.             self.update()
  45.  
  46.     def value(self) ->float:
  47.         return self.__value
  48.     def setValue(self, value: float) ->None:
  49.         if self.__value == value:
  50.             return
  51.  
  52.         self.__value = max(min(value, self.__maximum), self.__minimum)
  53.         self.valueChanged.emit(self.__value)
  54.         self.update()
  55.  
  56.     def lineWidth(self) ->int:
  57.         return self.__lineWidth
  58.     def setLineWidth(self, lineWidth: int) ->None:
  59.         if self.__lineWidth == lineWidth:
  60.             return
  61.  
  62.         self.__lineWidth = lineWidth
  63.         self.update()
  64.  
  65.     def arrowColor(self) ->QColor:
  66.         return self.__arrowColor
  67.     def setArrowColor(self, arrowColor: QColor) ->QColor:
  68.         if self.__arrowColor == arrowColor:
  69.             return
  70.  
  71.         self.__arrowColor = arrowColor
  72.         self.update()
  73.     def setChunks(self, chunks: List[Tuple[float, QColor]]) ->None:
  74.         weightsSum: float = sum(item[0] for item in chunks)
  75.         chunks = [(item[0] / weightsSum / 2, item[1]) for item in chunks]
  76.         self.__gradient = QConicalGradient()
  77.  
  78.         passedWeight = 0.25
  79.         for weight, color in chunks:
  80.             self.__gradient.setColorAt(passedWeight, color)
  81.             self.__gradient.setColorAt(passedWeight + weight - 0.0000000001, color)
  82.             passedWeight += weight
  83.  
  84.         self.__gradient.setCenter(QPoint(0, 0))
  85.         self.__gradient.setAngle(-90)
  86.         self.update()
  87.  
  88.  
  89.     def paintEvent(self, event: QPaintEvent) ->None:
  90.         super().paintEvent(event)
  91.         drawSize = self.width() / 2
  92.         painter = QPainter(self)
  93.         painter.drawRect(QRect(1, 1, self.width() - 2, self.height() - 2))
  94.         painter.translate(drawSize, drawSize)
  95.         painter.setRenderHint(QPainter.RenderHint.Antialiasing)
  96.         self.__drawColorArc(drawSize, painter)
  97.         self.__drawArrowPointer(drawSize, painter)
  98.  
  99.     def __drawColorArc(self, drawSize: int, painter: QPainter) ->None:
  100.         drawingRect = QRect(-drawSize, -drawSize, 2 * drawSize, 2 * drawSize)
  101.         excludeRect = QRect(-drawSize + self.__lineWidth, -drawSize + self.__lineWidth, 2 * (drawSize - self.__lineWidth), 2 * (drawSize - self.__lineWidth))
  102.         painter.save()
  103.  
  104.         outerRegion = QRegion(drawingRect, QRegion.RegionType.Ellipse).subtracted(QRegion(excludeRect, QRegion.RegionType.Ellipse))
  105.         painter.setClipRegion(outerRegion)
  106.         painter.setBrush(QBrush(self.__gradient))
  107.         painter.drawChord(drawingRect, 0, 180 * 16)
  108.         painter.restore()
  109.     def __drawArrowPointer(self, drawSize: int, painter: QPainter) ->None:
  110.         painter.save()
  111.         painter.setBrush(self.__arrowColor)
  112.         painter.scale(drawSize / 100.0, drawSize / 100.0)
  113.         painter.rotate(float(numpy.interp(self.__value, self.valueRange(), (-90, 90))))
  114.         painter.drawConvexPolygon(self.__arrow)
  115.         painter.restore()
  116.  
  117. class SizeBoundedWindow(QWidget):
  118.     def __init__(self, parent: QWidget = None) ->None:
  119.         super().__init__(parent)
  120.     def resizeEvent(self, event: QResizeEvent) ->None:
  121.         super().resizeEvent(event)
  122.         if self.width() * 0.6 > self.height():
  123.             self.resize(self.height() / 0.6, self.height())
  124.             self.update()
  125.         elif self.width() * 0.6 < self.height():
  126.             self.resize(self.width(), self.width() * 0.6)
  127.             self.update()
  128.  
  129.  
  130. if __name__ == '__main__':
  131.     app = QApplication(sys.argv)
  132.  
  133.     indicator_1 = ArrowPointerIndicator([
  134.         (1, Qt.GlobalColor.red),
  135.         (3, Qt.GlobalColor.yellow),
  136.         (2, Qt.GlobalColor.green),
  137.         (3, Qt.GlobalColor.yellow),
  138.         (1, Qt.GlobalColor.red)
  139.     ])
  140.     indicator_2 = ArrowPointerIndicator([
  141.         (10, Qt.GlobalColor.white),
  142.         (1, Qt.GlobalColor.red),
  143.         (1, Qt.GlobalColor.white),
  144.         (1, Qt.GlobalColor.red),
  145.         (10, Qt.GlobalColor.white)
  146.     ], 100000000)
  147.     indicator_1.setArrowColor(QColor(Qt.GlobalColor.black))
  148.     indicator_1.setValueRange(-10, 10)
  149.     indicator_2.setValueRange(-25, 25)
  150.  
  151.     window = SizeBoundedWindow()
  152.     layout = QGridLayout()
  153.     window.setLayout(layout)
  154.     layout.addWidget(indicator_1, 0, 0, 4, 5)
  155.     layout.addWidget(indicator_2, 1, 1, 2, 3)
  156.     window.show()
  157.  
  158.     sys.exit(app.exec())
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement