kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
Refactoring Charts
rodzic
6315bd06d6
commit
ae88b7ca4d
|
@ -30,7 +30,6 @@ from NanoVNASaver.Marker import Marker
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChartColors:
|
||||
background: QtGui.QColor = QtGui.QColor(QtCore.Qt.white)
|
||||
|
@ -52,41 +51,48 @@ class ChartDimensions:
|
|||
marker: int = 3
|
||||
point: int = 2
|
||||
|
||||
@dataclass
|
||||
class ChartDragBox:
|
||||
pos: Tuple[int] = (-1, -1)
|
||||
pos_start: Tuple[int] = (0, 0)
|
||||
state: bool = False
|
||||
move_x: int = -1
|
||||
move_y: int = -1
|
||||
|
||||
@dataclass
|
||||
class ChartMarkerFlags:
|
||||
draw_numbers: bool = False
|
||||
filled: bool = False
|
||||
at_tip: bool = False
|
||||
|
||||
@dataclass
|
||||
class ChartFlags:
|
||||
draw_lines: bool = False
|
||||
is_popout: bool = False
|
||||
|
||||
class Chart(QtWidgets.QWidget):
|
||||
|
||||
name: str = ""
|
||||
sweepTitle: str = ""
|
||||
|
||||
draggedMarker: Marker = None
|
||||
drawMarkerNumbers: bool = False
|
||||
filledMarkers: bool = False
|
||||
markerAtTip: bool = False
|
||||
|
||||
draggedBoxCurrent: Tuple[int] = (-1, -1)
|
||||
draggedBoxStart: Tuple[int] = (0, 0)
|
||||
draggedBox: bool = False
|
||||
|
||||
drawLines: bool = False
|
||||
isPopout: bool = False
|
||||
|
||||
moveStartX: int = -1
|
||||
moveStartY: int = -1
|
||||
|
||||
bands: ClassVar[Any] = None
|
||||
popoutRequested: ClassVar[Any] = pyqtSignal(object)
|
||||
|
||||
def __init__(self, name):
|
||||
super().__init__()
|
||||
self.name = name
|
||||
self.sweepTitle = ""
|
||||
|
||||
self.color = ChartColors()
|
||||
self.dim = ChartDimensions()
|
||||
self.dragbox = ChartDragBox()
|
||||
self.flag = ChartFlags()
|
||||
self.marker_flag = ChartMarkerFlags()
|
||||
|
||||
self.draggedMarker = None
|
||||
|
||||
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)
|
||||
|
@ -195,19 +201,19 @@ class Chart(QtWidgets.QWidget):
|
|||
return self.getXPosition(d), self.getYPosition(d)
|
||||
|
||||
def setDrawLines(self, draw_lines):
|
||||
self.drawLines = draw_lines
|
||||
self.flag.draw_lines = draw_lines
|
||||
self.update()
|
||||
|
||||
def setDrawMarkerNumbers(self, draw_marker_numbers):
|
||||
self.drawMarkerNumbers = draw_marker_numbers
|
||||
self.marker_flag.draw_numbers = draw_marker_numbers
|
||||
self.update()
|
||||
|
||||
def setMarkerAtTip(self, marker_at_tip):
|
||||
self.markerAtTip = marker_at_tip
|
||||
self.marker_flag.at_tip = marker_at_tip
|
||||
self.update()
|
||||
|
||||
def setFilledMarkers(self, filled_markers):
|
||||
self.filledMarkers = filled_markers
|
||||
self.marker_flag.filled = filled_markers
|
||||
self.update()
|
||||
|
||||
def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
|
||||
|
@ -217,25 +223,25 @@ class Chart(QtWidgets.QWidget):
|
|||
if event.buttons() == QtCore.Qt.MiddleButton:
|
||||
# Drag event
|
||||
event.accept()
|
||||
self.moveStartX = event.x()
|
||||
self.moveStartY = event.y()
|
||||
self.dragbox.move_x = event.x()
|
||||
self.dragbox.move_y = event.y()
|
||||
return
|
||||
if event.modifiers() == QtCore.Qt.ShiftModifier:
|
||||
self.draggedMarker = self.getNearestMarker(event.x(), event.y())
|
||||
elif event.modifiers() == QtCore.Qt.ControlModifier:
|
||||
event.accept()
|
||||
self.draggedBox = True
|
||||
self.draggedBoxStart = (event.x(), event.y())
|
||||
self.dragbox.state = True
|
||||
self.dragbox.pos_start = (event.x(), event.y())
|
||||
return
|
||||
self.mouseMoveEvent(event)
|
||||
|
||||
def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None:
|
||||
self.draggedMarker = None
|
||||
if self.draggedBox:
|
||||
self.zoomTo(self.draggedBoxStart[0], self.draggedBoxStart[1], a0.x(), a0.y())
|
||||
self.draggedBox = False
|
||||
self.draggedBoxCurrent = (-1, -1)
|
||||
self.draggedBoxStart = (0, 0)
|
||||
if self.dragbox.state:
|
||||
self.zoomTo(self.dragbox.pos_start[0], self.dragbox.pos_start[1], a0.x(), a0.y())
|
||||
self.dragbox.state = False
|
||||
self.dragbox.pos = (-1, -1)
|
||||
self.dragbox.pos_start = (0, 0)
|
||||
self.update()
|
||||
|
||||
def zoomTo(self, x1, y1, x2, y2):
|
||||
|
@ -255,20 +261,19 @@ class Chart(QtWidgets.QWidget):
|
|||
def copy(self):
|
||||
new_chart = self.__class__(self.name)
|
||||
new_chart.data = self.data
|
||||
new_chart.color = replace(self.color)
|
||||
new_chart.reference = self.reference
|
||||
new_chart.setBackgroundColor(self.color.background)
|
||||
new_chart.color = replace(self.color)
|
||||
new_chart.dim = replace(self.dim)
|
||||
new_chart.flag = replace(self.flag)
|
||||
new_chart.marker_flag = replace(self.marker_flag)
|
||||
new_chart.markers = self.markers
|
||||
new_chart.swrMarkers = self.swrMarkers
|
||||
new_chart.bands = self.bands
|
||||
new_chart.drawLines = self.drawLines
|
||||
new_chart.markerSize = self.dim.marker
|
||||
new_chart.drawMarkerNumbers = self.drawMarkerNumbers
|
||||
new_chart.filledMarkers = self.filledMarkers
|
||||
new_chart.markerAtTip = self.markerAtTip
|
||||
|
||||
new_chart.resize(self.width(), self.height())
|
||||
new_chart.setPointSize(self.dim.point)
|
||||
new_chart.setLineThickness(self.dim.line)
|
||||
new_chart.setBackgroundColor(self.color.background)
|
||||
return new_chart
|
||||
|
||||
def addSWRMarker(self, swr: float):
|
||||
|
@ -293,22 +298,22 @@ class Chart(QtWidgets.QWidget):
|
|||
self.update()
|
||||
|
||||
def drawMarker(self, x, y, qp: QtGui.QPainter, color: QtGui.QColor, number=0):
|
||||
if self.markerAtTip:
|
||||
if self.marker_flag.at_tip:
|
||||
y -= self.dim.marker
|
||||
pen = QtGui.QPen(color)
|
||||
qp.setPen(pen)
|
||||
qpp = QtGui.QPainterPath()
|
||||
qpp.moveTo(x, y + self.dim.marker)
|
||||
qpp.lineTo(x - self.dim.marker, y - self.markerSize)
|
||||
qpp.lineTo(x + self.dim.marker, y - self.markerSize)
|
||||
qpp.lineTo(x - self.dim.marker, y - self.dim.marker)
|
||||
qpp.lineTo(x + self.dim.marker, y - self.dim.marker)
|
||||
qpp.lineTo(x, y + self.dim.marker)
|
||||
|
||||
if self.filledMarkers:
|
||||
if self.marker_flag.filled:
|
||||
qp.fillPath(qpp, color)
|
||||
else:
|
||||
qp.drawPath(qpp)
|
||||
|
||||
if self.drawMarkerNumbers:
|
||||
if self.marker_flag.draw_numbers:
|
||||
number_x = x - 3
|
||||
number_y = y - self.dim.marker - 3
|
||||
qp.drawText(number_x, number_y, str(number))
|
||||
|
|
|
@ -420,21 +420,21 @@ class FrequencyChart(Chart):
|
|||
if a0.buttons() == QtCore.Qt.MiddleButton:
|
||||
# Drag the display
|
||||
a0.accept()
|
||||
if self.moveStartX != -1 and self.moveStartY != -1:
|
||||
dx = self.moveStartX - a0.x()
|
||||
dy = self.moveStartY - a0.y()
|
||||
if self.dragbox.move_x != -1 and self.dragbox.move_y != -1:
|
||||
dx = self.dragbox.move_x - a0.x()
|
||||
dy = self.dragbox.move_y - a0.y()
|
||||
self.zoomTo(self.leftMargin + dx, self.topMargin + dy,
|
||||
self.leftMargin + self.dim.width + dx,
|
||||
self.topMargin + self.dim.height + dy)
|
||||
|
||||
self.moveStartX = a0.x()
|
||||
self.moveStartY = a0.y()
|
||||
self.dragbox.move_x = a0.x()
|
||||
self.dragbox.move_y = a0.y()
|
||||
return
|
||||
if a0.modifiers() == QtCore.Qt.ControlModifier:
|
||||
# Dragging a box
|
||||
if not self.draggedBox:
|
||||
self.draggedBoxStart = (a0.x(), a0.y())
|
||||
self.draggedBoxCurrent = (a0.x(), a0.y())
|
||||
if not self.dragbox.state:
|
||||
self.dragbox.pos_start = (a0.x(), a0.y())
|
||||
self.dragbox.pos = (a0.x(), a0.y())
|
||||
self.update()
|
||||
a0.accept()
|
||||
return
|
||||
|
@ -472,11 +472,11 @@ class FrequencyChart(Chart):
|
|||
qp.drawText(self.leftMargin + self.dim.width/2 - 70,
|
||||
self.topMargin + self.dim.height/2 - 20,
|
||||
"Data outside frequency span")
|
||||
if self.draggedBox and self.draggedBoxCurrent[0] != -1:
|
||||
if self.dragbox.state and self.dragbox.pos[0] != -1:
|
||||
dashed_pen = QtGui.QPen(self.color.foreground, 1, QtCore.Qt.DashLine)
|
||||
qp.setPen(dashed_pen)
|
||||
top_left = QtCore.QPoint(self.draggedBoxStart[0], self.draggedBoxStart[1])
|
||||
bottom_right = QtCore.QPoint(self.draggedBoxCurrent[0], self.draggedBoxCurrent[1])
|
||||
top_left = QtCore.QPoint(self.dragbox.pos_start[0], self.dragbox.stateStart[1])
|
||||
bottom_right = QtCore.QPoint(self.dragbox.pos[0], self.dragbox.stateCurrent[1])
|
||||
rect = QtCore.QRect(top_left, bottom_right)
|
||||
qp.drawRect(rect)
|
||||
qp.end()
|
||||
|
@ -549,7 +549,7 @@ class FrequencyChart(Chart):
|
|||
continue
|
||||
if self.isPlotable(x, y):
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prevx = self.getXPosition(data[i - 1])
|
||||
prevy = y_function(data[i - 1])
|
||||
if prevy is None:
|
||||
|
|
|
@ -215,7 +215,7 @@ class GroupDelayChart(FrequencyChart):
|
|||
y = self.getYPositionFromDelay(self.groupDelay[i])
|
||||
if self.isPlotable(x, y):
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prevx = self.getXPosition(self.data[i - 1])
|
||||
prevy = self.getYPositionFromDelay(self.groupDelay[i - 1])
|
||||
qp.setPen(line_pen)
|
||||
|
@ -240,7 +240,7 @@ class GroupDelayChart(FrequencyChart):
|
|||
y = self.getYPositionFromDelay(self.groupDelayReference[i])
|
||||
if self.isPlotable(x, y):
|
||||
qp.drawPoint(int(x), int(y))
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prevx = self.getXPosition(self.reference[i - 1])
|
||||
prevy = self.getYPositionFromDelay(self.groupDelayReference[i - 1])
|
||||
qp.setPen(line_pen)
|
||||
|
|
|
@ -196,7 +196,7 @@ class PermeabilityChart(FrequencyChart):
|
|||
qp.setPen(secondary_pen)
|
||||
if self.isPlotable(x, y_im):
|
||||
qp.drawPoint(x, y_im)
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prev_x = self.getXPosition(self.data[i - 1])
|
||||
prev_y_re = self.getReYPosition(self.data[i-1])
|
||||
prev_y_im = self.getImYPosition(self.data[i-1])
|
||||
|
@ -256,7 +256,7 @@ class PermeabilityChart(FrequencyChart):
|
|||
qp.setPen(secondary_pen)
|
||||
if self.isPlotable(x, y_im):
|
||||
qp.drawPoint(x, y_im)
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prev_x = self.getXPosition(self.reference[i - 1])
|
||||
prev_y_re = self.getReYPosition(self.reference[i-1])
|
||||
prev_y_im = self.getImYPosition(self.reference[i-1])
|
||||
|
|
|
@ -83,7 +83,7 @@ class PolarChart(SquareChart):
|
|||
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.drawLines and i > 0:
|
||||
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)
|
||||
|
@ -105,7 +105,7 @@ class PolarChart(SquareChart):
|
|||
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.drawLines and i > 0:
|
||||
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)
|
||||
|
|
|
@ -303,7 +303,7 @@ class RealImaginaryChart(FrequencyChart):
|
|||
qp.setPen(secondary_pen)
|
||||
if self.isPlotable(x, y_im):
|
||||
qp.drawPoint(x, y_im)
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prev_x = self.getXPosition(self.data[i - 1])
|
||||
prev_y_re = self.getReYPosition(self.data[i-1])
|
||||
prev_y_im = self.getImYPosition(self.data[i-1])
|
||||
|
@ -363,7 +363,7 @@ class RealImaginaryChart(FrequencyChart):
|
|||
qp.setPen(secondary_pen)
|
||||
if self.isPlotable(x, y_im):
|
||||
qp.drawPoint(x, y_im)
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prev_x = self.getXPosition(self.reference[i - 1])
|
||||
prev_y_re = self.getReYPosition(self.reference[i-1])
|
||||
prev_y_im = self.getImYPosition(self.reference[i-1])
|
||||
|
|
|
@ -129,7 +129,7 @@ class SmithChart(SquareChart):
|
|||
x = self.getXPosition(self.data[i])
|
||||
y = int(self.height()/2 + self.data[i].im * -1 * self.dim.height/2)
|
||||
qp.drawPoint(x, y)
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prevx = self.getXPosition(self.data[i-1])
|
||||
prevy = int(self.height() / 2 + self.data[i-1].im * -1 * self.dim.height / 2)
|
||||
qp.setPen(line_pen)
|
||||
|
@ -151,7 +151,7 @@ class SmithChart(SquareChart):
|
|||
x = self.getXPosition(data)
|
||||
y = int(self.height()/2 + data.im * -1 * self.dim.height/2)
|
||||
qp.drawPoint(x, y)
|
||||
if self.drawLines and i > 0:
|
||||
if self.flag.draw_lines and i > 0:
|
||||
prevx = self.getXPosition(self.reference[i-1])
|
||||
prevy = int(self.height() / 2 + self.reference[i-1].im * -1 * self.dim.height / 2)
|
||||
qp.setPen(line_pen)
|
||||
|
|
|
@ -36,7 +36,7 @@ class SquareChart(Chart):
|
|||
self.dim.height = self.height()-40
|
||||
|
||||
def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
|
||||
if not self.isPopout:
|
||||
if not self.flag.is_popout:
|
||||
self.setFixedWidth(a0.size().height())
|
||||
self.dim.width = a0.size().height()-40
|
||||
self.dim.height = a0.size().height()-40
|
||||
|
|
|
@ -231,20 +231,20 @@ class TDRChart(Chart):
|
|||
if a0.buttons() == QtCore.Qt.MiddleButton:
|
||||
# Drag the display
|
||||
a0.accept()
|
||||
if self.moveStartX != -1 and self.moveStartY != -1:
|
||||
dx = self.moveStartX - a0.x()
|
||||
dy = self.moveStartY - a0.y()
|
||||
if self.dragbox.move_x != -1 and self.dragbox.move_y != -1:
|
||||
dx = self.dragbox.move_x - a0.x()
|
||||
dy = self.dragbox.move_y - a0.y()
|
||||
self.zoomTo(self.leftMargin + dx, self.topMargin + dy,
|
||||
self.leftMargin + self.dim.width + dx,
|
||||
self.topMargin + self.dim.height + dy)
|
||||
self.moveStartX = a0.x()
|
||||
self.moveStartY = a0.y()
|
||||
self.dragbox.move_x = a0.x()
|
||||
self.dragbox.move_y = a0.y()
|
||||
return
|
||||
if a0.modifiers() == QtCore.Qt.ControlModifier:
|
||||
# Dragging a box
|
||||
if not self.draggedBox:
|
||||
self.draggedBoxStart = (a0.x(), a0.y())
|
||||
self.draggedBoxCurrent = (a0.x(), a0.y())
|
||||
if not self.dragbox.state:
|
||||
self.dragbox.pos_start = (a0.x(), a0.y())
|
||||
self.dragbox.pos = (a0.x(), a0.y())
|
||||
self.update()
|
||||
a0.accept()
|
||||
return
|
||||
|
@ -402,11 +402,11 @@ class TDRChart(Chart):
|
|||
str(round(self.tdrWindow.distance_axis[self.markerLocation] / 2,
|
||||
2)) + "m")
|
||||
|
||||
if self.draggedBox and self.draggedBoxCurrent[0] != -1:
|
||||
if self.dragbox.state and self.dragbox.pos[0] != -1:
|
||||
dashed_pen = QtGui.QPen(self.color.foreground, 1, QtCore.Qt.DashLine)
|
||||
qp.setPen(dashed_pen)
|
||||
top_left = QtCore.QPoint(self.draggedBoxStart[0], self.draggedBoxStart[1])
|
||||
bottom_right = QtCore.QPoint(self.draggedBoxCurrent[0], self.draggedBoxCurrent[1])
|
||||
top_left = QtCore.QPoint(self.dragbox.pos_start[0], self.dragbox.stateStart[1])
|
||||
bottom_right = QtCore.QPoint(self.dragbox.pos[0], self.dragbox.stateCurrent[1])
|
||||
rect = QtCore.QRect(top_left, bottom_right)
|
||||
qp.drawRect(rect)
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue