Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/chat/sketchpad/SketchPad.py
blob: e15e4358b5147c8e4826408dce9b951e02018481 (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
# Copyright (C) 2006, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gtk, gobject

from Sketch import Sketch

from SVGdraw import drawing
from SVGdraw import svg

class SketchPad(gtk.DrawingArea):
    __gsignals__ = {
        'new-user-sketch':(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                          ([gobject.TYPE_PYOBJECT]))
    }

    def __init__(self, bgcolor=(0.6, 1, 0.4)):
        gtk.DrawingArea.__init__(self)

        self._active_sketch = None
        self._rgb = (0.0, 0.0, 0.0)
        self._bgcolor = bgcolor
        self._sketches = []

        self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
                        gtk.gdk.BUTTON_RELEASE_MASK |
                        gtk.gdk.BUTTON1_MOTION_MASK)
        self.connect("button-press-event", self._button_press_cb)
        self.connect("button-release-event", self._button_release_cb)
        self.connect("motion-notify-event", self._motion_notify_cb)
        self.connect('expose_event', self.expose)
    
    def clear(self):
        self._sketches = []
        self.window.invalidate_rect(None, False)

    def expose(self, widget, event):
        """Draw the background of the sketchpad."""
        rect = self.get_allocation()
        ctx = widget.window.cairo_create()
        
        ctx.set_source_rgb(self._bgcolor[0], self._bgcolor[1], self._bgcolor[2])
        ctx.rectangle(0, 0, rect.width, rect.height)
        ctx.fill_preserve()
        
        ctx.set_source_rgb(0, 0.3, 0.2)
        ctx.stroke()
        
        for sketch in self._sketches:
            sketch.draw(ctx)
        
        return False

    def set_color(self, color):
        """Sets the current drawing color of the sketchpad.
        color agument should be 3-item tuple of rgb values between 0 and 1."""
        self._rgb = color

    def add_sketch(self, sketch):
        """Add a sketch to the the pad.  Mostly for subclasses and clients."""
        self._sketches.append(sketch)
        self.window.invalidate_rect(None, False)

    def add_point(self, event):
        if not self._active_sketch:
            return
        self._active_sketch.add_point(event.x, event.y)    
        self.window.invalidate_rect(None, False)
    
    def _button_press_cb(self, widget, event):
        self._active_sketch = Sketch(self._rgb)
        self._sketches.append(self._active_sketch)
        self.add_point(event)
    
    def _button_release_cb(self, widget, event):
        self.add_point(event)
        self.emit('new-user-sketch', self._active_sketch)
        self._active_sketch = None
    
    def _motion_notify_cb(self, widget, event):
        self.add_point(event)
    
    def to_svg(self):
        """Return a string containing an SVG representation of this sketch."""
        d = drawing()
        s = svg()
        for sketch in self._sketches:
            s.addElement(sketch.draw_to_svg())
        d.setSVG(s)
        return d.toXml()

def test_quit(w, skpad):
    print skpad.to_svg()
    gtk.main_quit()

if __name__ == "__main__":
    window = gtk.Window()
    window.set_default_size(400, 300)
    window.connect("destroy", lambda w: gtk.main_quit())
        
    sketchpad = SketchPad()
    window.add(sketchpad)
    sketchpad.show()
    
    window.show()
    
    window.connect("destroy", test_quit, sketchpad)

    gtk.main()