Field switching with combobox in Spline UI

Signed-off-by: Ravago Jones <ravagojones@gmail.com>
Change-Id: Ia2a23bb2f3c6bebf675ebd82fc97714a1bf864f8
diff --git a/frc971/control_loops/python/path_edit.py b/frc971/control_loops/python/path_edit.py
index 65a376b..d743e0d 100755
--- a/frc971/control_loops/python/path_edit.py
+++ b/frc971/control_loops/python/path_edit.py
@@ -32,8 +32,9 @@
 
     def __init__(self):
         super(FieldWidget, self).__init__()
+        self.set_field(FIELD)
         self.set_size_request(
-            self.mToPx(FIELD.width), self.mToPx(FIELD.length))
+            self.mToPx(self.field.width), self.mToPx(self.field.length))
 
         self.points = Points()
         self.graph = Graph()
@@ -63,12 +64,15 @@
                         | Gdk.EventMask.POINTER_MOTION_MASK
                         | Gdk.EventMask.SCROLL_MASK)
 
+    def set_field(self, field):
+        self.field = field
         try:
             self.field_png = cairo.ImageSurface.create_from_png(
-                "frc971/control_loops/python/field_images/" + FIELD.field_id +
-                ".png")
+                "frc971/control_loops/python/field_images/" +
+                self.field.field_id + ".png")
         except cairo.Error:
             self.field_png = None
+        self.queue_draw()
 
     # returns the transform from widget space to field space
     @property
@@ -82,8 +86,8 @@
     # returns the scale from pixels in field space to meters in field space
     def pxToM_scale(self):
         available_space = self.get_allocation()
-        return np.maximum(FIELD.width / available_space.width,
-                          FIELD.length / available_space.height)
+        return np.maximum(self.field.width / available_space.width,
+                          self.field.length / available_space.height)
 
     def pxToM(self, p):
         return p * self.pxToM_scale()
@@ -103,9 +107,9 @@
         x_difference_o = p2[0] - p1[0]
         y_difference_o = p2[1] - p1[1]
         x_difference = x_difference_o * self.mToPx(
-            FIELD.robot.length / 2) / distance
+            self.field.robot.length / 2) / distance
         y_difference = y_difference_o * self.mToPx(
-            FIELD.robot.length / 2) / distance
+            self.field.robot.length / 2) / distance
 
         front_middle = []
         front_middle.append(p1[0] + x_difference)
@@ -118,8 +122,10 @@
         slope = [-(1 / x_difference_o) / (1 / y_difference_o)]
         angle = np.arctan(slope)
 
-        x_difference = np.sin(angle[0]) * self.mToPx(FIELD.robot.width / 2)
-        y_difference = np.cos(angle[0]) * self.mToPx(FIELD.robot.width / 2)
+        x_difference = np.sin(angle[0]) * self.mToPx(
+            self.field.robot.width / 2)
+        y_difference = np.cos(angle[0]) * self.mToPx(
+            self.field.robot.width / 2)
 
         front_1 = []
         front_1.append(front_middle[0] - x_difference)
@@ -138,9 +144,9 @@
         back_2.append(back_middle[1] + y_difference)
 
         x_difference = x_difference_o * self.mToPx(
-            FIELD.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance
+            self.field.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance
         y_difference = y_difference_o * self.mToPx(
-            FIELD.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance
+            self.field.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance
 
         #Calculate Ball
         ball_center = []
@@ -148,9 +154,9 @@
         ball_center.append(p1[1] + y_difference)
 
         x_difference = x_difference_o * self.mToPx(
-            FIELD.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance
+            self.field.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance
         y_difference = y_difference_o * self.mToPx(
-            FIELD.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance
+            self.field.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance
 
         #Calculate Panel
         panel_center = []
@@ -194,9 +200,6 @@
         cr.set_source_rgba(0, 0, 0, 1)
 
     def do_draw(self, cr):  # main
-
-        start_time = time.perf_counter()
-
         cr.set_matrix(self.transform.multiply(cr.get_matrix()))
 
         cr.save()
@@ -204,15 +207,16 @@
         set_color(cr, palette["BLACK"])
 
         cr.set_line_width(1.0)
-        cr.rectangle(0, 0, self.mToPx(FIELD.width), self.mToPx(FIELD.length))
+        cr.rectangle(0, 0, self.mToPx(self.field.width),
+                     self.mToPx(self.field.length))
         cr.set_line_join(cairo.LINE_JOIN_ROUND)
         cr.stroke()
 
         if self.field_png:
             cr.save()
             cr.scale(
-                self.mToPx(FIELD.width) / self.field_png.get_width(),
-                self.mToPx(FIELD.length) / self.field_png.get_height(),
+                self.mToPx(self.field.width) / self.field_png.get_width(),
+                self.mToPx(self.field.length) / self.field_png.get_height(),
             )
             cr.set_source_surface(self.field_png)
             cr.paint()
@@ -281,7 +285,7 @@
         self.path_to_export = os.path.join(
             self.module_path,  # position of the python
             "../../..",  # root of the repository
-            get_json_folder(FIELD),  # path from the root
+            get_json_folder(self.field),  # path from the root
             file_name  # selected file
         )
 
@@ -295,7 +299,7 @@
         self.path_to_export = os.path.join(
             self.module_path,  # position of the python
             "../../..",  # root of the repository
-            get_json_folder(FIELD),  # path from the root
+            get_json_folder(self.field),  # path from the root
             file_name  # selected file
         )