Make the Spline UI widgets more widgety

Change-Id: I89d9ad7b795cef716f6da55414175acbf49c1855
Signed-off-by: Ravago Jones <ravagojones@gmail.com>
diff --git a/frc971/control_loops/python/spline_graph.py b/frc971/control_loops/python/spline_graph.py
index 347837b..ad3ae96 100755
--- a/frc971/control_loops/python/spline_graph.py
+++ b/frc971/control_loops/python/spline_graph.py
@@ -1,12 +1,16 @@
 #!/usr/bin/python3
+
+# matplotlib overrides fontconfig locations, so has to be imported before gtk
+import matplotlib
 import gi
-from path_edit import *
-import numpy as np
+from path_edit import FieldWidget
+from basic_window import RunApp
+from constants import FIELDS, FIELD, SCREEN_SIZE
 gi.require_version('Gtk', '3.0')
 from gi.repository import Gdk, Gtk, GLib
 import cairo
 import basic_window
-
+import os
 
 class GridWindow(Gtk.Window):
     def method_connect(self, event, cb):
@@ -16,99 +20,68 @@
         self.connect(event, handler)
 
     def mouse_move(self, event):
-        # Changes event.x and event.y to be relative to the center.
-        x = event.x - self.drawing_area.window_shape[0] / 2
-        y = self.drawing_area.window_shape[1] / 2 - event.y
-        scale = self.drawing_area.get_current_scale()
-        event.x = x / scale + self.drawing_area.center[0]
-        event.y = y / scale + self.drawing_area.center[1]
-        self.drawing_area.mouse_move(event)
+        self.field.mouse_move(event)
         self.queue_draw()
 
     def button_press(self, event):
-        original_x = event.x
-        original_y = event.y
-        x = event.x - self.drawing_area.window_shape[0] / 2
-        y = self.drawing_area.window_shape[1] / 2 - event.y
-        scale = 2 * self.drawing_area.get_current_scale()
-        event.x = x / scale + self.drawing_area.center[0]
-        event.y = y / scale + self.drawing_area.center[1]
-        self.drawing_area.do_button_press(event)
-        event.x = original_x
-        event.y = original_y
+        self.field.button_press(event)
 
     def key_press(self, event):
-        self.drawing_area.do_key_press(event, self.file_name_box.get_text())
+        self.field.key_press(event, self.file_name_box.get_text())
         self.queue_draw()
 
     def configure(self, event):
-        self.drawing_area.window_shape = (event.width, event.height)
+        self.field.window_shape = (event.width, event.height)
 
     def output_json_clicked(self, button):
-        print("OUTPUT JSON CLICKED")
-        self.drawing_area.export_json(self.file_name_box.get_text())
+        self.field.export_json(self.file_name_box.get_text())
 
     def input_json_clicked(self, button):
-        print("INPUT JSON CLICKED")
-        self.drawing_area.import_json(self.file_name_box.get_text())
+        self.field.import_json(self.file_name_box.get_text())
         self.long_input.set_value(
-            self.drawing_area.points.getConstraint(
-                "LONGITUDINAL_ACCELERATION"))
+            self.field.points.getConstraint("LONGITUDINAL_ACCELERATION"))
         self.lat_input.set_value(
-            self.drawing_area.points.getConstraint("LATERAL_ACCELERATION"))
-        self.vol_input.set_value(
-            self.drawing_area.points.getConstraint("VOLTAGE"))
+            self.field.points.getConstraint("LATERAL_ACCELERATION"))
+        self.vol_input.set_value(self.field.points.getConstraint("VOLTAGE"))
 
     def long_changed(self, button):
         value = self.long_input.get_value()
-        self.drawing_area.points.setConstraint("LONGITUDINAL_ACCELERATION",
-                                               value)
+        self.field.points.setConstraint("LONGITUDINAL_ACCELERATION", value)
 
     def lat_changed(self, button):
         value = self.lat_input.get_value()
-        self.drawing_area.points.setConstraint("LATERAL_ACCELERATION", value)
+        self.field.points.setConstraint("LATERAL_ACCELERATION", value)
 
     def vel_changed(self, button):
         value = self.vel_input.get_value()
 
     def vol_changed(self, button):
         value = self.vol_input.get_value()
-        self.drawing_area.points.setConstraint("VOLTAGE", value)
+        self.field.points.setConstraint("VOLTAGE", value)
 
     def input_combobox_choice(self, combo):
         text = combo.get_active_text()
         if text is not None:
             print("Combo Clicked on: " + text)
-            set_field(text)
+            #set_field(text)
 
     def __init__(self):
         Gtk.Window.__init__(self)
 
         self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE)
 
-        flowBox = Gtk.FlowBox()
-        flowBox.set_valign(Gtk.Align.START)
-        flowBox.set_selection_mode(Gtk.SelectionMode.NONE)
-
-        flowBox.set_valign(Gtk.Align.START)
-
-        self.add(flowBox)
-
-        container = Gtk.Fixed()
-        flowBox.add(container)
+        container = Gtk.Grid()
+        container.set_vexpand(True)
+        self.add(container)
 
         self.eventBox = Gtk.EventBox()
-        container.add(self.eventBox)
-
         self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
                                  | Gdk.EventMask.BUTTON_RELEASE_MASK
                                  | Gdk.EventMask.POINTER_MOTION_MASK
                                  | Gdk.EventMask.SCROLL_MASK
                                  | Gdk.EventMask.KEY_PRESS_MASK)
 
-        #add the graph box
-        self.drawing_area = GTK_Widget()
-        self.eventBox.add(self.drawing_area)
+        self.field = FieldWidget()
 
         self.method_connect("delete-event", basic_window.quit_main_loop)
         self.method_connect("key-release-event", self.key_press)
@@ -118,12 +91,9 @@
 
         self.file_name_box = Gtk.Entry()
         self.file_name_box.set_size_request(200, 40)
-
         self.file_name_box.set_text(FIELD.field_id + ".json")
         self.file_name_box.set_editable(True)
 
-        container.put(self.file_name_box, 0, 0)
-
         self.long_input = Gtk.SpinButton()
         self.long_input.set_size_request(100, 20)
         self.long_input.set_numeric(True)
@@ -134,8 +104,7 @@
         self.long_label = Gtk.Label()
         self.long_label.set_text("Longitudinal Acceleration Restriction")
         self.long_input.set_value(
-            self.drawing_area.points.getConstraint(
-                "LONGITUDINAL_ACCELERATION"))
+            self.field.points.getConstraint("LONGITUDINAL_ACCELERATION"))
 
         self.lat_input = Gtk.SpinButton()
         self.lat_input.set_size_request(100, 20)
@@ -147,7 +116,7 @@
         self.lat_label = Gtk.Label()
         self.lat_label.set_text("Lateral Acceleration Restriction")
         self.lat_input.set_value(
-            self.drawing_area.points.getConstraint("LATERAL_ACCELERATION"))
+            self.field.points.getConstraint("LATERAL_ACCELERATION"))
 
         self.vel_input = Gtk.SpinButton()
         self.vel_input.set_size_request(100, 20)
@@ -170,19 +139,9 @@
         self.vol_input.connect("value-changed", self.vol_changed)
         self.vol_label = Gtk.Label()
         self.vol_label.set_text("Voltage Restriction")
-        self.vol_input.set_value(
-            self.drawing_area.points.getConstraint("VOLTAGE"))
+        self.vol_input.set_value(self.field.points.getConstraint("VOLTAGE"))
 
-        container.put(self.long_input, 0, 60)
-        container.put(self.lat_input, 0, 110)
-        container.put(self.vel_input, 0, 160)
-        container.put(self.vol_input, 0, 210)
-        container.put(self.long_label, 0, 40)
-        container.put(self.lat_label, 0, 90)
-        container.put(self.vel_label, 0, 140)
-        container.put(self.vol_label, 0, 190)
-
-        self.output_json = Gtk.Button.new_with_label("Output")
+        self.output_json = Gtk.Button.new_with_label("Export")
         self.output_json.set_size_request(100, 40)
         self.output_json.connect("clicked", self.output_json_clicked)
 
@@ -190,40 +149,57 @@
         self.input_json.set_size_request(100, 40)
         self.input_json.connect("clicked", self.input_json_clicked)
 
-        container.put(self.output_json, 210, 0)
-        container.put(self.input_json, 320, 0)
-
-
         #Dropdown feature
-        self.label = Gtk.Label("Change Map:")
-        self.label.set_size_request(100,40)
-        container.put(self.label,430,0)
+        self.label = Gtk.Label()
+        self.label.set_text("Change Field:")
+        self.label.set_size_request(100, 40)
 
         game_store = Gtk.ListStore(str)
-        games = [
-           "2020 Field",
-           "2019 Field",
-           "2021 Galactic Search ARed",
-           "2021 Galactic Search ABlue",
-           "2021 Galactic Search BRed",
-           "2021 Galactic Search BBlue",
-           "2021 AutoNav Barrel Racing",
-           "2021 AutoNav Slalom",
-           "2021 AutoNav Bounce",
-           ]
 
         self.game_combo = Gtk.ComboBoxText()
         self.game_combo.set_entry_text_column(0)
         self.game_combo.connect("changed", self.input_combobox_choice)
 
-        for game in games:
-          self.game_combo.append_text(game)
+        for game in FIELDS.keys():
+            self.game_combo.append_text(game)
 
         self.game_combo.set_active(0)
-        self.game_combo.set_size_request(100,40)
-        container.put(self.game_combo,440,30)
+        self.game_combo.set_size_request(100, 40)
+
+        limitControls = Gtk.FlowBox()
+        limitControls.set_min_children_per_line(1)
+        limitControls.set_max_children_per_line(2)
+        limitControls.add(self.long_label)
+        limitControls.add(self.long_input)
+
+        limitControls.add(self.lat_label)
+        limitControls.add(self.lat_input)
+
+        limitControls.add(self.vel_label)
+        limitControls.add(self.vel_input)
+
+        limitControls.add(self.vol_label)
+        limitControls.add(self.vol_input)
+
+        container.attach(limitControls, 5, 1, 1, 1)
+
+        jsonControls = Gtk.FlowBox()
+        jsonControls.set_min_children_per_line(3)
+        jsonControls.add(self.file_name_box)
+        jsonControls.add(self.output_json)
+        jsonControls.add(self.input_json)
+        container.attach(jsonControls, 1, 0, 1, 1)
+
+        container.attach(self.label, 4, 0, 1, 1)
+        container.attach(self.game_combo, 5, 0, 1, 1)
+
+        self.eventBox.add(self.field)
+        container.attach(self.eventBox, 1, 1, 4, 4)
+
+        container.attach(self.field.graph, 0, 10, 10, 1)
 
         self.show_all()
 
+
 window = GridWindow()
 RunApp()