-
Notifications
You must be signed in to change notification settings - Fork 0
/
done.py
948 lines (785 loc) · 32.7 KB
/
done.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# from kivy.config import Config
#
# Config.set("graphics", "resizable", False)
from os import path
from enum import Enum
from kivy.lang import Builder
from kivymd.app import Clock, MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.list import OneLineAvatarIconListItem, OneLineListItem
from kivymd.uix.list.list import MDCheckbox
from kivymd.uix.textfield import MDTextField
from kivy.animation import Animation
from kivy.uix.scrollview import ScrollView
from kivy.uix.popup import Popup
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.core.window import Window
from kivymd.uix.list import IconRightWidget, IconLeftWidget
from kivymd.uix.filemanager import MDFileManager
from kivymd.toast import toast
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton, MDIconButton, MDRectangleFlatIconButton
from kivymd.uix.behaviors.toggle_behavior import MDToggleButton
from kivymd.uix.selectioncontrol import MDCheckbox
import func
class MyCheckBox(MDCheckbox):
def __init__(self, task_list_item, **kwargs):
super().__init__(**kwargs)
self.md_bg_color = [1, 1, 1, 0]
self.theme_text_color = "Custom"
self.color_active = "#add8e6"
app = MDApp.get_running_app()
if app.theme_cls.theme_style == "Dark":
self.color_inactive = "white"
else:
self.color_inactive = "black"
self.checkbox_icon_normal = "checkbox-blank-outline"
self.checkbox_icon_down = "checkbox-marked"
self.task_list_item = task_list_item
if self.task_list_item.task_object.done is True:
self.state = "down"
else:
self.state = "normal"
def on_active(self, instance, value):
app = MDApp.get_running_app()
current_task = self.task_list_item.task_object
if self.state == "down" and current_task.done is False:
new_task = func.Task("x " + current_task.raw_text)
app.task_manager.edit_task(old_task=current_task, new_task=new_task)
self.task_list_item.task_object = new_task
self.task_list_item.text = new_task.raw_text
elif self.state == "normal" and current_task.done is True:
old_task = self.task_list_item.task_object
new_task = func.Task(old_task.raw_text[2:])
app.task_manager.edit_task(old_task=current_task, new_task=new_task)
self.task_list_item.task_object = new_task
self.task_list_item.text = new_task.raw_text
app.task_manager.save_tasks()
TasksScrollView.sort_all()
class DoneLeftIcon(IconLeftWidget):
def __init__(self, task_list_item, *args, **kwargs):
super().__init__(*args, **kwargs)
self.icon = "assets/images/transparent.png"
self.done_check_box = MyCheckBox(task_list_item)
self.add_widget(self.done_check_box)
class TaskListItem(OneLineAvatarIconListItem):
def __init__(self, task_object: func.Task, **kwargs):
# Passes self to icon so it can access task text
self.task_object = task_object
super(TaskListItem, self).__init__(
DoneLeftIcon(self), DeleteIcon(self), **kwargs
)
self.text = task_object.raw_text
self.theme_text_color = "Custom"
app = MDApp.get_running_app()
if app.theme_cls.theme_style == "Dark":
self.text_color = "white"
else:
self.text_color = "black"
# Passes self to popup, so it can access task text
def on_press(self):
self.edit_task_popup = EditTaskField(self)
self.edit_task_popup.open()
class MyTextField(MDTextField):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class EditTaskField(Popup):
def __init__(self, task_list_item, **kwargs):
# parent_widget should be the object where popup was called from
# because self.parent is None
# it's required for access
super(EditTaskField, self).__init__(**kwargs)
# Window.borderless = True
# Hide the title
self.title = ""
self.separator_height = 0
self.task_list_item = task_list_item
self.size_hint = (0.8, 0.1)
self.content = MDBoxLayout(orientation="horizontal")
# on_text_validate is what happens when enter is pressed
self.current_input_field = MyTextField(
on_text_validate=self.accept_task_edit,
text=self.task_list_item.task_object.raw_text,
)
self.content.add_widget(self.current_input_field)
def on_open(self):
self.current_input_field.focus = True
def accept_task_edit(self, *args):
app = MDApp.get_running_app()
old_task = self.task_list_item.task_object
new_task = func.Task(self.current_input_field.text)
app.task_manager.edit_task(old_task=old_task, new_task=new_task)
app.task_manager.save_tasks()
self.task_list_item.task_object = new_task
self.task_list_item.text = self.current_input_field.text
# Add tags widget if it doesn't already exist
all_tags_widgets = get_tag_widgets(app.root.ids.mdlist.children)
for w in all_tags_widgets:
if sorted(self.task_list_item.task_object.tags) == w.tags:
break
else:
app.root.ids.mdlist.add_widget(
TagsItem(self.task_list_item.task_object.tags)
)
# Add project widgets if it doesn't already exist
all_project_widgets = get_project_widgets(app.root.ids.mdlist.children)
for w in all_project_widgets:
if sorted(self.task_list_item.task_object.projects) == w.projects:
break
else:
app.root.ids.mdlist.add_widget(
ProjectsItem(self.task_list_item.task_object.projects)
)
if new_task.done is True:
self.task_list_item.children[1].children[0].children[0].state = "down"
elif new_task.done is False:
self.task_list_item.children[1].children[0].children[0].state = "normal"
TasksScrollView.sort_all()
self.dismiss()
class AddTaskButton(MDIconButton):
"""
Button class that has hidden popup with text field which is shown when button is released
"""
def __init__(self, **kwargs):
super(AddTaskButton, self).__init__(**kwargs)
self.md_bg_color = "#add8e6"
self.line_width: 1
self.theme_icon_color = "Custom"
self.icon_size = "48sp"
self.task_input_popup = AddTaskTextField()
def on_release(self):
self.task_input_popup.open()
class AddTaskTextField(Popup):
def __init__(self, **kwargs):
super(AddTaskTextField, self).__init__(**kwargs)
# Hide the title
self.title = ""
self.separator_height = 0
self.size_hint = (0.8, 0.1)
self.input_field = MDTextField(on_text_validate=self.on_enter)
self.content = MDBoxLayout(orientation="horizontal")
self.content.add_widget(self.input_field)
def on_open(self):
self.input_field.focus = True
def on_dismiss(self):
self.input_field.text = ""
return super().on_dismiss()
def on_enter(self, *args):
if self.input_field.text == "":
toast("Task cannot be empty")
self.dismiss()
return
app = MDApp.get_running_app()
task_to_add = func.Task(self.input_field.text)
app.task_manager.add_task(task_to_add)
app.task_manager.save_tasks()
widget_to_add = TaskListItem(task_to_add)
app.root.ids.mdlist.add_widget(widget_to_add)
# Add tags widget if it doesn't already exist
all_tags_widgets = get_tag_widgets(app.root.ids.mdlist.children)
for w in all_tags_widgets:
if sorted(widget_to_add.task_object.tags) == w.tags:
break
else:
app.root.ids.mdlist.add_widget(TagsItem(widget_to_add.task_object.tags))
# Add project widgets if it doesn't already exist
all_project_widgets = get_project_widgets(app.root.ids.mdlist.children)
for w in all_project_widgets:
if sorted(widget_to_add.task_object.projects) == w.projects:
break
else:
app.root.ids.mdlist.add_widget(
ProjectsItem(widget_to_add.task_object.projects)
)
self.input_field.text = ""
TasksScrollView.sort_all()
# If the task added is the first in the list
# highlight it and set appropriate index
if len(app.task_manager.tasks) == 1:
list_items = app.root.ids.mdlist.children
app.selected_item_id = len(list_items) - 1
app.selected_item = list_items[app.selected_item_id]
set_active_element_theme(app.selected_item)
# Allow for multiple entries
Clock.schedule_once(self.refocus_ti)
def refocus_ti(self, *args):
self.input_field.focus = True
class DeleteIcon(IconRightWidget):
def __init__(self, task_list_item, **kwargs):
# parent_widget should be the object where popup was called from
# because self.parent is None
# it's passed so this class can access text from parent
super(DeleteIcon, self).__init__(**kwargs)
self.icon = "trash-can-outline"
self.task_list_item = task_list_item
self.theme_icon_color = "Custom"
app = MDApp.get_running_app()
if app.theme_cls.theme_style == "Dark":
self.icon_color = "white"
else:
self.icon_color = "black"
def on_press(self):
self.delete_task()
def delete_task(self, *args):
app = MDApp.get_running_app()
app.task_manager.delete_task(self.task_list_item.task_object)
self.task_list_item.parent.remove_widget(self.task_list_item)
app.task_manager.save_tasks()
TasksScrollView.sort_all()
class TagsItem(OneLineListItem):
def __init__(self, tags, *args, **kwargs):
super(TagsItem, self).__init__(*args, **kwargs)
self.tags = tags
text_processed = ""
for tag in tags:
text_processed += str(tag) + " "
self.text = text_processed[:-1]
self.font_style = "H6"
app = MDApp.get_running_app()
self.theme_text_color = "Custom"
app = MDApp.get_running_app()
if app.theme_cls.theme_style == "Dark":
self.text_color = "white"
else:
self.text_color = "black"
class ProjectsItem(OneLineListItem):
def __init__(self, projects, *args, **kwargs):
super(ProjectsItem, self).__init__(*args, **kwargs)
self.projects = projects
text_processed = ""
for project in projects:
text_processed += str(project) + " "
self.text = text_processed[:-1]
self.font_style = "H6"
app = MDApp.get_running_app()
self.theme_text_color = "Custom"
app = MDApp.get_running_app()
if app.theme_cls.theme_style == "Dark":
self.text_color = "white"
else:
self.text_color = "black"
class TasksScrollView(ScrollView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@staticmethod
def sort_all():
app = MDApp.get_running_app()
if app.sort_mode == SortMode.PRIORITY:
TasksScrollView.sort_by_priority()
elif app.sort_mode == SortMode.TAG:
TasksScrollView.sort_by_tags()
elif app.sort_mode == SortMode.PROJECT:
TasksScrollView.sort_by_projects()
@staticmethod
def sort_by_priority():
app = MDApp.get_running_app()
all_widgets = get_all_widgets()
task_widgets = get_task_widgets(all_widgets)
tags_items = get_tag_widgets(all_widgets)
project_widgets = get_project_widgets(all_widgets)
for w in project_widgets:
w.opacity = 0
for w in tags_items:
w.opacity = 0
current_search_text = app.root.ids.search_text_input.text
searched, unsearched = filter_by_search_text(current_search_text, task_widgets)
display_widget_lists(project_widgets, tags_items, unsearched, searched)
app.sort_mode = app.sort_mode.PRIORITY
@staticmethod
def sort_by_tags():
app = MDApp.get_running_app()
all_widgets = get_all_widgets()
task_widgets = get_task_widgets(all_widgets)
projects_widgets = get_project_widgets(all_widgets)
for project_widget in projects_widgets:
project_widget.opacity = 0
tag_widgets = get_tag_widgets(all_widgets)
current_search_text = app.root.ids.search_text_input.text
searched, unsearched = filter_by_search_text(current_search_text, task_widgets)
list_to_display = []
tag_widgets.sort(key=lambda x: x.text, reverse=True)
# Moving irrelevant widgets to the bottom
list_to_display.extend(projects_widgets)
list_to_display.extend(unsearched)
# Moving empty tags to the end of the list
# so tasks without tags appear last
if tag_widgets[-1].tags == []:
tag_widgets.insert(0, tag_widgets.pop())
hidden_tag_widgets = []
for tag_widget in tag_widgets:
current_tasks = []
for task_widget in searched:
if tag_widget.tags == sorted(task_widget.task_object.tags):
current_tasks.append(task_widget)
if len(current_tasks) > 0:
tag_widget.opacity = 1
current_tasks.append(tag_widget)
else:
tag_widget.opacity = 0
hidden_tag_widgets.append(tag_widget)
list_to_display.extend(current_tasks)
display_widget_lists(hidden_tag_widgets, list_to_display)
app.sort_mode = app.sort_mode.TAG
@staticmethod
def sort_by_projects():
app = MDApp.get_running_app()
all_widgets = get_all_widgets()
project_widgets = get_project_widgets(all_widgets)
task_widgets = get_task_widgets(all_widgets)
tag_widgets = get_tag_widgets(all_widgets)
for w in tag_widgets:
w.opacity = 0
current_search_text = app.root.ids.search_text_input.text
searched, unsearched = filter_by_search_text(current_search_text, task_widgets)
project_widgets.sort(key=lambda x: x.text, reverse=True)
list_to_display = []
# Moving unnesessary widgets to the end of displaying
list_to_display.extend(tag_widgets)
# Moving empty projects to the end of the list
# so tasks without tags appear last
if project_widgets[-1].projects == []:
project_widgets.insert(0, project_widgets.pop())
hidden_project_tags = []
for project_widget in project_widgets:
current_tasks = []
for task_widget in searched:
if project_widget.projects == sorted(task_widget.task_object.projects):
current_tasks.append(task_widget)
if len(current_tasks) > 0:
project_widget.opacity = 1
current_tasks.append(project_widget)
else:
project_widget.opacity = 0
hidden_project_tags.append(project_widget)
list_to_display.extend(current_tasks)
display_widget_lists(hidden_project_tags, unsearched, list_to_display)
app.sort_mode = app.sort_mode.PROJECT
class DarkSearchTextInput(MDTextField):
def __init__(self, **kwargs):
super(DarkSearchTextInput, self).__init__(**kwargs)
self.mode = "round"
self.default_size_hint_x = 0.6
self.default_size_hint_y = 0.08
self.default_size_height = 40
# self.size_hint_y = self.default_size_hint_y
self.size_hint_x = self.default_size_hint_x
self.height = 40
# self.width = 30
self.valign = "center"
self.pos_hint = {"center_x": 0.5, "center_y": 0.5}
self.hint_text = "search"
# self.font_size = 300.35 * self.size_hint_y
self.font_size = 20
self.active_line = False
self.multiline = False
self.icon_left = "magnify"
# Dark theme
self.fill_color_normal = "#000000"
self.hint_text_color_normal = 1, 1, 1, 1
self.hint_text_color_focus = 1, 1, 1, 1
self.icon_left_color_normal = 1, 1, 1, 1
self.icon_left_color_focus = 1, 1, 1, 1
self.text_color_normal = [1, 1, 1, 1]
self.text_color_focus = [1, 1, 1, 1]
def on_focus(self, *args):
duration = 0.05
if self.focus:
# Scaling up
anim = Animation(size=(self.width, self.height + 5), duration=duration)
anim.start(self)
else:
# Scaling to default
anim = Animation(
size=(self.width, self.default_size_height), duration=duration
)
anim.start(self) # start the animation
# TODO: Highlight search results
# https://stackoverflow.com/questions/36666797/changing-color-of-a-part-of-text-of-a-kivy-widget
# https://kivy.org/doc/stable/api-kivy.core.text.markup.html
# on_text is called everytime text in the input field is changed
def on_text(self, instance, value):
TasksScrollView.sort_all()
class LightSearchTextInput(DarkSearchTextInput):
def __init__(self, **kwargs):
super(LightSearchTextInput, self).__init__(**kwargs)
self.hint_text_color_normal = 0, 0, 0, 1
self.hint_text_color_focus = 0, 0, 0, 1
self.icon_left_color_normal = 0, 0, 0, 1
self.icon_left_color_focus = 0, 0, 0, 1
self.text_color_normal = [0, 0, 0, 1]
self.text_color_focus = [0, 0, 0, 1]
self.fill_color_normal = "#F5F5DC"
def filter_by_search_text_tags(
search_text: str, task_widgets: list[TaskListItem]
) -> tuple[list[TaskListItem], list[TaskListItem]]:
"""
return searched_sorted_by_priority, unsearched
"""
search_text = search_text.lower()
searched = []
unsearched = []
for task_widget in task_widgets:
if search_text in task_widget.text.lower():
task_widget.opacity = 1
task_widget.disabled = False
searched.append(task_widget)
else:
task_widget.opacity = 0
task_widget.disabled = True
unsearched.append(task_widget)
searched_sorted_by_priority = sorted(
searched,
key=lambda x: (
x.task_object.raw_text,
sorted(x.task_object.tags),
x.task_object.priority,
),
reverse=True,
)
return searched_sorted_by_priority, unsearched
def filter_by_search_text(
search_text: str, task_widgets: list[TaskListItem]
) -> tuple[list[TaskListItem], list[TaskListItem]]:
"""
return searched_sorted_by_priority, unsearched
"""
search_text = search_text.lower()
searched = []
unsearched = []
for task_widget in task_widgets:
if search_text in task_widget.text.lower():
task_widget.opacity = 1
task_widget.disabled = False
searched.append(task_widget)
else:
task_widget.opacity = 0
task_widget.disabled = True
unsearched.append(task_widget)
searched_sorted_by_priority = sorted(
searched, key=func.none_priority_to_end_key_for_widgets, reverse=True
)
return searched_sorted_by_priority, unsearched
def filter_by_search_text(
search_text: str, task_widgets: list[TaskListItem]
) -> tuple[list[TaskListItem], list[TaskListItem]]:
"""
return searched_sorted_by_priority, unsearched
"""
search_text = search_text.lower()
searched = []
unsearched = []
for task_widget in task_widgets:
if search_text in task_widget.text.lower():
task_widget.opacity = 1
task_widget.disabled = False
searched.append(task_widget)
else:
task_widget.opacity = 0
task_widget.disabled = True
unsearched.append(task_widget)
searched_sorted_by_priority = sorted(
searched, key=func.none_priority_to_end_key_for_widgets, reverse=True
)
return searched_sorted_by_priority, unsearched
def get_all_widgets():
return MDApp.get_running_app().root.ids.mdlist.children
def display_widget_lists(*widget_lists: list):
"""
Displays the widgets in the lists in the reverse order they are passed
Example:
display_widget_lists([1, 2, 3], [4, 5, 6])
will display 6, 5, 4, 3, 2, 1
"""
all_widgets = []
for widget_list in widget_lists:
for widget in widget_list:
all_widgets.append(widget)
app = MDApp.get_running_app()
app.root.ids.mdlist.children = all_widgets
# Navbar content below
class MyToggleButton(MDRectangleFlatIconButton, MDToggleButton):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.background_down = self.theme_cls.primary_color
def on_state(self, *_):
app = MDApp.get_running_app()
if app.root.ids.toggledark.state == "down":
set_dark_theme()
elif app.root.ids.togglelight.state == "down":
set_light_theme()
def get_task_widgets(all_widgets):
return [widget for widget in all_widgets if isinstance(widget, TaskListItem)]
def get_tag_widgets(all_widgets):
return [widget for widget in all_widgets if isinstance(widget, TagsItem)]
def get_project_widgets(all_widgets):
return [widget for widget in all_widgets if isinstance(widget, ProjectsItem)]
def set_dark_theme():
app = MDApp.get_running_app()
task_widgets = get_task_widgets(app.root.ids.mdlist.children)
tags_widgets = get_tag_widgets(app.root.ids.mdlist.children)
projects_widgets = get_project_widgets(app.root.ids.mdlist.children)
# Setting dark theme
app.theme_cls.theme_style = "Dark"
app.root.ids.settingslabel.text_color = "white"
for task in task_widgets:
task.text_color = "white"
# Trash icon color
task.children[0].children[0].icon_color = "white"
# Checkbox color
task.children[1].children[0].children[0].color_inactive = "white"
task.children[1].children[0].children[0].color_active = "#add8e6"
for tag in tags_widgets:
tag.text_color = "white"
for project in projects_widgets:
project.text_color = "white"
app.root.ids.add_task_button.line_color = "#add8e6"
# Creating a new search field with the light_theme
# and adding it to widget tree
dark_search_field = DarkSearchTextInput()
search_field_to_delete = app.root.ids.search_text_input
app.root.ids.mainbox.remove_widget(search_field_to_delete)
app.root.ids["search_text_input"] = dark_search_field
app.root.ids.mainbox.add_widget(dark_search_field, 2)
func.save_settings(theme=app.theme_cls.theme_style)
def set_light_theme():
app = MDApp.get_running_app()
task_widgets = get_task_widgets(app.root.ids.mdlist.children)
tags_widgets = get_tag_widgets(app.root.ids.mdlist.children)
projects_widgets = get_project_widgets(app.root.ids.mdlist.children)
# Setting light theme
app.theme_cls.theme_style = "Light"
app.root.ids.settingslabel.text_color = "black"
for task in task_widgets:
task.text_color = "black"
# Trash icon color
task.children[0].children[0].icon_color = "black"
# Checkbox color
task.children[1].children[0].children[0].color_inactive = "black"
task.children[1].children[0].children[0].color_active = "#add8e6"
for tag in tags_widgets:
tag.text_color = "black"
for project in projects_widgets:
project.text_color = "black"
app.root.ids.add_task_button.line_color = "black"
# Creating a new search field with the light_theme
# and adding it to widget tree
light_search_field = LightSearchTextInput()
search_field_to_delete = app.root.ids.search_text_input
app.root.ids.mainbox.remove_widget(search_field_to_delete)
app.root.ids["search_text_input"] = light_search_field
app.root.ids.mainbox.add_widget(light_search_field, 2)
func.save_settings(theme=app.theme_cls.theme_style)
def set_normal_element_theme(item, theme):
if item is None:
return
if theme == "Dark":
item.text_color = "white"
else:
item.text_color = "black"
def set_active_element_theme(item):
item.text_color = "red"
class ContentNavigationDrawer(MDBoxLayout):
pass
class ChooseFileButton(MDRectangleFlatIconButton):
def __init__(self, **kwargs):
super(ChooseFileButton, self).__init__(**kwargs)
def on_release(self):
app = MDApp.get_running_app()
app.root.ids.mdlist.clear_widgets()
app.file_manager_open()
class SortMode(Enum):
PRIORITY = 1
TAG = 2
PROJECT = 3
class MainApp(MDApp):
def __init__(self, **kwargs):
super(MainApp, self).__init__(**kwargs)
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "BlueGray"
self.title = "Done"
self.path = "./tutorial.txt"
self.task_manager = None
self.manager_open = False
self.sort_mode = SortMode.PRIORITY
self.selected_item = None
self.selected_item_id = None
self.file_manager = MDFileManager(
exit_manager=self.exit_manager,
select_path=self.select_path,
ext=[".txt"],
)
self.dialog = MDDialog(
text="Select .txt file",
buttons=[MDFlatButton(text="OK", on_release=self.close_dialog)],
)
def build(self):
Window.bind(on_keyboard=self.on_keyboard)
root = Builder.load_file("done.kv")
return root
def on_keyboard(self, window, key, scancode, codepoint, modifier):
app = MDApp.get_running_app()
# Adding task
if codepoint == "a" and (
modifier == ["ctrl", "shift"] or modifier == ["shift", "ctrl"]
):
app.root.ids.add_task_button.on_release()
# Starting search
elif codepoint == "s" and (
modifier == ["ctrl", "shift"] or modifier == ["shift", "ctrl"]
):
app.root.ids.search_text_input.focus = True
# Sort by priority
elif codepoint == "1" and (
modifier == ["ctrl", "shift"] or modifier == ["shift", "ctrl"]
):
toast("Sorting by priority")
app.root.ids.tasks_scroll_view.sort_by_priority()
# Sort by tags
elif codepoint == "2" and (
modifier == ["ctrl", "shift"] or modifier == ["shift", "ctrl"]
):
toast("Sorting by tags")
app.root.ids.tasks_scroll_view.sort_by_tags()
# Sort by projects
elif codepoint == "3" and (
modifier == ["ctrl", "shift"] or modifier == ["shift", "ctrl"]
):
toast("Sorting by projects")
app.root.ids.tasks_scroll_view.sort_by_projects()
# Edit selected task
elif codepoint == "e" and (
modifier == ["ctrl", "shift"] or modifier == ["ctrl", "shift"]
):
self.selected_item.on_press()
# Delete selected task
elif codepoint == "d" and (
modifier == ["ctrl", "shift"] or modifier == ["shift", "ctrl"]
):
delete_object = self.selected_item.children[0].children[0]
delete_object.delete_task()
items_list = app.root.ids.mdlist.children
if self.selected_item_id < len(items_list) - 1:
self.selected_item = items_list[self.selected_item_id]
else:
self.selected_item_id -= 1
self.selected_item = items_list[self.selected_item_id]
set_active_element_theme(self.selected_item)
# Mark selected task as done
elif codepoint == "x" and (
modifier == ["ctrl", "shift"] or modifier == ["ctrl", "shift"]
):
checkbox = self.selected_item.children[1].children[0].children[0]
checkbox.state = "down" if checkbox.state == "normal" else "normal"
set_normal_element_theme(self.selected_item, app.theme_cls.theme_style)
item_id = self.selected_item_id
self.selected_item = app.root.ids.mdlist.children[item_id]
set_active_element_theme(self.selected_item)
# Arrow up
elif key == 273:
list_items = app.root.ids.mdlist.children
if self.selected_item_id + 1 > len(list_items) - 1:
return
previous_item = self.selected_item
if not isinstance(list_items[self.selected_item_id + 1], TaskListItem):
if self.selected_item_id + 2 > len(list_items) - 1:
return
self.selected_item_id += 2
else:
self.selected_item_id += 1
self.selected_item = list_items[self.selected_item_id]
set_normal_element_theme(previous_item, app.theme_cls.theme_style)
set_active_element_theme(self.selected_item)
# Arrow down
elif key == 274:
list_items = app.root.ids.mdlist.children
if self.selected_item_id - 1 < 0:
return
previous_item = self.selected_item
if self.selected_item_id - 1 >= 0:
next_item = list_items[self.selected_item_id - 1]
if isinstance(next_item, TaskListItem):
self.selected_item_id -= 1
elif self.selected_item_id - 2 >= 0:
next_item = list_items[self.selected_item_id - 2]
if isinstance(next_item, TaskListItem):
self.selected_item_id -= 2
else:
return
self.selected_item = list_items[self.selected_item_id]
set_normal_element_theme(previous_item, app.theme_cls.theme_style)
set_active_element_theme(self.selected_item)
def on_start(self):
settings = func.read_settings()
app = MDApp.get_running_app()
search_widget = DarkSearchTextInput()
app.root.ids["search_text_input"] = search_widget
app.root.ids.mainbox.add_widget(search_widget, 2)
if settings or path.exists(self.path):
if settings:
self.task_manager = func.TaskManager(settings["path"])
if settings["theme"] == "Dark":
set_dark_theme()
else:
set_light_theme()
else:
self.task_manager = func.TaskManager(self.path)
set_light_theme()
self.add_and_display_all_widgets()
else:
self.dialog.open()
items_list = self.root.ids.mdlist.children
length = len(items_list)
if length <= 2:
return
self.selected_item_id = length - 1
self.selected_item = self.root.ids.mdlist.children[self.selected_item_id]
set_active_element_theme(self.selected_item)
def close_dialog(self, *_):
self.dialog.dismiss()
self.file_manager_open()
def file_manager_open(self):
self.file_manager.show(
self.file_manager.current_path
) # output manager to the screen
self.manager_open = True
def add_and_display_all_widgets(self):
app = MDApp.get_running_app()
for task in self.task_manager.tasks:
app.root.ids.mdlist.add_widget(TaskListItem(task))
unique_tags_combinations = self.task_manager.get_unique_tags_combinations(
self.task_manager.tasks
)
for tag_combination in unique_tags_combinations:
app.root.ids.mdlist.add_widget(TagsItem(tag_combination))
unique_projects_combinations = (
self.task_manager.get_unique_projects_combinations(self.task_manager.tasks)
)
for project_combination in unique_projects_combinations:
app.root.ids.mdlist.add_widget(ProjectsItem(project_combination))
TasksScrollView.sort_all()
def select_path(self, path):
"""It will be called when you click on the file name
or the catalog selection button.
:type path: str;
:param path: path to the selected directory or file;
"""
self.exit_manager()
self.task_manager = func.TaskManager(path)
func.save_settings(path=path)
toast(path)
self.add_and_display_all_widgets()
set_light_theme()
func.save_settings(theme="Light")
items_list = self.root.ids.mdlist.children
length = len(items_list)
if length <= 2:
return
self.selected_item_id = length - 1
self.selected_item = self.root.ids.mdlist.children[self.selected_item_id]
set_active_element_theme(self.selected_item)
def exit_manager(self, *args):
"""Called when the user reaches the root of the directory tree."""
self.manager_open = False
self.file_manager.close()
if __name__ == "__main__":
MainApp().run()