Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/scribblewidget.py
blob: 34068909be905b22c08e37780a7d00dd2a09be9b (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
# Copyright (C) 2008, 2009 Sayamindu Dasgupta <sayamindu@gmail.com>
#
# 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 gtk
import gobject
import goocanvas

import math
import uuid

DEFAULT_WIDTH = 10
DEFAULT_HEIGHT = 10

CANVAS_WIDTH = 1187
CANVAS_HEIGHT = 767

class ScribbleWidget(goocanvas.Canvas):
    __gsignals__ = {
        'item-added': (gobject.SIGNAL_RUN_FIRST,
                          gobject.TYPE_NONE,
                          ([])),
    }
    def __init__(self):
        goocanvas.Canvas.__init__(self)
        self.set_size_request(CANVAS_WIDTH, CANVAS_HEIGHT)
        self.set_bounds(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)

        self.connect("button_press_event", self.on_button_press)
        self.connect("button_release_event", self.on_button_release)
        self.connect("motion_notify_event", self.on_motion)

        self._root = self.get_root_item()

        self.tool = None
        self.item = None
        self.item_id = None
        self.item_orig_x = 0
        self.item_orig_y = 0
        self.item_width = 0
        self.item_height = 0
        self.line_points = []
        self.prev_time = 0
        self._fill_color = 0
        self._stroke_color = 0
        self.cmd = None # method to draw the last item
        self.cmd_list = "" # list of methods to draw the entire canvas

    def set_fill_color(self, color):
        self._fill_color = int(color.strip('#')+'FF', 16)

    def set_stroke_color(self, color):
        self._stroke_color = int(color.strip('#')+'FF', 16)

    def set_tool(self, tool):
        self.tool = tool

    def create_item(self, x, y):
        self.item_id = str(uuid.uuid4())

        self.item_orig_x = x
        self.item_orig_y = y

        self.item_width = DEFAULT_WIDTH
        self.item_height = DEFAULT_HEIGHT

        if self.tool == 'circle':
            self.item = goocanvas.Ellipse(parent=self._root, center_x=x, \
                    center_y=y, radius_x = DEFAULT_WIDTH/2, \
                    radius_y = DEFAULT_HEIGHT/2, title=self.item_id, \
                    fill_color_rgba = self._fill_color, \
                    stroke_color_rgba = self._stroke_color)
        elif self.tool == 'rect':
            self.item = goocanvas.Rect(parent=self._root, x=x, y=y, \
                    width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, \
                    fill_color_rgba = self._fill_color, \
                    stroke_color_rgba = self._stroke_color, title=self.item_id)
        elif self.tool == 'pencil':
            self.line_points = [] #Reset
            self.line_points.append((x, y))
            self.item = goocanvas.Polyline(parent=self._root, \
                    points=goocanvas.Points(self.line_points), \
                    stroke_color_rgba = self._stroke_color, title=self.item_id)
        else:
            pass

    def process_motion(self, x, y, time):
        dx = x - self.item_orig_x
        dy = y - self.item_orig_y
        dt = time - self.prev_time

        if self.tool == 'circle':
            self.item.props.radius_x = abs(dx)
            self.item.props.radius_y = abs(dy)
        elif self.tool == 'rect':
            if dx < 0:
                self.item.props.x = x
                #self.item_orig_x = x
            if dy < 0:
                self.item.props.y = y
                #self.item_orig_y = y
            self.item.props.width = abs(dx)
            self.item.props.height = abs(dy)
        elif self.tool == 'pencil':
            #XXX: This is pretty ugly - we should try some curve fitting stuff
            dist = abs(math.sqrt(dx*dx + dy*dy))
            self.line_points.append((x, y)) 
            if dist > 10 or dt > 10:
                self.item.props.points = goocanvas.Points(self.line_points)
            self.item_orig_x = x
            self.item_orig_y = y

        self.prev_time = time

    def get_cmd(self):
        return self.cmd

    def get_cmd_list(self):
        return self.cmd_list

    def process_item_finalize(self):
        if self.tool == 'circle':
            self.cmd = "goocanvas.Ellipse(parent=self._root, center_x=%d, \
                center_y=%d, radius_x = %d, radius_y = %d, \
                fill_color_rgba = %d, stroke_color_rgba = %d, \
                title = '%s')" % (self.item.props.center_x, \
                self.item.props.center_y, self.item.props.radius_x, \
                self.item.props.radius_y, self._fill_color, \
                self._stroke_color, self.item_id)
        elif self.tool == 'rect':
            self.cmd = "goocanvas.Rect(parent=self._root, x=%d, y=%d, \
                width=%d, height=%d, fill_color_rgba = %d, \
                stroke_color_rgba = %d, title = '%s')" % (self.item.props.x, \
                self.item.props.y, self.item.props.width, \
                self.item.props.height, self._fill_color, self._stroke_color, \
                self.item_id)
        elif self.tool == 'pencil':
            self.cmd = "goocanvas.Polyline(parent=self._root, \
                points=goocanvas.Points(%s), stroke_color_rgba = %d, \
                title = '%s')" % (str(self.line_points), self._stroke_color, \
                self.item_id)

        #print self.cmd

        if len(self.cmd_list) > 0:
            self.cmd_list += (';' + self.cmd)
        else:
            self.cmd_list = self.cmd

        self.emit('item-added')

    def process_cmd(self, cmd):
        #print 'Processing cmd :' + cmd
        exec(cmd) #FIXME: Ugly hack, but I'm too lazy to do this nicely

        if len(self.cmd_list) > 0:
            self.cmd_list += (';' + cmd)
        else:
            self.cmd_list = cmd


    def on_button_press(self, canvas, event):
        self.create_item(event.x, event.y)
        fleur = gtk.gdk.Cursor(gtk.gdk.FLEUR) 
        canvas.pointer_grab(self.item, \
            gtk.gdk.POINTER_MOTION_MASK | \
                gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK,
            fleur, event.time)
        return True

    def on_button_release(self, canvas, event):
        canvas.pointer_ungrab(self.item, event.time)
        self.process_item_finalize()
        return True

    def on_motion(self, canvas, event):
        if event.is_hint:
            x, y, state = event.window.get_pointer()
        else:
            x = event.x
            y = event.y
            state = event.state

        if not state & gtk.gdk.BUTTON1_MASK:
            return False

        self.process_motion(x, y, event.time)
        return True