Butttttooonns

Nutton editor shows the path now
I need to fix it but Im taking a break
This commit is contained in:
Daniel Carta
2024-07-03 15:12:25 -06:00
parent 64080620f3
commit bd590e0cf2
2 changed files with 77 additions and 27 deletions
+45 -4
View File
@@ -11,7 +11,6 @@ class ButtonEditor(QMainWindow):
self.setWindowTitle("Button Editor")
self.path_planner = path_planner
#Set background image to the field
self.image_label = QLabel(self)
script_dir = os.path.dirname(os.path.abspath(__file__))
@@ -23,7 +22,6 @@ class ButtonEditor(QMainWindow):
else:
self.image_label.setPixmap(self.pixmap)
#Layout stuff
self.path_planner_button = QPushButton("Main Window")
self.path_planner_button.clicked.connect(self.show_path_planner)
self.button_editor_button = QPushButton("Button Editor")
@@ -33,7 +31,6 @@ class ButtonEditor(QMainWindow):
button_layout.addWidget(self.path_planner_button)
button_layout.addWidget(self.button_editor_button)
#Layout of the auto planner
layout = QVBoxLayout()
layout.addLayout(button_layout)
layout.addWidget(self.image_label)
@@ -50,4 +47,48 @@ class ButtonEditor(QMainWindow):
def show_path_planner(self):
self.hide()
self.path_planner.show()
self.path_planner.show()
def update_scene(self):
self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png"))
painter = QPainter(self.pixmap)
grey_pen = QPen(QColor(127, 127, 127))
grey_pen.setWidth(2)
painter.setPen(grey_pen)
# Draw the Bezier curve segments
if self.path_planner.coordinates.size > 0:
for i in range(len(self.path_planner.coordinates) - 1):
start = QPoint(self.path_planner.coordinates[i][0], self.path_planner.coordinates[i][1])
end = QPoint(self.path_planner.coordinates[i + 1][0], self.path_planner.coordinates[i + 1][1])
if i < len(self.path_planner.control_points):
control_pair = self.path_planner.control_points[i]
pen = QPen(Qt.yellow)
pen.setWidth(2)
painter.setPen(pen)
path = QPainterPath()
path.moveTo(start)
path.cubicTo(control_pair[0], control_pair[1], end)
painter.drawPath(path)
# Draw the nodes
painter.setPen(Qt.white)
font = painter.font()
font.setPointSize(25)
painter.setFont(font)
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.white)
for i, (x, y) in enumerate(self.path_planner.coordinates):
node_rect = QRect(x - self.path_planner.node_size // 2, y - self.path_planner.node_size // 2,
self.path_planner.node_size, self.path_planner.node_size)
painter.drawEllipse(node_rect)
painter.drawText(node_rect, Qt.AlignCenter, str(i + 1))
painter.end()
self.image_label.setPixmap(self.pixmap)
+32 -23
View File
@@ -13,7 +13,11 @@ class PathPlanner(QMainWindow):
self.setWindowTitle("Auto Planner")
#Set background image to the field
self.coordinates = np.empty((0, 2), dtype=int)
self.control_points = []
self.rotation_handles = []
self.node_angles = []
self.image_label = QLabel(self)
script_dir = os.path.dirname(os.path.abspath(__file__))
@@ -25,10 +29,6 @@ class PathPlanner(QMainWindow):
else:
self.image_label.setPixmap(self.pixmap)
#I want to put these in a side toolbar
#self.clear_button = QPushButton("Clear Auto")
#self.clear_button.clicked.connect(self.show_clear_warning)
self.main_window_button = QPushButton("Main Window")
self.main_window_button.clicked.connect(self.show_main_window)
self.button_editor_button = QPushButton("Button Editor")
@@ -38,7 +38,6 @@ class PathPlanner(QMainWindow):
button_layout.addWidget(self.main_window_button)
button_layout.addWidget(self.button_editor_button)
#Layout of the auto planner
layout = QVBoxLayout()
layout.addLayout(button_layout)
layout.addWidget(self.image_label)
@@ -53,7 +52,6 @@ class PathPlanner(QMainWindow):
self.setMouseTracking(True)
#Variables
self.last_click_pos = QPoint()
self.coordinates = np.empty((0, 2), dtype=int)
@@ -83,7 +81,7 @@ class PathPlanner(QMainWindow):
self.calculate_control_points()
self.calculate_rotation_handle_pos()
self.draw_scene()
self.draw_scene()
self.button_editor.update_scene()
elif event.button() == Qt.LeftButton:
current_time = event.timestamp()
if (current_time - self.last_click_time < 300 and
@@ -96,22 +94,23 @@ class PathPlanner(QMainWindow):
if control_point_index != (-1, -1):
self.smoothPoints(control_point_index[0], control_point_index[1])
self.draw_scene()
self.button_editor.update_scene()
else:
control_point_index = self.is_point_in_control_point(x, y)
if control_point_index != (-1, -1):
self.dragging_control_point = True
self.dragging_control_point_index = control_point_index
control_point_index = self.is_point_in_control_point(x, y)
if control_point_index != (-1, -1):
self.dragging_control_point = True
self.dragging_control_point_index = control_point_index
else:
node_index = self.is_point_in_node(x, y)
if node_index != -1:
self.dragging_node = True
self.dragging_node_index = node_index
else:
node_index = self.is_point_in_node(x, y)
if node_index != -1:
self.dragging_node = True
self.dragging_node_index = node_index
else:
rotation_handle_index = self.is_point_in_rotation_handle(x, y)
if rotation_handle_index != -1:
self.dragging_rotation_handle = True
self.dragging_rotation_handle_index = rotation_handle_index
rotation_handle_index = self.is_point_in_rotation_handle(x, y)
if rotation_handle_index != -1:
self.dragging_rotation_handle = True
self.dragging_rotation_handle_index = rotation_handle_index
self.last_click_time = current_time
self.last_click_pos = pos
@@ -125,6 +124,7 @@ class PathPlanner(QMainWindow):
if index < len(self.rotation_handles):
del self.rotation_handles[index]
self.draw_scene()
self.button_editor.update_scene()
def mouseMoveEvent(self, event: QMouseEvent):
if self.dragging_control_point:
@@ -133,6 +133,7 @@ class PathPlanner(QMainWindow):
curve_index, point_index = self.dragging_control_point_index
self.control_points[curve_index][point_index] = QPoint(x, y)
self.draw_scene()
self.button_editor.update_scene()
elif self.dragging_node:
pos = self.image_label.mapFrom(self, event.position().toPoint())
x, y = pos.x(), pos.y()
@@ -140,6 +141,7 @@ class PathPlanner(QMainWindow):
self.calculate_control_points()
self.calculate_rotation_handle_pos()
self.draw_scene()
self.button_editor.update_scene()
elif self.dragging_rotation_handle:
pos = self.image_label.mapFrom(self, event.position().toPoint())
x, y = pos.x(), pos.y()
@@ -154,7 +156,7 @@ class PathPlanner(QMainWindow):
rotation_handle_y = int(node_y + self.rotation_handle_distance * np.sin(angle))
self.rotation_handles[self.dragging_rotation_handle_index] = QPoint(rotation_handle_x, rotation_handle_y)
self.draw_scene()
self.draw_scene()
self.button_editor.update_scene()
#Resets dragging when mouse is released
def mouseReleaseEvent(self, event: QMouseEvent):
@@ -165,6 +167,7 @@ class PathPlanner(QMainWindow):
self.dragging_node_index = -1
self.dragging_rotation_handle = False
self.dragging_rotation_handle_index = -1
self.button_editor.update_scene()
#These 3 are for distinguishing what the user is clicking
def is_point_in_rotation_handle(self, x, y):
@@ -215,6 +218,10 @@ class PathPlanner(QMainWindow):
else:
self.rotation_handles[i] = QPoint(rotation_handle_x, rotation_handle_y)
#Updates buttonEditor
def update_scene(self):
self.button_editor.update_scene(self.coordinates, self.control_points)
#Draws the scene, big important function
def draw_scene(self):
self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png"))
@@ -289,6 +296,7 @@ class PathPlanner(QMainWindow):
painter.setBrush(Qt.NoBrush)
self.image_label.setPixmap(self.pixmap)
self.button_editor.update_scene()
#S M O O T H
def smoothPoints(self, curve_index: int, point_index: int):
@@ -318,6 +326,7 @@ class PathPlanner(QMainWindow):
self.rotation_handles.clear()
self.node_angles.clear()
self.draw_scene()
self.button_editor.update_scene()
#Warning for clearing
def show_clear_warning(self):