from .mode import Mode from .opengl_widget import set_current_points, set_drawing_event from .points import PointSet # Size of point for drawing __POINT_SIZE = 8 # There are a lot of module-global variables being used because of the # nature of state management in OpenGL. __point_set = PointSet(__POINT_SIZE) def __refresh_point_list(ctx): """ Refreshes the point list display. @param ctx A handle to the window context. """ # In order to make some guarantees and avoid duplicate # data we will clear the point list widget and re-populate # it using the current __point_set. ctx.point_list_widget.clear() for p in __point_set.points: ctx.point_list_widget.addItem("({}, {})".format(p.x, p.y)) def __handle_add_point(ctx, event): """ Event handler for the add point mode. Sets the drawing mode for the OpenGL Widget using `set_drawing_mode`, converts a point to our point representation, and adds it to the list. @param ctx A context handle to the main window. @param event The click event. """ global __point_set # No attribute at the moment. __point_set.add_point(event.x(), event.y()) __refresh_point_list(ctx) set_drawing_event(event) set_current_points(__point_set) ctx.opengl_widget.update() ctx.point_list_widget.update() def __handle_edit_point(ctx, event): # TODO: This function and delete definitely need to make sure they are # on a point we have. # # Since points are unique consider a hashmap of points to make O(1) # lookups for addition and deletion. This list can be maintained here # in this module. It should be a dictionary - from point to # attributes in the case of algorithms that require points to have # weights or something. # # Should move the associated point in the list to the new location if # applicable. # Store old x, y from event set_drawing_event(event) ctx.update() # after this remove the point from the list def __handle_move_points(ctx, event): # TODO: Should move the associated points in the list to the new location. # Store list of old points that are captured set_drawing_event(event) ctx.update() # Find and move all points from the old list to their new locations def __handle_delete_point(ctx, event): set_drawing_event(event) __point_set.remove_point(event.x(), event.y()) __refresh_point_list(ctx) ctx.opengl_widget.update() ctx.point_list_widget.update() # Simple dispatcher to make it easy to dispatch the right mode # function when the OpenGL window is clicked. MODE_HANDLER_MAP = { Mode.OFF: lambda: None, Mode.ADD: __handle_add_point, Mode.EDIT: __handle_edit_point, Mode.MOVE: __handle_move_points, Mode.DELETE: __handle_delete_point }