[Sugar-devel] [Memorize PATCH 2/2] Implement new toolbar in Memorize
godiard at sugarlabs.org
godiard at sugarlabs.org
Wed Sep 7 16:11:49 EDT 2011
From: Gonzalo Odiard <godiard at gmail.com>
Signed-off-by: Gonzalo Odiard <gonzalo at laptop.org>
---
activity.py | 127 +++++++++++++++++++++++------------------
cardlist.py | 2 +-
createtoolbar.py | 161 +++++++++++++++++++++++++---------------------------
game.py | 1 -
memorizetoolbar.py | 49 +++++-----------
5 files changed, 166 insertions(+), 174 deletions(-)
diff --git a/activity.py b/activity.py
index 81c36ae..4fcc2e4 100644
--- a/activity.py
+++ b/activity.py
@@ -33,6 +33,10 @@ import gtk
import telepathy
import telepathy.client
+from sugar.activity.widgets import ActivityToolbarButton
+from sugar.activity.widgets import StopButton
+from sugar.graphics.toolbarbox import ToolbarButton, ToolbarBox
+
from sugar.activity.activity import Activity, ActivityToolbox
from sugar.presence import presenceservice
from sugar.presence.tubeconn import TubeConnection
@@ -52,34 +56,60 @@ SERVICE = 'org.laptop.Memorize'
IFACE = SERVICE
PATH = '/org/laptop/Memorize'
-_TOOLBAR_PLAY = 1
-_TOOLBAR_CREATE = 2
+_MODE_PLAY = 1
+_MODE_CREATE = 2
class MemorizeActivity(Activity):
def __init__(self, handle):
Activity.__init__(self, handle)
- self.create_load = False
self.play_mode = None
- toolbox = ActivityToolbox(self)
- activity_toolbar = toolbox.get_activity_toolbar()
-
- self._memorizeToolbar = memorizetoolbar.MemorizeToolbar(self)
- toolbox.add_toolbar(_('Play'), self._memorizeToolbar)
- self._memorizeToolbar.show()
+ toolbar_box = ToolbarBox()
+ self.set_toolbar_box(toolbar_box)
+
+ self.activity_button = ActivityToolbarButton(self)
+ toolbar_box.toolbar.insert(self.activity_button, -1)
+
+ self._memorizeToolbarBuilder = \
+ memorizetoolbar.MemorizeToolbarBuilder(self)
+
+ toolbar_box.toolbar.insert(gtk.SeparatorToolItem(), -1)
+
+ self._createToolbarBuilder = \
+ createtoolbar.CreateToolbarBuilder(self)
+
+ separator = gtk.SeparatorToolItem()
+ separator.set_expand(True)
+ separator.set_draw(False)
+ toolbar_box.toolbar.insert(separator, -1)
+
+ toolbar_box.toolbar.insert(StopButton(self), -1)
- self._createToolbar = createtoolbar.CreateToolbar(self)
- toolbox.add_toolbar(_('Create'), self._createToolbar)
- self._createToolbar.show()
-
- self.set_toolbox(toolbox)
- toolbox.show()
-
# Play game mode
self.table = cardtable.CardTable()
self.scoreboard = scoreboard.Scoreboard()
+ self.cardlist = cardlist.CardList()
+ self.createcardpanel = createcardpanel.CreateCardPanel()
+ self.cardlist.connect('pair-selected',
+ self.createcardpanel.pair_selected)
+ self.cardlist.connect('update-create-toolbar',
+ self._createToolbarBuilder.update_create_toolbar)
+ self.cardlist.connect('update-create-buttons',
+ self._createToolbarBuilder.update_buttons_status)
+ self.createcardpanel.connect('add-pair',
+ self.cardlist.add_pair)
+ self.createcardpanel.connect('update-pair',
+ self.cardlist.update_selected)
+ self._createToolbarBuilder.connect('create_new_game',
+ self.cardlist.clean_list)
+ self._createToolbarBuilder.connect('create_new_game',
+ self.createcardpanel.clean)
+ self._createToolbarBuilder.connect('create_save_game',
+ self.cardlist.save_game)
+ self._createToolbarBuilder.connect('create_equal_pairs',
+ self.createcardpanel.change_equal_pairs)
self.game = game.MemorizeGame()
self.table.connect('key-press-event', self.table.key_press_event)
@@ -107,10 +137,13 @@ class MemorizeActivity(Activity):
self.game.connect('load_game', self.table.load_game)
self.game.connect('change_game', self.table.change_game)
- self.game.connect('load_game', self._memorizeToolbar.update_toolbar)
- self.game.connect('change_game', self._memorizeToolbar.update_toolbar)
-
- self._memorizeToolbar.connect('game_changed', self.game.change_game)
+ self.game.connect('load_game',
+ self._memorizeToolbarBuilder.update_toolbar)
+ self.game.connect('change_game',
+ self._memorizeToolbarBuilder.update_toolbar)
+
+ self._memorizeToolbarBuilder.connect('game_changed',
+ self.change_game)
self.hbox = gtk.HBox(False)
self.set_canvas(self.hbox)
@@ -126,8 +159,10 @@ class MemorizeActivity(Activity):
# start on the game toolbar, might change this
# to the create toolbar later
- self.toolbox.connect('current-toolbar-changed', self.change_mode)
- self.toolbox.set_current_toolbar(_TOOLBAR_PLAY)
+ # TODO:
+ #self.toolbox.connect('current-toolbar-changed', self.change_mode)
+ #self.toolbox.set_current_toolbar(_TOOLBAR_PLAY)
+ self.set_mode_play()
# Get the Presence Service
self.pservice = presenceservice.get_instance()
@@ -152,53 +187,33 @@ class MemorizeActivity(Activity):
_logger.debug('buddy joined - __init__: %s', self.owner.props.nick)
game_file = join(dirname(__file__), 'demos', 'addition.zip')
self.game.load_game(game_file, 4, 'demo')
+ self.cardlist.load_game(game_file)
_logger.debug('loading conventional')
self.game.add_buddy(self.owner)
self.show_all()
def read_file(self, file_path):
if self.metadata['mime_type'] == 'application/x-memorize-project':
- self.toolbox.set_current_toolbar(_TOOLBAR_PLAY)
if self.metadata.has_key('icon-color'):
color = self.metadata['icon-color']
else:
color = profile.get_color().to_string()
- self.game.change_game(None, file_path, 4, 'file',
+ self.change_game(None, file_path, 4, 'file',
self.metadata['title'], color)
- def change_mode(self, notebook, index):
- if index == _TOOLBAR_CREATE:
- if not self.create_load:
- # Create game mode
- self.cardlist = cardlist.CardList()
- self.createcardpanel = createcardpanel.CreateCardPanel()
- self.createcardpanel.connect('add-pair', self.cardlist.add_pair)
- self.createcardpanel.connect('update-pair',
- self.cardlist.update_selected)
- self.cardlist.connect('pair-selected',
- self.createcardpanel.pair_selected)
- self.cardlist.connect('update-create-toolbar',
- self._createToolbar.update_create_toolbar)
- self.cardlist.connect('update-create-buttons',
- self._createToolbar.update_buttons_status)
- self._createToolbar.connect('create_new_game',
- self.cardlist.clean_list)
- self._createToolbar.connect('create_new_game',
- self.createcardpanel.clean)
- self._createToolbar.connect('create_load_game',
- self.cardlist.load_game)
- self._createToolbar.connect('create_save_game',
- self.cardlist.save_game)
- self._createToolbar.connect('create_equal_pairs', \
- self.createcardpanel.change_equal_pairs)
- self.create_load = True
-
+ def set_mode_play(self):
+ self._change_mode(_MODE_PLAY)
+
+ def set_mode_create(self):
+ self._change_mode(_MODE_CREATE)
+
+ def _change_mode(self, mode):
+ if mode == _MODE_CREATE:
self.hbox.remove(self.scoreboard)
self.hbox.remove(self.table)
self.hbox.pack_start(self.createcardpanel, False)
self.hbox.pack_start(self.cardlist)
self.play_mode = False
-
else:
if self.play_mode == False:
self.hbox.remove(self.createcardpanel)
@@ -211,9 +226,11 @@ class MemorizeActivity(Activity):
def restart(self, widget):
self.game.reset()
- def change_game(self, game_name, size, title=None, color=None):
- self.game.change_game(game_name, size, title, color)
-
+ def change_game(self, widget, game_name, size, mode,
+ title=None, color=None):
+ self.game.change_game(widget, game_name, size, mode, title, color)
+ self.cardlist.load_game(game_name)
+
def _shared_cb(self, activity):
_logger.debug('My activity was shared')
self.initiating = True
diff --git a/cardlist.py b/cardlist.py
index 35fcea4..5409a88 100644
--- a/cardlist.py
+++ b/cardlist.py
@@ -66,7 +66,7 @@ class CardList(gtk.EventBox):
self.add(scroll)
self.show_all()
- def load_game(self, widget, game_name):
+ def load_game(self, game_name):
self.model.read(game_name)
self.current_game_key = self.model.data['game_file']
self.emit('update-create-toolbar', self.model.data['name'],
diff --git a/createtoolbar.py b/createtoolbar.py
index b5c72fe..7fd871b 100644
--- a/createtoolbar.py
+++ b/createtoolbar.py
@@ -19,142 +19,137 @@ import logging
from gettext import gettext as _
import gtk
+import gobject
from os.path import join, dirname
from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT
from sugar.graphics.toolbutton import ToolButton
+from sugar.graphics.toolbarbox import ToolbarButton
from sugar.graphics.toggletoolbutton import ToggleToolButton
from sugar.graphics.toolcombobox import ToolComboBox
from sugar.graphics.objectchooser import ObjectChooser
+from sugar.graphics.alert import ConfirmationAlert
+
+
+class CreateToolbarBuilder(gobject.GObject):
-class CreateToolbar(gtk.Toolbar):
__gtype_name__ = 'CreateToolbar'
__gsignals__ = {
'create_new_game': (SIGNAL_RUN_FIRST, None, []),
- 'create_load_game': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
'create_save_game': (SIGNAL_RUN_FIRST, None, 3 * [TYPE_PYOBJECT]),
'create_equal_pairs': (SIGNAL_RUN_FIRST, None, [TYPE_PYOBJECT]),
}
def __init__(self, activity):
- gtk.Toolbar.__init__(self)
+ gobject.GObject.__init__(self)
self.activity = activity
self._lock = True
+ create_toolbar_button = ToolbarButton()
+ create_toolbar_button.props.icon_name = 'view-source'
+ self.toolbar = gtk.Toolbar()
+ create_toolbar_button.props.page = self.toolbar
+ activity.get_toolbar_box().toolbar.insert(create_toolbar_button, -1)
+
# New Button
- new_icon = join(dirname(__file__), 'images', 'game-new.svg')
- new_image = gtk.Image()
- new_image.set_from_file(new_icon)
- self._new_button = ToolButton()
- self._new_button.set_icon_widget(new_image)
- self._new_button.set_tooltip(_('New game'))
- self._new_button.connect('clicked', self._new_game_bt)
- self._add_widget(self._new_button)
+ self._edit_button = ToggleToolButton('game-create')
+ self._edit_button.set_tooltip(_('Click to edit'))
+ self._edit_button.connect('toggled', self._edit_game_bt)
+ self.toolbar.insert(self._edit_button, -1)
- # Load Button
- load_icon = join(dirname(__file__), 'images', 'game-load.svg')
- load_image = gtk.Image()
- load_image.set_from_file(load_icon)
- self._load_button = ToolButton()
- self._load_button.set_icon_widget(load_image)
- self._load_button.set_tooltip(_('Load game'))
- self._load_button.connect('clicked', self._load_game)
- self._add_widget(self._load_button)
-
# Save Button
- save_icon = join(dirname(__file__), 'images', 'game-save.svg')
- save_image = gtk.Image()
- save_image.set_from_file(save_icon)
- self._save_button = ToolButton()
- self._save_button.set_icon_widget(save_image)
+ self._save_button = ToolButton('game-save')
self._save_button.set_tooltip(_('Save game'))
self._save_button.connect('clicked', self._save_game_bt)
- self._save_button.set_sensitive(False)
- self._add_widget(self._save_button)
+ self.toolbar.insert(self._save_button, -1)
- # Separator
- separator2 = gtk.SeparatorToolItem()
- separator2.set_draw(True)
- self.insert(separator2, -1)
+ self.toolbar.insert(gtk.SeparatorToolItem(), -1)
self._add_widget(gtk.Label(_('Game name') + ': '))
self.game_name_entry = gtk.Entry()
self._add_widget(self.game_name_entry)
- self._equal_pairs = gtk.CheckButton(_('Equal pairs'))
- self._add_widget(self._equal_pairs)
+ self._equal_pairs = ToggleToolButton('pair-equals')
+ self._equal_pairs.set_tooltip(_('Equal pairs'))
self._equal_pairs.connect('toggled', self._emit_equal_pairs)
+ self.toolbar.insert(self._equal_pairs, -1)
- self._grouped_icon1 = join(dirname(__file__), 'images', 'equal_pairs1.svg')
- self._grouped_icon2 = join(dirname(__file__), 'images', 'equal_pairs2.svg')
- self._grouped_image1 = gtk.Image()
- self._grouped_image2 = gtk.Image()
- self._grouped_image1.set_from_file(self._grouped_icon1)
- self._grouped_image2.set_from_file(self._grouped_icon2)
- self._grouped = ToggleToolButton()
- self._grouped.set_icon_widget(self._grouped_image1)
+ self._grouped = ToggleToolButton('grouped_game1')
self._grouped.set_tooltip(_('Click for grouped game'))
self._grouped.connect('toggled', self._grouped_cb)
- self._add_widget(self._grouped)
+ self.toolbar.insert(self._grouped, -1)
+
+ self.toolbar.insert(gtk.SeparatorToolItem(), -1)
+
+ self._clear_button = ToolButton('edit-delete')
+ self._clear_button.set_tooltip(_('Clear current game'))
+ self._clear_button.connect('clicked', self._clear_game_bt)
+ self.toolbar.insert(self._clear_button, -1)
+
+ self._update_sensibility_controls()
+ self.toolbar.show_all()
def _add_widget(self, widget, expand=False):
tool_item = gtk.ToolItem()
tool_item.set_expand(expand)
tool_item.add(widget)
widget.show()
- self.insert(tool_item, -1)
+ self.toolbar.insert(tool_item, -1)
tool_item.show()
-
- def _game_changed_cb(self, combobox, game_name):
- self.game_name_entry.set_text(game_name)
- self.emit('create_load_game', game_name)
-
- def _load_game(self, button):
- chooser = ObjectChooser(_('Choose memorize game'),
- parent=self.activity,
- flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
- jobject = ''
- try:
- result = chooser.run()
- if result == gtk.RESPONSE_ACCEPT:
- logging.debug('ObjectChooser: %r' % chooser.get_selected_object())
- jobject = chooser.get_selected_object()
- if not jobject or not jobject.file_path:
- return
- finally:
- chooser.destroy()
- del chooser
-
- if jobject and jobject.file_path:
- self.emit('create_load_game', jobject.file_path)
- self._save_button.set_sensitive(False)
-
- def _new_game_bt(self, button):
- self.game_name_entry.set_text('')
- self._equal_pairs.set_active(False)
- self._grouped.set_active(False)
- self.emit('create_new_game')
- self._new_button.set_sensitive(False)
- self._save_button.set_sensitive(False)
+
+ def _edit_game_bt(self, button):
+ if self._edit_button.get_active():
+ self.activity.set_mode_create()
+ self._edit_button.set_tooltip(_('Click to play'))
+ else:
+ self.activity.set_mode_play()
+ self._edit_button.set_tooltip(_('Click to edit'))
+ self._update_sensibility_controls()
+
+ def _clear_game_bt(self, button):
+ alert = ConfirmationAlert()
+ alert.props.title = _('Remove all the tiles from the game?')
+ alert.connect('response', self._clear_game_alert_cb)
+ self.activity.add_alert(alert)
+
+ def _clear_game_alert_cb(self, alert, response_id):
+ self.activity.remove_alert(alert)
+ if response_id == gtk.RESPONSE_OK:
+ self.game_name_entry.set_text('')
+ self._equal_pairs.set_active(False)
+ self._grouped.set_active(False)
+ self.activity.set_mode_create()
+ self.emit('create_new_game')
+
+ def _update_sensibility_controls(self):
+ self._save_button.set_sensitive(self._edit_button.get_active())
+ self.game_name_entry.set_sensitive(self._edit_button.get_active())
+ self._equal_pairs.set_sensitive(self._edit_button.get_active())
+ self._grouped.set_sensitive(self._edit_button.get_active())
+ self._clear_button.set_sensitive(self._edit_button.get_active())
def _save_game_bt(self, button):
self.emit('create_save_game', self.game_name_entry.get_text(), self._equal_pairs.get_active(), self._grouped.get_active())
self._save_button.set_sensitive(False)
- def _emit_equal_pairs(self, checkbutton):
- self.emit('create_equal_pairs', checkbutton.get_active())
+ def _emit_equal_pairs(self, widget):
+ self.emit('create_equal_pairs', widget.get_active())
self._save_button.set_sensitive(True)
+ if self._equal_pairs.get_active():
+ self._equal_pairs.set_named_icon('pair-non-equals')
+ self._equal_pairs.set_tooltip(_('Click for equal pairs'))
+ else:
+ self._equal_pairs.set_named_icon('pair-equals')
+ self._equal_pairs.set_tooltip(_('Click for non equal pairs'))
def _grouped_cb(self, widget):
self._save_button.set_sensitive(True)
if self._grouped.get_active():
- self._grouped.set_icon_widget(self._grouped_image2)
- self._grouped_image2.show()
+ self._grouped.set_named_icon('grouped_game2')
self._grouped.set_tooltip(_('Click for ungrouped game'))
else:
- self._grouped.set_icon_widget(self._grouped_image1)
- self._grouped_image1.show()
+ self._grouped.set_named_icon('grouped_game1')
self._grouped.set_tooltip(_('Click for grouped game'))
def update_create_toolbar(self, widget, game_name, equal_pairs, grouped):
@@ -163,6 +158,4 @@ class CreateToolbar(gtk.Toolbar):
self._grouped.set_active(grouped == '1')
def update_buttons_status(self, widget, new, save):
- self._new_button.set_sensitive(new)
self._save_button.set_sensitive(save)
-
diff --git a/game.py b/game.py
index 4c4b04d..783029f 100644
--- a/game.py
+++ b/game.py
@@ -281,7 +281,6 @@ class MemorizeGame(GObject):
if mouse and self.model.grid[identifier]['state'] == '0' or not mouse:
self.emit('highlight-card', identifier, True)
-
def increase_point(self, buddy, inc=1):
self.players_score[buddy] += inc
for i_ in range(inc):
diff --git a/memorizetoolbar.py b/memorizetoolbar.py
index 782f818..c9847c6 100644
--- a/memorizetoolbar.py
+++ b/memorizetoolbar.py
@@ -16,6 +16,7 @@
#
import gtk
+import gobject
from os.path import join, dirname
from gettext import gettext as _
@@ -29,8 +30,10 @@ from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT
_logger = logging.getLogger('memorize-activity')
-class MemorizeToolbar(gtk.Toolbar):
- __gtype_name__ = 'MemoryToolbar'
+
+class MemorizeToolbarBuilder(gobject.GObject):
+
+ __gtype_name__ = 'MemoryToolbarBuilder'
standard_game_names = ['Load demo games',
'addition',
@@ -48,36 +51,26 @@ class MemorizeToolbar(gtk.Toolbar):
}
def __init__(self, activity):
- gtk.Toolbar.__init__(self)
+ gobject.GObject.__init__(self)
self.activity = activity
+ self.toolbar = self.activity.get_toolbar_box().toolbar
self._lock = True
self.jobject = None
# Reset Button
- restart_icon = join(dirname(__file__), 'images', 'game-restart.svg')
- restart_image = gtk.Image()
- restart_image.set_from_file(restart_icon)
- self._restart_button = ToolButton()
- self._restart_button.set_icon_widget(restart_image)
+ self._restart_button = ToolButton('game-new')
self._restart_button.connect('clicked', self._game_reset_cb)
self._restart_button.set_tooltip(_('Restart Game'))
- self.insert(self._restart_button, -1)
+ self.toolbar.insert(self._restart_button, -1)
self._restart_button.show()
# Load Button
- load_icon = join(dirname(__file__), 'images', 'game-load.svg')
- load_image = gtk.Image()
- load_image.set_from_file(load_icon)
- self._load_button = ToolButton()
- self._load_button.set_icon_widget(load_image)
+ self._load_button = ToolButton('game-load')
self._load_button.set_tooltip(_('Load game'))
self._load_button.connect('clicked', self._load_game)
- self._add_widget(self._load_button)
+ self.toolbar.insert(self._load_button, -1)
- # Separator
- separator = gtk.SeparatorToolItem()
- separator.set_draw(True)
- self.insert(separator, -1)
+ self.toolbar.insert(gtk.SeparatorToolItem(), -1)
# Change size combobox
self._size_combo = ToolComboBox()
@@ -86,12 +79,10 @@ class MemorizeToolbar(gtk.Toolbar):
self._size_combo.combo.append_item(i, f)
self.size_handle_id = self._size_combo.combo.connect( \
'changed', self._game_size_cb)
- self._add_widget(self._size_combo)
+ self.toolbar.insert(self._size_combo, -1)
self._size_combo.combo.set_active(0)
- separator = gtk.SeparatorToolItem()
- separator.set_draw(True)
- self.insert(separator, -1)
+ self.toolbar.insert(gtk.SeparatorToolItem(), -1)
self._lock = False
# Change demo games combobox
@@ -101,16 +92,8 @@ class MemorizeToolbar(gtk.Toolbar):
self._game_combo.combo.append_item(i, f)
self._game_combo.combo.set_active(0)
self._game_combo.combo.connect('changed', self._game_changed_cb)
- self._add_widget(self._game_combo)
-
- def _add_widget(self, widget, expand=False):
- tool_item = gtk.ToolItem()
- tool_item.set_expand(expand)
- tool_item.add(widget)
- widget.show()
- self.insert(tool_item, -1)
- tool_item.show()
-
+ self.toolbar.insert(self._game_combo, -1)
+
def _game_reset_cb(self, widget):
self.emit('game_changed', None, None, 'reset', None, None)
--
1.7.4.4
More information about the Sugar-devel
mailing list