Enhance SketchPad for easier subclass usage

- New 'new-user-sketch' signal when a local path is closed
- Trap BUTTON_RELEASE_EVENTs now
This commit is contained in:
Dan Williams 2006-10-19 09:54:51 -04:00
parent 4f54f7921f
commit 4260c5a4a5

View File

@ -15,7 +15,7 @@
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA. # Boston, MA 02111-1307, USA.
import gtk import gtk, gobject
from Sketch import Sketch from Sketch import Sketch
@ -23,6 +23,11 @@ from SVGdraw import drawing
from SVGdraw import svg from SVGdraw import svg
class SketchPad(gtk.DrawingArea): 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)): def __init__(self, bgcolor=(0.6, 1, 0.4)):
gtk.DrawingArea.__init__(self) gtk.DrawingArea.__init__(self)
@ -32,10 +37,11 @@ class SketchPad(gtk.DrawingArea):
self._sketches = [] self._sketches = []
self.add_events(gtk.gdk.BUTTON_PRESS_MASK | self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.BUTTON1_MOTION_MASK) gtk.gdk.BUTTON1_MOTION_MASK)
self.connect("button-press-event", self.__button_press_cb) self.connect("button-press-event", self._button_press_cb)
self.connect("button-release-event", self.__button_release_cb) self.connect("button-release-event", self._button_release_cb)
self.connect("motion-notify-event", self.__motion_notify_cb) self.connect("motion-notify-event", self._motion_notify_cb)
self.connect('expose_event', self.expose) self.connect('expose_event', self.expose)
def clear(self): def clear(self):
@ -65,23 +71,27 @@ class SketchPad(gtk.DrawingArea):
self._rgb = color self._rgb = color
def add_sketch(self, sketch): def add_sketch(self, sketch):
"""Add a sketch to the the pad. Mostly for subclasses and clients."""
self._sketches.append(sketch) self._sketches.append(sketch)
def add_point(self, event):
if self._active_sketch:
self._active_sketch.add_point(event.x, event.y)
self.window.invalidate_rect(None, False) self.window.invalidate_rect(None, False)
def __button_press_cb(self, widget, event): 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._active_sketch = Sketch(self._rgb)
self.add_sketch(self._active_sketch) self._sketches.append(self._active_sketch)
self.add_point(event) self.add_point(event)
def __button_release_cb(self, widget, event): def _button_release_cb(self, widget, event):
self.add_point(event) self.add_point(event)
self.emit('new-user-sketch', self._active_sketch)
self._active_sketch = None self._active_sketch = None
def __motion_notify_cb(self, widget, event): def _motion_notify_cb(self, widget, event):
self.add_point(event) self.add_point(event)
def to_svg(self): def to_svg(self):