~ubuntu-branches/debian/stretch/electrum/stretch

« back to all changes in this revision

Viewing changes to gui/kivy/tools/.buildozer/android/platform/python-for-android/dist/kivy/python-install/share/kivy-examples/tutorials/notes/final/main.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2016-04-04 03:02:39 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20160404030239-0szgkio8yryjv7c9
Tags: 2.6.3-1
* New upstream release.
  - Drop backported install-wizard-connect.patch.
* Add Suggests: python-zbar and update the installation hint to suggest
  apt-get instead of pip (closes: #819517).
* Bump Standards-Version to 3.9.7 (no changes).
* Update Vcs-* links.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
Notes
 
3
=====
 
4
 
 
5
Simple application for reading/writing notes.
 
6
 
 
7
'''
 
8
 
 
9
__version__ = '1.0'
 
10
 
 
11
import json
 
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
 
20
 
 
21
 
 
22
class MutableTextInput(FloatLayout):
 
23
 
 
24
    text = StringProperty()
 
25
    multiline = BooleanProperty(True)
 
26
 
 
27
    def __init__(self, **kwargs):
 
28
        super(MutableTextInput, self).__init__(**kwargs)
 
29
        Clock.schedule_once(self.prepare, 0)
 
30
 
 
31
    def prepare(self, *args):
 
32
        self.w_textinput = self.ids.w_textinput.__self__
 
33
        self.w_label = self.ids.w_label.__self__
 
34
        self.view()
 
35
 
 
36
    def on_touch_down(self, touch):
 
37
        if self.collide_point(*touch.pos) and touch.is_double_tap:
 
38
            self.edit()
 
39
            return True
 
40
        return super(MutableTextInput, self).on_touch_down(touch)
 
41
 
 
42
    def edit(self):
 
43
        self.clear_widgets()
 
44
        self.add_widget(self.w_textinput)
 
45
        self.w_textinput.focus = True
 
46
 
 
47
    def view(self):
 
48
        self.clear_widgets()
 
49
        self.add_widget(self.w_label)
 
50
 
 
51
    def check_focus_and_view(self, textinput):
 
52
        if not textinput.focus:
 
53
            self.text = textinput.text
 
54
            self.view()
 
55
 
 
56
 
 
57
class NoteView(Screen):
 
58
 
 
59
    note_index = NumericProperty()
 
60
    note_title = StringProperty()
 
61
    note_content = StringProperty()
 
62
 
 
63
 
 
64
class NoteListItem(BoxLayout):
 
65
 
 
66
    note_title = StringProperty()
 
67
    note_index = NumericProperty()
 
68
 
 
69
 
 
70
class Notes(Screen):
 
71
 
 
72
    data = ListProperty()
 
73
 
 
74
    def args_converter(self, row_index, item):
 
75
        return {
 
76
            'note_index': row_index,
 
77
            'note_content': item['content'],
 
78
            'note_title': item['title']}
 
79
 
 
80
 
 
81
class NoteApp(App):
 
82
 
 
83
    def build(self):
 
84
        self.notes = Notes(name='notes')
 
85
        self.load_notes()
 
86
 
 
87
        self.transition = SlideTransition(duration=.35)
 
88
        root = ScreenManager(transition=self.transition)
 
89
        root.add_widget(self.notes)
 
90
        return root
 
91
 
 
92
    def load_notes(self):
 
93
        if not exists(self.notes_fn):
 
94
            return
 
95
        with open(self.notes_fn, 'rb') as fd:
 
96
            data = json.load(fd)
 
97
        self.notes.data = data
 
98
 
 
99
    def save_notes(self):
 
100
        with open(self.notes_fn, 'wb') as fd:
 
101
            json.dump(self.notes.data, fd)
 
102
 
 
103
    def del_note(self, note_index):
 
104
        del self.notes.data[note_index]
 
105
        self.save_notes()
 
106
        self.refresh_notes()
 
107
        self.go_notes()
 
108
 
 
109
    def edit_note(self, note_index):
 
110
        note = self.notes.data[note_index]
 
111
        name = 'note{}'.format(note_index)
 
112
 
 
113
        if self.root.has_screen(name):
 
114
            self.root.remove_widget(self.root.get_screen(name))
 
115
 
 
116
        view = NoteView(
 
117
            name=name,
 
118
            note_index=note_index,
 
119
            note_title=note.get('title'),
 
120
            note_content=note.get('content'))
 
121
 
 
122
        self.root.add_widget(view)
 
123
        self.transition.direction = 'left'
 
124
        self.root.current = view.name
 
125
 
 
126
    def add_note(self):
 
127
        self.notes.data.append({'title': 'New note', 'content': ''})
 
128
        note_index = len(self.notes.data) - 1
 
129
        self.edit_note(note_index)
 
130
 
 
131
    def set_note_content(self, note_index, note_content):
 
132
        self.notes.data[note_index]['content'] = note_content
 
133
        data = self.notes.data
 
134
        self.notes.data = []
 
135
        self.notes.data = data
 
136
        self.save_notes()
 
137
        self.refresh_notes()
 
138
 
 
139
    def set_note_title(self, note_index, note_title):
 
140
        self.notes.data[note_index]['title'] = note_title
 
141
        self.save_notes()
 
142
        self.refresh_notes()
 
143
 
 
144
    def refresh_notes(self):
 
145
        data = self.notes.data
 
146
        self.notes.data = []
 
147
        self.notes.data = data
 
148
 
 
149
    def go_notes(self):
 
150
        self.transition.direction = 'right'
 
151
        self.root.current = 'notes'
 
152
 
 
153
    @property
 
154
    def notes_fn(self):
 
155
        return join(self.user_data_dir, 'notes.json')
 
156
 
 
157
if __name__ == '__main__':
 
158
    NoteApp().run()