blob: 7885ec1c2c23aeba3ab53f06fa8a5eb3eb362b8c [file] [log] [blame]
John Park91e69732019-03-03 13:12:43 -08001#!/usr/bin/python3
2import gi
3from path_edit import *
4import numpy as np
5gi.require_version('Gtk', '3.0')
6from gi.repository import Gdk, Gtk, GLib
7import cairo
8
9class GridWindow(Gtk.Window):
10 def method_connect(self, event, cb):
11 def handler(obj, *args):
12 cb(*args)
13
John Park91e69732019-03-03 13:12:43 -080014 self.connect(event, handler)
15
16 def mouse_move(self, event):
James Kuszmaulb2b89b22020-02-29 13:27:05 -080017 # Changes event.x and event.y to be relative to the center.
John Park91e69732019-03-03 13:12:43 -080018 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 Kuszmaulb2b89b22020-02-29 13:27:05 -080027 original_x = event.x
28 original_y = event.y
John Park91e69732019-03-03 13:12:43 -080029 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 Kuszmaulb2b89b22020-02-29 13:27:05 -080035 event.x = original_x
36 event.y = original_y
John Park91e69732019-03-03 13:12:43 -080037
38 def key_press(self, event):
John Park91e69732019-03-03 13:12:43 -080039 self.drawing_area.do_key_press(event, self.file_name_box.get_text())
40 self.queue_draw()
41
42 def configure(self, event):
John Park91e69732019-03-03 13:12:43 -080043 self.drawing_area.window_shape = (event.width, event.height)
44
John Park909c0392020-03-05 23:56:30 -080045 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 Park91e69732019-03-03 13:12:43 -080052
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 Kuszmaulb2b89b22020-02-29 13:27:05 -080090 self.file_name_box.set_text("output_file_name.json")
John Park91e69732019-03-03 13:12:43 -080091 self.file_name_box.set_editable(True)
92
93 container.put(self.file_name_box, 0, 0)
94
John Park909c0392020-03-05 23:56:30 -080095 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 Park91e69732019-03-03 13:12:43 -0800106 self.show_all()
107
108
109window = GridWindow()
110RunApp()