5
Simple application for reading/writing notes.
12
from os.path import join, exists
13
from kivy.app import App
14
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
15
from kivy.properties import ListProperty, StringProperty, \
16
NumericProperty, BooleanProperty
17
from kivy.uix.boxlayout import BoxLayout
18
from kivy.uix.floatlayout import FloatLayout
19
from kivy.clock import Clock
22
class MutableTextInput(FloatLayout):
24
text = StringProperty()
25
multiline = BooleanProperty(True)
27
def __init__(self, **kwargs):
28
super(MutableTextInput, self).__init__(**kwargs)
29
Clock.schedule_once(self.prepare, 0)
31
def prepare(self, *args):
32
self.w_textinput = self.ids.w_textinput.__self__
33
self.w_label = self.ids.w_label.__self__
36
def on_touch_down(self, touch):
37
if self.collide_point(*touch.pos) and touch.is_double_tap:
40
return super(MutableTextInput, self).on_touch_down(touch)
44
self.add_widget(self.w_textinput)
45
self.w_textinput.focus = True
49
self.add_widget(self.w_label)
51
def check_focus_and_view(self, textinput):
52
if not textinput.focus:
53
self.text = textinput.text
57
class NoteView(Screen):
59
note_index = NumericProperty()
60
note_title = StringProperty()
61
note_content = StringProperty()
64
class NoteListItem(BoxLayout):
66
note_title = StringProperty()
67
note_index = NumericProperty()
74
def args_converter(self, row_index, item):
76
'note_index': row_index,
77
'note_content': item['content'],
78
'note_title': item['title']}
84
self.notes = Notes(name='notes')
87
self.transition = SlideTransition(duration=.35)
88
root = ScreenManager(transition=self.transition)
89
root.add_widget(self.notes)
93
if not exists(self.notes_fn):
95
with open(self.notes_fn, 'rb') as fd:
97
self.notes.data = data
100
with open(self.notes_fn, 'wb') as fd:
101
json.dump(self.notes.data, fd)
103
def del_note(self, note_index):
104
del self.notes.data[note_index]
109
def edit_note(self, note_index):
110
note = self.notes.data[note_index]
111
name = 'note{}'.format(note_index)
113
if self.root.has_screen(name):
114
self.root.remove_widget(self.root.get_screen(name))
118
note_index=note_index,
119
note_title=note.get('title'),
120
note_content=note.get('content'))
122
self.root.add_widget(view)
123
self.transition.direction = 'left'
124
self.root.current = view.name
127
self.notes.data.append({'title': 'New note', 'content': ''})
128
note_index = len(self.notes.data) - 1
129
self.edit_note(note_index)
131
def set_note_content(self, note_index, note_content):
132
self.notes.data[note_index]['content'] = note_content
133
data = self.notes.data
135
self.notes.data = data
139
def set_note_title(self, note_index, note_title):
140
self.notes.data[note_index]['title'] = note_title
144
def refresh_notes(self):
145
data = self.notes.data
147
self.notes.data = data
150
self.transition.direction = 'right'
151
self.root.current = 'notes'
155
return join(self.user_data_dir, 'notes.json')
157
if __name__ == '__main__':