Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/view.py
blob: f3305a8ef28715aa081fd6142e1c6d2bc0d07f77 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# 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, Line, geometry
from gaphas import tool

# 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

        GtkView.__init__(self)

        self.canvas = Canvas()

        self.tool = tool.ToolChain().               \
                    append(NewThoughtTool()).       \
                    append(tool.HoverTool()).       \
                    append(ItemTool()).             \
                    append(TextEditTool()).         \
                    append(tool.RubberbandTool())

        if model is not None:
            self.model = model

    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 _calculate_ports(self, child_view, parent_view):
        child_rect = child_view.get_rectangle()
        parent_rect = parent_view.get_rectangle()

        point_in_child = self._get_closest_intersection(child_rect, parent_rect)
        point_in_parent = self._get_closest_intersection(parent_rect, child_rect)

        return point_in_child, point_in_parent

    def _get_closest_intersection(self, rect1, rect2):
        center1 = rect1.x + rect1.width / 2, rect1.y + rect1.height / 2
        center2 = rect2.x + rect2.width / 2, rect2.y + rect2.height / 2

        return self._intersection_rect_line(rect1, (center1, center2))

    def _intersection_rect_line(self, rect, line):
        top_edge = (rect.x, rect.y), (rect.x + rect.width, rect.y)
        left_edge = (rect.x, rect.y), (rect.x, rect.y + rect.height)
        bottom_edge = (rect.x, rect.y + rect.height), \
                      (rect.x + rect.width, rect.y + rect.height)
        right_edge = (rect.x + rect.width, rect.y + rect.height), \
                     (rect.x + rect.width, rect.y)

        for edge in (top_edge, left_edge, bottom_edge, right_edge):
            point = geometry.intersect_line_line(line[0], line[1],
                                                 edge[0], edge[1])
            if point is not None:
                return point

        raise ValueError('Rect %r and line %r dont intersect!' % (rect, line,))

    def _update_connection(self, parent_view, thought_view):
        handle_tool = tool.ConnectHandleTool()

        if thought_view.line_to_parent is None:
            line = Connection()
            self.canvas.add(line)
            thought_view.line_to_parent = line
        else:
            line = thought_view.line_to_parent
            line_handle = line.handles()[0]
            handle_tool.disconnect(self, line, line_handle)
            handle_tool.disconnect(self, line, line.opposite(line_handle))

        start, end = self._calculate_ports(thought_view, parent_view)

        line_handle = line.handles()[0]
        handle_tool.connect(self, line, line_handle, start)
        handle_tool.connect(self, line, line.opposite(line_handle), end)

        self.canvas.request_update(line)

    def __row_changed_cb(self, model, path, iter):
        row = model[iter]
        #logging.debug('__row_changed_cb %r' % ((row[0], row[1], row[2], row[3], row[4],),))

        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.color = row[4]

        gobject.idle_add(self.__update_connections_cb, thought_view, row)

    def __update_connections_cb(self, thought_view, row):
        if row.parent is not None:
            parent_view = self._get_thought_by_id(row.parent[0])
            self._update_connection(parent_view, thought_view)

        for child in row.iterchildren():
            child_view = self._get_thought_by_id(child[0])
            self._update_connection(thought_view, child_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

class NewThoughtTool(tool.HandleTool):

    def __init__(self):
        tool.HandleTool.__init__(self)
        self._new_connection = None
        self._parent_thought = None

    def grab_handle(self, item, handle):
        logging.debug('NewThoughtTool.grab_handle %r %r' % (item, handle))
        tool.HandleTool.grab_handle(self, item, handle)

    def ungrab_handle(self):
        logging.debug('NewThoughtTool.ungrab_handle')
        tool.HandleTool.ungrab_handle(self)

    def on_button_release(self, context, event):
        logging.debug('NewThoughtTool.on_button_release')

        if self._new_connection is not None:
            context.view.canvas.remove(self._new_connection)
            self._new_connection = None

            context.view.model.create_new_thought(x=event.x, y=event.y,
                                                  parent_id=self._parent_thought.id)

        tool.HandleTool.on_button_release(self, context, event)

    def move(self, view, item, handle, pos):
        #logging.debug('NewThoughtTool.move')
        if isinstance(item, ThoughtView) and handle == item.new_thought_handle:
            matrix = view.canvas.get_matrix_i2c(item)
            offset = matrix[4], matrix[5]

            if self._new_connection is None:
                self._new_connection = Line()
                view.canvas.add(self._new_connection)
                start_handle = self._new_connection.handles()[0]
                start_handle.x = item.handles()[4].pos[0] + offset[0]
                start_handle.y = item.handles()[4].pos[1] + offset[1]
                #logging.debug('created line starting at %r %r' % item.handles()[4].pos)

                self._parent_thought = item

            end_handle = self._new_connection.handles()[1]
            end_handle.x = pos[0]
            end_handle.y = pos[1]
            view.canvas.request_update(self._new_connection)
            #logging.debug('moved line end to %r %r' % (end_handle.x, end_handle.y))
        else:
            tool.HandleTool.move(self, view, item, handle, pos)

    def glue(self, view, item, handle, vpos):
        #logging.debug('NewThoughtTool.glue')
        tool.HandleTool.glue(self, view, item, handle, vpos)

    def connect(self, view, item, handle, vpos):
        logging.debug('NewThoughtTool.connect')
        tool.HandleTool.connect(self, view, item, handle, vpos)

    def disconnect(self, view, item, handle):
        logging.debug('NewThoughtTool.disconnect')
        tool.HandleTool.disconnect(self, view, item, handle)

class ItemTool(tool.ItemTool):

    def __init__(self):
        tool.ItemTool.__init__(self)

    def on_button_release(self, context, event):
        logging.debug('ItemTool.on_button_release')

        for item in self._movable_items:
            if not isinstance(item, ThoughtView):
                continue
            x, y = item.get_position()
            row = context.view.model.find_by_id(item.id)
            if row[2] != x or row[3] != y:
                context.view.model.set(row.iter, 2, x, 3, y)

        tool.ItemTool.on_button_release(self, context, event)

class Connection(Line):
    def __init__(self):
        super(Connection, self).__init__()

    def draw_head(self, context):
        cr = context.cairo
        cr.move_to(2, 0)
        cr.line_to(12, 10)
        cr.stroke()

        cr.move_to(2, 0)
        cr.line_to(12, -10)
        cr.stroke()

        super(Connection, self).draw_head(context)

    def draw(self, context):
        cr = context.cairo
        cr.set_source_rgb(0, 0, .8)
        super(Connection, self).draw(context)

class TextEditTool(tool.Tool):

    def __init__(self):
        super(tool.Tool, self).__init__()
        self._clicked_item = None

    def on_double_click(self, context, event):
        view = context.view
        self._clicked_item = view.get_item_at_point((event.x, event.y))
        if not isinstance(self._clicked_item, ThoughtView):
            return False

        window = gtk.Window()
        window.set_parent_window(context.view.window)
        window.size_allocate(gtk.gdk.Rectangle(int(event.x), int(event.y), 50, 50))

        x, y = self._clicked_item.get_position()
        origin_x, origin_y = context.view.window.get_origin()
        window.move(x + origin_x, y + origin_y)

        window.connect('focus-out-event', self.__focus_out_event_cb)
        window.connect('realize', self.__realize_cb)

        text_view = gtk.TextView()

        buf = text_view.get_buffer()
        buf.set_text(self._clicked_item.name)
        buf.select_range(buf.get_start_iter(), buf.get_end_iter())

        text_view.connect('key-press-event', self.__key_press_event_cb)
        text_view.show()

        window.add(text_view)

        window.show()

        return True

    def __realize_cb(self, widget):
        widget.set_decorated(False)
        widget.set_resize_mode(gtk.RESIZE_IMMEDIATE)
        widget.set_modal(True)
        widget.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)

    def __key_press_event_cb(self, widget, event):
        if event.keyval == gtk.keysyms.Return:
            buf = widget.get_buffer()
            self._clicked_item.name = buf.get_text(buf.get_start_iter(),
                                                   buf.get_end_iter())
            widget.get_toplevel().destroy()
            return True
        elif event.keyval == gtk.keysyms.Escape:
            widget.get_toplevel().destroy()
            return True
        else:
            return False

    def __focus_out_event_cb(self, widget, event):
        widget.destroy()