-
Notifications
You must be signed in to change notification settings - Fork 11
/
todogui.py
executable file
·229 lines (179 loc) · 5.62 KB
/
todogui.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
from todolist import *
import glob
import os
import sys
import pygtk
pygtk.require('2.0')
import gtk
import pango
json_folder = os.path.expanduser('~') + '/.todopy/'
class TodoGUI:
lists = {}
def __init__(self):
"""
Creates the GUI display.
"""
# Initialise default list.
# TODO: Support multiple lists
self.todolist = TodoList(json_folder + 'todo.json')
# Start of multiple lists support. Currently unused
for infile in glob.glob( os.path.join(json_folder, '*.json') ):
listname = infile.replace(json_folder, '').replace('.json', '')
self.lists[listname] = TodoList(infile)
print 'Found:', infile.replace(json_folder, '').replace('.json', '')
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title('Simple Python Todo')
send_button = gtk.Button(None, gtk.STOCK_ADD)
send_button.connect('clicked', self.send_button_clicked)
vbox = gtk.VBox(False, 10)
vbox.set_border_width(10)
hbox_note_area = gtk.HBox(False, 0)
hbox_send_area = gtk.HBox(False, 0)
self.current_list = ListPanel(self.todolist)
sw_display = self.current_list.sw
hbox_note_area.pack_start(sw_display)
# Set up the text view for adding new notes
sw_add = gtk.ScrolledWindow()
sw_add.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.textview_add = self.create_text_view(230, 80)
self.textview_add.connect('key_press_event', self.textbox_key_pressed)
sw_add.add(self.textview_add)
hbox_send_area.pack_start(sw_add)
hbox_send_area.pack_start(send_button)
# Organise the boxes
vbox.pack_start(hbox_note_area)
hbox_note_area.show()
vbox.pack_start(hbox_send_area)
hbox_send_area.show()
self.window.add(vbox)
self.window.connect('delete_event', self.delete_event)
self.window.show_all()
def main(self):
"""
Starts the Todo GUI
"""
gtk.main()
def delete_event(self, widget, event, Data=None):
"""
Saves the list and closes the app.
"""
self.todolist.save()
gtk.main_quit()
return False
def textbox_key_pressed(self, widget, event, Data=None):
"""
Checks if enter was pressed, and if so, adds a new item
"""
if event.keyval == gtk.gdk.keyval_from_name('Return') or \
event.keyval == gtk.gdk.keyval_from_name('KP_Enter'):
self.add_item_to_list(self.current_list)
return True
def send_button_clicked(self, widget, event, Data=None):
"""
Adds a new item when the send button is clicked.
"""
self.add_item_to_list(self.current_list)
return True
def add_item_to_list(self, todolist):
"""
Copies the text from the entry box to a todolist.
"""
note = self.get_all_text_view_text(self.textview_add)
todolist.add_item(note)
self.textview_add.get_buffer().set_text('')
def create_text_view(self, width=200, height=200, editable=True, wrap=True):
"""
Creates a text view with certain settings.
"""
textview = gtk.TextView()
textview.set_size_request(width, height)
if wrap is True:
textview.set_wrap_mode(gtk.WRAP_WORD)
textview.set_editable(editable)
# Don't show cursor in uneditable text views.
textview.set_cursor_visible(editable)
return textview
def get_all_text_view_text(self, textview):
"""
Gets all the text from a text view.
"""
textbuffer = textview.get_buffer()
startiter, enditer = textbuffer.get_bounds()
text = startiter.get_slice(enditer)
return text
class ListPanel:
def __init__(self, todolist):
"""
Set up the tree view for showing a todo list.
"""
self.todolist = todolist
self.selection_id = None;
self.store = self.create_model(todolist)
self.display_view = gtk.TreeView(self.store)
self.display_view.set_size_request(280, 200)
self.display_view.set_rules_hint(True)
self.display_view.connect('key_press_event', self.key_pressed)
selection = self.display_view.get_selection()
selection.connect("changed", self.selection_changed)
selection.set_mode(gtk.SELECTION_SINGLE)
self.create_columns(self.display_view)
self.sw = gtk.ScrolledWindow()
self.sw.add(self.display_view)
self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
def create_model(self, todolist):
"""
Sets up the store for the TreeView
"""
store = gtk.ListStore(int, str)
for todo in todolist:
new_row = (todo.id, todo.text)
print new_row
store.append(new_row)
return store
def create_columns(self, tree_view):
"""
Sets up the columns for the TreeView
"""
renderer_text = gtk.CellRendererText()
renderer_text.props.wrap_width = 280
renderer_text.props.wrap_mode = pango.WRAP_WORD
column = gtk.TreeViewColumn('Text', renderer_text, text=1)
column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
column.set_sort_column_id(0)
tree_view.append_column(column)
def add_item(self, text):
"""
Adds an item to the todo list.
"""
new_todo = self.todolist.add(text)
self.store.append((new_todo.id, text))
def remove_item(self, item_id):
"""
Remove an item from the list, and the display.
"""
self.todolist.remove(item_id)
store = self.store
for row in store:
if row[0] == item_id:
store.remove(row.iter)
break
def key_pressed(self, widget, event, Data=None):
"""
Deletes the item from the list if the delete key is pressed.
"""
if event.keyval == gtk.gdk.keyval_from_name('Delete') and self.selection_id is not None:
self.remove_item(self.selection_id)
self.selection_id = None;
return True
def selection_changed(self, selection):
"""
Sets the currently selected items ID
"""
store, titer = selection.get_selected()
if titer is not None:
print store.get_path(titer)
for item in store:
print item[0], item[1]
self.selection_id = store.get_value(titer, 0)
TodoGUI().main()