Activity tutorial: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{{dated}}
: ''As things evolve rapidly, it's sometimes hard to keep [http://lists.laptop.org/pipermail/sugar/2007-June/002719.html tutorial and demo material upto date]... ''
: ''In the meantime you may want to take a look at http://dev.laptop.org/~edsiper/sugar/Test.activity.tar.gz''
: {{Pending|need a new tutorial?}}
----

The tutorial explains step by step how to create the [http://evan.martin.googlepages.com/HelloWorld-1.xo Hello World activity bundle].
The tutorial explains step by step how to create the [http://evan.martin.googlepages.com/HelloWorld-1.xo Hello World activity bundle].


Line 44: Line 38:
Code your activity. Module and class names should match those specified in the exec field of the info. That is, the exec must be specified as a direct dotted path to the class name. The top-level is the hello.activity directory, so for the activity.info file above, we specify a top-level module named HelloWorldActivity.HelloWorldActivity (please note that the use of uppercase names in module names is considered poor Python style, feel free to use an activity name with more standard style names).
Code your activity. Module and class names should match those specified in the exec field of the info. That is, the exec must be specified as a direct dotted path to the class name. The top-level is the hello.activity directory, so for the activity.info file above, we specify a top-level module named HelloWorldActivity.HelloWorldActivity (please note that the use of uppercase names in module names is considered poor Python style, feel free to use an activity name with more standard style names).


from sugar.activity import activity
import logging
import logging
from sugar.activity import activity
import sys, os
import sys, os
Line 51: Line 45:
class HelloWorldActivity(activity.Activity):
class HelloWorldActivity(activity.Activity):
# This is a callback function. The data arguments are ignored
# in this example. More on callbacks below.
def hello(self, widget, data=None):
def hello(self, widget, data=None):
logging.info('Hello World')
logging.info('Hello World')
def __init__(self, handle):
def __init__(self, handle):
print "running activity init", handle
activity.Activity.__init__(self, handle)
activity.Activity.__init__(self, handle)
print "activity running"
self.set_title('Hello World')
# Creates the Toolbox. It contains the Activity Toolbar, which is the
# bar that appears on every Sugar window and contains essential
# functionalities, such as the 'Collaborate' and 'Close' buttons.
toolbox = activity.ActivityToolbox(self)
self.set_toolbox(toolbox)
toolbox.show()
# Creates a new button with the label "Hello World".
# Creates a new button with the label "Hello World".
self.button = gtk.Button("Hello World")
self.button = gtk.Button("Hello World")
# When the button receives the "clicked" signal, it will call the
# When the button receives the "clicked" signal, it will call the
# function hello() passing it None as its argument. The hello()
# function hello() passing it None as its argument. The hello()
# function is defined above.
# function is defined above.
self.button.connect("clicked", self.hello, None)
self.button.connect("clicked", self.hello, None)
# This packs the button into our child (a Gtk container widget of type
# Set the button to be our canvas. The canvas is the main section of
# gtk.VBox). Every Sugar window (such as ourselves) has a container
# every Sugar Window. It fills all the area below the toolbox.
self.set_canvas(self.button)
# like this one, which organizes its children into a single column.
self.get_child().add(self.button)
# The final step is to display this newly created widget.
# The final step is to display this newly created widget.
self.button.show()
self.button.show()
print "AT END OF THE CLASS"
self.set_title('Hello World')



Create a MANIFEST, containing the list of the files to include in the package. (Note: Be sure '''not''' to leave blank lines at the end of the file.)
Create a MANIFEST, containing the list of the files to include in the package. (Note: Be sure '''not''' to leave blank lines at the end of the file.)

Revision as of 17:55, 28 June 2007

The tutorial explains step by step how to create the Hello World activity bundle.

Create the bundle directory structure:

mkdir HelloWorldActivity.activity
mkdir HelloWorldActivity.activity/activity

Write the activity.info file, to describe your bundle in the activity sub-directory (i.e. HelloWorldActivity.activity/activity/activity.info). The Activity Bundles specification explain in detail the meaning of each field.

[Activity]
name = HelloWorld
service_name = com.ywwg.HelloWorldActivity
class = HelloWorldActivity.HelloWorldActivity
icon = activity-helloworld
activity_version = 1
show_launcher = yes

Design an icon for your activity, according to the icon format and place it in the activity sub-directory. The file name should match the one specified in the info "activity-helloworld.svg".

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
  <!ENTITY fill_color "#FFFFFF">
  <!ENTITY stroke_color "#000000">
]>
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
  <rect x="1" y="1" width="48" height="48"
  style="fill:&fill_color;;stroke:&stroke_color;;stroke-width:2"/>
</svg>

Write the setup.py script in the top level directory (i.e. hello.activity/setup.py), which in most cases will look like this:

#!/usr/bin/env python
from sugar.activity import bundlebuilder
if __name__ == "__main__":
    bundlebuilder.start()

Code your activity. Module and class names should match those specified in the exec field of the info. That is, the exec must be specified as a direct dotted path to the class name. The top-level is the hello.activity directory, so for the activity.info file above, we specify a top-level module named HelloWorldActivity.HelloWorldActivity (please note that the use of uppercase names in module names is considered poor Python style, feel free to use an activity name with more standard style names).

from sugar.activity import activity
import logging

import sys, os
import gtk

class HelloWorldActivity(activity.Activity):
    def hello(self, widget, data=None):
        logging.info('Hello World')

    def __init__(self, handle):
        print "running activity init", handle
        activity.Activity.__init__(self, handle)
        print "activity running"

        self.set_title('Hello World')

        # Creates the Toolbox. It contains the Activity Toolbar, which is the
        # bar that appears on every Sugar window and contains essential
        # functionalities, such as the 'Collaborate' and 'Close' buttons.
        toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(toolbox)
        toolbox.show()

        # Creates a new button with the label "Hello World".
        self.button = gtk.Button("Hello World")
    
        # When the button receives the "clicked" signal, it will call the
        # function hello() passing it None as its argument.  The hello()
        # function is defined above.
        self.button.connect("clicked", self.hello, None)
    
        # Set the button to be our canvas. The canvas is the main section of
        # every Sugar Window. It fills all the area below the toolbox.
        self.set_canvas(self.button)
    
        # The final step is to display this newly created widget.
        self.button.show()
    
        print "AT END OF THE CLASS"

Create a MANIFEST, containing the list of the files to include in the package. (Note: Be sure not to leave blank lines at the end of the file.)

HelloWorldActivity.py

Your directory structure should now look like this:

HelloWorldActivity.activity/
HelloWorldActivity.activity/setup.py
HelloWorldActivity.activity/activity
HelloWorldActivity.activity/activity/activity.info
HelloWorldActivity.activity/activity/activity-helloworld.svg
HelloWorldActivity.activity/HelloWorldActivity.py
HelloWorldActivity.activity/MANIFEST

Setup your bundle for development

./setup.py dev

(It appears this just creates a symlink in ~/Activities .)

Running

If you now run sugar the activity icon should be visible on the frame. (You have to restart sugar to get it to pick up the change if you just installed it. Hit ctrl-alt-erase.)

You can also edit the code in your bundle directory directly. Note that the first time your Activity is launched, it leaves a process around even if you close the window, so you must kill the sugar-activity-factory to get it to reload when you click again.

Distribution

Create a xo package to distribute your bundle. (An xo file is essentially a zip file built from the MANIFEST with some extra metadata, like a JAR file. It also has some localization ability, and in the future we expect to be able to sign these too.)

 ./setup.py dist

To install the xo on a laptop you can use the installer script.

 sugar-install-bundle HelloWorld-1.xo

XXX Explain what the -1 suffix means.