Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/GNUChessActivity.py
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2012-07-20 18:11:28 (GMT)
committer Walter Bender <walter.bender@gmail.com>2012-07-20 18:11:28 (GMT)
commit41edaf53b46d78cd5f233d3dc4c0896484251f86 (patch)
treedc14fa2aa3f786cbfc396ecfe60b724cdfe16c01 /GNUChessActivity.py
parentcee52201c3871749860abf702ad3df390baf02ef (diff)
progess
Diffstat (limited to 'GNUChessActivity.py')
-rw-r--r--GNUChessActivity.py259
1 files changed, 132 insertions, 127 deletions
diff --git a/GNUChessActivity.py b/GNUChessActivity.py
index a728091..01dc5d6 100644
--- a/GNUChessActivity.py
+++ b/GNUChessActivity.py
@@ -14,16 +14,10 @@ import gtk
from sugar.activity import activity
from sugar import profile
-try:
- from sugar.graphics.toolbarbox import ToolbarBox
- _have_toolbox = True
-except ImportError:
- _have_toolbox = False
-
-if _have_toolbox:
- from sugar.activity.widgets import ActivityToolbarButton
- from sugar.activity.widgets import StopButton
- from sugar.graphics.toolbarbox import ToolbarButton
+from sugar.graphics.toolbarbox import ToolbarBox
+from sugar.activity.widgets import ActivityToolbarButton
+from sugar.activity.widgets import StopButton
+from sugar.graphics.toolbarbox import ToolbarButton
from toolbar_utils import button_factory, label_factory, separator_factory, \
radio_factory, entry_factory
@@ -50,15 +44,20 @@ PATH = '/org/augarlabs/GNUChessActivity'
class GNUChessActivity(activity.Activity):
- """ Gnuchess interface from Sugar """
+ ''' Gnuchess interface from Sugar '''
def __init__(self, handle):
- """ Initialize the toolbars and the gnuchess """
+ ''' Initialize the toolbars and the gnuchess '''
try:
super(GNUChessActivity, self).__init__(handle)
except dbus.exceptions.DBusException, e:
_logger.error(str(e))
+ self.playing_white = True
+ self.playing_mode = 'easy'
+ self.playing_robot = True
+ self._restoring = False
+
self.nick = profile.get_nick_name()
self._reload_custom = False
if profile.get_color() is not None:
@@ -66,13 +65,9 @@ class GNUChessActivity(activity.Activity):
else:
self.colors = ['#A0FFA0', '#FF8080']
- self._setup_toolbars(_have_toolbox)
+ self._setup_toolbars()
self._setup_dispatch_table()
- self.playing_white = True
- self.mode = 'easy'
- self.playing_robot = True
-
# Create a canvas
canvas = gtk.DrawingArea()
canvas.set_size_request(gtk.gdk.screen_width(), \
@@ -88,16 +83,14 @@ class GNUChessActivity(activity.Activity):
self._setup_presence_service()
if 'saved_game' in self.metadata:
+ self._restoring = True
self._restore()
else:
self._gnuchess.new_game()
+ self._restoring = False
- self._make_custom_toolbar()
- if self._reload_custom:
- self._custom_cb()
-
- def _setup_toolbars(self, have_toolbox):
- """ Setup the toolbars. """
+ def _setup_toolbars(self):
+ ''' Setup the toolbars. '''
self.max_participants = 1 # No sharing to begin with
@@ -105,57 +98,47 @@ class GNUChessActivity(activity.Activity):
self.view_toolbar = gtk.Toolbar()
self.adjust_toolbar = gtk.Toolbar()
self.custom_toolbar = gtk.Toolbar()
- if have_toolbox:
- toolbox = ToolbarBox()
-
- # Activity toolbar
- activity_button = ActivityToolbarButton(self)
- toolbox.toolbar.insert(activity_button, 0)
- activity_button.show()
-
- edit_toolbar_button = ToolbarButton(
- label=_("Edit"), page=self.edit_toolbar,
- icon_name='toolbar-edit')
- self.edit_toolbar.show()
- toolbox.toolbar.insert(edit_toolbar_button, -1)
- edit_toolbar_button.show()
-
- view_toolbar_button = ToolbarButton(
- label=_("View"), page=self.view_toolbar,
- icon_name='toolbar-view')
- self.view_toolbar.show()
- toolbox.toolbar.insert(view_toolbar_button, -1)
- view_toolbar_button.show()
+ toolbox = ToolbarBox()
+
+ activity_button = ActivityToolbarButton(self)
+ toolbox.toolbar.insert(activity_button, 0)
+ activity_button.show()
+
+ edit_toolbar_button = ToolbarButton(label=_("Edit"),
+ page=self.edit_toolbar,
+ icon_name='toolbar-edit')
+ self.edit_toolbar.show()
+ toolbox.toolbar.insert(edit_toolbar_button, -1)
+ edit_toolbar_button.show()
+
+ view_toolbar_button = ToolbarButton(label=_("View"),
+ page=self.view_toolbar,
+ icon_name='toolbar-view')
+ self.view_toolbar.show()
+ toolbox.toolbar.insert(view_toolbar_button, -1)
+ view_toolbar_button.show()
- adjust_toolbar_button = ToolbarButton(
- label=_('Adjust'), page=self.adjust_toolbar,
- icon_name='preferences-system')
- self.adjust_toolbar.show()
- toolbox.toolbar.insert(adjust_toolbar_button, -1)
- adjust_toolbar_button.show()
-
- custom_toolbar_button = ToolbarButton(
- label=_("Custom"), page=self.custom_toolbar,
- icon_name='view-source')
- self.custom_toolbar.show()
- toolbox.toolbar.insert(custom_toolbar_button, -1)
- custom_toolbar_button.show()
-
- self.set_toolbar_box(toolbox)
- toolbox.show()
- self.toolbar = toolbox.toolbar
- else:
- # Use pre-0.86 toolbar design
- toolbox = activity.ActivityToolbox(self)
- self.set_toolbox(toolbox)
- toolbox.add_toolbar(_('Edit'), self.edit_toolbar)
- toolbox.add_toolbar(_('View'), self.view_toolbar)
- toolbox.add_toolbar(_('Adjust'), self.adjust_toolbar)
- toolbox.add_toolbar(_('Custom'), self.custom_toolbar)
- toolbox.show()
- toolbox.set_current_toolbar(1)
- gnuchess_toolbar = gtk.Toolbar()
- self.toolbar = gnuchess_toolbar
+ adjust_toolbar_button = ToolbarButton(label=_('Adjust'),
+ page=self.adjust_toolbar,
+ icon_name='preferences-system')
+ self.adjust_toolbar.show()
+ toolbox.toolbar.insert(adjust_toolbar_button, -1)
+ adjust_toolbar_button.show()
+
+ '''
+ custom_toolbar_button = ToolbarButton(label=_("Custom"),
+ page=self.custom_toolbar,
+ icon_name='view-source')
+ self.custom_toolbar.show()
+ toolbox.toolbar.insert(custom_toolbar_button, -1)
+ custom_toolbar_button.show()
+ '''
+
+ self.set_toolbar_box(toolbox)
+ toolbox.show()
+ self.toolbar = toolbox.toolbar
+
+ adjust_toolbar_button.set_expanded(True)
button_factory('edit-copy',
self.edit_toolbar,
@@ -169,9 +152,6 @@ class GNUChessActivity(activity.Activity):
tooltip=_('Paste'),
accelerator='<Ctrl>v')
- if not _have_toolbox:
- separator_factory(gnuchess_toolbar, False, True)
-
button_factory('view-fullscreen',
self.view_toolbar,
self.do_fullscreen_cb,
@@ -198,11 +178,11 @@ class GNUChessActivity(activity.Activity):
group=None,
tooltip=_('Play White'))
- play_black_button = radio_factory('black-rook',
- self.adjust_toolbar,
- self._play_black_cb,
- group=play_white_button,
- tooltip=_('Play Black'))
+ self.play_black_button = radio_factory('black-rook',
+ self.adjust_toolbar,
+ self._play_black_cb,
+ group=play_white_button,
+ tooltip=_('Play Black'))
separator_factory(self.adjust_toolbar, False, True)
@@ -212,11 +192,11 @@ class GNUChessActivity(activity.Activity):
group=None,
tooltip=_('Beginner'))
- hard_button = radio_factory('expert',
- self.adjust_toolbar,
- self._hard_cb,
- group=easy_button,
- tooltip=_('Expert'))
+ self.hard_button = radio_factory('expert',
+ self.adjust_toolbar,
+ self._hard_cb,
+ group=easy_button,
+ tooltip=_('Expert'))
separator_factory(self.adjust_toolbar, False, True)
@@ -226,11 +206,11 @@ class GNUChessActivity(activity.Activity):
group=None,
tooltip=_('Play against the computer'))
- human_button = radio_factory('human',
- self.adjust_toolbar,
- self._human_cb,
- group=robot_button,
- tooltip=_('Play against a person'))
+ self.human_button = radio_factory('human',
+ self.adjust_toolbar,
+ self._human_cb,
+ group=robot_button,
+ tooltip=_('Play against a person'))
button_factory('new-game',
self.toolbar,
@@ -242,60 +222,85 @@ class GNUChessActivity(activity.Activity):
self._undo_cb,
tooltip=_('Undo'))
- button_factory('edit-undo',
+ button_factory('hint',
self.toolbar,
- self._undo_cb,
+ self._hint_cb,
tooltip=_('Hint'))
separator_factory(self.toolbar, False, False)
self.status = label_factory(self.toolbar, '')
self.status.set_label(_("It is White's move."))
- if _have_toolbox:
- separator_factory(toolbox.toolbar, True, False)
- stop_button = StopButton(self)
- stop_button.props.accelerator = '<Ctrl>q'
- toolbox.toolbar.insert(stop_button, -1)
- stop_button.show()
+ separator_factory(toolbox.toolbar, True, False)
+ stop_button = StopButton(self)
+ stop_button.props.accelerator = '<Ctrl>q'
+ toolbox.toolbar.insert(stop_button, -1)
+ stop_button.show()
def do_fullscreen_cb(self, button):
''' Hide the Sugar toolbars. '''
self.fullscreen()
def _copy_cb(self, *args):
- return
+ ''' Copying as JSON data (FIXME) '''
+ clipboard = gtk.Clipboard()
+ _logger.debug('Serialize the game and copy to clipboard.')
+ text = json_dump(self._gnuchess.save_game())
+ if text is not None:
+ clipboard.set_text(text)
def _paste_cb(self, *args):
- return
+ ''' Pasting from JSON data (FIXME) '''
+ clipboard = gtk.Clipboard()
+ _logger.debug('Restore from paste')
+ text = clipboard.wait_for_text()
+ if text is not None:
+ self._gnuchess.restore_game(json_load(text))
def _undo_cb(self, *args):
self._gnuchess.undo()
+ def _hint_cb(self, *args):
+ self._gnuchess.hint()
+
def _play_white_cb(self, *args):
- self.playing_white = True
+ _logger.debug('in play white cb')
+ if not self._restoring:
+ _logger.debug('setting play white')
+ self.playing_white = True
+ self._gnuchess.new_game()
return
def _play_black_cb(self, *args):
- self.playing_white = False
+ _logger.debug('in play black cb')
+ if not self._restoring:
+ _logger.debug('setting play black')
+ self.playing_white = False
+ self._gnuchess.new_game()
return
def _easy_cb(self, *args):
- self.mode = 'easy'
+ if not self._restoring:
+ self.playing_mode = 'easy'
+ self._gnuchess.new_game()
return
def _hard_cb(self, *args):
- self.mode = 'hard'
+ if not self._restoring:
+ self.playing_mode = 'hard'
+ self._gnuchess.new_game()
return
def _robot_cb(self, *args):
- self.playing_robot = True
+ if not self._restoring:
+ self.playing_robot = True
+ self._gnuchess.new_game()
return
def _human_cb(self, *args):
- self.playing_robot = False
- return
-
- def _make_custom_toolbar(self):
+ if not self._restoring:
+ self.playing_robot = False
+ self._gnuchess.new_game()
return
def _new_gnuchess_cb(self, button=None):
@@ -306,43 +311,43 @@ class GNUChessActivity(activity.Activity):
return
def write_file(self, file_path):
- """ Write the grid status to the Journal """
+ ''' Write the grid status to the Journal '''
self.metadata['saved_game'] = json_dump(self._gnuchess.save_game())
if self.playing_white:
self.metadata['playing_white'] = 'True'
else:
self.metadata['playing_white'] = 'False'
- self.metadata['playing_mode'] = self.mode
+ self.metadata['playing_mode'] = self.playing_mode
if self.playing_robot:
self.metadata['playing_robot'] = 'True'
else:
self.metadata['playing_robot'] = 'False'
- _logger.debug(json_dump(self._gnuchess.save_game()))
def _restore(self):
- """ Restore the gnuchess state from metadata """
+ ''' Restore the gnuchess state from metadata '''
if 'playing_white' in self.metadata:
- _logger.debug(self.metadata['playing_white'])
if self.metadata['playing_white'] == 'False':
self.playing_white = False
+ self.play_black_button.set_active(True)
self._gnuchess.reskin()
if 'playing_mode' in self.metadata:
_logger.debug(self.metadata['playing_mode'])
- self.playing_white = self.metadata['playing_mode']
+ self.playing_mode = self.metadata['playing_mode']
+ if self.playing_mode == 'hard':
+ self.hard_button.set_active(True)
if 'playing_robot' in self.metadata:
_logger.debug(self.metadata['playing_robot'])
if self.metadata['playing_robot'] == 'False':
self.playing_robot = False
- _logger.debug(self.metadata['saved_game'])
+ self.play_human_button.set_active(True)
self._gnuchess.restore_game(json_load(self.metadata['saved_game']))
- return
# Collaboration-related methods
# FIXME: share mode is not set up properly
def _setup_presence_service(self):
- """ Setup the Presence Service. """
+ ''' Setup the Presence Service. '''
self.pservice = presenceservice.get_instance()
self.initiating = None # sharing (True) or joining (False)
@@ -353,15 +358,15 @@ class GNUChessActivity(activity.Activity):
self.connect('joined', self._joined_cb)
def _shared_cb(self, activity):
- """ Either set up initial share..."""
+ ''' Either set up initial share...'''
self._new_tube_common(True)
def _joined_cb(self, activity):
- """ ...or join an exisiting share. """
+ ''' ...or join an exisiting share. '''
self._new_tube_common(False)
def _new_tube_common(self, sharer):
- """ Joining and sharing are mostly the same... """
+ ''' Joining and sharing are mostly the same... '''
if self._shared_activity is None:
_logger.debug("Error: Failed to share or join activity ... \
_shared_activity is null in _shared_cb()")
@@ -389,16 +394,16 @@ class GNUChessActivity(activity.Activity):
self._gnuchess.set_sharing(True)
def _list_tubes_reply_cb(self, tubes):
- """ Reply to a list request. """
+ ''' Reply to a list request. '''
for tube_info in tubes:
self._new_tube_cb(*tube_info)
def _list_tubes_error_cb(self, e):
- """ Log errors. """
+ ''' Log errors. '''
_logger.debug('Error: ListTubes() failed: %s' % (e))
def _new_tube_cb(self, id, initiator, type, service, params, state):
- """ Create a new tube. """
+ ''' Create a new tube. '''
_logger.debug('New tube: ID=%d initator=%d type=%d service=%s \
params=%r state=%d' % (id, initiator, type, service, params, state))
@@ -440,13 +445,13 @@ params=%r state=%d' % (id, initiator, type, service, params, state))
self._gnuchess.restore_game(json_load(payload))
def send_event(self, entry):
- """ Send event through the tube. """
+ ''' Send event through the tube. '''
if hasattr(self, 'chattube') and self.chattube is not None:
self.chattube.SendText(entry)
class ChatTube(ExportedGObject):
- """ Class for setting up tube for sharing """
+ ''' Class for setting up tube for sharing '''
def __init__(self, tube, is_initiator, stack_received_cb):
super(ChatTube, self).__init__(tube, PATH)