From 91baa22a14ed0f0351a4f341054f08b9c0a5a61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20M=C3=BCller?= Date: Sat, 26 Jun 2021 23:08:56 +0200 Subject: [PATCH] chart refactoring --- NanoVNASaver/Charts/Chart.py | 104 +++++++++++++++---------------- NanoVNASaver/Charts/Frequency.py | 14 ++--- NanoVNASaver/Charts/RI.py | 5 +- 3 files changed, 62 insertions(+), 61 deletions(-) diff --git a/NanoVNASaver/Charts/Chart.py b/NanoVNASaver/Charts/Chart.py index 1ec5ddd..5ec9bcd 100644 --- a/NanoVNASaver/Charts/Chart.py +++ b/NanoVNASaver/Charts/Chart.py @@ -16,71 +16,81 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import math -from typing import List, Set, Tuple import logging +import math + +from dataclasses import dataclass +from typing import List, Set, Tuple, ClassVar from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5.QtCore import pyqtSignal from NanoVNASaver.RFTools import Datapoint from NanoVNASaver.Marker import Marker + logger = logging.getLogger(__name__) - +@dataclass class Chart(QtWidgets.QWidget): - sweepColor = QtCore.Qt.darkYellow - secondarySweepColor = QtCore.Qt.darkMagenta - referenceColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.blue) - referenceColor.setAlpha(64) - secondaryReferenceColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.blue) - secondaryReferenceColor.setAlpha(64) + backgroundColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.white) foregroundColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.lightGray) + referenceColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.blue).setAlpha(64) + secondaryReferenceColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.blue).setAlpha(64) + secondarySweepColor: QtGui.QColor = QtCore.Qt.darkMagenta + sweepColor: QtGui.QColor = QtCore.Qt.darkYellow + swrColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.red).setAlpha(128) textColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.black) - swrColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.red) - swrColor.setAlpha(128) - data: List[Datapoint] = [] - reference: List[Datapoint] = [] - markers: List[Marker] = [] - swrMarkers: Set[float] = set() - bands = None - draggedMarker: Marker = None - name = "" - sweepTitle = "" - drawLines = False - minChartHeight = 200 - minChartWidth = 200 - chartWidth = minChartWidth - chartHeight = minChartHeight - lineThickness = 1 - pointSize = 2 - markerSize = 3 - drawMarkerNumbers = False - markerAtTip = False - filledMarkers = False - draggedBox = False - draggedBoxStart = (0, 0) - draggedBoxCurrent = (-1, -1) - moveStartX = -1 - moveStartY = -1 - isPopout = False - popoutRequested = pyqtSignal(object) + name: str = "" + sweepTitle: str = "" + + minChartHeight: int = 200 + minChartWidth: int = 200 + chartHeight: int = 200 + chartWidth: int = 200 + + + draggedMarker: Marker = None + drawMarkerNumbers: bool = False + filledMarkers: bool = False + markerAtTip: bool = False + markerSize: int = 3 + + draggedBoxCurrent: Tuple[int] = (-1, -1) + draggedBoxStart: Tuple[int] = (0, 0) + draggedBox: bool = False + + drawLines: bool = False + isPopout: bool = False + + lineThickness: int = 1 + moveStartX: int = -1 + moveStartY: int = -1 + pointSize: int = 2 + + bands: ClassVar = None + popoutRequested: ClassVar = pyqtSignal(object) def __init__(self, name): super().__init__() self.name = name - self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) - self.action_save_screenshot = QtWidgets.QAction("Save image") - self.action_save_screenshot.triggered.connect(self.saveScreenshot) - self.addAction(self.action_save_screenshot) + self.data: List[Datapoint] = [] + self.reference: List[Datapoint] = [] + self.markers: List[Marker] = [] + self.swrMarkers: Set[float] = set() + + self.action_popout = QtWidgets.QAction("Popout chart") self.action_popout.triggered.connect(lambda: self.popoutRequested.emit(self)) self.addAction(self.action_popout) - self.swrMarkers = set() + self.action_save_screenshot = QtWidgets.QAction("Save image") + self.action_save_screenshot.triggered.connect(self.saveScreenshot) + self.addAction(self.action_save_screenshot) + + self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) def setSweepColor(self, color: QtGui.QColor): self.sweepColor = color @@ -195,16 +205,6 @@ class Chart(QtWidgets.QWidget): self.filledMarkers = filled_markers self.update() - @staticmethod - def shortenFrequency(frequency: int) -> str: - if frequency < 50000: - return str(frequency) - if frequency < 5000000: - return str(round(frequency / 1000)) + "k" - if frequency < 50000000: - return str(round(frequency / 1000000, 2)) + "M" - return str(round(frequency / 1000000, 1)) + "M" - def mousePressEvent(self, event: QtGui.QMouseEvent) -> None: if event.buttons() == QtCore.Qt.RightButton: event.ignore() diff --git a/NanoVNASaver/Charts/Frequency.py b/NanoVNASaver/Charts/Frequency.py index ffbd859..0694051 100644 --- a/NanoVNASaver/Charts/Frequency.py +++ b/NanoVNASaver/Charts/Frequency.py @@ -23,7 +23,7 @@ from typing import List import numpy as np from PyQt5 import QtWidgets, QtGui, QtCore -from NanoVNASaver.Formatting import parse_frequency +from NanoVNASaver.Formatting import parse_frequency, format_frequency_chart from NanoVNASaver.RFTools import Datapoint from .Chart import Chart @@ -84,11 +84,11 @@ class FrequencyChart(Chart): self.x_menu.addSeparator() self.action_set_fixed_start = QtWidgets.QAction( - "Start (" + Chart.shortenFrequency(self.minFrequency) + ")") + "Start (" + format_frequency_chart(self.minFrequency) + ")") self.action_set_fixed_start.triggered.connect(self.setMinimumFrequency) self.action_set_fixed_stop = QtWidgets.QAction( - "Stop (" + Chart.shortenFrequency(self.maxFrequency) + ")") + "Stop (" + format_frequency_chart(self.maxFrequency) + ")") self.action_set_fixed_stop.triggered.connect(self.setMaximumFrequency) self.x_menu.addAction(self.action_set_fixed_start) @@ -167,9 +167,9 @@ class FrequencyChart(Chart): def contextMenuEvent(self, event): self.action_set_fixed_start.setText( - f"Start ({Chart.shortenFrequency(self.minFrequency)})") + f"Start ({format_frequency_chart(self.minFrequency)})") self.action_set_fixed_stop.setText( - f"Stop ({Chart.shortenFrequency(self.maxFrequency)})") + f"Stop ({format_frequency_chart(self.maxFrequency)})") self.action_set_fixed_minimum.setText( f"Minimum ({self.minDisplayValue})") self.action_set_fixed_maximum.setText( @@ -496,7 +496,7 @@ class FrequencyChart(Chart): qp.setPen(self.textColor) qp.drawText(self.leftMargin - 20, self.topMargin + self.chartHeight + 15, - Chart.shortenFrequency(self.fstart)) + format_frequency_chart(self.fstart)) ticks = math.floor(self.chartWidth / 100) # Number of ticks does not include the origin for i in range(ticks): x = self.leftMargin + round((i + 1) * self.chartWidth / ticks) @@ -510,7 +510,7 @@ class FrequencyChart(Chart): qp.setPen(self.textColor) qp.drawText(x - 20, self.topMargin + self.chartHeight + 15, - Chart.shortenFrequency(freq)) + format_frequency_chart(freq)) def drawBands(self, qp, fstart, fstop): qp.setBrush(self.bands.color) diff --git a/NanoVNASaver/Charts/RI.py b/NanoVNASaver/Charts/RI.py index 9d5d2d4..191546b 100644 --- a/NanoVNASaver/Charts/RI.py +++ b/NanoVNASaver/Charts/RI.py @@ -22,6 +22,7 @@ from typing import List from PyQt5 import QtWidgets, QtGui +from NanoVNASaver.Formatting import format_frequency_chart from NanoVNASaver.Marker import Marker from NanoVNASaver.RFTools import Datapoint from NanoVNASaver.SITools import Format, Value @@ -517,9 +518,9 @@ class RealImaginaryChart(FrequencyChart): def contextMenuEvent(self, event): self.action_set_fixed_start.setText( - f"Start ({Chart.shortenFrequency(self.minFrequency)})") + f"Start ({format_frequency_chart(self.minFrequency)})") self.action_set_fixed_stop.setText( - f"Stop ({Chart.shortenFrequency(self.maxFrequency)})") + f"Stop ({format_frequency_chart(self.maxFrequency)})") self.action_set_fixed_minimum_real.setText( f"Minimum R ({self.minDisplayReal})") self.action_set_fixed_maximum_real.setText(