Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model.py
blob: 2084773a3e10cd691c829bf8879880f76fbf0f27 (plain)
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
# Copyright (C) 2009, Tomeu Vizoso
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging

import gobject
import gtk
import cjson

from sugar import dispatch

class MindMapModel(gtk.GenericTreeModel):

    _COLUMN_TYPES = (str, str, long, long, str)

    def __init__(self):
        gobject.GObject.__init__(self)

        self._next_thought_id = 0
        self._thoughts_by_id = {}
        self._thoughts = []

    def create_new_thought(self):
        thought = Thought(self._next_thought_id)
        self._add_thought(thought)

    def _add_thought(self, thought):
        thought.changed.connect(self.__thought_changed_cb)
        self._thoughts.append(thought)
        self._thoughts_by_id[thought.id] = thought
        self._next_thought_id += 1

        path = (thought.id,)
        self.row_inserted(path, self.get_iter(path))

    def get_thought(self, thought_id):
        return self._thoughts_by_id[int(thought_id)]

    def __thought_changed_cb(self, **kwargs):
        thought = kwargs['sender']
        path = (thought.id,)
        self.row_changed(path, self.get_iter(path))

    def get_thoughts(self):
        return self._thoughts

    def serialize(self):
        thoughts = []
        for thought in self._thoughts:
            thought_dict = {}
            thought_dict['id'] = thought.id
            thought_dict['name'] = thought.name
            thought_dict['x'] = thought.x
            thought_dict['y'] = thought.y
            thought_dict['color'] = thought.color
            thoughts.append(thought_dict)

        return cjson.encode({'thoughts': thoughts})

    def unserialize(self, data):
        thoughts = cjson.decode(data)['thoughts']
        for thought_dict in thoughts:
            thought = Thought(thought_dict['id'])
            thought.name = thought_dict.get('name', None)
            thought.x = thought_dict.get('x', None)
            thought.y = thought_dict.get('y', None)
            thought.color = thought_dict.get('color', None)
            self._add_thought(thought)

    # gtk.GenericTreeModel methods
    # paths and rowrefs are the thought ids
    def on_get_flags(self):
        return gtk.TREE_MODEL_ITERS_PERSIST

    def on_get_n_columns(self):
        return len(self._COLUMN_TYPES)

    def on_get_column_type(self, n):
        return self._COLUMN_TYPES[n]

    def on_get_iter(self, path):
        logging.debug('on_get_iter %r %r' % (type(path), path))
        if path[0] in self._thoughts_by_id:
            return path[0]
        else:
            return None

    def on_get_path(self, rowref):
        logging.debug('on_get_path %r' % rowref)
        return (rowref,)

    def on_get_value(self, rowref, column):
        #logging.debug('on_get_value %r %r' % (rowref, column))
        if rowref >= len(self._thoughts):
            return None
        thought = self._thoughts[rowref]
        value_tuple = thought.get_tuple()
        return value_tuple[column]

    def on_iter_next(self, rowref):
        #logging.debug('on_iter_next %r' % rowref)
        if rowref not in self._thoughts_by_id:
            return None
        thought = self._thoughts_by_id[rowref]
        
        index = self._thoughts.index(thought)
        if index + 1 >= len(self._thoughts):
            return None
        next_thought = self._thoughts[index + 1]
        #logging.debug('on_iter_next returning %r' % next_thought.id)
        return next_thought.id

    def on_iter_children(self, rowref):
        logging.debug('on_iter_children %r' % rowref)
        if rowref:
            return None
        if self._thoughts:
            return self._thoughts[0].id
        else:
            return None

    def on_iter_has_child(self, rowref):
        return False

    def on_iter_n_children(self, rowref):
        logging.debug('on_iter_n_children %r' % rowref)
        if rowref:
            return 0
        return len(self._thoughts)

    def on_iter_nth_child(self, rowref, n):
        logging.debug('on_iter_nth_child %r %r' % (rowref, n))
        if rowref is not None:
            return None
        if n < len(self._thoughts):
            return self._thoughts[n].id

    def on_iter_parent(child):
        return None

class Thought(object):

    def __init__(self, thought_id, name=None, x=0, y=0, color=None):
        if thought_id is None:
            raise ValueError('thought_id cannot be None')
        self._id = thought_id
        self._name = name
        self._x = x
        self._y = y
        self._color = color

        self.changed = dispatch.Signal()

    def get_tuple(self):
        return (self.id, self.name, self.x, self.y, self.color)

    def set_id(self, new_id):
        if self._id == new_id:
            return
        self._id = new_id
        self.changed.send(self)

    def get_id(self):
        return self._id

    id = property(get_id, set_id)

    def set_name(self, new_name):
        if self._name == new_name:
            return
        self._name = new_name
        self.changed.send(self)

    def get_name(self):
        return self._name

    name = property(get_name, set_name)

    def set_x(self, new_x):
        if self._x == new_x:
            return
        self._x = new_x
        self.changed.send(self)

    def get_x(self):
        return self._x

    x = property(get_x, set_x)

    def set_y(self, new_y):
        if self._y == new_y:
            return
        self._y = new_y
        self.changed.send(self)

    def get_y(self):
        return self._y

    y = property(get_y, set_y)

    def set_color(self, new_color):
        if self._color == new_color:
            return
        self._color = new_color
        self.changed.send(self)

    def get_color(self):
        return self._color

    color = property(get_color, set_color)