Activity tutorial: Difference between revisions

From OLPC
Jump to navigation Jump to search
m (re-categorization)
(dated notice with ref)
Line 1: Line 1:
{{OLPC}}
{{OLPC}}
{{dated}}
<blockquote>
Marco Pesenti Gritti on [http://mailman.laptop.org/pipermail/sugar/2007-February/001226.html Mon Feb 5 11:48:39 EST 2007] says:
:The tutorial is obsolete. Until we have an updated version your best bet is to look at an existing activity:

::http://dev.laptop.org/git.do?p=journal-activity;a=tree

:Also we have a spec about activity bundles but it's not fully implemented yet:

::[[Activity bundles]]
</blockquote>
----


Here is a short tutorial about writing a sugar activity. It's a step by step guide to get you started quickly, without going in details of the activity system. Download the [http://www.snowedin.net/misc/sugar-drawing-0.2.tar.gz current example sources] (Marco's original sources are [http://www.gnome.org/~marco/sugar-drawing-0.1.tar.gz here]).
Here is a short tutorial about writing a sugar activity. It's a step by step guide to get you started quickly, without going in details of the activity system. Download the [http://www.snowedin.net/misc/sugar-drawing-0.2.tar.gz current example sources] (Marco's original sources are [http://www.gnome.org/~marco/sugar-drawing-0.1.tar.gz here]).

Revision as of 17:03, 5 February 2007

  This page is monitored by the OLPC team.
Emblem-warning.png The currency of this article or section may be limited by out-of-date information.
There may be relevant discussion on its talk page

Marco Pesenti Gritti on Mon Feb 5 11:48:39 EST 2007 says:

The tutorial is obsolete. Until we have an updated version your best bet is to look at an existing activity:
http://dev.laptop.org/git.do?p=journal-activity;a=tree
Also we have a spec about activity bundles but it's not fully implemented yet:
Activity bundles

Here is a short tutorial about writing a sugar activity. It's a step by step guide to get you started quickly, without going in details of the activity system. Download the current example sources (Marco's original sources are here).

Write the build system

Create the autogen.sh script

You should only need to change the module name and activity file from this example.

#!/bin/sh
# Run this to generate all the initial makefiles, etc.

srcdir=`dirname $0`
test -z "$srcdir" && srcdir=.

PKG_NAME="sugar-drawing"

(test -f $srcdir/drawing.activity) || {
    echo -n "**Error**: Directory "\`$srcdir\'" does not look like the"
    echo " top-level $PKG_NAME directory"
    exit 1
}

which gnome-autogen.sh || {
    echo "You need to install gnome-common from the GNOME CVS"
    exit 1
}

REQUIRED_AUTOMAKE_VERSION=1.9 USE_GNOME2_MACROS=1 . gnome-autogen.sh

Create a configure.ac file

The first line of configure.ac specifies the name of the application, the version number, a bug reporting address, and the name of the module.

The configure.ac file contains a list of all the Makefiles in your project. You will need one Makefile for every directory in your project.

AC_INIT([Sugar Drawing],[0.1],[],[sugar-drawing])
AM_INIT_AUTOMAKE

AM_PATH_PYTHON

AC_OUTPUT([
Makefile
other_dir/Makefile
])

Other required files

Automake checks for the existence of a few required files, you have to create them even if empty:

$ touch NEWS README AUTHORS ChangeLog

Makefile.am

This file tells automake how to install your program.

in the Makefile.am, SUBDIRS is a space-separated list of subdirectories of the current folder, an activitydir that describes where files in the current folder will be installed, and activity_PYTHON which describes what files to install.

The backslash at the end of the activity_PYTHON lines indicates that more data exists on the following lines. (Note that the last line does not have a backslash).

(fixme: exactly what does EXTRA_DIST mean? -- I think it specifies non-*.py files that need to be included in the distribution) (fixme #2: the variable datadir appears to be undefined. I received the error "make[1]: sugar-setup-activity command not found" setting the variable datadir in autogen.sh fixed it for me datadir=[sugar-jhbuild]/source)

SUBDIRS = foo bar
activitydir = $(datadir)/sugar/activities/drawing
activity_PYTHON =               \
        __init__.py             \
        DrawingActivity.py

EXTRA_DIST = drawing.activity

install-data-local:
        sugar-setup-activity $(srcdir)/drawing.activity

Write the activity code

Write a subclass of sugar.activity.Activity. It's a GtkWindow so you can use the "add" method to insert your own widgets. The source of DrawingActivity.py demonstrates it:

import gtk

from sugar.activity.Activity import Activity

class DrawingActivity(Activity):
        def __init__(self):
                Activity.__init__(self)
                button = gtk.Button('Drawing')
                self.add(button)
                button.show()

Or if you are adapting a full application, your activity could look something like this: (I found I needed to delete the app object explicitly)

import gtk

from sugar.activity.Activity import Activity
from my_module.my_app import my_app

class DrawingActivity(Activity):
        def __init__(self):
                Activity.__init__(self)

                app = my_app.my_app()
                self.add(app.some_widget)
                app.do_show()
                self.connect('destroy',self.do_quit, app)

        def do_quit(self, event, app):
                app.do_quit()
                del app

create __init__.py

This is the package initialization file. It can be blank.

$ touch __init__.py

drawing.activity

Include the necessary information about the activity.

(fixme: what is default_type?)

[Activity]
name = Drawing
id = org.laptop.sugar.Drawing
python_module = drawing.DrawingActivity.DrawingActivity
default_type = _drawing_olpc._udp
show_launcher = yes

Build and install

Initialize the build system. The value of prefix depends on the path of sugar-jhbuild:

$ ./autogen.sh --prefix=[SUGAR-JHBUILD]/build

Build and install:

$ [SUGAR-JHBUILD]/sugar-jhbuild shell
$ make
$ make install