-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
executable file
·165 lines (140 loc) · 5.51 KB
/
graph.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python
import urwid
class GraphView(urwid.WidgetWrap):
"""
A class responsible for providing the application's interface and
graph display.
"""
palette = [
('body', 'black', 'light gray', 'standout'),
('header', 'white', 'dark red', 'bold'),
('screen edge', 'light blue', 'dark cyan'),
('main shadow', 'dark gray', 'black'),
('line', 'black', 'light gray', 'standout'),
('bg background','light gray', 'black'),
('bg 1', 'black', 'dark blue', 'standout'),
('bg 1 smooth', 'dark blue', 'black'),
('bg 2', 'black', 'dark cyan', 'standout'),
('bg 2 smooth', 'dark cyan', 'black'),
('button normal','light gray', 'dark blue', 'standout'),
('button select','white', 'dark green'),
('line', 'black', 'light gray', 'standout'),
('pg normal', 'white', 'black', 'standout'),
('pg complete', 'white', 'dark magenta'),
('pg smooth', 'dark magenta','black')
]
def __init__(self, controller):
self.controller = controller
urwid.WidgetWrap.__init__(self, self.main_window())
def update_graph(self, force_update=False):
return True
def on_mode_button(self, button, state):
"""Notify the controller of a new mode setting."""
if state:
# The new mode is the label of the button
self.controller.set_mode( button.get_label() )
def on_mode_change(self, m):
"""Handle external mode change by updating radio buttons."""
for rb in self.mode_buttons:
if rb.get_label() == m:
rb.set_state(True, do_callback=False)
break
def on_unicode_checkbox(self, w, state):
self.graph = self.bar_graph( state )
self.graph_wrap._w = self.graph
self.update_graph( True )
def main_shadow(self, w):
"""Wrap a shadow and background around widget w."""
bg = urwid.AttrWrap(urwid.SolidFill(u"\u2592"), 'screen edge')
shadow = urwid.AttrWrap(urwid.SolidFill(u" "), 'main shadow')
bg = urwid.Overlay( shadow, bg,
('fixed left', 3), ('fixed right', 1),
('fixed top', 2), ('fixed bottom', 1))
w = urwid.Overlay( w, bg,
('fixed left', 2), ('fixed right', 3),
('fixed top', 1), ('fixed bottom', 2))
return w
def bar_graph(self, smooth=False):
satt = None
if smooth:
satt = {(1,0): 'bg 1 smooth', (2,0): 'bg 2 smooth'}
w = urwid.BarGraph(['bg background','bg 1','bg 2'], satt=satt)
return w
def button(self, t, fn):
w = urwid.Button(t, fn)
w = urwid.AttrWrap(w, 'button normal', 'button select')
return w
def radio_button(self, g, l, fn):
w = urwid.RadioButton(g, l, False, on_state_change=fn)
w = urwid.AttrWrap(w, 'button normal', 'button select')
return w
def exit_program(self, w):
raise urwid.ExitMainLoop()
def graph_controls(self):
modes = self.controller.get_modes()
self.mode_buttons = []
group = []
for m in modes:
rb = self.radio_button( group, m, self.on_mode_button )
self.mode_buttons.append( rb )
if urwid.get_encoding_mode() == "utf8":
unicode_checkbox = urwid.CheckBox(
"Enable Unicode Graphics",
on_state_change=self.on_unicode_checkbox)
else:
unicode_checkbox = urwid.Text(
"UTF-8 encoding not detected")
l = [ urwid.Text("Mode",align="center"),
] + self.mode_buttons + [
urwid.Divider(),
urwid.Text("Animation",align="center"),
urwid.Divider(),
urwid.LineBox( unicode_checkbox ),
urwid.Divider(),
self.button("Quit", self.exit_program ),
]
w = urwid.ListBox(urwid.SimpleListWalker(l))
return w
def main_window(self):
self.graph = self.bar_graph()
self.graph_wrap = urwid.WidgetWrap( self.graph )
vline = urwid.AttrWrap( urwid.SolidFill(u'\u2502'), 'line')
c = self.graph_controls()
w = urwid.Columns([('weight',2,self.graph_wrap),
('fixed',1,vline), c],
dividechars=1, focus_column=2)
w = urwid.Padding(w,('fixed left',1),('fixed right',0))
w = urwid.AttrWrap(w,'body')
w = urwid.LineBox(w)
w = urwid.AttrWrap(w,'line')
w = self.main_shadow(w)
return w
class GraphController:
"""
A class responsible for setting up the model and view and running
the application.
"""
def __init__(self):
self.view = GraphView( self )
# use the first mode as the default
mode = self.get_modes()[0]
# update the view
self.view.on_mode_change( mode )
self.view.update_graph(True)
def get_modes(self):
"""Allow our view access to the list of modes."""
return ["limit", "market"]
def set_mode(self, m):
"""Allow our view to set the mode."""
self.view.update_graph(True)
return m
def get_data(self, offset, range):
"""Provide data to our view for the graph."""
return self.model.get_data( offset, range )
def main(self):
self.loop = urwid.MainLoop(self.view, self.view.palette)
self.loop.run()
def main():
GraphController().main()
if '__main__'==__name__:
main()