Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model.py
blob: cefa1c7def00d72e5e3742c5658c0b0fde90075e (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
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
# 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
from gettext import gettext as _

import gobject
import gtk
import cjson

from sugar import dispatch

class MindMapModel(gtk.GenericTreeModel):

    _COLUMN_TYPES = (str, str, long, long, str)
    
    THOUGHTS = 0
    LINKS = 1

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

        self._next_element_id = 2
        self._thoughts_by_id = {}
        self._thoughts = []
        self._links_by_id = {}
        self._links = []

    def create_new_thought(self):
        thought = Thought(self._next_element_id)
        self._next_element_id += 1
        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

        path = (self.THOUGHTS, len(self._thoughts) - 1)
        self.row_inserted(path, self.get_iter(path))

    def get_thought(self, path):
        if isinstance(path, basestring):
            path = path.split(':')
            for i in range(len(path)):
                path[i] = int(path[i])

        if path[0] != self.THOUGHTS or path[1] >= len(path):
            raise ValueError('Invalid path %r' % (path,))

        return self._thoughts[path[1]]

    def __thought_changed_cb(self, **kwargs):
        thought = kwargs['sender']
        path = (self.THOUGHTS, self._thoughts.index(thought))
        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'])
            self._next_element_id = max(self._next_element_id + 1,
                                        int(thought_dict['id']) + 1)
            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 are 0 for the THOUGHTS category, 1 for the LINKS category and the
    # position in their list for elements
    # rowrefs are 0 for the THOUGHTS category, 1 for the LINKS category and the
    # id for elements
    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 len(path) == 1 and path[0] == self.THOUGHTS:
            return self.THOUGHTS
        elif len(path) == 1 and path[0] == self.LINKS:
            return self.LINKS
        elif path[0] == self.THOUGHTS:
            element_list = self._thoughts
        elif path[0] == self.LINKS:
            element_list = self._links
        else:
            return None

        if path[1] < len(element_list):
            return element_list[path[1]].id
        else:
            return None            

    def on_get_path(self, rowref):
        logging.debug('on_get_path %r' % rowref)
        if rowref == self.THOUGHTS:
            return (self.THOUGHTS,)
        elif rowref == self.LINKS:
            return (self.LINKS,)
        elif rowref in self._thoughts_by_id:
            index = self._thoughts.index(self._thoughts_by_id[rowref])
            return (self.THOUGHTS, index,)
        elif rowref in self._links_by_id:
            index = self._links.index(self._links_by_id[rowref])
            return (self.LINKS, index,)
        else:
            return None

    def on_get_value(self, rowref, column):
        #logging.debug('on_get_value %r %r' % (rowref, column))
        if rowref == self.THOUGHTS and column == 1:
            return _('Thoughts')
        elif rowref == self.LINKS and column == 1:
            return _('Links')
        elif rowref in self._thoughts_by_id:
            element = self._thoughts_by_id[rowref]
        elif rowref in self._links_by_id:
            element = self._links_by_id[rowref]
        else:
            return None

        value_tuple = element.get_tuple()
        return value_tuple[column]

    def on_iter_next(self, rowref):
        #logging.debug('on_iter_next %r' % rowref)
        if rowref == self.THOUGHTS:
            return self.LINKS
        if rowref in self._thoughts_by_id:
            element = self._thoughts_by_id[rowref]
            element_list = self._thoughts
        elif rowref in self._links_by_id:
            element = self._links_by_id[rowref]
            element_list = self._links
        else:
            return None
        
        index = element_list.index(element)
        if index + 1 >= len(element_list):
            return None
        next_element = element_list[index + 1]
        return next_element.id

    def on_iter_children(self, rowref):
        #logging.debug('on_iter_children %r' % rowref)
        if rowref is None:
            return self.THOUGHTS
        elif rowref == self.THOUGHTS and self._thoughts:
            return self._thoughts[0].id
        elif rowref == self.LINKS and self._links:
            return self._links[0].id
        else:
            return None

    def on_iter_has_child(self, rowref):
        #logging.debug('on_iter_has_child %r' % rowref)
        if rowref == self.THOUGHTS and self._thoughts:
            return True
        elif rowref == self.LINKS and self._links:
            return True
        else:
            return False

    def on_iter_n_children(self, rowref):
        #logging.debug('on_iter_n_children %r' % rowref)
        if rowref == self.THOUGHTS:
            return len(self._thoughts)
        elif rowref == self.LINKS:
            return len(self._links)
        else:
            return 0

    def on_iter_nth_child(self, rowref, n):
        #logging.debug('on_iter_nth_child %r %r' % (rowref, n))
        if rowref is None and n == self.THOUGHTS:
            return self.THOUGHTS
        elif rowref is None and n == self.LINKS:
            return self.LINKS
        elif rowref == self.THOUGHTS and n < len(self._thoughts):
            return self._thoughts[n].id
        elif rowref == self.LINKS and n < len(self._links):
            return self._links[n].id
        else:
            return None

    def on_iter_parent(self, rowref):
        #logging.debug('on_iter_parent %r' % rowref)
        if rowref in self._thoughts_by_id:
            return self.THOUGHTS
        elif rowref in self._links_by_id:
            return self.LINKS
        else:
            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)