nanovna-saver/src/NanoVNASaver/Windows/DeviceSettings.py

223 wiersze
9.1 KiB
Python
Czysty Zwykły widok Historia

# NanoVNASaver
2020-06-25 17:52:30 +00:00
#
# A python program to view and export Touchstone data from a NanoVNA
2020-06-25 17:52:30 +00:00
# Copyright (C) 2019, 2020 Rune B. Broberg
2021-06-30 05:21:14 +00:00
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
2019-08-29 13:10:35 +00:00
#
# 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/>.
2019-09-16 13:47:37 +00:00
import logging
2019-08-28 18:20:07 +00:00
from PyQt6.QtGui import QIntValidator
2023-03-12 07:02:58 +00:00
from PyQt6 import QtWidgets, QtCore, QtGui
2019-08-28 18:20:07 +00:00
2023-02-27 20:01:35 +00:00
from NanoVNASaver.Windows.Defaults import make_scrollable
from NanoVNASaver.Windows.Screenshot import ScreenshotWindow
2019-08-28 18:20:07 +00:00
2019-09-16 13:47:37 +00:00
logger = logging.getLogger(__name__)
2020-06-21 18:54:23 +00:00
2019-12-07 23:07:55 +00:00
class DeviceSettingsWindow(QtWidgets.QWidget):
custom_points_checkBox = QtWidgets.QCheckBox
custom_points_Eidt = QtWidgets.QLineEdit
def __init__(self, app: QtWidgets.QWidget):
2019-12-07 23:07:55 +00:00
super().__init__()
self.app = app
self.setWindowTitle("Device settings")
self.setWindowIcon(self.app.icon)
2023-03-12 07:02:58 +00:00
QtGui.QShortcut(QtCore.Qt.Key.Key_Escape, self, self.hide)
2019-12-07 23:07:55 +00:00
2020-07-04 17:59:47 +00:00
self.label = {
"status": QtWidgets.QLabel("Not connected."),
"firmware": QtWidgets.QLabel("Not connected."),
"calibration": QtWidgets.QLabel("Not connected."),
"SN": QtWidgets.QLabel("Not connected."),
2020-07-04 17:59:47 +00:00
}
top_layout = QtWidgets.QHBoxLayout()
left_layout = QtWidgets.QVBoxLayout()
right_layout = QtWidgets.QVBoxLayout()
top_layout.addLayout(left_layout)
top_layout.addLayout(right_layout)
2023-02-27 20:01:35 +00:00
make_scrollable(self, top_layout)
2019-12-07 23:07:55 +00:00
status_box = QtWidgets.QGroupBox("Status")
status_layout = QtWidgets.QFormLayout(status_box)
2020-07-04 17:59:47 +00:00
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"])
2019-12-07 23:07:55 +00:00
status_layout.addRow(QtWidgets.QLabel("Features:"))
2020-07-04 17:59:47 +00:00
2019-12-07 23:07:55 +00:00
self.featureList = QtWidgets.QListWidget()
status_layout.addRow(self.featureList)
settings_box = QtWidgets.QGroupBox("Settings")
settings_layout = QtWidgets.QFormLayout(settings_box)
2022-09-15 19:59:55 +00:00
self.chkValidateInputData = QtWidgets.QCheckBox(
2023-03-08 08:40:39 +00:00
"Validate received data"
)
2022-09-15 19:59:55 +00:00
validate_input = self.app.settings.value(
2023-03-08 08:40:39 +00:00
"SerialInputValidation", False, bool
)
self.chkValidateInputData.setChecked(validate_input)
2019-12-07 23:07:55 +00:00
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)
2020-07-14 09:30:28 +00:00
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)
2020-07-14 09:30:28 +00:00
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):
2023-03-08 08:40:39 +00:00
self.datapoints.setCurrentIndex(self.datapoints.findText(str(dpoints)))
2019-12-07 23:07:55 +00:00
2020-07-14 09:30:28 +00:00
def _set_bandwidth_index(self, bw: int):
2023-03-08 08:40:39 +00:00
self.bandwidth.setCurrentIndex(self.bandwidth.findText(str(bw)))
2020-07-14 09:30:28 +00:00
2019-12-07 23:07:55 +00:00
def show(self):
super().show()
self.updateFields()
def updateFields(self):
2020-07-05 12:06:57 +00:00
if not self.app.vna.connected():
2020-07-04 17:59:47 +00:00
self.label["status"].setText("Not connected.")
self.label["firmware"].setText("Not connected.")
self.label["calibration"].setText("Not connected.")
self.label["SN"].setText("Not connected.")
2019-12-07 23:07:55 +00:00
self.featureList.clear()
self.btnCaptureScreenshot.setDisabled(True)
2020-07-04 17:59:47 +00:00
return
2023-03-08 08:40:39 +00:00
self.label["status"].setText(f"Connected to {self.app.vna.name}.")
2020-07-04 17:59:47 +00:00
self.label["firmware"].setText(
2023-03-08 08:40:39 +00:00
f"{self.app.vna.name} v{self.app.vna.version}"
)
2020-07-04 17:59:47 +00:00
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)
2020-07-04 17:59:47 +00:00
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))
2020-07-04 17:59:47 +00:00
cur_dps = self.app.vna.datapoints
2020-07-07 10:33:32 +00:00
for d in sorted(self.app.vna.valid_datapoints):
2020-07-04 17:59:47 +00:00
self.datapoints.addItem(str(d))
self._set_datapoint_index(cur_dps)
2020-07-14 09:30:28 +00:00
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)
2019-12-07 23:07:55 +00:00
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))
2020-07-28 15:07:53 +00:00
with self.app.sweep.lock:
self.app.sweep.points = self.app.vna.datapoints
self.app.sweep_control.update_step_size()
2020-07-14 09:30:28 +00:00
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.points = self.app.vna.datapoints
self.app.sweep_control.update_step_size()
self.custom_points_Eidt.setText(str(points))