Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/view.py
blob: ccbbc82b84ce60e20b3de17cb03cbb779f6ffa04 (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
# 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
from gaphas import GtkView, Canvas, state

# We'd rather not depend on sugar in this class
from sugar.graphics import style

from thoughtview import ThoughtView

class MindMapView(GtkView):

    __gtype_name__ = 'MindMapView'

    def __init__(self, model=None):
        self._model = None
        self._row_changed_sid = None
        self._row_deleted_sid = None
        self._selected_thought = None

        GtkView.__init__(self)

        self.canvas = Canvas()

        if model is not None:
            self.model = model

        state.observers.add(self.__state_changed_cb)

    def __state_changed_cb(self, event):
        func, args, dict_ = event
        if len(args) > 1 and isinstance(args[1], ThoughtView):
            thought_view = args[1]
            x, y = thought_view.get_position()
            row = self.model.find_by_id(thought_view.id)
            if row[2] != x or row[3] != y:
                self.model.set(row.iter, 2, x, 3, y)

    def set_model(self, new_model):
        logging.debug('set_model %r' % new_model)
        if self._model == new_model:
            return

        if self._model is not None:
            self._model.disconnect(self._row_changed_sid)
            self._model.disconnect(self._row_deleted_sid)
            
        self._model = new_model

        if self._model is not None:
            self._row_changed_sid = \
                    self._model.connect('row-changed', self.__row_changed_cb)
            self._row_deleted_sid = \
                    self._model.connect('row-deleted', self.__row_deleted_cb)

            for item in self.canvas.get_all_items():
                self.canvas.remove(item)
            self._populate_from_model(self._model)

    def _populate_from_model(self, rows):
        for row in rows:
            thought_view = ThoughtView(row[0], row[1], row[2], row[3], row[4])
            self._populate_from_model(row.iterchildren())
            self.add_element(thought_view)

    def get_model(self):
        return self._model

    model = property(get_model, set_model)

    def __row_changed_cb(self, model, path, iter):
        #logging.debug('__row_changed_cb %r' % path)

        row = model[iter]
        thought_view = self._get_thought_by_id(row[0])
        if thought_view is None:
            thought_view = ThoughtView(row[0], row[1], row[2], row[3], row[4])
            self.canvas.add(thought_view)
        else:
            thought_view.name = row[1]
            thought_view.set_position(row[2], row[3])
            thought_view.x = row[2]
            thought_view.y = row[3]
            thought_view.color = row[4]

        self.canvas.request_update(thought_view)

    def __row_deleted_cb(self, model, path):
        logging.debug('__row_deleted_cb %r' % path)

        raise NotImplementedError()
        thought_view = self._get_thought_by_id(path[0])
        self.remove_element(thought_view)

    def _get_thought_by_id(self, thought_id):
        for item in self.canvas.get_all_items():
            if isinstance(item, ThoughtView) and item.id == thought_id:
                return item
        return None