John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | import gi |
| 3 | from path_edit import * |
| 4 | import numpy as np |
| 5 | gi.require_version('Gtk', '3.0') |
| 6 | from gi.repository import Gdk, Gtk, GLib |
| 7 | import cairo |
| 8 | |
| 9 | class GridWindow(Gtk.Window): |
| 10 | def method_connect(self, event, cb): |
| 11 | def handler(obj, *args): |
| 12 | cb(*args) |
| 13 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 14 | self.connect(event, handler) |
| 15 | |
| 16 | def mouse_move(self, event): |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 17 | # Changes event.x and event.y to be relative to the center. |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 18 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 19 | y = self.drawing_area.window_shape[1] / 2 - event.y |
| 20 | scale = self.drawing_area.get_current_scale() |
| 21 | event.x = x / scale + self.drawing_area.center[0] |
| 22 | event.y = y / scale + self.drawing_area.center[1] |
| 23 | self.drawing_area.mouse_move(event) |
| 24 | self.queue_draw() |
| 25 | |
| 26 | def button_press(self, event): |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 27 | original_x = event.x |
| 28 | original_y = event.y |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 29 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 30 | y = self.drawing_area.window_shape[1] / 2 - event.y |
| 31 | scale = 2 * self.drawing_area.get_current_scale() |
| 32 | event.x = x / scale + self.drawing_area.center[0] |
| 33 | event.y = y / scale + self.drawing_area.center[1] |
| 34 | self.drawing_area.do_button_press(event) |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 35 | event.x = original_x |
| 36 | event.y = original_y |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 37 | |
| 38 | def key_press(self, event): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 39 | self.drawing_area.do_key_press(event, self.file_name_box.get_text()) |
| 40 | self.queue_draw() |
| 41 | |
| 42 | def configure(self, event): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 43 | self.drawing_area.window_shape = (event.width, event.height) |
| 44 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 45 | def output_json_clicked(self, button): |
| 46 | print("OUTPUT JSON CLICKED") |
| 47 | self.drawing_area.export_json(self.file_name_box.get_text()) |
| 48 | |
| 49 | def input_json_clicked(self, button): |
| 50 | print("INPUT JSON CLICKED") |
| 51 | self.drawing_area.import_json(self.file_name_box.get_text()) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 52 | |
| 53 | def __init__(self): |
| 54 | Gtk.Window.__init__(self) |
| 55 | |
| 56 | self.set_default_size(1.5 * SCREEN_SIZE, SCREEN_SIZE) |
| 57 | |
| 58 | flowBox = Gtk.FlowBox() |
| 59 | flowBox.set_valign(Gtk.Align.START) |
| 60 | flowBox.set_selection_mode(Gtk.SelectionMode.NONE) |
| 61 | |
| 62 | flowBox.set_valign(Gtk.Align.START) |
| 63 | |
| 64 | self.add(flowBox) |
| 65 | |
| 66 | container = Gtk.Fixed() |
| 67 | flowBox.add(container) |
| 68 | |
| 69 | self.eventBox = Gtk.EventBox() |
| 70 | container.add(self.eventBox) |
| 71 | |
| 72 | self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
| 73 | | Gdk.EventMask.BUTTON_RELEASE_MASK |
| 74 | | Gdk.EventMask.POINTER_MOTION_MASK |
| 75 | | Gdk.EventMask.SCROLL_MASK |
| 76 | | Gdk.EventMask.KEY_PRESS_MASK) |
| 77 | |
| 78 | #add the graph box |
| 79 | self.drawing_area = GTK_Widget() |
| 80 | self.eventBox.add(self.drawing_area) |
| 81 | |
| 82 | self.method_connect("key-release-event", self.key_press) |
| 83 | self.method_connect("button-release-event", self.button_press) |
| 84 | self.method_connect("configure-event", self.configure) |
| 85 | self.method_connect("motion_notify_event", self.mouse_move) |
| 86 | |
| 87 | self.file_name_box = Gtk.Entry() |
| 88 | self.file_name_box.set_size_request(200, 40) |
| 89 | |
James Kuszmaul | b2b89b2 | 2020-02-29 13:27:05 -0800 | [diff] [blame] | 90 | self.file_name_box.set_text("output_file_name.json") |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 91 | self.file_name_box.set_editable(True) |
| 92 | |
| 93 | container.put(self.file_name_box, 0, 0) |
| 94 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 95 | self.output_json = Gtk.Button.new_with_label("Output") |
| 96 | self.output_json.set_size_request(100, 40) |
| 97 | self.output_json.connect("clicked", self.output_json_clicked) |
| 98 | |
| 99 | self.input_json = Gtk.Button.new_with_label("Import") |
| 100 | self.input_json.set_size_request(100, 40) |
| 101 | self.input_json.connect("clicked", self.input_json_clicked) |
| 102 | |
| 103 | container.put(self.output_json, 210, 0) |
| 104 | container.put(self.input_json, 320, 0) |
| 105 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 106 | self.show_all() |
| 107 | |
| 108 | |
| 109 | window = GridWindow() |
| 110 | RunApp() |