2006-10-15 01:08:44 +02:00
|
|
|
# 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.
|
|
|
|
|
2006-10-19 15:54:51 +02:00
|
|
|
import gtk, gobject
|
2006-05-19 18:45:08 +02:00
|
|
|
|
|
|
|
from Sketch import Sketch
|
|
|
|
|
2006-05-19 20:18:41 +02:00
|
|
|
from SVGdraw import drawing
|
|
|
|
from SVGdraw import svg
|
|
|
|
|
2006-05-19 18:45:08 +02:00
|
|
|
class SketchPad(gtk.DrawingArea):
|
2006-10-19 15:54:51 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'new-user-sketch':(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
|
|
|
}
|
|
|
|
|
2006-10-18 17:14:02 +02:00
|
|
|
def __init__(self, bgcolor=(0.6, 1, 0.4)):
|
2006-05-19 18:45:08 +02:00
|
|
|
gtk.DrawingArea.__init__(self)
|
|
|
|
|
|
|
|
self._active_sketch = None
|
2006-05-22 04:21:42 +02:00
|
|
|
self._rgb = (0.0, 0.0, 0.0)
|
2006-10-18 17:14:02 +02:00
|
|
|
self._bgcolor = bgcolor
|
2006-05-19 18:45:08 +02:00
|
|
|
self._sketches = []
|
|
|
|
|
|
|
|
self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
|
2006-10-19 15:54:51 +02:00
|
|
|
gtk.gdk.BUTTON_RELEASE_MASK |
|
2006-05-19 18:45:08 +02:00
|
|
|
gtk.gdk.BUTTON1_MOTION_MASK)
|
2006-10-19 15:54:51 +02:00
|
|
|
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)
|
2006-05-19 18:45:08 +02:00
|
|
|
self.connect('expose_event', self.expose)
|
2006-05-22 19:36:58 +02:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._sketches = []
|
|
|
|
self.window.invalidate_rect(None, False)
|
2006-05-19 18:45:08 +02:00
|
|
|
|
|
|
|
def expose(self, widget, event):
|
2006-05-22 04:21:42 +02:00
|
|
|
"""Draw the background of the sketchpad."""
|
2006-05-19 18:45:08 +02:00
|
|
|
rect = self.get_allocation()
|
|
|
|
ctx = widget.window.cairo_create()
|
2006-05-21 00:50:39 +02:00
|
|
|
|
2006-10-18 17:14:02 +02:00
|
|
|
ctx.set_source_rgb(self._bgcolor[0], self._bgcolor[1], self._bgcolor[2])
|
2006-05-21 00:50:39 +02:00
|
|
|
ctx.rectangle(0, 0, rect.width, rect.height)
|
|
|
|
ctx.fill_preserve()
|
|
|
|
|
|
|
|
ctx.set_source_rgb(0, 0.3, 0.2)
|
|
|
|
ctx.stroke()
|
2006-05-19 18:45:08 +02:00
|
|
|
|
|
|
|
for sketch in self._sketches:
|
|
|
|
sketch.draw(ctx)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2006-05-22 04:21:42 +02:00
|
|
|
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
|
|
|
|
|
2006-05-19 18:45:08 +02:00
|
|
|
def add_sketch(self, sketch):
|
2006-10-19 15:54:51 +02:00
|
|
|
"""Add a sketch to the the pad. Mostly for subclasses and clients."""
|
2006-05-19 18:45:08 +02:00
|
|
|
self._sketches.append(sketch)
|
2006-10-19 15:54:51 +02:00
|
|
|
self.window.invalidate_rect(None, False)
|
|
|
|
|
2006-05-22 03:31:57 +02:00
|
|
|
def add_point(self, event):
|
2006-10-19 15:54:51 +02:00
|
|
|
if not self._active_sketch:
|
|
|
|
return
|
|
|
|
self._active_sketch.add_point(event.x, event.y)
|
2006-05-22 03:31:57 +02:00
|
|
|
self.window.invalidate_rect(None, False)
|
|
|
|
|
2006-10-19 15:54:51 +02:00
|
|
|
def _button_press_cb(self, widget, event):
|
2006-05-22 04:21:42 +02:00
|
|
|
self._active_sketch = Sketch(self._rgb)
|
2006-10-19 15:54:51 +02:00
|
|
|
self._sketches.append(self._active_sketch)
|
2006-05-22 03:31:57 +02:00
|
|
|
self.add_point(event)
|
2006-05-19 18:45:08 +02:00
|
|
|
|
2006-10-19 15:54:51 +02:00
|
|
|
def _button_release_cb(self, widget, event):
|
2006-05-22 03:31:57 +02:00
|
|
|
self.add_point(event)
|
2006-10-19 15:54:51 +02:00
|
|
|
self.emit('new-user-sketch', self._active_sketch)
|
2006-05-19 18:45:08 +02:00
|
|
|
self._active_sketch = None
|
|
|
|
|
2006-10-19 15:54:51 +02:00
|
|
|
def _motion_notify_cb(self, widget, event):
|
2006-05-22 03:31:57 +02:00
|
|
|
self.add_point(event)
|
2006-05-19 20:18:41 +02:00
|
|
|
|
|
|
|
def to_svg(self):
|
2006-05-22 04:21:42 +02:00
|
|
|
"""Return a string containing an SVG representation of this sketch."""
|
2006-05-19 20:18:41 +02:00
|
|
|
d = drawing()
|
|
|
|
s = svg()
|
|
|
|
for sketch in self._sketches:
|
|
|
|
s.addElement(sketch.draw_to_svg())
|
|
|
|
d.setSVG(s)
|
|
|
|
return d.toXml()
|
|
|
|
|
2006-06-01 00:01:24 +02:00
|
|
|
def test_quit(w, skpad):
|
|
|
|
print skpad.to_svg()
|
2006-05-19 20:18:41 +02:00
|
|
|
gtk.main_quit()
|
|
|
|
|
2006-05-19 18:45:08 +02:00
|
|
|
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()
|
|
|
|
|
2006-05-19 20:18:41 +02:00
|
|
|
window.connect("destroy", test_quit, sketchpad)
|
|
|
|
|
2006-05-19 18:45:08 +02:00
|
|
|
gtk.main()
|