Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/thoughtview.py
blob: 809d1aca48183f58e2217ac69cdd0ac2254e261d (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
# 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 gaphas.examples import Box, Text
from gaphas.connector import Handle
from gaphas.geometry import Rectangle

class ThoughtView(Box):

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

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

        self.new_thought_handle = Handle()
        self._handles.append(self.new_thought_handle)
        
        self.id = thought_id
        self.line_to_parent = None
        self._name = name
        self._color = color

        self.set_position(x, y)

    def set_name(self, name):
        if self._name != name:
            self._name = name
            self.request_update(update=True, matrix=True)

    def get_name(self):
        return self._name

    name = property(get_name, set_name)

    def set_color(self, color):
        if self._color != color:
            self._color = color
            self.request_update(update=True, matrix=True)

    def get_color(self):
        return self._color

    color = property(get_color, set_color)

    def set_position(self, x, y):
        if (x, y) != self.get_position():
            self.matrix = (1.0, 0.0, 0.0, 1, x, y)

    def get_position(self):
        return self.matrix[4], self.matrix[5]

    position = property(get_position, set_position)

    def draw(self, context):
        super(ThoughtView, self).draw(context)
        self._draw_label(context)

    def _draw_label(self, context):
        context.cairo.save()
        layout = context.cairo.create_layout()

        if self._name is not None and self._name:
            layout.set_text(self._name)
        else:
            layout.set_text(_('Untitled'))
        
        width, height = layout.get_pixel_size()
        #logging.debug('_draw_label %r %r %r' % (layout.get_text(), width, height))
        self.min_width = width + self._PADDING * 2
        self.min_height = height + self._PADDING * 2

        self.width = max(self.width, self.min_width)
        self.height = max(self.height, self.min_height)

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

    def normalize(self):
        updated = super(ThoughtView, self).normalize()
        self.new_thought_handle.x = 20
        self.new_thought_handle.y = 20
        return updated

    def get_rectangle(self):
        x, y = self.get_position()
        return Rectangle(x, y, self.width, self.height)