Browse Source

Point display - edit, delete, move, etc tomorrow

tb-init-ui-render
Taylor Bockman 5 years ago
parent
commit
ebb5bc9a8a
  1. 33
      clusterview/mode_handlers.py
  2. 43
      clusterview/opengl_widget.py
  3. 6
      main_window.py

33
clusterview/mode_handlers.py

@ -1,5 +1,11 @@
from .mode import Mode from .mode import Mode
from .opengl_widget import set_drawing_event from .opengl_widget import set_current_points, set_drawing_event
from .points import PointSet
# There are a lot of module-global variables being used because of the
# nature of state management in OpenGL.
__point_set = PointSet()
def __handle_add_point(ctx, event): def __handle_add_point(ctx, event):
""" """
@ -8,12 +14,29 @@ def __handle_add_point(ctx, event):
Sets the drawing mode for the OpenGL Widget using Sets the drawing mode for the OpenGL Widget using
`set_drawing_mode`, converts a point to our point `set_drawing_mode`, converts a point to our point
representation, and adds it to the list. 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())
set_drawing_event(event) set_drawing_event(event)
ctx.update() set_current_points(__point_set)
# Convert to our point representation and add to list widget
# Also force widget to update - also everywhere below any time you # In order to make some guarantees and avoid duplicate
# modify the point list you need to force update the point list widget. # 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[0], p[1]))
ctx.opengl_widget.update()
ctx.point_list_widget.update()
def __handle_edit_point(ctx, event): def __handle_edit_point(ctx, event):
# TODO: This function and delete definitely need to make sure they are # TODO: This function and delete definitely need to make sure they are

43
clusterview/opengl_widget.py

@ -18,6 +18,7 @@ from OpenGL.GL import (glBegin, glClearColor, glColor4f, glEnable,
from .exceptions import InvalidModeError, InvalidStateError from .exceptions import InvalidModeError, InvalidStateError
from .mode import Mode from .mode import Mode
from .points import PointSet
class Color(Enum): class Color(Enum):
BLUE = 0 BLUE = 0
@ -45,6 +46,7 @@ __HEIGHT = None
# function local. # function local.
__current_mode = None __current_mode = None
__current_event = None __current_event = None
__current_points = None
__current_context = None __current_context = None
@ -57,6 +59,25 @@ def set_drawing_context(ctx):
__current_context = ctx __current_context = ctx
def set_current_points(points):
"""
Sets the point state variable that will be passed in to the paint_gl
function, and further to the point painter functions in order to
render the scene.
Each time a point is added, removed, or edited this point set must be
updated.
@param points The PointSet representing the current scene.
"""
global __current_points
if not isinstance(points, PointSet):
raise ValueError("set_current_points must recieve a PointSet as its " +
"argument.")
__current_points = points
def set_drawing_mode(mode): def set_drawing_mode(mode):
""" """
@ -137,11 +158,13 @@ def paint_gl():
raise InvalidStateError("Event must exist for ADD, EDIT, MOVE, " + raise InvalidStateError("Event must exist for ADD, EDIT, MOVE, " +
"and DELETE") "and DELETE")
if (__current_mode in [Mode.ADD, Mode.EDIT, Mode.MOVE, Mode.DELETE] and
__current_points is None):
raise InvalidStateError("Points must exist for ADD, EDIT, MOVE, " +
"and DELETE")
if __current_mode is Mode.ADD: if __current_mode is Mode.ADD:
# TODO: This needs to be modified to instead take the point list draw_points(__current_points, Color.BLUE)
# and redraw the entire list (which will have the new point
# added) each click.
draw_points(__current_event.x(), __current_event.y(), Color.BLUE)
elif __current_mode is Mode.EDIT: elif __current_mode is Mode.EDIT:
raise NotImplementedError("Drawing for EDIT not implemented.") raise NotImplementedError("Drawing for EDIT not implemented.")
elif __current_mode is Mode.MOVE: elif __current_mode is Mode.MOVE:
@ -174,7 +197,7 @@ def __clamp_y(y):
return y_w return y_w
def draw_points(x, y, color): def draw_points(point_set, color):
""" """
Simple point drawing function. Simple point drawing function.
@ -182,8 +205,7 @@ def draw_points(x, y, color):
function will draw the given point with the given function will draw the given point with the given
color. color.
@param x The x-coordinate. @param point_set The PointSet to draw.
@param y The y-coordinate.
@param color The Color Enum. @param color The Color Enum.
""" """
global __current_context global __current_context
@ -202,9 +224,10 @@ def draw_points(x, y, color):
glColor4f(ct[0], ct[1], ct[2], ct[3]) glColor4f(ct[0], ct[1], ct[2], ct[3])
glBegin(GL_POINTS) glBegin(GL_POINTS)
glVertex3f(__clamp_x(__current_event.x()), for point in point_set.points:
__clamp_y(__current_event.y()), glVertex3f(__clamp_x(point[0]),
0.0) # Z is currently fixed to 0 __clamp_y(point[1]),
0.0) # Z is currently fixed to 0
glEnd() glEnd()

6
main_window.py

@ -99,5 +99,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# Map from Mode -> function # Map from Mode -> function
# where the function is a handler for the # where the function is a handler for the
# OpenGL Widget. # OpenGL event. The context passed to these functions allows
MODE_HANDLER_MAP[self.__mode](self.opengl_widget, event) # them to modify on screen widgets such as the QOpenGLWidget and
# QListWidget.
MODE_HANDLER_MAP[self.__mode](self, event)

Loading…
Cancel
Save