-
Notifications
You must be signed in to change notification settings - Fork 3
/
calendario.py
357 lines (311 loc) · 13.6 KB
/
calendario.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010-2012, Yader Velasquez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import re
from database import DataBase
from model import ComboBoxModel, TasksModel
from sugar3.activity import activity, widgets
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from gettext import gettext as _
from datetime import date, datetime
import logging
from gi.repository import Pango
log = logging.getLogger('Calendario-Log')
class CalendarioActivity(activity.Activity):
def __init__(self, handle):
'''Constructor'''
activity.Activity.__init__(self, handle)
self.main_container = Gtk.HBox()
toolbox = ToolbarBox()
self.set_toolbar_box(toolbox)
toolbox.show()
activity_button = ActivityToolbarButton(self)
toolbox.toolbar.insert(activity_button, -1)
activity_button.show()
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
separator.set_expand(True)
toolbox.toolbar.insert(separator, -1)
separator.show()
stop_button = StopButton(self)
toolbox.toolbar.insert(stop_button, -1)
stop_button.show()
self.show_all()
self.path = self.get_activity_root()
###left side###
self.left_container = Gtk.VBox()
d = datetime.today()
self.label_date = Gtk.Label(label=d.strftime("%d %B %Y"))
self.calendar = Gtk.Calendar()
self.calendar.connect('day-selected', self._day_selected_cb)
self.calendar.connect('month-changed', self._mark_day_cb)
self.calendar.connect('next-year', self._mark_day_cb)
self.calendar.connect('prev-year', self._mark_day_cb)
self.mark_day()
self.tool_frame = Gtk.Frame(label=_("Tools"))
self.reminder_expander = Gtk.Expander(label=_("Tasks reminder"))
self.query_expander = Gtk.Expander(label=_("View by"))
self.tool_box = Gtk.VBox()
#reminder
self.scroll_reminder = Gtk.ScrolledWindow()
self.scroll_reminder.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.scroll_reminder.set_size_request(-1, 170)
self.reminder_list = Gtk.TreeView()
self.scroll_reminder.add(self.reminder_list)
self.reminder_expander.add(self.scroll_reminder)
#query
self.query_box = Gtk.VBox()
self.label_q_priority = Gtk.Label(label=_("Priority"))
self.label_q_category = Gtk.Label(label=_("Category"))
comboBoxModel = ComboBoxModel()
cat_model = comboBoxModel.get_category_model()
pri_model = comboBoxModel.get_priority_model()
cell = Gtk.CellRendererText()
self.combobox_q_priority = Gtk.ComboBox()
self.combobox_q_priority.pack_start(cell, True)
self.combobox_q_priority.add_attribute(cell, 'text', 0)
self.combobox_q_category = Gtk.ComboBox()
self.combobox_q_category.pack_start(cell, True)
self.combobox_q_category.add_attribute(cell, 'text', 0)
self.combobox_q_category.set_model(cat_model)
self.combobox_q_priority.set_model(pri_model)
self.filter_button = Gtk.Button(_('Filter'))
self.filter_button.connect('clicked', self._filter_query_cb)
self.query_box.pack_start(self.label_q_priority, False, False, 0)
self.query_box.pack_start(self.combobox_q_priority, False, False, 0)
self.query_box.pack_start(self.label_q_category, False, False, 0)
self.query_box.pack_start(self.combobox_q_category, False, False, 0)
self.query_box.pack_start(self.filter_button, True, True, 5)
self.query_expander.add(self.query_box)
self.tool_box.pack_start(self.reminder_expander, False, False, 0)
self.tool_box.pack_start(self.query_expander, False, False, 0)
self.tool_frame.add(self.tool_box)
self.left_container.pack_start(self.label_date, False, False, 5)
self.left_container.pack_start(self.calendar, False, False, 0)
self.left_container.pack_start(self.tool_frame, True, True, 5)
###right side###
self.right_container = Gtk.VBox()
self.tasks_frame = Gtk.Frame(label=_("Tasks list"))
self.scroll_tasks = Gtk.ScrolledWindow()
self.scroll_tasks.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.tasks_list = Gtk.TreeView()
self.get_tasks()
self.add_columns()
self.scroll_tasks.add(self.tasks_list)
self.tasks_frame.add(self.scroll_tasks)
#options
self.options_expander = Gtk.Expander(label=_("Options"))
self.options_box = Gtk.HBox()
self.label_b_priority = Gtk.Label(label=_("Priority"))
self.combobox_priority = Gtk.ComboBox()
self.combobox_priority.pack_start(cell, True)
self.combobox_priority.add_attribute(cell, 'text', 0)
self.label_b_category = Gtk.Label(label=_("Category"))
self.combobox_category = Gtk.ComboBox()
self.combobox_category.pack_start(cell, True)
self.combobox_category.add_attribute(cell, 'text', 0)
self.combobox_category.set_model(cat_model)
self.combobox_priority.set_model(pri_model)
self.priority_box = Gtk.VBox()
self.priority_box.pack_start(self.label_b_priority, True, True, 0)
self.priority_box.pack_start(self.combobox_priority, True, True, 0)
self.category_box = Gtk.VBox()
self.category_box.pack_start(self.label_b_category, True, True, 0)
self.category_box.pack_start(self.combobox_category, True, True, 0)
self.options_box.pack_start(self.category_box, True, True, 5)
self.options_box.pack_start(self.priority_box, True, True, 5)
self.options_expander.add(self.options_box)
#input
self.scroll_text = Gtk.ScrolledWindow()
self.scroll_text.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.text_input = Gtk.TextView()
self.text_input.set_size_request(0, 80)
self.scroll_text.add(self.text_input)
self.save_button = Gtk.Button(_("Add new"))
self.save_button.connect('clicked', self._save_cb)
self.remove_button = Gtk.Button(_("Remove"))
self.remove_button.connect('clicked', self._delete_row_cb)
self.buttons_box = Gtk.VBox()
self.buttons_box.pack_start(self.save_button, False, False, 0)
self.buttons_box.pack_start(self.remove_button, False, False, 0)
self.input_box = Gtk.HBox()
self.input_box.pack_start(self.scroll_text, True, True, 5)
self.input_box.pack_start(self.buttons_box, False, False, 0)
self.right_container.pack_start(self.tasks_frame, True, True, 5)
self.right_container.pack_start(self.options_expander, False, False, 0)
self.right_container.pack_start(self.input_box, False, False, 5)
self.main_container.pack_start(self.left_container, False, False, 5)
self.main_container.pack_start(self.right_container, True, True, 5)
self.main_container.show_all()
self.set_canvas(self.main_container)
def get_tasks(self):
'''Get the rows'''
date_formated = date_format(self.calendar.get_date())
db = DataBase(self.path)
data = db.get(date_formated)
reminder = db.get_reminder()
db.close()
tasks_model = TasksModel(data)
reminder_model = TasksModel(reminder)
self.tasks_list.set_model(tasks_model.get_model())
self.reminder_list.set_model(reminder_model.get_model())
def add_columns(self):
'''Add columns to TreeView'''
#for the tasks list
cell_text = Gtk.CellRendererText()
titles = (_('Tasks'), _('Category'), _('Priority'))
i = 1
for title in titles:
#if the colum is the first
if i == 1:
column = Gtk.TreeViewColumn(title, cell_text, markup=i)
#sizes commented are for emuler testing
#column.set_min_width(300)
column.set_min_width(440)
else:
column = Gtk.TreeViewColumn(title, cell_text, text=i)
#column.set_min_width(100)
column.set_min_width(135)
self.tasks_list.append_column(column)
i = i + 1
cell_toggle = Gtk.CellRendererToggle()
cell_toggle.connect('toggled', self._toggle_row_cb)
column = Gtk.TreeViewColumn('', cell_toggle, active=4)
self.tasks_list.append_column(column)
#for the reminder
column = Gtk.TreeViewColumn(titles[0], cell_text, text=1)
self.reminder_list.append_column(column)
column = Gtk.TreeViewColumn(titles[2], cell_text, text=3)
self.reminder_list.append_column(column)
def mark_day(self):
'''Mark the days of the month with tasks'''
date = date_format(self.calendar.get_date())
#just the month
data = (date[3:],)
db = DataBase(self.path)
day_list = db.get_days(data)
db.close()
for i in day_list:
self.calendar.mark_day(int(i))
def _save_cb(self, widget, data=None):
'''Save a task'''
input_buffer = self.text_input.get_buffer()
start, end = input_buffer.get_bounds()
text = input_buffer.get_text(start, end, False).strip()
text = strip_tag(text)
if len(text) > 0:
input_buffer.set_text('')
category = self.combobox_category.get_active()
priority = self.combobox_priority.get_active()
self.combobox_category.set_active(-1)
self.combobox_priority.set_active(-1)
date_formated = date_format(self.calendar.get_date())
data = (text, category, priority, 0, date_formated)
db = DataBase(self.path)
db.add(data)
db.close()
self.get_tasks()
self.mark_day()
def _toggle_row_cb(self, widget, path):
'''Toggle button row'''
model = self.tasks_list.get_model()
row_iter = model.get_iter(path)
#row is a Gtk.TreeModelRow()
row = model[row_iter]
row[4] = not row[4]
#task, completed, id for the moment category and
#priority are excluded
data = (row[4], row[0])
db = DataBase(self.path)
db.update(data)
db.close()
self.get_tasks()
def _delete_row_cb(self, widget, data=None):
'''Delete a row'''
selection = self.tasks_list.get_selection()
model, row_iter = selection.get_selected()
row = model[row_iter]
data = (row[0],)
db = DataBase(self.path)
db.delete(data)
db.close()
self.get_tasks()
def _day_selected_cb(self, widget, data=None):
'''Select new day'''
date_formated = date_format(self.calendar.get_date())
db = DataBase(self.path)
data = db.get(date_formated)
db.close()
tasks_model = TasksModel(data)
self.tasks_list.set_model(tasks_model.get_model())
def _mark_day_cb(self, widget, data=None):
'''Mark calendar days with bold font'''
self.calendar.clear_marks()
self.mark_day()
def _filter_query_cb(self, widget, data=None):
'''Query for filter the tasks'''
category = self.combobox_q_category.get_active()
priority = self.combobox_q_priority.get_active()
self.combobox_q_category.set_active(-1)
self.combobox_q_priority.set_active(-1)
date_formated = date_format(self.calendar.get_date())
data = (date_formated, category, priority)
db = DataBase(self.path)
tasks = db.filter_tasks(data)
db.close()
tasks_model = TasksModel(tasks)
self.tasks_list.set_model(tasks_model.get_model())
def close(self, skip_save=False):
'''Override the close method'''
activity.Activity.close(self, True)
def read_file(self, filepath):
pass
def write_file(self, filepath):
pass
class Task(object):
'''Class represeting a task object'''
def __init__(self, task_id, task, category, priority, completed):
self.task_id = task_id;
self.task = task
self.category = category
self.priority = priority
self.completed = completed
def get_task_id(self):
return self.task_id
def get_task(self):
return self.task
def get_category(self):
return self.category
def get_priority(self):
return self.priority
def get_complete(self):
return self.completed
def date_format(cal_tuple):
'''create a new format from the
calendar get_date() return'''
d = date(cal_tuple[0], cal_tuple[1] + 1, cal_tuple[2])
return d.strftime("%d/%m/%Y")
def strike_string(string):
'''Add pango markup'''
return '<s>%s</s>' %(string)
def strip_tag(string):
'''Delete all the tags'''
return re.sub(r'<[^>]*?>', '', string)