forked from NanoVNA-Saver/nanovna-saver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceSettings.py
226 lines (188 loc) · 9.13 KB
/
DeviceSettings.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
# NanoVNASaver
#
# A python program to view and export Touchstone data from a NanoVNA
# Copyright (C) 2019, 2020 Rune B. Broberg
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
from PyQt6.QtGui import QIntValidator
from PyQt6 import QtWidgets, QtCore, QtGui
from NanoVNASaver.Windows.Defaults import make_scrollable
from NanoVNASaver.Windows.Screenshot import ScreenshotWindow
logger = logging.getLogger(__name__)
class DeviceSettingsWindow(QtWidgets.QWidget):
custom_points_checkBox = QtWidgets.QCheckBox
custom_points_Eidt = QtWidgets.QLineEdit
def __init__(self, app: QtWidgets.QWidget):
super().__init__()
self.app = app
self.setWindowTitle("Device settings")
self.setWindowIcon(self.app.icon)
QtGui.QShortcut(QtCore.Qt.Key.Key_Escape, self, self.hide)
self.label = {
"status": QtWidgets.QLabel("Not connected."),
"firmware": QtWidgets.QLabel("Not connected."),
"calibration": QtWidgets.QLabel("Not connected."),
"SN": QtWidgets.QLabel("Not connected."),
}
top_layout = QtWidgets.QHBoxLayout()
left_layout = QtWidgets.QVBoxLayout()
right_layout = QtWidgets.QVBoxLayout()
top_layout.addLayout(left_layout)
top_layout.addLayout(right_layout)
make_scrollable(self, top_layout)
status_box = QtWidgets.QGroupBox("Status")
status_layout = QtWidgets.QFormLayout(status_box)
status_layout.addRow("Status:", self.label["status"])
status_layout.addRow("Firmware:", self.label["firmware"])
status_layout.addRow("Calibration:", self.label["calibration"])
status_layout.addRow("SN:", self.label["SN"])
status_layout.addRow(QtWidgets.QLabel("Features:"))
self.featureList = QtWidgets.QListWidget()
status_layout.addRow(self.featureList)
settings_box = QtWidgets.QGroupBox("Settings")
settings_layout = QtWidgets.QFormLayout(settings_box)
self.chkValidateInputData = QtWidgets.QCheckBox(
"Validate received data"
)
validate_input = self.app.settings.value(
"SerialInputValidation", False, bool
)
self.chkValidateInputData.setChecked(validate_input)
self.chkValidateInputData.stateChanged.connect(self.updateValidation)
settings_layout.addRow("Validation", self.chkValidateInputData)
control_layout = QtWidgets.QHBoxLayout()
self.btnRefresh = QtWidgets.QPushButton("Refresh")
self.btnRefresh.clicked.connect(self.updateFields)
control_layout.addWidget(self.btnRefresh)
self.screenshotWindow = ScreenshotWindow()
self.btnCaptureScreenshot = QtWidgets.QPushButton("Screenshot")
self.btnCaptureScreenshot.clicked.connect(self.captureScreenshot)
control_layout.addWidget(self.btnCaptureScreenshot)
left_layout.addWidget(status_box)
left_layout.addLayout(control_layout)
self.datapoints = QtWidgets.QComboBox()
self.datapoints.addItem(str(self.app.vna.datapoints))
self.datapoints.currentIndexChanged.connect(self.updateNrDatapoints)
self.custom_points_checkBox = QtWidgets.QCheckBox("Custom points")
self.custom_points_checkBox.stateChanged.connect(self.customPoint_check)
self.custom_points_Eidt = QtWidgets.QLineEdit("101")
self.custom_points_Eidt.setValidator(
QIntValidator(self.app.vna.sweep_points_min,
self.app.vna.sweep_points_max))
self.custom_points_Eidt.textEdited.connect(self.updatecustomPoint)
self.custom_points_Eidt.setDisabled(True)
self.bandwidth = QtWidgets.QComboBox()
self.bandwidth.addItem(str(self.app.vna.bandwidth))
self.bandwidth.currentIndexChanged.connect(self.updateBandwidth)
form_layout = QtWidgets.QFormLayout()
form_layout.addRow(QtWidgets.QLabel("Datapoints"), self.datapoints)
form_layout.addRow(self.custom_points_checkBox, self.custom_points_Eidt)
form_layout.addRow(QtWidgets.QLabel("Bandwidth"), self.bandwidth)
right_layout.addWidget(settings_box)
settings_layout.addRow(form_layout)
def _set_datapoint_index(self, dpoints: int):
self.datapoints.setCurrentIndex(self.datapoints.findText(str(dpoints)))
def _set_bandwidth_index(self, bw: int):
self.bandwidth.setCurrentIndex(self.bandwidth.findText(str(bw)))
def show(self):
super().show()
self.updateFields()
def updateFields(self):
if not self.app.vna.connected():
self.label["status"].setText("Not connected.")
self.label["firmware"].setText("Not connected.")
self.label["calibration"].setText("Not connected.")
self.label["SN"].setText("Not connected.")
self.featureList.clear()
self.btnCaptureScreenshot.setDisabled(True)
return
self.label["status"].setText(f"Connected to {self.app.vna.name}.")
self.label["firmware"].setText(
f"{self.app.vna.name} v{self.app.vna.version}"
)
if self.app.worker.running:
self.label["calibration"].setText("(Sweep running)")
else:
self.label["calibration"].setText(self.app.vna.getCalibration())
self.label["SN"].setText(self.app.vna.SN)
self.featureList.clear()
features = self.app.vna.getFeatures()
for item in features:
self.featureList.addItem(item)
self.btnCaptureScreenshot.setDisabled("Screenshots" not in features)
if "Customizable data points" in features:
self.datapoints.clear()
self.custom_points_Eidt.setValidator(
QIntValidator(self.app.vna.sweep_points_min,
self.app.vna.sweep_points_max))
cur_dps = self.app.vna.datapoints
for d in sorted(self.app.vna.valid_datapoints):
self.datapoints.addItem(str(d))
self._set_datapoint_index(cur_dps)
self.datapoints.setDisabled(False)
else:
self.datapoints.setDisabled(True)
if "Bandwidth" in features:
self.bandwidth.clear()
cur_bw = self.app.vna.bandwidth
for d in sorted(self.app.vna.get_bandwidths()):
self.bandwidth.addItem(str(d))
self._set_bandwidth_index(cur_bw)
self.bandwidth.setDisabled(False)
else:
self.bandwidth.setDisabled(True)
def updateValidation(self, validate_data: bool):
self.app.vna.validateInput = validate_data
self.app.settings.setValue("SerialInputValidation", validate_data)
def captureScreenshot(self):
if not self.app.worker.running:
pixmap = self.app.vna.getScreenshot()
self.screenshotWindow.setScreenshot(pixmap)
self.screenshotWindow.show()
# TODO: Tell the user no screenshots while sweep is running?
# TODO: Consider having a list of widgets that want to be
# disabled when a sweep is running?
def updateNrDatapoints(self, i):
if i < 0 or self.app.worker.running:
return
logger.debug("DP: %s", self.datapoints.itemText(i))
self.app.vna.datapoints = int(self.datapoints.itemText(i))
self.app.sweep.set_points(self.app.vna.datapoints)
self.app.sweep_control.update_step_size()
def updateBandwidth(self, i):
if i < 0 or self.app.worker.running:
return
logger.debug("Bandwidth: %s", self.bandwidth.itemText(i))
self.app.vna.set_bandwidth(int(self.bandwidth.itemText(i)))
def customPoint_check(self, validate_data: bool):
self.datapoints.setDisabled(validate_data)
self.custom_points_Eidt.setDisabled(not validate_data)
def updatecustomPoint(self, points_str: str):
if self.custom_points_checkBox.isChecked():
# points_str = self.custom_points_Eidt.text()
if len(points_str) == 0:
return
points = int(points_str)
if points < self.app.vna.sweep_points_min:
return
if points > self.app.vna.sweep_points_max:
points = int(self.app.vna.sweep_points_max)
if points != self.app.vna.datapoints:
logger.debug("DP: %s", points)
self.app.vna.datapoints = points
self.app.sweep.set_points(self.app.vna.datapoints)
self.app.sweep_control.update_step_size()
self.custom_points_Eidt.setText(str(points))