Permalink
Browse files

Runs both as a Sugar activity and as a stand alone app

  • Loading branch information...
1 parent f79c6a0 commit f383f4ba4276a68b2f74fde0fc4817aa30367f64 @cgueret committed Mar 1, 2011
Showing with 204 additions and 22 deletions.
  1. +26 −14 src/Backend.py
  2. +6 −6 src/Frontend.py
  3. +9 −0 src/MANIFEST
  4. +40 −0 src/SortGameActivity.py
  5. +9 −0 src/activity/activity.info
  6. +101 −0 src/activity/sort.svg
  7. +4 −2 src/main.py
  8. +9 −0 src/setup.py
View
@@ -7,6 +7,7 @@
from rdflib import RDF, Literal, URIRef
from sugar.graphics import style
import gtk
+import os
class Box(DatastoreItem):
'''
@@ -29,12 +30,13 @@ class Item(DatastoreItem):
def __init__(self, id=None):
#DatastoreItem.__init__(self, 'Item', id)
super(Item, self).__init__('Item', id)
- print 'fdfds'
self.pixbuf = None
-
+ self.activity_root = None
+
def get_depiction(self):
if self.pixbuf == None:
- self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.get_metadata('hasDepiction')[0], style.zoom(160), style.zoom(120))
+ file_name = os.path.join(self.activity_root, 'data', self.get_metadata('hasDepiction')[0])
+ self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(file_name, style.zoom(160), style.zoom(120))
return self.pixbuf
def set_depiction(self, file_name):
@@ -46,42 +48,50 @@ def get_name(self):
def set_name(self, name):
self.set_metadata('name', Literal(name))
+ def set_activity_root(self, activity_root):
+ self.activity_root = activity_root
+
@classmethod
def cast(cls, instance):
instance.__class__ = cls
instance.pixbuf = None
+ instance.activity_root = None
class BackEnd(object):
- def __init__(self, datastore):
+ def __init__(self, datastore, activity_root):
self.datastore = datastore
+ self.activity_root = activity_root
def add_item(self, file_name):
'''
Store a new item in the data store
'''
+ # Create the entry in the data store
item = Item()
item.set_name(file_name.split('.')[0])
item.set_depiction(file_name)
self.datastore.save_item(item)
+ # Save the image
+ imagebytes = file.read(open(file_name, 'r'))
+ f = open(os.path.join(self.activity_root, 'data', file_name), 'w')
+ try:
+ f.write(imagebytes)
+ finally:
+ f.close()
def get_items(self):
'''
Query the datastore for all the items
'''
- items = self.datastore.get_items('Item')
- for item in items:
- item = Item.cast(item)
- #print items[0].get_metadata()
- #print items
- return items
-
- query = 'SELECT * WHERE { ?s <%s> <%s>}' % (RDF.type, OLPC['Item'])
+ #items = self.datastore.get_items('Item')
+ query = 'SELECT * WHERE { ?s <%s> <%s>} ORDER BY ?s' % (RDF.type, OLPC['Item'])
items = []
for line in self.datastore.sparql_get(query):
uri = line.split(',')[-1][4:-1]
id = uri.split('/')[-1]
print "load item %s" % id
item = Item(id)
+ item.set_activity_root(self.activity_root)
items.append(item)
# Load all the meta data
query = 'SELECT * WHERE { <%s> ?p ?o}' % item.get_resource()
@@ -94,9 +104,11 @@ def get_items(self):
o = URIRef(o[4:-1])
elif o[0] == '"':
o = Literal(o[1:-1])
- item.set_metadata(p, o)
-
+ item.set_metadata(p, o)
return items
+ #for item in items:
+ # item = Item.cast(item)
+ #return items
def get_boxes(self):
'''
View
@@ -18,7 +18,7 @@ def __init__(self):
class SortingPanel(object):
DND_TARGET = [("text/plain", gtk.TARGET_SAME_APP, 0)]
- def __init__(self, datastore):
+ def __init__(self, datastore, backend):
'''
Constructor
'''
@@ -32,6 +32,7 @@ def __init__(self, datastore):
# The data store to dialog with
self.datastore = datastore
+ self.backend = backend
# Create the left part
left_part = gtk.Notebook()
@@ -64,9 +65,8 @@ def __init__(self, datastore):
self.widget.attach(right_part, 1, 2, 0, 1, xoptions=gtk.EXPAND | gtk.FILL, yoptions=gtk.EXPAND | gtk.FILL, xpadding=style.DEFAULT_SPACING, ypadding=style.DEFAULT_SPACING)
# Get the data from the backend
- backend = BackEnd(datastore)
- boxes = backend.get_boxes()
- items = backend.get_items()
+ boxes = self.backend.get_boxes()
+ items = self.backend.get_items()
# Load the boxes
for box in boxes:
@@ -166,7 +166,7 @@ def get_widget(self):
class MainWindow(object):
- def __init__(self, datastore):
+ def __init__(self, datastore, backend):
'''
Constructor
'''
@@ -179,7 +179,7 @@ def __init__(self, datastore):
self.window.set_position(gtk.WIN_POS_CENTER)
# Create the panels of the application
- sortingPanel = SortingPanel(datastore)
+ sortingPanel = SortingPanel(datastore, backend)
# Pack everything
self.panels = gtk.Notebook()
View
@@ -0,0 +1,9 @@
+Frontend.py
+SortGameActivity.py
+Backend.py
+main.py
+Datastore.py
+setup.py
+test.py
+activity/activity.info
+activity/sort.svg
View
@@ -0,0 +1,40 @@
+'''
+Created on Mar 1, 2011
+
+@author: cgueret
+'''
+from sugar.activity import activity
+from sugar.graphics import style
+from Frontend import SortingPanel
+from Datastore import Datastore
+from Backend import BackEnd
+import pango
+import logging
+
+logger = logging.getLogger('sortgame-activity')
+
+
+
+class SortGameActivity(activity.Activity):
+ def __init__(self, handle):
+ activity.Activity.__init__(self, handle)
+ logger.debug("test")
+
+ # Configure the toolbox
+ toolbox = activity.ActivityToolbox(self)
+ activity_toolbar = toolbox.get_activity_toolbar()
+ activity_toolbar.keep.props.visible = False
+ activity_toolbar.share.props.visible = False
+ self.set_toolbox(toolbox)
+ toolbox.show()
+
+ # Create a data store and the app
+ datastore = Datastore("127.0.0.1:8080")
+ backend = BackEnd(datastore, self.get_activity_root())
+ main = SortingPanel(datastore, backend)
+ widget = main.get_widget()
+
+ # pack
+ self.set_canvas(widget)
+ widget.grab_focus()
+ widget.modify_font(pango.FontDescription("sans %d" % style.zoom(10)))
@@ -0,0 +1,9 @@
+[Activity]
+name = Sort Game
+service_name = nl.vu.SortGameActivity
+icon = sort
+exec = sugar-activity SortGameActivity.SortGameActivity
+show_launcher = yes
+activity_version = 1
+mime_types = text/plain
+license = GPLv2+
Oops, something went wrong.

0 comments on commit f383f4b

Please sign in to comment.