kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
- Merged LogMag chart into Chart.py
- Dark mode! (Charts only for now)pull/12/head
rodzic
3fab005f57
commit
e706eec51e
|
@ -27,6 +27,8 @@ class Chart(QtWidgets.QWidget):
|
|||
sweepColor = QtCore.Qt.darkYellow
|
||||
referenceColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.blue)
|
||||
referenceColor.setAlpha(64)
|
||||
backgroundColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.white)
|
||||
textColor: QtGui.QColor = QtGui.QColor(QtCore.Qt.black)
|
||||
data: List[Datapoint] = []
|
||||
reference: List[Datapoint] = []
|
||||
markers: List[Marker] = []
|
||||
|
@ -41,6 +43,17 @@ class Chart(QtWidgets.QWidget):
|
|||
self.referenceColor = color
|
||||
self.update()
|
||||
|
||||
def setBackgroundColor(self, color: QtGui.QColor):
|
||||
self.backgroundColor = color
|
||||
pal = self.palette()
|
||||
pal.setColor(QtGui.QPalette.Background, color)
|
||||
self.setPalette(pal)
|
||||
self.update()
|
||||
|
||||
def setTextColor(self, color: QtGui.QColor):
|
||||
self.textColor = color
|
||||
self.update()
|
||||
|
||||
def setReference(self, data):
|
||||
self.reference = data
|
||||
self.update()
|
||||
|
@ -83,7 +96,7 @@ class PhaseChart(Chart):
|
|||
self.setMinimumSize(self.chartWidth + 20 + self.leftMargin, self.chartHeight + 40)
|
||||
self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding))
|
||||
pal = QtGui.QPalette()
|
||||
pal.setColor(QtGui.QPalette.Background, QtGui.QColor("white"))
|
||||
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
||||
self.setPalette(pal)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
|
@ -103,6 +116,7 @@ class PhaseChart(Chart):
|
|||
qp.end()
|
||||
|
||||
def drawChart(self, qp: QtGui.QPainter):
|
||||
qp.setPen(QtGui.QPen(self.textColor))
|
||||
qp.drawText(3, 15, self.name)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin, 20, self.leftMargin, 20+self.chartHeight+5)
|
||||
|
@ -113,11 +127,11 @@ class PhaseChart(Chart):
|
|||
for i in range(minAngle, maxAngle, 90):
|
||||
y = 30 + round((i-minAngle)/span*(self.chartHeight-10))
|
||||
if i != minAngle and i != maxAngle:
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("black")))
|
||||
qp.setPen(QtGui.QPen(self.textColor))
|
||||
qp.drawText(3, y+3, str(-i) + "°")
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin-5, y, self.leftMargin+self.chartWidth, y)
|
||||
qp.setPen(QtCore.Qt.black)
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(3, 35, str(-minAngle) + "°")
|
||||
qp.drawText(3, self.chartHeight+20, str(-maxAngle) + "°")
|
||||
|
||||
|
@ -148,7 +162,7 @@ class PhaseChart(Chart):
|
|||
x = self.leftMargin + round((i+1)*self.chartWidth/ticks)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(x, 20, x, 20+self.chartHeight+5)
|
||||
qp.setPen(QtCore.Qt.black)
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(x-20, 20+self.chartHeight+15, Chart.shortenFrequency(round(fspan/ticks*(i+1) + fstart)))
|
||||
|
||||
if self.mouselocation != 0:
|
||||
|
@ -240,7 +254,7 @@ class VSWRChart(Chart):
|
|||
self.setMinimumSize(self.chartWidth + 20 + self.leftMargin, self.chartHeight + 40)
|
||||
self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding))
|
||||
pal = QtGui.QPalette()
|
||||
pal.setColor(QtGui.QPalette.Background, QtGui.QColor("white"))
|
||||
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
||||
self.setPalette(pal)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
|
@ -260,6 +274,7 @@ class VSWRChart(Chart):
|
|||
qp.end()
|
||||
|
||||
def drawChart(self, qp: QtGui.QPainter):
|
||||
qp.setPen(QtGui.QPen(self.textColor))
|
||||
qp.drawText(3, 15, self.name)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin, 20, self.leftMargin, 20+self.chartHeight+5)
|
||||
|
@ -306,12 +321,12 @@ class VSWRChart(Chart):
|
|||
for i in range(minVSWR, maxVSWR, ticksize):
|
||||
y = 30 + round((maxVSWR-i)/span*(self.chartHeight-10))
|
||||
if i != minVSWR and i != maxVSWR:
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("black")))
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(3, y+3, str(i))
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin-5, y, self.leftMargin+self.chartWidth, y)
|
||||
qp.drawLine(self.leftMargin - 5, 30, self.leftMargin + self.chartWidth, 30)
|
||||
qp.setPen(QtCore.Qt.black)
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(3, 35, str(maxVSWR))
|
||||
qp.drawText(3, self.chartHeight+20, str(minVSWR))
|
||||
# At least 100 px between ticks
|
||||
|
@ -321,7 +336,7 @@ class VSWRChart(Chart):
|
|||
x = self.leftMargin + round((i+1)*self.chartWidth/ticks)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(x, 20, x, 20+self.chartHeight+5)
|
||||
qp.setPen(QtCore.Qt.black)
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(x-20, 20+self.chartHeight+15, Chart.shortenFrequency(round(fspan/ticks*(i+1) + fstart)))
|
||||
|
||||
if self.mouselocation != 0:
|
||||
|
@ -417,7 +432,7 @@ class PolarChart(Chart):
|
|||
sizepolicy.setHeightForWidth(True)
|
||||
self.setSizePolicy(sizepolicy)
|
||||
pal = QtGui.QPalette()
|
||||
pal.setColor(QtGui.QPalette.Background, QtGui.QColor("white"))
|
||||
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
||||
self.setPalette(pal)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
|
@ -441,6 +456,7 @@ class PolarChart(Chart):
|
|||
def drawChart(self, qp: QtGui.QPainter):
|
||||
centerX = int(self.width()/2)
|
||||
centerY = int(self.height()/2)
|
||||
qp.setPen(QtGui.QPen(self.textColor))
|
||||
qp.drawText(3, 15, self.name)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawEllipse(QtCore.QPoint(centerX, centerY), int(self.chartWidth/2), int(self.chartHeight/2))
|
||||
|
@ -529,7 +545,7 @@ class SmithChart(Chart):
|
|||
sizepolicy.setHeightForWidth(True)
|
||||
self.setSizePolicy(sizepolicy)
|
||||
pal = QtGui.QPalette()
|
||||
pal.setColor(QtGui.QPalette.Background, QtGui.QColor("white"))
|
||||
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
||||
self.setPalette(pal)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
|
@ -553,6 +569,7 @@ class SmithChart(Chart):
|
|||
def drawSmithChart(self, qp: QtGui.QPainter):
|
||||
centerX = int(self.width()/2)
|
||||
centerY = int(self.height()/2)
|
||||
qp.setPen(QtGui.QPen(self.textColor))
|
||||
qp.drawText(3, 15, self.name)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawEllipse(QtCore.QPoint(centerX, centerY), int(self.chartWidth/2), int(self.chartHeight/2))
|
||||
|
@ -633,3 +650,187 @@ class SmithChart(Chart):
|
|||
|
||||
def heightForWidth(self, a0: int) -> int:
|
||||
return a0
|
||||
|
||||
|
||||
class LogMagChart(Chart):
|
||||
def __init__(self, name=""):
|
||||
super().__init__()
|
||||
self.leftMargin = 30
|
||||
self.chartWidth = 360
|
||||
self.chartHeight = 360
|
||||
self.name = name
|
||||
self.fstart = 0
|
||||
self.fstop = 0
|
||||
self.mouselocation = 0
|
||||
|
||||
self.setMinimumSize(self.chartWidth + 20 + self.leftMargin, self.chartHeight + 40)
|
||||
self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding))
|
||||
pal = QtGui.QPalette()
|
||||
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
||||
self.setPalette(pal)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
self.marker1Color = QtGui.QColor(255, 0, 20)
|
||||
self.marker2Color = QtGui.QColor(20, 0, 255)
|
||||
|
||||
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
|
||||
self.chartWidth = a0.size().width()-20-self.leftMargin
|
||||
self.chartHeight = a0.size().height()-40
|
||||
self.update()
|
||||
|
||||
def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
|
||||
qp = QtGui.QPainter(self)
|
||||
#qp.begin(self) # Apparently not needed?
|
||||
self.drawChart(qp)
|
||||
self.drawValues(qp)
|
||||
qp.end()
|
||||
|
||||
def drawChart(self, qp: QtGui.QPainter):
|
||||
qp.setPen(QtGui.QPen(self.textColor))
|
||||
qp.drawText(3, 15, self.name)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin, 20, self.leftMargin, 20+self.chartHeight+5)
|
||||
qp.drawLine(self.leftMargin-5, 20+self.chartHeight, self.leftMargin+self.chartWidth, 20 + self.chartHeight)
|
||||
|
||||
def drawValues(self, qp: QtGui.QPainter):
|
||||
if len(self.data) == 0 and len(self.reference) == 0:
|
||||
return
|
||||
pen = QtGui.QPen(self.sweepColor)
|
||||
pen.setWidth(2)
|
||||
line_pen = QtGui.QPen(self.sweepColor)
|
||||
line_pen.setWidth(1)
|
||||
highlighter = QtGui.QPen(QtGui.QColor(20, 0, 255))
|
||||
highlighter.setWidth(1)
|
||||
if len(self.data) > 0:
|
||||
fstart = self.data[0].freq
|
||||
fstop = self.data[len(self.data)-1].freq
|
||||
else:
|
||||
fstart = self.reference[0].freq
|
||||
fstop = self.reference[len(self.reference) - 1].freq
|
||||
self.fstart = fstart
|
||||
self.fstop = fstop
|
||||
fspan = fstop-fstart
|
||||
# Find scaling
|
||||
minValue = 100
|
||||
maxValue = 0
|
||||
for d in self.data:
|
||||
logmag = self.logMag(d)
|
||||
if logmag > maxValue:
|
||||
maxValue = logmag
|
||||
if logmag < minValue:
|
||||
minValue = logmag
|
||||
for d in self.reference: # Also check min/max for the reference sweep
|
||||
if d.freq < fstart or d.freq > fstop:
|
||||
continue
|
||||
logmag = self.logMag(d)
|
||||
if logmag > maxValue:
|
||||
maxValue = logmag
|
||||
if logmag < minValue:
|
||||
minValue = logmag
|
||||
|
||||
minValue = 10*math.floor(minValue/10)
|
||||
maxValue = 10*math.ceil(maxValue/10)
|
||||
span = maxValue-minValue
|
||||
for i in range(minValue, maxValue, 10):
|
||||
y = 30 + round((i-minValue)/span*(self.chartHeight-10))
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin-5, y, self.leftMargin+self.chartWidth, y)
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(3, 35, str(-minValue))
|
||||
qp.drawText(3, self.chartHeight+20, str(-maxValue))
|
||||
# At least 100 px between ticks
|
||||
qp.drawText(self.leftMargin-20, 20 + self.chartHeight + 15, LogMagChart.shortenFrequency(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)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(x, 20, x, 20+self.chartHeight+5)
|
||||
qp.setPen(self.textColor)
|
||||
qp.drawText(x-20, 20+self.chartHeight+15, LogMagChart.shortenFrequency(round(fspan/ticks*(i+1) + fstart)))
|
||||
|
||||
if self.mouselocation != 0:
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor(224,224,224)))
|
||||
x = self.leftMargin + 1 + round(self.chartWidth * (self.mouselocation - fstart) / fspan)
|
||||
qp.drawLine(x, 20, x, 20 + self.chartHeight +5)
|
||||
|
||||
qp.setPen(pen)
|
||||
for i in range(len(self.data)):
|
||||
logmag = self.logMag(self.data[i])
|
||||
x = self.leftMargin + 1 + round(self.chartWidth/len(self.data) * i)
|
||||
y = 30 + round((logmag-minValue)/span*(self.chartHeight-10))
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
logmag = self.logMag(self.data[i-1])
|
||||
prevx = self.leftMargin + 1 + round(self.chartWidth / len(self.data) * (i-1))
|
||||
prevy = 30 + round((logmag - minValue) / span * (self.chartHeight - 10))
|
||||
qp.setPen(line_pen)
|
||||
qp.drawLine(x, y, prevx, prevy)
|
||||
qp.setPen(pen)
|
||||
pen.setColor(self.referenceColor)
|
||||
line_pen.setColor(self.referenceColor)
|
||||
qp.setPen(pen)
|
||||
for i in range(len(self.reference)):
|
||||
if self.reference[i].freq < fstart or self.reference[i].freq > fstop:
|
||||
continue
|
||||
logmag = self.logMag(self.reference[i])
|
||||
x = self.leftMargin + 1 + round(self.chartWidth*(self.reference[i].freq - fstart)/fspan)
|
||||
y = 30 + round((logmag-minValue)/span*(self.chartHeight-10))
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
logmag = self.logMag(self.reference[i-1])
|
||||
prevx = self.leftMargin + 1 + round(self.chartWidth*(self.reference[i-1].freq - fstart)/fspan)
|
||||
prevy = 30 + round((logmag - minValue) / span * (self.chartHeight - 10))
|
||||
qp.setPen(line_pen)
|
||||
qp.drawLine(x, y, prevx, prevy)
|
||||
qp.setPen(pen)
|
||||
# Now draw the markers
|
||||
for m in self.markers:
|
||||
if m.location != -1:
|
||||
highlighter.setColor(m.color)
|
||||
qp.setPen(highlighter)
|
||||
logmag = self.logMag(self.data[m.location])
|
||||
x = self.leftMargin + 1 + round(self.chartWidth/len(self.data) * m.location)
|
||||
y = 30 + round((logmag - minValue) / span * (self.chartHeight - 10))
|
||||
qp.drawLine(int(x), int(y) + 3, int(x) - 3, int(y) - 3)
|
||||
qp.drawLine(int(x), int(y) + 3, int(x) + 3, int(y) - 3)
|
||||
qp.drawLine(int(x) - 3, int(y) - 3, int(x) + 3, int(y) - 3)
|
||||
#qp.drawPoint(int(x), int(y))
|
||||
|
||||
@staticmethod
|
||||
def shortenFrequency(frequency):
|
||||
if frequency < 50000:
|
||||
return frequency
|
||||
if frequency < 5000000:
|
||||
return str(round(frequency / 1000)) + "k"
|
||||
return str(round(frequency / 1000000, 1)) + "M"
|
||||
|
||||
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
|
||||
self.mouseMoveEvent(a0)
|
||||
|
||||
def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:
|
||||
x = a0.x()
|
||||
absx = x - self.leftMargin
|
||||
if absx < 0 or absx > self.chartWidth:
|
||||
self.mouselocation = 0
|
||||
a0.ignore()
|
||||
return
|
||||
a0.accept()
|
||||
if self.fstop - self.fstart > 0:
|
||||
span = self.fstop - self.fstart
|
||||
step = span/self.chartWidth
|
||||
f = self.fstart + absx * step
|
||||
# self.mouselocation = f
|
||||
self.markers[0].setFrequency(str(round(f)))
|
||||
else:
|
||||
self.mouselocation = 0
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def logMag(p: Datapoint) -> float:
|
||||
re = p.re
|
||||
im = p.im
|
||||
re50 = 50 * (1 - re * re - im * im) / (1 + re * re + im * im - 2 * re)
|
||||
im50 = 50 * (2 * im) / (1 + re * re + im * im - 2 * re)
|
||||
# Calculate the reflection coefficient
|
||||
mag = math.sqrt((re50 - 50) * (re50 - 50) + im50 * im50) / math.sqrt((re50 + 50) * (re50 + 50) + im50 * im50)
|
||||
return -20 * math.log10(mag)
|
|
@ -1,221 +0,0 @@
|
|||
# NanoVNASaver - a python program to view and export Touchstone data from a NanoVNA
|
||||
# Copyright (C) 2019. Rune B. Broberg
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
# 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 collections
|
||||
import math
|
||||
from typing import List
|
||||
|
||||
from PyQt5 import QtWidgets, QtGui, QtCore
|
||||
|
||||
from .Chart import Chart
|
||||
from .Marker import Marker
|
||||
|
||||
Datapoint = collections.namedtuple('Datapoint', 'freq re im')
|
||||
|
||||
|
||||
class LogMagChart(Chart):
|
||||
def __init__(self, name=""):
|
||||
super().__init__()
|
||||
self.leftMargin = 30
|
||||
self.chartWidth = 360
|
||||
self.chartHeight = 360
|
||||
self.name = name
|
||||
self.fstart = 0
|
||||
self.fstop = 0
|
||||
self.mouselocation = 0
|
||||
|
||||
self.setMinimumSize(self.chartWidth + 20 + self.leftMargin, self.chartHeight + 40)
|
||||
self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding))
|
||||
pal = QtGui.QPalette()
|
||||
pal.setColor(QtGui.QPalette.Background, QtGui.QColor("white"))
|
||||
self.setPalette(pal)
|
||||
self.setAutoFillBackground(True)
|
||||
|
||||
self.marker1Color = QtGui.QColor(255, 0, 20)
|
||||
self.marker2Color = QtGui.QColor(20, 0, 255)
|
||||
|
||||
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
|
||||
self.chartWidth = a0.size().width()-20-self.leftMargin
|
||||
self.chartHeight = a0.size().height()-40
|
||||
self.update()
|
||||
|
||||
def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
|
||||
qp = QtGui.QPainter(self)
|
||||
#qp.begin(self) # Apparently not needed?
|
||||
self.drawChart(qp)
|
||||
self.drawValues(qp)
|
||||
qp.end()
|
||||
|
||||
def drawChart(self, qp: QtGui.QPainter):
|
||||
qp.drawText(3, 15, self.name)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin, 20, self.leftMargin, 20+self.chartHeight+5)
|
||||
qp.drawLine(self.leftMargin-5, 20+self.chartHeight, self.leftMargin+self.chartWidth, 20 + self.chartHeight)
|
||||
|
||||
def drawValues(self, qp: QtGui.QPainter):
|
||||
if len(self.data) == 0 and len(self.reference) == 0:
|
||||
return
|
||||
pen = QtGui.QPen(self.sweepColor)
|
||||
pen.setWidth(2)
|
||||
line_pen = QtGui.QPen(self.sweepColor)
|
||||
line_pen.setWidth(1)
|
||||
highlighter = QtGui.QPen(QtGui.QColor(20, 0, 255))
|
||||
highlighter.setWidth(1)
|
||||
if len(self.data) > 0:
|
||||
fstart = self.data[0].freq
|
||||
fstop = self.data[len(self.data)-1].freq
|
||||
else:
|
||||
fstart = self.reference[0].freq
|
||||
fstop = self.reference[len(self.reference) - 1].freq
|
||||
self.fstart = fstart
|
||||
self.fstop = fstop
|
||||
fspan = fstop-fstart
|
||||
# Find scaling
|
||||
minValue = 100
|
||||
maxValue = 0
|
||||
for d in self.data:
|
||||
logmag = self.logMag(d)
|
||||
if logmag > maxValue:
|
||||
maxValue = logmag
|
||||
if logmag < minValue:
|
||||
minValue = logmag
|
||||
for d in self.reference: # Also check min/max for the reference sweep
|
||||
if d.freq < fstart or d.freq > fstop:
|
||||
continue
|
||||
logmag = self.logMag(d)
|
||||
if logmag > maxValue:
|
||||
maxValue = logmag
|
||||
if logmag < minValue:
|
||||
minValue = logmag
|
||||
|
||||
minValue = 10*math.floor(minValue/10)
|
||||
maxValue = 10*math.ceil(maxValue/10)
|
||||
span = maxValue-minValue
|
||||
for i in range(minValue, maxValue, 10):
|
||||
y = 30 + round((i-minValue)/span*(self.chartHeight-10))
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(self.leftMargin-5, y, self.leftMargin+self.chartWidth, y)
|
||||
qp.setPen(QtCore.Qt.black)
|
||||
qp.drawText(3, 35, str(-minValue))
|
||||
qp.drawText(3, self.chartHeight+20, str(-maxValue))
|
||||
# At least 100 px between ticks
|
||||
qp.drawText(self.leftMargin-20, 20 + self.chartHeight + 15, LogMagChart.shortenFrequency(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)
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor("lightgray")))
|
||||
qp.drawLine(x, 20, x, 20+self.chartHeight+5)
|
||||
qp.setPen(QtCore.Qt.black)
|
||||
qp.drawText(x-20, 20+self.chartHeight+15, LogMagChart.shortenFrequency(round(fspan/ticks*(i+1) + fstart)))
|
||||
|
||||
if self.mouselocation != 0:
|
||||
qp.setPen(QtGui.QPen(QtGui.QColor(224,224,224)))
|
||||
x = self.leftMargin + 1 + round(self.chartWidth * (self.mouselocation - fstart) / fspan)
|
||||
qp.drawLine(x, 20, x, 20 + self.chartHeight +5)
|
||||
|
||||
qp.setPen(pen)
|
||||
for i in range(len(self.data)):
|
||||
logmag = self.logMag(self.data[i])
|
||||
x = self.leftMargin + 1 + round(self.chartWidth/len(self.data) * i)
|
||||
y = 30 + round((logmag-minValue)/span*(self.chartHeight-10))
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
logmag = self.logMag(self.data[i-1])
|
||||
prevx = self.leftMargin + 1 + round(self.chartWidth / len(self.data) * (i-1))
|
||||
prevy = 30 + round((logmag - minValue) / span * (self.chartHeight - 10))
|
||||
qp.setPen(line_pen)
|
||||
qp.drawLine(x, y, prevx, prevy)
|
||||
qp.setPen(pen)
|
||||
pen.setColor(self.referenceColor)
|
||||
line_pen.setColor(self.referenceColor)
|
||||
qp.setPen(pen)
|
||||
for i in range(len(self.reference)):
|
||||
if self.reference[i].freq < fstart or self.reference[i].freq > fstop:
|
||||
continue
|
||||
logmag = self.logMag(self.reference[i])
|
||||
x = self.leftMargin + 1 + round(self.chartWidth*(self.reference[i].freq - fstart)/fspan)
|
||||
y = 30 + round((logmag-minValue)/span*(self.chartHeight-10))
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
logmag = self.logMag(self.reference[i-1])
|
||||
prevx = self.leftMargin + 1 + round(self.chartWidth*(self.reference[i-1].freq - fstart)/fspan)
|
||||
prevy = 30 + round((logmag - minValue) / span * (self.chartHeight - 10))
|
||||
qp.setPen(line_pen)
|
||||
qp.drawLine(x, y, prevx, prevy)
|
||||
qp.setPen(pen)
|
||||
# Now draw the markers
|
||||
for m in self.markers:
|
||||
if m.location != -1:
|
||||
highlighter.setColor(m.color)
|
||||
qp.setPen(highlighter)
|
||||
logmag = self.logMag(self.data[m.location])
|
||||
x = self.leftMargin + 1 + round(self.chartWidth/len(self.data) * m.location)
|
||||
y = 30 + round((logmag - minValue) / span * (self.chartHeight - 10))
|
||||
qp.drawLine(int(x), int(y) + 3, int(x) - 3, int(y) - 3)
|
||||
qp.drawLine(int(x), int(y) + 3, int(x) + 3, int(y) - 3)
|
||||
qp.drawLine(int(x) - 3, int(y) - 3, int(x) + 3, int(y) - 3)
|
||||
#qp.drawPoint(int(x), int(y))
|
||||
|
||||
@staticmethod
|
||||
def shortenFrequency(frequency):
|
||||
if frequency < 50000:
|
||||
return frequency
|
||||
if frequency < 5000000:
|
||||
return str(round(frequency / 1000)) + "k"
|
||||
return str(round(frequency / 1000000, 1)) + "M"
|
||||
|
||||
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
|
||||
self.mouseMoveEvent(a0)
|
||||
|
||||
def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:
|
||||
x = a0.x()
|
||||
absx = x - self.leftMargin
|
||||
if absx < 0 or absx > self.chartWidth:
|
||||
self.mouselocation = 0
|
||||
a0.ignore()
|
||||
return
|
||||
a0.accept()
|
||||
if self.fstop - self.fstart > 0:
|
||||
span = self.fstop - self.fstart
|
||||
step = span/self.chartWidth
|
||||
f = self.fstart + absx * step
|
||||
# self.mouselocation = f
|
||||
self.markers[0].setFrequency(str(round(f)))
|
||||
else:
|
||||
self.mouselocation = 0
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def logMag(p: Datapoint) -> float:
|
||||
re = p.re
|
||||
im = p.im
|
||||
re50 = 50 * (1 - re * re - im * im) / (1 + re * re + im * im - 2 * re)
|
||||
im50 = 50 * (2 * im) / (1 + re * re + im * im - 2 * re)
|
||||
# Calculate the reflection coefficient
|
||||
mag = math.sqrt((re50 - 50) * (re50 - 50) + im50 * im50) / math.sqrt((re50 + 50) * (re50 + 50) + im50 * im50)
|
||||
return -20 * math.log10(mag)
|
|
@ -24,11 +24,10 @@ import serial
|
|||
from PyQt5 import QtWidgets, QtCore, QtGui
|
||||
from serial.tools import list_ports
|
||||
|
||||
from .Chart import Chart, PhaseChart, VSWRChart, PolarChart, SmithChart
|
||||
from .Chart import Chart, PhaseChart, VSWRChart, PolarChart, SmithChart, LogMagChart
|
||||
from .Calibration import CalibrationWindow, Calibration
|
||||
from .Marker import Marker
|
||||
from .SweepWorker import SweepWorker
|
||||
from .LogMagChart import LogMagChart
|
||||
from .Touchstone import Touchstone
|
||||
|
||||
Datapoint = collections.namedtuple('Datapoint', 'freq re im')
|
||||
|
@ -1023,9 +1022,15 @@ class DisplaySettingsWindow(QtWidgets.QWidget):
|
|||
display_options_box = QtWidgets.QGroupBox("Options")
|
||||
display_options_layout = QtWidgets.QFormLayout(display_options_box)
|
||||
|
||||
self.show_lines_option = QtWidgets.QCheckBox("Show lines - Displays a thin line between data points")
|
||||
self.show_lines_option = QtWidgets.QCheckBox("Show lines")
|
||||
show_lines_label = QtWidgets.QLabel("Displays a thin line between data points")
|
||||
self.show_lines_option.stateChanged.connect(self.changeShowLines)
|
||||
display_options_layout.addWidget(self.show_lines_option)
|
||||
display_options_layout.addRow(self.show_lines_option, show_lines_label)
|
||||
|
||||
self.dark_mode_option = QtWidgets.QCheckBox("Dark mode")
|
||||
dark_mode_label = QtWidgets.QLabel("Black background with white text")
|
||||
self.dark_mode_option.stateChanged.connect(self.changeDarkMode)
|
||||
display_options_layout.addRow(self.dark_mode_option, dark_mode_label)
|
||||
|
||||
layout.addWidget(display_options_box)
|
||||
|
||||
|
@ -1105,4 +1110,15 @@ class DisplaySettingsWindow(QtWidgets.QWidget):
|
|||
def changeShowLines(self):
|
||||
state = self.show_lines_option.isChecked()
|
||||
for c in self.app.charts:
|
||||
c.setDrawLines(state)
|
||||
c.setDrawLines(state)
|
||||
|
||||
def changeDarkMode(self):
|
||||
state = self.dark_mode_option.isChecked()
|
||||
if state:
|
||||
for c in self.app.charts:
|
||||
c.setBackgroundColor(QtGui.QColor(QtCore.Qt.black))
|
||||
c.setTextColor(QtGui.QColor(QtCore.Qt.white))
|
||||
else:
|
||||
for c in self.app.charts:
|
||||
c.setBackgroundColor(QtGui.QColor(QtCore.Qt.white))
|
||||
c.setTextColor(QtGui.QColor(QtCore.Qt.black))
|
||||
|
|
Ładowanie…
Reference in New Issue