Skip to content

Commit

Permalink
#187 - Prototype lines and circles for new UI track elements
Browse files Browse the repository at this point in the history
  • Loading branch information
dmh23 committed Mar 2, 2023
1 parent 533ad46 commit e29a206
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/analyze/util/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def multi_draw(track_graphics :TrackGraphics, visitors, colours, threshold):
assert len(colours) == len(visitors)
assert threshold >= 1

min_x = visitors[0].min_x
min_x = visitors[0]._min_x
min_y = visitors[0].min_y
granularity = visitors[0].granularity

Expand Down
9 changes: 5 additions & 4 deletions src/prototype_ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from prototype_ui.actions import Actions
from prototype_ui.menubar import MenuBarManager
from prototype_ui.toolbar import ToolBarManager
from prototype_ui.track_analysis_canvas import TrackAnalysisCanvas, SolidCircle, TrackArea
from prototype_ui.track_analysis_canvas import TrackAnalysisCanvas, FilledCircle, TrackArea, Line


class MainWindow(QMainWindow):
Expand Down Expand Up @@ -43,9 +43,10 @@ def __init__(self):

# Example of drawing stuff in the track canvas until I integrate with Tracks etc.
self.canvas.set_track_area(TrackArea(0, 0, 100, 100))
self.canvas.add_fixed_shape(SolidCircle((90, 90), 20, Qt.GlobalColor.red))
self.canvas.add_fixed_shape(SolidCircle((50, 50), 20, Qt.GlobalColor.white))
self.canvas.add_fixed_shape(SolidCircle((10, 10), 20, Qt.GlobalColor.blue))
self.canvas.add_fixed_shape(FilledCircle((90, 90), 20, Qt.GlobalColor.red))
self.canvas.add_fixed_shape(FilledCircle((50, 50), 20, Qt.GlobalColor.white))
self.canvas.add_fixed_shape(FilledCircle((10, 10), 20, Qt.GlobalColor.blue))
self.canvas.add_fixed_shape(Line((0, 100), (100, 0), 10, Qt.GlobalColor.yellow))

def _new_file(self):
print("New File")
Expand Down
43 changes: 33 additions & 10 deletions src/prototype_ui/track_analysis_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from PyQt6.QtGui import QBrush, QPen, QPainter, QColor
from PyQt6.QtCore import Qt, QPointF, QPoint

Point = (float | int, float | int) # Typedef which we will use a lot for graphics


class CanvasItem(ABC):
pass
Expand All @@ -32,10 +34,18 @@ def __init__(self, min_x, min_y, max_x, max_y):


class Scale:
def __init__(self, min_x, max_y, scale):
self.min_x = min_x
self.max_y = max_y
self.scale = scale
def __init__(self, min_x, max_y, scale_factor):
self._min_x = min_x
self._max_y = max_y
self._scale_factor = scale_factor

def apply(self, point: Point) -> Point:
(x, y) = point

x = (x - self._min_x) * self._scale_factor
y = (self._max_y - y) * self._scale_factor

return round(x), round(y)


class TrackAnalysisCanvas(Canvas):
Expand Down Expand Up @@ -98,20 +108,33 @@ def add_fixed_shape(self, item: FixedShape):

# plot_dot() in v3

class SolidCircle(FixedShape):
def __init__(self, point: (float | int, float | int), radius: float | int, colour: Qt.GlobalColor):
class FilledCircle(FixedShape):
def __init__(self, point: Point, radius: float | int, colour: Qt.GlobalColor):
self._point = point
self._pen = QPen(colour)
self._radius = radius
self._pen.setWidth(radius)

def paint(self, painter: QPainter, scale: Scale):
(x, y) = self._point
x, y = scale.apply(self._point)
painter.setPen(self._pen)
painter.drawEllipse(round(x), round(y), self._radius, self._radius)


x = (x - scale.min_x) * scale.scale
y = (scale.max_y - y) * scale.scale
# def plot_line(self, point1, point2, width, fill_colour, dash_pattern=None) in v3

class Line(FixedShape):
def __init__(self, start: Point, finish: Point, width: int, colour: Qt.GlobalColor, dash_pattern="??????"):
self._start = start
self._finish = finish
self._pen = QPen(colour)
self._pen.setWidth(width)

def paint(self, painter: QPainter, scale: Scale):
x1, y1 = scale.apply(self._start)
x2, y2 = scale.apply(self._finish)
painter.setPen(self._pen)
painter.drawPoint(int(x), int(y))
painter.drawLine(x1, y1, x2, y2)



0 comments on commit e29a206

Please sign in to comment.