refactor square charts

pull/499/head
Holger Mueller 2022-04-02 21:53:37 +02:00
rodzic 4729603002
commit 71b6db4d30
3 zmienionych plików z 130 dodań i 231 usunięć

Wyświetl plik

@ -1,8 +1,8 @@
# 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
# Copyright (C) 2019, 2020 Rune B. Broberg
# Copyright (C) 2020ff 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
@ -16,12 +16,9 @@
#
# 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 math
import logging
from PyQt5 import QtGui, QtCore
from NanoVNASaver.RFTools import Datapoint
from NanoVNASaver.Charts.Chart import Chart
from NanoVNASaver.Charts.Square import SquareChart
@ -29,129 +26,32 @@ logger = logging.getLogger(__name__)
class PolarChart(SquareChart):
def __init__(self, name=""):
super().__init__(name)
self.dim.width = 250
self.dim.height = 250
self.setMinimumSize(self.dim.width + 40, self.dim.height + 40)
pal = QtGui.QPalette()
pal.setColor(QtGui.QPalette.Background, Chart.color.background)
self.setPalette(pal)
self.setAutoFillBackground(True)
def paintEvent(self, _: QtGui.QPaintEvent) -> None:
qp = QtGui.QPainter(self)
self.drawChart(qp)
self.drawValues(qp)
qp.end()
def drawChart(self, qp: QtGui.QPainter):
centerX = int(self.width()/2)
centerY = int(self.height()/2)
center_x = int(self.width()/2)
center_y = int(self.height()/2)
width_2 = int(self.dim.width / 2)
height_2 = int(self.dim.height / 2)
width_45 = width_2 * 0.7071
height_45 = height_2 * 0.7071
qp.setPen(QtGui.QPen(Chart.color.text))
qp.drawText(3, 15, self.name)
qp.setPen(QtGui.QPen(Chart.color.foreground))
qp.drawEllipse(QtCore.QPoint(centerX, centerY),
int(self.dim.width / 2),
int(self.dim.height / 2))
qp.drawEllipse(QtCore.QPoint(centerX, centerY),
int(self.dim.width / 4),
int(self.dim.height / 4))
qp.drawLine(centerX - int(self.dim.width / 2), centerY,
centerX + int(self.dim.width / 2), centerY)
qp.drawLine(centerX, centerY - int(self.dim.height / 2),
centerX, centerY + int(self.dim.height / 2))
qp.drawLine(centerX + int(self.dim.height / 2 * math.sin(math.pi / 4)),
centerY + int(self.dim.height / 2 * math.sin(math.pi / 4)),
centerX - int(self.dim.height / 2 * math.sin(math.pi / 4)),
centerY - int(self.dim.height / 2 * math.sin(math.pi / 4)))
qp.drawLine(centerX + int(self.dim.height / 2 * math.sin(math.pi / 4)),
centerY - int(self.dim.height / 2 * math.sin(math.pi / 4)),
centerX - int(self.dim.height / 2 * math.sin(math.pi / 4)),
centerY + int(self.dim.height / 2 * math.sin(math.pi / 4)))
qp.drawEllipse(QtCore.QPoint(center_x, center_y), width_2, height_2)
qp.drawEllipse(QtCore.QPoint(center_x, center_y),
width_2 // 2, height_2 // 2)
qp.drawLine(center_x - width_2, center_y, center_x + width_2, center_y)
qp.drawLine(center_x, center_y - height_2,
center_x, center_y + height_2)
qp.drawLine(center_x + width_45, center_y + height_45,
center_x - width_45, center_y - height_45)
qp.drawLine(center_x + width_45, center_y - height_45,
center_x - width_45, center_y + height_45)
self.drawTitle(qp)
def drawValues(self, qp: QtGui.QPainter):
if len(self.data) == 0 and len(self.reference) == 0:
return
pen = QtGui.QPen(Chart.color.sweep)
pen.setWidth(self.dim.point)
line_pen = QtGui.QPen(Chart.color.sweep)
line_pen.setWidth(self.dim.line)
qp.setPen(pen)
for i in range(len(self.data)):
x = self.getXPosition(self.data[i])
y = self.height()/2 + self.data[i].im * -1 * self.dim.height/2
qp.drawPoint(int(x), int(y))
if self.flag.draw_lines and i > 0:
prevx = self.getXPosition(self.data[i-1])
prevy = self.height() / 2 + self.data[i-1].im * -1 * self.dim.height / 2
qp.setPen(line_pen)
qp.drawLine(x, y, prevx, prevy)
qp.setPen(pen)
pen.setColor(Chart.color.reference)
line_pen.setColor(Chart.color.reference)
qp.setPen(pen)
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
for i in range(len(self.reference)):
data = self.reference[i]
if data.freq < fstart or data.freq > fstop:
continue
x = self.getXPosition(self.reference[i])
y = self.height()/2 + data.im * -1 * self.dim.height/2
qp.drawPoint(int(x), int(y))
if self.flag.draw_lines and i > 0:
prevx = self.getXPosition(self.reference[i-1])
prevy = self.height() / 2 + self.reference[i-1].im * -1 * self.dim.height / 2
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 and m.location < len(self.data):
x = self.getXPosition(self.data[m.location])
y = self.height() / 2 + self.data[m.location].im * -1 * self.dim.height / 2
self.drawMarker(x, y, qp, m.color, self.markers.index(m)+1)
def getXPosition(self, d: Datapoint) -> int:
return self.width()/2 + d.re * self.dim.width/2
def getYPosition(self, d: Datapoint) -> int:
return self.height()/2 + d.im * -1 * self.dim.height/2
def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:
if a0.buttons() == QtCore.Qt.RightButton:
a0.ignore()
return
x = a0.x()
y = a0.y()
absx = x - (self.width() - self.dim.width) / 2
absy = y - (self.height() - self.dim.height) / 2
if absx < 0 or absx > self.dim.width or absy < 0 or absy > self.dim.height \
or len(self.data) == len(self.reference) == 0:
a0.ignore()
return
a0.accept()
if len(self.data) > 0:
target = self.data
else:
target = self.reference
positions = []
for d in target:
thisx = self.width() / 2 + d.re * self.dim.width / 2
thisy = self.height() / 2 + d.im * -1 * self.dim.height / 2
positions.append(math.sqrt((x - thisx)**2 + (y - thisy)**2))
minimum_position = positions.index(min(positions))
m = self.getActiveMarker()
if m is not None:
m.setFrequency(str(round(target[minimum_position].freq)))
m.frequencyInput.setText(str(round(target[minimum_position].freq)))
return
def zoomTo(self, x1, y1, x2, y2):
raise NotImplementedError()

Wyświetl plik

@ -1,8 +1,8 @@
# 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
# Copyright (C) 2019, 2020 Rune B. Broberg
# Copyright (C) 2020ff 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
@ -16,13 +16,9 @@
#
# 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 math
import logging
from typing import List
from PyQt5 import QtGui, QtCore
from NanoVNASaver.RFTools import Datapoint
from NanoVNASaver.Charts.Chart import Chart
from NanoVNASaver.Charts.Square import SquareChart
@ -30,25 +26,7 @@ logger = logging.getLogger(__name__)
class SmithChart(SquareChart):
def __init__(self, name=""):
super().__init__(name)
self.dim.width = 250
self.dim.height = 250
self.setMinimumSize(self.dim.width + 40, self.dim.height + 40)
pal = QtGui.QPalette()
pal.setColor(QtGui.QPalette.Background, Chart.color.background)
self.setPalette(pal)
self.setAutoFillBackground(True)
def paintEvent(self, _: QtGui.QPaintEvent) -> None:
qp = QtGui.QPainter(self)
# qp.begin(self) # Apparently not needed?
self.drawSmithChart(qp)
self.drawValues(qp)
qp.end()
def drawSmithChart(self, qp: QtGui.QPainter):
def drawChart(self, qp: QtGui.QPainter) -> None:
centerX = int(self.width()/2)
centerY = int(self.height()/2)
qp.setPen(QtGui.QPen(Chart.color.text))
@ -117,83 +95,5 @@ class SmithChart(SquareChart):
QtCore.QRect(centerX - 50, centerY - 4 + r, 100, 20),
QtCore.Qt.AlignCenter, str(swr))
def draw_data(self, qp: QtGui.QPainter, color: QtGui.QColor,
data: List[Datapoint]):
if not data:
return
pen = QtGui.QPen(color)
pen.setWidth(self.dim.point)
line_pen = QtGui.QPen(color)
line_pen.setWidth(self.dim.line)
qp.setPen(pen)
prev_x = self.getXPosition(data[0])
prev_y = int(self.height() / 2 + data[0].im * -1 * self.dim.height / 2)
for i, d in enumerate(data):
x = self.getXPosition(d)
y = int(self.height()/2 + d.im * -1 * self.dim.height/2)
qp.drawPoint(x, y)
if self.flag.draw_lines and i > 0:
qp.setPen(line_pen)
qp.drawLine(x, y, prev_x, prev_y)
qp.setPen(pen)
prev_x, prev_y = x, y
def drawValues(self, qp: QtGui.QPainter):
if not (self.data or self.reference):
return
highlighter = QtGui.QPen(QtGui.QColor(20, 0, 255))
highlighter.setWidth(1)
self.draw_data(qp, Chart.color.sweep, self.data)
self.draw_data(qp, Chart.color.reference, self.reference)
# Now draw the markers
for m in self.markers:
if m.location != -1:
x = self.getXPosition(self.data[m.location])
y = self.height() / 2 + self.data[m.location].im * -1 * self.dim.height / 2
self.drawMarker(x, y, qp, m.color, self.markers.index(m)+1)
def zoomTo(self, x1, y1, x2, y2):
raise NotImplementedError()
def getXPosition(self, d: Datapoint) -> int:
return int(self.width()/2 + d.re * self.dim.width/2)
def getYPosition(self, d: Datapoint) -> int:
return int(self.height()/2 + d.im * -1 * self.dim.height/2)
def mouseMoveEvent(self, a0: QtGui.QMouseEvent):
if a0.buttons() == QtCore.Qt.RightButton:
a0.ignore()
return
x = a0.x()
y = a0.y()
absx = x - (self.width() - self.dim.width) / 2
absy = y - (self.height() - self.dim.height) / 2
if absx < 0 or absx > self.dim.width or absy < 0 or absy > self.dim.height \
or len(self.data) == len(self.reference) == 0:
a0.ignore()
return
a0.accept()
target = self.data or self.reference
positions = []
dim_x_2 = self.dim.width / 2
dim_y_2 = self.dim.height / 2
width_2 = self.width() / 2
height_2 = self.height() / 2
positions = [
math.sqrt(
(x - (width_2 + d.re * dim_x_2))**2 +
(y - (height_2 - d.im * dim_y_2))**2)
for d in target
]
minimum_position = positions.index(min(positions))
if m := self.getActiveMarker():
m.setFrequency(str(round(target[minimum_position].freq)))
m.frequencyInput.setText(str(round(target[minimum_position].freq)))

Wyświetl plik

@ -17,23 +17,81 @@
# 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
import math
from typing import List
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets, QtGui
from NanoVNASaver.Charts.Chart import Chart
from NanoVNASaver.RFTools import Datapoint
logger = logging.getLogger(__name__)
class SquareChart(Chart):
def __init__(self, name):
def __init__(self, name=''):
super().__init__(name)
sizepolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Fixed,
QtWidgets.QSizePolicy.MinimumExpanding)
self.setSizePolicy(sizepolicy)
self.dim.width = self.width()-40
self.dim.height = self.height()-40
self.dim.width = 250
self.dim.height = 250
self.setMinimumSize(self.dim.width + 40, self.dim.height + 40)
pal = QtGui.QPalette()
pal.setColor(QtGui.QPalette.Background, Chart.color.background)
self.setPalette(pal)
self.setAutoFillBackground(True)
def paintEvent(self, _: QtGui.QPaintEvent) -> None:
qp = QtGui.QPainter(self)
self.drawChart(qp)
self.drawValues(qp)
qp.end()
def drawChart(self, qp: QtGui.QPainter) -> None:
raise NotImplementedError()
def draw_data(self, qp: QtGui.QPainter, color: QtGui.QColor,
data: List[Datapoint], fstart: int=0, fstop: int=0):
if not data:
return
fstop = fstop or data[-1].freq
pen = QtGui.QPen(color)
pen.setWidth(self.dim.point)
line_pen = QtGui.QPen(color)
line_pen.setWidth(self.dim.line)
qp.setPen(pen)
prev_x = self.getXPosition(data[0])
prev_y = int(self.height() / 2 + data[0].im * -1 * self.dim.height / 2)
for i, d in enumerate(data):
x = self.getXPosition(d)
y = int(self.height()/2 + d.im * -1 * self.dim.height/2)
if d.freq > fstart and d.freq < fstop:
qp.drawPoint(x, y)
if self.flag.draw_lines and i > 0:
qp.setPen(line_pen)
qp.drawLine(x, y, prev_x, prev_y)
qp.setPen(pen)
prev_x, prev_y = x, y
def drawValues(self, qp: QtGui.QPainter):
if not (self.data or self.reference):
return
self.draw_data(qp, Chart.color.sweep, self.data)
fstart = self.data[0].freq if self.data else 0
fstop = self.data[-1].freq if self.data else 0
self.draw_data(qp, Chart.color.reference, self.reference, fstart, fstop)
for m in self.markers:
if m.location != -1 and m.location < len(self.data):
x = self.getXPosition(self.data[m.location])
y = self.height() / 2 + self.data[m.location].im * -1 * self.dim.height / 2
self.drawMarker(x, y, qp, m.color, self.markers.index(m)+1)
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
if not self.flag.is_popout:
@ -44,3 +102,44 @@ class SquareChart(Chart):
min_dimension = min(a0.size().height(), a0.size().width())
self.dim.width = self.dim.height = min_dimension - 40
self.update()
def mouseMoveEvent(self, a0: QtGui.QMouseEvent):
if a0.buttons() == QtCore.Qt.RightButton:
a0.ignore()
return
x = a0.x()
y = a0.y()
absx = x - (self.width() - self.dim.width) / 2
absy = y - (self.height() - self.dim.height) / 2
if absx < 0 or absx > self.dim.width or absy < 0 or absy > self.dim.height \
or len(self.data) == len(self.reference) == 0:
a0.ignore()
return
a0.accept()
target = self.data or self.reference
positions = []
dim_x_2 = self.dim.width / 2
dim_y_2 = self.dim.height / 2
width_2 = self.width() / 2
height_2 = self.height() / 2
positions = [
math.sqrt(
(x - (width_2 + d.re * dim_x_2))**2 +
(y - (height_2 - d.im * dim_y_2))**2)
for d in target
]
minimum_position = positions.index(min(positions))
if m := self.getActiveMarker():
m.setFrequency(str(round(target[minimum_position].freq)))
m.frequencyInput.setText(str(round(target[minimum_position].freq)))
def getXPosition(self, d: Datapoint) -> int:
return int(self.width()/2 + d.re * self.dim.width/2)
def getYPosition(self, d: Datapoint) -> int:
return int(self.height()/2 + d.im * -1 * self.dim.height/2)