-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuiTest.py
190 lines (159 loc) · 6.47 KB
/
uiTest.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
from asciimatics.widgets import Frame, ListBox, Layout, Divider, Text, \
Button, TextBox, Widget
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError, NextScene, StopApplication
import sys
import sqlite3
class ContactModel():
def __init__(self):
# Create a database in RAM.
self._db = sqlite3.connect(':memory:')
self._db.row_factory = sqlite3.Row
# Create the basic contact table.
self._db.cursor().execute('''
CREATE TABLE contacts(
id INTEGER PRIMARY KEY,
name TEXT,
phone TEXT,
address TEXT,
email TEXT,
notes TEXT)
''')
self._db.commit()
# Current contact when editing.
self.current_id = None
def add(self, contact):
self._db.cursor().execute('''
INSERT INTO contacts(name, phone, address, email, notes)
VALUES(:name, :phone, :address, :email, :notes)''',
contact)
self._db.commit()
def get_summary(self):
return self._db.cursor().execute(
"SELECT name, id from contacts").fetchall()
def get_contact(self, contact_id):
return self._db.cursor().execute(
"SELECT * from contacts WHERE id=:id", {"id": contact_id}).fetchone()
def get_current_contact(self):
if self.current_id is None:
return {"name": "", "address": "", "phone": "", "email": "", "notes": ""}
else:
return self.get_contact(self.current_id)
def update_current_contact(self, details):
if self.current_id is None:
self.add(details)
else:
self._db.cursor().execute('''
UPDATE contacts SET name=:name, phone=:phone, address=:address,
email=:email, notes=:notes WHERE id=:id''',
details)
self._db.commit()
def delete_contact(self, contact_id):
self._db.cursor().execute('''
DELETE FROM contacts WHERE id=:id''', {"id": contact_id})
self._db.commit()
class ListView(Frame):
def __init__(self, screen, model):
super(ListView, self).__init__(screen,
screen.height * 2 // 3,
screen.width * 2 // 3,
on_load=self._reload_list,
hover_focus=True,
can_scroll=False,
title="Contact List")
# Save off the model that accesses the contacts database.
self._model = model
# Create the form for displaying the list of contacts.
self._list_view = ListBox(
Widget.FILL_FRAME,
model.get_summary(),
name="contacts",
add_scroll_bar=True,
on_change=self._on_pick,
on_select=self._edit)
self._edit_button = Button("Edit", self._edit)
self._delete_button = Button("Delete", self._delete)
layout = Layout([100], fill_frame=True)
self.add_layout(layout)
layout.add_widget(self._list_view)
layout.add_widget(Divider())
layout2 = Layout([1, 1, 1, 1])
self.add_layout(layout2)
layout2.add_widget(Button("Add", self._add), 0)
layout2.add_widget(self._edit_button, 1)
layout2.add_widget(self._delete_button, 2)
layout2.add_widget(Button("Quit", self._quit), 3)
self.fix()
self._on_pick()
def _on_pick(self):
self._edit_button.disabled = self._list_view.value is None
self._delete_button.disabled = self._list_view.value is None
def _reload_list(self, new_value=None):
self._list_view.options = self._model.get_summary()
self._list_view.value = new_value
def _add(self):
self._model.current_id = None
raise NextScene("Edit Contact")
def _edit(self):
self.save()
self._model.current_id = self.data["contacts"]
raise NextScene("Edit Contact")
def _delete(self):
self.save()
self._model.delete_contact(self.data["contacts"])
self._reload_list()
@staticmethod
def _quit():
raise StopApplication("User pressed quit")
class ContactView(Frame):
def __init__(self, screen, model):
super(ContactView, self).__init__(screen,
screen.height * 2 // 3,
screen.width * 2 // 3,
hover_focus=True,
can_scroll=False,
title="Contact Details",
reduce_cpu=True)
# Save off the model that accesses the contacts database.
self._model = model
# Create the form for displaying the list of contacts.
layout = Layout([100], fill_frame=True)
self.add_layout(layout)
layout.add_widget(Text("Name:", "name"))
layout.add_widget(Text("Address:", "address"))
layout.add_widget(Text("Phone number:", "phone"))
layout.add_widget(Text("Email address:", "email"))
layout.add_widget(TextBox(
Widget.FILL_FRAME, "Notes:", "notes", as_string=True, line_wrap=True))
layout2 = Layout([1, 1, 1, 1])
self.add_layout(layout2)
layout2.add_widget(Button("OK", self._ok), 0)
layout2.add_widget(Button("Cancel", self._cancel), 3)
self.fix()
def reset(self):
# Do standard reset to clear out form, then populate with new data.
super(ContactView, self).reset()
self.data = self._model.get_current_contact()
def _ok(self):
self.save()
self._model.update_current_contact(self.data)
raise NextScene("Main")
@staticmethod
def _cancel():
raise NextScene("Main")
def demo(screen, scene):
scenes = [
Scene([ListView(screen, contacts)], -1, name="Main"),
Scene([ContactView(screen, contacts)], -1, name="Edit Contact")
]
screen.play(scenes, stop_on_resize=True, start_scene=scene, allow_int=True)
contacts = ContactModel()
last_scene = None
while True:
try:
Screen.wrapper(demo, catch_interrupt=True, arguments=[last_scene])
sys.exit(0)
except ResizeScreenError as e:
last_scene = e.scene