-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_widgets.py
137 lines (99 loc) · 4.99 KB
/
gui_widgets.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from PySide6.QtWidgets import QSlider, QStyle
from PySide6.QtCore import Signal, Qt, QPoint
from PySide6.QtGui import QPaintEvent, QPainter, QBrush, QColorConstants, QPen, QFontMetrics
import math
class DoubleSlider(QSlider):
# create our our signal that we can connect to if necessary
doubleValueChanged = Signal(float)
def __init__(self, decimals=3, *args, **kargs):
super(DoubleSlider, self).__init__(*args, **kargs)
self._multi = 10 ** decimals
self.valueChanged.connect(self.emitDoubleValueChanged)
def emitDoubleValueChanged(self):
value = float(super(DoubleSlider, self).value()) / self._multi
self.doubleValueChanged.emit(value)
def value(self):
return float(super(DoubleSlider, self).value()) / self._multi
def setMinimum(self, value):
return super(DoubleSlider, self).setMinimum(value * self._multi)
def setMaximum(self, value):
return super(DoubleSlider, self).setMaximum(value * self._multi)
def setSingleStep(self, value):
return super(DoubleSlider, self).setSingleStep(value * self._multi)
def singleStep(self):
return float(super(DoubleSlider, self).singleStep()) / self._multi
def setValue(self, value):
super(DoubleSlider, self).setValue(int(value * self._multi))
class ColoredDoubleSlider(QSlider):
# create our our signal that we can connect to if necessary
doubleValueChanged = Signal(float)
def __init__(self,parent=None, decimals=3, *args, **kargs):
super(ColoredDoubleSlider, self).__init__(parent)
self._multi = 10 ** decimals
self.valueChanged.connect(self.emitDoubleValueChanged)
self.background_value = 0.
self.qpainter = QPainter(self)
def emitDoubleValueChanged(self):
value = float(super(ColoredDoubleSlider, self).value()) / self._multi
self.doubleValueChanged.emit(value)
def value(self):
return float(super(ColoredDoubleSlider, self).value()) / self._multi
def setMinimum(self, value):
return super(ColoredDoubleSlider, self).setMinimum(value * self._multi)
def setMaximum(self, value):
return super(ColoredDoubleSlider, self).setMaximum(value * self._multi)
def setSingleStep(self, value):
return super(ColoredDoubleSlider, self).setSingleStep(value * self._multi)
def singleStep(self):
return float(super(ColoredDoubleSlider, self).singleStep()) / self._multi
def setValue(self, value):
super(ColoredDoubleSlider, self).setValue(int(value * self._multi))
def updateBackground(self, value):
assert 0 <= value <= 1
self.background_value = value
def paintEvent(self, ev: QPaintEvent) -> None:
orientation = self.orientation()
self.qpainter.begin(self)
self.qpainter.setBrush(QBrush(QColorConstants.Red))
if orientation == Qt.Orientation.Horizontal:
self.qpainter.fillRect(0,0,self.background_value*self.width(), self.height(),QColorConstants.Svg.lightblue)
else:
self.qpainter.fillRect(0, (1.-self.background_value) * self.height(), self.width(), self.background_value * self.height(), QColorConstants.Svg.lightblue)
self.qpainter.end()
super().paintEvent(ev)
class LogarithmicSlider(QSlider):
# create our our signal that we can connect to if necessary
doubleValueChanged = Signal(float)
def __init__(self, *args, **kargs):
super(LogarithmicSlider, self).__init__(*args, **kargs)
decimals = 5
self._multi = 10 ** decimals
self.valueChanged.connect(self.emitDoubleValueChanged)
def emitDoubleValueChanged(self):
value = 10**(float(super(LogarithmicSlider, self).value()) / self._multi)
self.doubleValueChanged.emit(value)
def value(self):
return 10**(float(super(LogarithmicSlider, self).value()) / self._multi)
def setMinimum(self, value):
if value <= 0: raise ValueError("Value has to be bigger than 0")
return super(LogarithmicSlider, self).setMinimum(math.log10(value) * self._multi)
def setMaximum(self, value):
if value < 0: raise ValueError("Value has to be bigger than 0")
return super(LogarithmicSlider, self).setMaximum(math.log10(value) * self._multi)
def setSingleStep(self, value):
return super(LogarithmicSlider, self).setSingleStep(value * self._multi)
def singleStep(self):
return float(super(LogarithmicSlider, self).singleStep()) / self._multi
def setValue(self, value):
print(int(value*self._multi))
super(LogarithmicSlider, self).setValue(int(math.log10(value) * self._multi))
class StyledMouseSlider(DoubleSlider):
def __init__(self, decimals=3, *args, **kargs):
super().__init__(decimals, *args, **kargs)
self.setMinimum(0)
self.setMaximum(3)
self.setValue(1.)
self.setOrientation(Qt.Orientation.Horizontal)
self.setTracking(False)
self.setTickInterval(10**decimals)
self.setTickPosition(self.TickPosition.TicksBelow)