I'm so close to tears

Fixed the left and right click issues
This commit is contained in:
Daniel Carta
2024-07-02 16:32:53 -06:00
parent ac6469310e
commit fbeef10b97
+28 -7
View File
@@ -47,23 +47,29 @@ class MainWindow(QMainWindow):
self.handles = [] self.handles = []
self.dragging_handle = False self.dragging_handle = False
self.dragging_handle_index = -1 self.dragging_handle_index = -1
self.dragging_robot = False
self.dragging_robot_index = -1
def mousePressEvent(self, event: QMouseEvent): def mousePressEvent(self, event: QMouseEvent):
pos = self.image_label.mapFrom(self, event.position().toPoint()) pos = self.image_label.mapFrom(self, event.position().toPoint())
x, y = pos.x(), pos.y() x, y = pos.x(), pos.y()
if 0 <= x < self.pixmap.width() and 0 <= y < self.pixmap.height(): if 0 <= x < self.pixmap.width() and 0 <= y < self.pixmap.height():
if event.button() == Qt.LeftButton: if event.button() == Qt.RightButton:
self.coordinates = np.vstack((self.coordinates, [x, y]))
if len(self.coordinates) >= 2:
self.calculate_handle_pos()
self.draw_scene()
elif event.button() == Qt.LeftButton:
handle_index = self.is_point_in_handle(x, y) handle_index = self.is_point_in_handle(x, y)
if handle_index != -1: if handle_index != -1:
self.dragging_handle = True self.dragging_handle = True
self.dragging_handle_index = handle_index self.dragging_handle_index = handle_index
else: else:
self.coordinates = np.vstack((self.coordinates, [x, y])) robot_index = self.is_point_in_robot(x, y)
if len(self.coordinates) >= 2: if robot_index != -1:
self.calculate_handle_pos() self.dragging_robot = True
elif event.button() == Qt.RightButton: self.dragging_robot_index = robot_index
self.draw_scene()
def mouseMoveEvent(self, event: QMouseEvent): def mouseMoveEvent(self, event: QMouseEvent):
if self.dragging_handle: if self.dragging_handle:
@@ -71,11 +77,19 @@ class MainWindow(QMainWindow):
x, y = pos.x(), pos.y() x, y = pos.x(), pos.y()
self.handles[self.dragging_handle_index] = QPoint(x, y) self.handles[self.dragging_handle_index] = QPoint(x, y)
self.draw_scene() self.draw_scene()
elif self.dragging_robot:
pos = self.image_label.mapFrom(self, event.position().toPoint())
x, y = pos.x(), pos.y()
self.coordinates[self.dragging_robot_index] = [x, y]
self.calculate_handle_pos()
self.draw_scene()
def mouseReleaseEvent(self, event: QMouseEvent): def mouseReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.LeftButton: if event.button() == Qt.LeftButton:
self.dragging_handle = False self.dragging_handle = False
self.dragging_handle_index = -1 self.dragging_handle_index = -1
self.dragging_robot = False
self.dragging_robot_index = -1
def is_point_in_handle(self, x, y): def is_point_in_handle(self, x, y):
for i, handle in enumerate(self.handles): for i, handle in enumerate(self.handles):
@@ -83,6 +97,13 @@ class MainWindow(QMainWindow):
abs(y - handle.y()) <= self.handle_size // 2): abs(y - handle.y()) <= self.handle_size // 2):
return i return i
return -1 return -1
def is_point_in_robot(self, x, y):
for i, (robot_x, robot_y) in enumerate(self.coordinates):
if (abs(x - robot_x) <= self.robot_size // 2 and
abs(y - robot_y) <= self.robot_size // 2):
return i
return -1
def calculate_handle_pos(self): def calculate_handle_pos(self):
if len(self.coordinates) >= 2: if len(self.coordinates) >= 2:
@@ -124,7 +145,7 @@ class MainWindow(QMainWindow):
self.handle_size, self.handle_size,
self.handle_size self.handle_size
) )
painter.setBrush(Qt.NoBrush) # Reset to no brush after drawing handle painter.setBrush(Qt.NoBrush)
else: else:
painter.drawLine(start, end) painter.drawLine(start, end)