-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidebarSettings.py
240 lines (196 loc) · 8.33 KB
/
sidebarSettings.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from PySide6.QtWidgets import QFrame, QVBoxLayout, QHBoxLayout, QGroupBox, QFormLayout, QCheckBox, QStyle, QWidget
from PySide6.QtWidgets import QPushButton, QLabel, QLineEdit, QInputDialog, QMessageBox, QSlider, QSizePolicy, QRadioButton
from PySide6.QtWidgets import QColorDialog, QMessageBox, QButtonGroup
from PySide6.QtCore import Qt, Signal, Slot, QRectF
import numpy as np
import scipy.signal as sps
from functools import partial
class SidebarSettings(QFrame):
changeSpecgramContrastSignal = Signal(float, bool)
changeSpecgramLogScaleSignal = Signal(bool)
addSmaSignal = Signal(int)
deleteSmaSignal = Signal(int)
changeSmaColourSignal = Signal(int, int, int, int)
changeToAmpPlotSignal = Signal()
changeToReimPlotSignal = Signal()
showHideAmpPlotSignal = Signal(bool)
def __init__(self,
parent=None, f=Qt.WindowFlags()
):
super().__init__(parent, f)
self.setMaximumWidth(400) # Set a maximum extent
# Outer layout
self.layout = QHBoxLayout()
self.settingsWidget = QWidget() # Used to hide/show
self.settingsLayout = QVBoxLayout()
self.settingsWidget.setLayout(self.settingsLayout)
self.setLayout(self.layout)
# Add the hide button
self.hideBtn = QPushButton()
self.hideBtn.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
self.hideBtn.setIcon(self.style().standardIcon(QStyle.SP_ToolBarHorizontalExtensionButton))
self.hideBtn.clicked.connect(self.toggleSidebar)
self.layout.addWidget(self.hideBtn)
# And the actual settings layout
self.layout.addWidget(self.settingsWidget)
# Start out hidden
self.settingsWidget.hide()
# Initialise amp-plot groupbox
self.initAmpPlotGroupbox()
# Initialise spectrogram groupbox
self.initSpecgramGroupbox()
# At the end, add a stretch
self.settingsLayout.addStretch()
############### Global settings
@Slot()
def reset(self):
# Call the individual resets
self.resetPlotType()
self.clearSma()
self.resetSpecgramGroupbox()
############### Amplitude-plot settings
def initAmpPlotGroupbox(self):
self.ampplotgroupbox = QGroupBox("Amplitude-Time Plot")
self.ampplotlayout = QFormLayout()
self.ampplotgroupbox.setLayout(self.ampplotlayout)
self.settingsLayout.addWidget(self.ampplotgroupbox)
# Add the show/hide checkbox
self.ampplotShowHide = QCheckBox()
self.ampplotShowHide.setChecked(True)
self.ampplotShowHide.stateChanged.connect(self.showHideAmpPlot)
self.ampplotlayout.addRow("Show/Hide", self.ampplotShowHide)
# Add real/imag view
self.reimgroupbox = QGroupBox() # QButtonGroup() # QGroupBox()
self.reimgrouplayout = QHBoxLayout()
self.reimgroupbox.setLayout(self.reimgrouplayout)
self.ampviewBtn = QRadioButton("Amplitude")
self.ampviewBtn.toggled.connect(self.plotAmp) # Use toggled to ensure only fires when state changes
self.ampviewBtn.setChecked(True)
self.reimgrouplayout.addWidget(self.ampviewBtn)
self.reimviewBtn = QRadioButton("Real/Imag")
self.reimviewBtn.toggled.connect(self.plotReim) # Use toggled to ensure only fires when state changes
self.reimgrouplayout.addWidget(self.reimviewBtn)
self.ampplotlayout.addRow("Plot Type", self.reimgroupbox)
# Add average filter options
self.smalens = {}
self.addsmaBtn = QPushButton("Add")
self.ampplotlayout.addRow("Moving Average", self.addsmaBtn)
# Connection
self.addsmaBtn.clicked.connect(self.addsma)
@Slot(bool)
def plotAmp(self, checked: bool):
if checked: # Only if state is checked
self.changeToAmpPlotSignal.emit()
@Slot(bool)
def plotReim(self, checked: bool):
if checked: # Only if state is checked
self.changeToReimPlotSignal.emit()
@Slot(int)
def showHideAmpPlot(self, toShow: int):
self.showHideAmpPlotSignal.emit(toShow > 0)
@Slot()
def addsma(self):
val, ok = QInputDialog.getInt(self,
"Add New Moving Average",
"Moving Average Length: ",
value=25
)
if ok:
if val not in self.smalens:
# Create the widget that contains the others
hwidget = QWidget()
hlayout = QHBoxLayout()
hwidget.setLayout(hlayout)
# Deletion button
smadelBtn = QPushButton(parent=hwidget) # Ensure child deletion
smadelBtn.setIcon(self.style().standardIcon(QStyle.SP_DialogCancelButton))
# Connection
smadelBtn.clicked.connect(partial(self.deleteSma, val))
# Color Button
smaColorBtn = QPushButton(parent=hwidget)
smaColorBtn.setStyleSheet("background-color: rgb(255,0,0);")
# Connection
smaColorBtn.clicked.connect(partial(self.colourSma, val))
# Add to the UI
hlayout.addWidget(smadelBtn)
hlayout.addWidget(smaColorBtn)
self.ampplotlayout.addRow("MA: %d" % (val), hwidget)
# Add to internals
self.smalens[val] = [hwidget, smaColorBtn]
# Emit signal
self.addSmaSignal.emit(val)
else:
# Display error dialog
QMessageBox.critical(self, "Moving Average Error",
"Repeat moving average lengths are not allowed."
)
print(self.smalens)
@Slot()
def deleteSma(self, val: int):
# Remove from the UI
self.ampplotlayout.removeRow(self.smalens[val][0])
# Remove from internals
self.smalens.pop(val)
# Emit signal
self.deleteSmaSignal.emit(val)
@Slot()
def colourSma(self, val: int):
colour = QColorDialog.getColor(Qt.red, self)
if colour.isValid():
# Recolour the button
self.smalens[val][1].setStyleSheet(
"background-color: rgb(%d,%d,%d)" % (
colour.red(),
colour.green(),
colour.blue())
)
print(colour)
# Emit signal
self.changeSmaColourSignal.emit(val, colour.red(), colour.green(), colour.blue())
@Slot()
def clearSma(self):
# Remove the UI rows
for val in self.smalens:
self.ampplotlayout.removeRow(self.smalens[val][0])
# Then clear the dict
self.smalens.clear()
@Slot()
def resetPlotType(self):
self.ampviewBtn.setChecked(True)
############### Specgram settings
def initSpecgramGroupbox(self):
self.specgroupbox = QGroupBox("Spectrogram")
self.speclayout = QFormLayout()
self.specgroupbox.setLayout(self.speclayout)
self.settingsLayout.addWidget(self.specgroupbox)
# Add a colour slider control
self.contrastSlider = QSlider(Qt.Horizontal)
self.contrastSlider.setRange(0, 99)
self.speclayout.addRow("Contrast", self.contrastSlider)
# Connection
self.contrastSlider.valueChanged.connect(self.changeSpecgramContrast)
# Add Logarithmic view option
self.logCheckbox = QCheckBox()
self.speclayout.addRow("Logarithmic Scale", self.logCheckbox)
# Connection
self.logCheckbox.stateChanged.connect(self.changeSpecgramLogScale)
@Slot()
def resetSpecgramGroupbox(self):
self.contrastSlider.setValue(0)
self.logCheckbox.setChecked(False)
@Slot()
def toggleSidebar(self):
if self.settingsWidget.isHidden():
self.settingsWidget.show()
else:
self.settingsWidget.hide()
@Slot()
def changeSpecgramContrast(self):
percentile = (100 - self.contrastSlider.value())/100.0
# percentile = np.exp((percentile-1)/0.25) # Scale log? don't use this any more
self.changeSpecgramContrastSignal.emit(percentile, self.logCheckbox.isChecked())
@Slot()
def changeSpecgramLogScale(self):
self.changeSpecgramLogScaleSignal.emit(self.logCheckbox.isChecked())
# Must reset contrast bar
self.contrastSlider.setValue(0)