Make colors work in Sketch

This commit is contained in:
Dan Williams 2006-05-21 00:29:20 -04:00
parent 1a2f5cc3f7
commit 1c54ef5002
11 changed files with 47 additions and 70 deletions

View File

@ -1,4 +1,4 @@
AC_INIT([Sugar],[0.6],[],[sugar]) AC_INIT([Sugar],[0.4],[],[sugar])
AC_PREREQ([2.59]) AC_PREREQ([2.59])
@ -21,7 +21,6 @@ sugar/Makefile
sugar/__installed__.py sugar/__installed__.py
sugar/browser/Makefile sugar/browser/Makefile
sugar/chat/Makefile sugar/chat/Makefile
sugar/chat/sketchpad/Makefile
sugar/p2p/Makefile sugar/p2p/Makefile
sugar/p2p/model/Makefile sugar/p2p/model/Makefile
sugar/shell/Makefile sugar/shell/Makefile

View File

@ -16,6 +16,4 @@ activitydir = $(pkgdatadir)/activities
activity_DATA = browser.activity activity_DATA = browser.activity
EXTRA_DIST = \ EXTRA_DIST = \
$(rc_DATA) \
$(activity_DATA) \
$(icon_DATA) $(icon_DATA)

View File

@ -322,8 +322,6 @@ class BrowserShell(dbus.service.Object):
get_instance = staticmethod(get_instance) get_instance = staticmethod(get_instance)
def __init__(self): def __init__(self):
geckoembed.set_profile_path(sugar.env.get_user_dir())
session_bus = dbus.SessionBus() session_bus = dbus.SessionBus()
bus_name = dbus.service.BusName('com.redhat.Sugar.Browser', bus=session_bus) bus_name = dbus.service.BusName('com.redhat.Sugar.Browser', bus=session_bus)
object_path = '/com/redhat/Sugar/Browser' object_path = '/com/redhat/Sugar/Browser'

View File

@ -1,5 +1,3 @@
SUBDIRS = sketchpad
sugardir = $(pythondir)/sugar/chat sugardir = $(pythondir)/sugar/chat
sugar_PYTHON = \ sugar_PYTHON = \
__init__.py \ __init__.py \
@ -14,6 +12,4 @@ icon_DATA = \
activitydir = $(pkgdatadir)/activities activitydir = $(pkgdatadir)/activities
activity_DATA = chat.activity activity_DATA = chat.activity
EXTRA_DIST = \ EXTRA_DIST = $(icon_DATA)
$(icon_DATA) \
$(activity_DATA)

View File

@ -19,7 +19,6 @@ from sugar.p2p.Stream import Stream
from sugar.session.LogWriter import LogWriter from sugar.session.LogWriter import LogWriter
from sugar.chat.sketchpad.Toolbox import Toolbox from sugar.chat.sketchpad.Toolbox import Toolbox
from sugar.chat.sketchpad.SketchPad import SketchPad from sugar.chat.sketchpad.SketchPad import SketchPad
from sugar.chat.Emoticons import Emoticons
import sugar.env import sugar.env
import richtext import richtext
@ -48,6 +47,7 @@ class Chat(activity.Activity):
toolbox = Toolbox() toolbox = Toolbox()
toolbox.connect('tool-selected', self._tool_selected) toolbox.connect('tool-selected', self._tool_selected)
toolbox.connect('color-selected', self._color_selected)
vbox.pack_start(toolbox, False) vbox.pack_start(toolbox, False)
toolbox.show() toolbox.show()
@ -64,6 +64,9 @@ class Chat(activity.Activity):
def __send_button_clicked_cb(self, button): def __send_button_clicked_cb(self, button):
self.send_sketch(self._sketchpad.to_svg()) self.send_sketch(self._sketchpad.to_svg())
def _color_selected(self, toolbox, color):
self._sketchpad.set_color(color)
def _tool_selected(self, toolbox, tool_id): def _tool_selected(self, toolbox, tool_id):
if tool_id == 'text': if tool_id == 'text':
@ -193,8 +196,6 @@ class Chat(activity.Activity):
# FIXME self._controller.notify_activate(self) # FIXME self._controller.notify_activate(self)
def _insert_rich_message(self, nick, msg): def _insert_rich_message(self, nick, msg):
msg = Emoticons.get_instance().replace(msg)
buf = self._chat_view.get_buffer() buf = self._chat_view.get_buffer()
aniter = buf.get_end_iter() aniter = buf.get_end_iter()
buf.insert(aniter, nick + ": ") buf.insert(aniter, nick + ": ")

View File

@ -59,7 +59,7 @@ class RichTextView(gtk.TextView):
it = self.__get_event_iter(event) it = self.__get_event_iter(event)
if it and self.__iter_is_link(it): if it and self.__iter_is_link(it):
buf = self.get_buffer() buf = self.get_buffer()
address_tag = buf.get_tag_table().lookup("object-id") address_tag = buf.get_tag_table().lookup("link-address")
address_end = it.copy() address_end = it.copy()
address_end.backward_to_tag_toggle(address_tag) address_end.backward_to_tag_toggle(address_tag)
@ -81,18 +81,9 @@ class RichTextBuffer(gtk.TextBuffer):
def append_link(self, title, address): def append_link(self, title, address):
it = self.get_iter_at_mark(self.get_insert()) it = self.get_iter_at_mark(self.get_insert())
self.insert_with_tags_by_name(it, address, "link", "object-id") self.insert_with_tags_by_name(it, address, "link", "link-address")
self.insert_with_tags_by_name(it, title, "link") self.insert_with_tags_by_name(it, title, "link")
def append_icon(self, name, it = None):
if not it:
it = self.get_iter_at_mark(self.get_insert())
self.insert_with_tags_by_name(it, name, "icon", "object-id")
icon_theme = gtk.icon_theme_get_default()
pixbuf = icon_theme.load_icon(name, 16, 0)
self.insert_pixbuf(it, pixbuf)
def apply_tag(self, tag_name): def apply_tag(self, tag_name):
self.active_tags.append(tag_name) self.active_tags.append(tag_name)
@ -110,13 +101,11 @@ class RichTextBuffer(gtk.TextBuffer):
self.remove_tag_by_name(tag_name, start, end) self.remove_tag_by_name(tag_name, start, end)
def __create_tags(self): def __create_tags(self):
tag = self.create_tag("icon")
tag = self.create_tag("link") tag = self.create_tag("link")
tag.set_property("underline", pango.UNDERLINE_SINGLE) tag.set_property("underline", pango.UNDERLINE_SINGLE)
tag.set_property("foreground", "#0000FF") tag.set_property("foreground", "#0000FF")
tag = self.create_tag("object-id") tag = self.create_tag("link-address")
tag.set_property("invisible", True) tag.set_property("invisible", True)
tag = self.create_tag("bold") tag = self.create_tag("bold")
@ -235,8 +224,6 @@ class RichTextHandler(xml.sax.handler.ContentHandler):
self.tags.append(tag) self.tags.append(tag)
if name == "link": if name == "link":
self.href = attrs['href'] self.href = attrs['href']
elif name == "icon":
self.buf.append_icon(attrs['name'], self.buf.get_end_iter())
def characters(self, data): def characters(self, data):
start_it = it = self.buf.get_end_iter() start_it = it = self.buf.get_end_iter()
@ -248,8 +235,8 @@ class RichTextHandler(xml.sax.handler.ContentHandler):
self.buf.apply_tag_by_name(tag, start_it, it) self.buf.apply_tag_by_name(tag, start_it, it)
if tag == "link": if tag == "link":
self.buf.insert_with_tags_by_name(start_it, self.href, self.buf.insert_with_tags_by_name(start_it, self.href,
"link", "object-id") "link", "link-address")
def endElement(self, name): def endElement(self, name):
if not self._done and self._in_richtext: if not self._done and self._in_richtext:
if name != "richtext": if name != "richtext":
@ -271,17 +258,9 @@ class RichTextSerializer:
return "font-size-" + attributes["size"] return "font-size-" + attributes["size"]
elif el_name == "link": elif el_name == "link":
return "link" return "link"
elif el_name == "icon":
return "icon"
else: else:
return None return None
def _parse_object_id(self, it):
object_id_tag = self.buf.get_tag_table().lookup("object-id")
end = it.copy()
end.forward_to_tag_toggle(object_id_tag)
return self.buf.get_text(it, end)
def serialize_tag_start(self, tag, it): def serialize_tag_start(self, tag, it):
name = tag.get_property("name") name = tag.get_property("name")
if name == "bold": if name == "bold":
@ -289,12 +268,12 @@ class RichTextSerializer:
elif name == "italic": elif name == "italic":
return "<italic>" return "<italic>"
elif name == "link": elif name == "link":
address = self._parse_object_id(it) address_tag = self.buf.get_tag_table().lookup("link-address")
end = it.copy()
end.forward_to_tag_toggle(address_tag)
address = self.buf.get_text(it, end)
return "<link " + "href=\"" + address + "\">" return "<link " + "href=\"" + address + "\">"
elif name == "icon": elif name == "link-address":
name = self._parse_object_id(it)
return "<icon " + "name=\"" + name + "\"/>"
elif name == "object-id":
return "" return ""
elif name.startswith("font-size-"): elif name.startswith("font-size-"):
tag_name = name.replace("font-size-", "", 1) tag_name = name.replace("font-size-", "", 1)
@ -310,9 +289,7 @@ class RichTextSerializer:
return "</italic>" return "</italic>"
elif name == "link": elif name == "link":
return "</link>" return "</link>"
elif name == "icon": elif name == "link-address":
return ""
elif name == "object-id":
return "" return ""
elif name.startswith("font-size-"): elif name.startswith("font-size-"):
return "</font>" return "</font>"
@ -392,7 +369,6 @@ if __name__ == "__main__":
test_xml += "<bold><italic>Test two</italic></bold>" test_xml += "<bold><italic>Test two</italic></bold>"
test_xml += "<font size=\"xx-small\">Test three</font>" test_xml += "<font size=\"xx-small\">Test three</font>"
test_xml += "<link href=\"http://www.gnome.org\">Test link</link>" test_xml += "<link href=\"http://www.gnome.org\">Test link</link>"
test_xml += "<icon name=\"stock_help-chat\"/>"
test_xml += "</richtext>" test_xml += "</richtext>"
RichTextSerializer().deserialize(test_xml, rich_buf) RichTextSerializer().deserialize(test_xml, rich_buf)

View File

@ -1,25 +1,27 @@
from SVGdraw import path from SVGdraw import path
class Sketch: class Sketch:
def __init__(self): def __init__(self, rgb):
self._points = [] self._points = []
self._rgb = (float(rgb[0]), float(rgb[1]), float(rgb[2]))
def add_point(self, x, y): def add_point(self, x, y):
self._points.append([x, y]) self._points.append((x, y))
def draw(self, ctx): def draw(self, ctx):
start = True start = True
for [x, y] in self._points: for (x, y) in self._points:
if start: if start:
ctx.move_to(x, y) ctx.move_to(x, y)
start = False start = False
else: else:
ctx.line_to(x, y) ctx.line_to(x, y)
ctx.set_source_rgb(self._rgb[0], self._rgb[1], self._rgb[2])
ctx.stroke() ctx.stroke()
def draw_to_svg(self): def draw_to_svg(self):
i = 0 i = 0
for [x, y] in self._points: for (x, y) in self._points:
coords = str(x) + ' ' + str(y) + ' ' coords = str(x) + ' ' + str(y) + ' '
if i == 0: if i == 0:
path_data = 'M ' + coords path_data = 'M ' + coords
@ -28,4 +30,5 @@ class Sketch:
else: else:
path_data += coords path_data += coords
i += 1 i += 1
return path(path_data, fill = 'none', stroke = '#000000') color = "#%02X%02X%02X" % (255 * self._rgb[0], 255 * self._rgb[1], 255 * self._rgb[2])
return path(path_data, fill = 'none', stroke = color)

View File

@ -13,6 +13,7 @@ class SketchPad(gtk.DrawingArea):
gtk.DrawingArea.__init__(self) gtk.DrawingArea.__init__(self)
self._active_sketch = None self._active_sketch = None
self._rgb = (0.0, 0.0, 0.0)
self._sketches = [] self._sketches = []
self.add_events(gtk.gdk.BUTTON_PRESS_MASK | self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
@ -23,6 +24,7 @@ class SketchPad(gtk.DrawingArea):
self.connect('expose_event', self.expose) self.connect('expose_event', self.expose)
def expose(self, widget, event): def expose(self, widget, event):
"""Draw the background of the sketchpad."""
rect = self.get_allocation() rect = self.get_allocation()
ctx = widget.window.cairo_create() ctx = widget.window.cairo_create()
@ -38,27 +40,28 @@ class SketchPad(gtk.DrawingArea):
return False 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): def add_sketch(self, sketch):
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)
def __button_press_cb(self, widget, event): def __button_press_cb(self, widget, event):
self._active_sketch = Sketch() self._active_sketch = Sketch(self._rgb)
self.add_sketch(self._active_sketch) self.add_sketch(self._active_sketch)
self.add_point(event)
def __button_release_cb(self, widget, event): def __button_release_cb(self, widget, event):
self.add_point(event)
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) if self._active_sketch:
self._active_sketch.add_point(event.x, event.y)
self.window.invalidate_rect(None, False)
def to_svg(self): def to_svg(self):
"""Return a string containing an SVG representation of this sketch."""
d = drawing() d = drawing()
s = svg() s = svg()
for sketch in self._sketches: for sketch in self._sketches:

View File

@ -18,6 +18,9 @@ class ColorButton(gtk.RadioButton):
self.add(drawing_area) self.add(drawing_area)
drawing_area.show() drawing_area.show()
def color(self):
return self._rgb
def expose(self, widget, event): def expose(self, widget, event):
rect = widget.get_allocation() rect = widget.get_allocation()
ctx = widget.window.cairo_create() ctx = widget.window.cairo_create()
@ -31,7 +34,9 @@ class ColorButton(gtk.RadioButton):
class Toolbox(gtk.VBox): class Toolbox(gtk.VBox):
__gsignals__ = { __gsignals__ = {
'tool-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, 'tool-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_STRING])) ([gobject.TYPE_STRING])),
'color-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
} }
def __init__(self): def __init__(self):
@ -96,4 +101,5 @@ class Toolbox(gtk.VBox):
self.emit("tool-selected", tool_id) self.emit("tool-selected", tool_id)
def __color_clicked_cb(self, button, rgb): def __color_clicked_cb(self, button, rgb):
pass self.emit("color-selected", button.color())

View File

@ -4,9 +4,6 @@ try:
from sugar.__uninstalled__ import * from sugar.__uninstalled__ import *
except ImportError: except ImportError:
from sugar.__installed__ import * from sugar.__installed__ import *
def get_user_dir():
return os.path.expanduser('~/.sugar/')
def get_data_file(filename): def get_data_file(filename):
for data_dir in get_data_dirs(): for data_dir in get_data_dirs():

View File

@ -20,7 +20,7 @@ def start(console):
act_dir = os.path.join(data_dir, env.get_activities_dir()) act_dir = os.path.join(data_dir, env.get_activities_dir())
activities_dirs.append(act_dir) activities_dirs.append(act_dir)
activities_dirs.append(os.path.join(env.get_user_dir(), 'activities')) activities_dirs.append(os.path.expanduser('~/.sugar/activities'))
for activities_dir in activities_dirs: for activities_dir in activities_dirs:
if os.path.isdir(activities_dir): if os.path.isdir(activities_dir):