Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/view.py
blob: 3708b6a087f8bcbee78b63c2ea1006f8e5eb9d8c (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
# 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

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

from canvas import Canvas, CanvasElement

class MindMapView(Canvas):

    __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

        Canvas.__init__(self)

        self.dragging_started.connect(self.__dragging_started_cb)
        self.dragging_finished.connect(self.__dragging_finished_cb)

        self.connect('button-release-event', self.__button_release_event_cb)

        if model is not None:
            self.model = model

    def __dragging_started_cb(self, **kwargs):
        x, y = kwargs['position']
        logging.debug('__dragging_started_cb %r %r' % (x, y))
        thought = kwargs['element']
        thought.dragging = True
        self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1))

    def __dragging_finished_cb(self, **kwargs):
        self.window.set_cursor(None)
        x, y = kwargs['position']
        thought = kwargs['element']
        logging.debug('__dragging_finished_cb %r %r' % (x, y))
        thought.dragging = False
        
        rect = self.get_allocation()
        thought_rect = thought.get_rect()
        if (x >= 0 and x + thought_rect.width <= rect.width) and \
                (y >= 0 and y + thought_rect.height <= rect.height):
            row = self.model.find_by_id(thought.id)
            self.model.set(row.iter, 2, x, 3, y)

    def __button_release_event_cb(self, widget, event):
        logging.debug('button_release_event_cb')

        if self._selected_thought is not None:
            self._selected_thought.selected = False
            self._selected_thought = None

        thought = self.get_element_at(event.x, event.y)
        if thought is not None:
            thought.selected = True
            self._selected_thought = thought

        return True

    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)

            self.remove_all_elements()
            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.add_element(thought_view)
        else:
            thought_view.name = row[1]
            thought_view.x = row[2]
            thought_view.y = row[3]
            thought_view.color = row[4]
        thought_view.invalidate()

    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 thought_view in self.get_elements():
            if thought_view.id == thought_id:
                return thought_view
        return None

class ThoughtView(CanvasElement):

    _PADDING = style.zoom(15)
    _LINE_WIDTH = style.zoom(2)

    def __init__(self, thought_id, name, x, y, color):
        CanvasElement.__init__(self)

        logging.debug('ThoughtView %r %r' % (thought_id, name))
        
        self.id = thought_id
        self.name = name
        self.x = x
        self.y = y
        self.color = color

        self._last_width = -1
        self._last_height = -1
        self._dragging = False
        self._selected = False
        
    def set_dragging(self, dragging):
        if self._dragging != dragging:
            self._dragging = dragging
            self.invalidate()

    def get_dragging(self):
        return self._dragging

    dragging = property(get_dragging, set_dragging)

    def set_selected(self, selected):
        if self._selected != selected:
            self._selected = selected
            self.invalidate()

    selected = property(None, set_selected)

    def _get_name_layout(self, context=None):
        if context is None:
            visual = gtk.gdk.screen_get_default().get_system_visual()
            pixmap = gtk.gdk.Pixmap(None, 1, 1, visual.depth)
            context = pixmap.cairo_create()

        if self.name is None or not self.name:
            name = _('Unnamed')
        else:
            name = self.name

        layout = context.create_layout()
        layout.set_text(name)

        return layout

    def draw(self, context):
        self._draw_background(context)
        width, height = self._draw_text(context)
        self._draw_bounding_box(context, width, height)

        self._last_width = width
        self._last_height = height
        
        CanvasElement.draw(self, context)
        
    def _draw_background(self, context):
        if self.color is None or not self.color:
            color = style.Color('#FFFFFF')
        else:
            color = style.Color(self.color)

        r, g, b, a = color.get_rgba()
        context.save()
        context.set_source_rgb(r, g, b)
        context.paint()
        context.restore()

    def _draw_text(self, context):
        context.save()

        context.set_source_rgb(0.0, 0.0, 0.0)

        layout = self._get_name_layout(context)

        width, height = layout.get_pixel_size()
        width += self._PADDING * 2
        height += self._PADDING * 2

        x = self.x + self._PADDING
        y = self.y + self._PADDING
        context.translate(x, y)
        context.show_layout(layout)
        context.restore()

        return width, height

    def _draw_bounding_box(self, context, width, height):
        context.save()

        if self._selected or self._dragging:
            context.set_source_rgb(0.6, 0.6, 0.6)
            context.set_line_width(self._LINE_WIDTH * 2)
        else:
            context.set_source_rgb(0, 0, 0)
            context.set_line_width(self._LINE_WIDTH)
        
        x = self.x + self._LINE_WIDTH / 2
        y = self.y + self._LINE_WIDTH / 2
        rect_width = width - self._LINE_WIDTH
        rect_height = height - self._LINE_WIDTH

        context.rectangle(x, y, rect_width, rect_height)
        context.stroke()
        context.restore()

    def get_rect(self):
        if -1 in (self._last_width, self._last_height):
            layout = self._get_name_layout()
            width, height = layout.get_pixel_size()
            self._last_width = width + self._PADDING * 2
            self._last_height = height + self._PADDING * 2

        return gtk.gdk.Rectangle(self.x, self.y,
                                 self._last_width, self._last_height)