-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
141 lines (110 loc) · 4.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Color, Rectangle
from googlemaps import *
import core
class Input(GridLayout):
def __init__(self, **kwargs):
super(Input, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text='Starting Address'))
self.start = TextInput(multiline=False)
self.add_widget(self.start)
self.add_widget(Label(text='Destination Address'))
self.end = TextInput(multiline=False)
self.add_widget(self.end)
def check_valid(self):
if len(self.start.text + self.end.text) > 0:
return True
else:
return False
def clear(self):
self.start.text = ""
self.end.text = ""
def get_waypoints(self):
return [self.start.text, self.end.text]
class Output(BoxLayout):
def __init__(self, **kwargs):
super(Output, self).__init__(**kwargs)
self.orientation = "vertical"
def test(self):
self.add_widget(Label(text="This worked!"))
def get_route(self, waypoints):
data = core.find_route(waypoints)
info = data.get("info")
for line in info:
self.add_widget(Label(text=line))
steps = data.get("steps")
for step in steps:
box = BoxLayout(orientation="horizontal")
textbox = BoxLayout(orientation='vertical')
raw_text = step.get("text")
if "font-size" in raw_text:
raw_text = raw_text.replace('</div>', '')
str = raw_text.split('<div style="font-size:0.9em">')
text = Label(text=str[0])
subtext = Label(text=str[1], font_size=10)
textbox.add_widget(text)
textbox.add_widget(subtext)
else:
text = Label(text=raw_text)
textbox.add_widget(text)
box.add_widget(textbox)
distance = Label(text=step.get("distance"))
box.add_widget(distance)
self.add_widget(box)
class InterfaceManager(BoxLayout):
def __init__(self, **kwargs):
super(InterfaceManager, self).__init__(**kwargs)
with self.canvas.before:
Color(0.2, 0.2, 0.2, 1)
self.rect = Rectangle(size=self.size,
pos=self.pos)
def update_rect(instance, value):
instance.rect.pos = instance.pos
instance.rect.size = instance.size
# listen to size and position changes
self.bind(pos=update_rect, size=update_rect)
title = Label(text="SimpleDirections")
input = Input()
submit = Button(text="Submit", size_hint=(0.2, 0.2), halign="center")
submit.bind(on_press=self.show_result)
self.title = title
self.input = input
self.submit = submit
reset = Button(text="Reset", size_hint=(0.2, 0.2))
reset.bind(on_press=self.show_home)
self.reset = reset
output = Output()
self.output = output
self.add_widget(title)
self.add_widget(input)
self.add_widget(submit)
def show_result(self, button):
if self.input.check_valid() == False:
return
try:
self.output.get_route(self.input.get_waypoints())
except googlemaps.exceptions.TransportError as e:
print(e)
return
self.clear_widgets()
self.add_widget(self.output)
self.add_widget(self.reset)
def show_home(self, button):
self.clear_widgets()
self.input.clear()
self.output.clear_widgets()
self.add_widget(self.title)
self.add_widget(self.input)
self.add_widget(self.submit)
class Directions(App):
def build(self):
return InterfaceManager(orientation="vertical")
if __name__ == '__main__':
Directions().run()