Factor out to a generic icon with menu

This commit is contained in:
Marco Pesenti Gritti 2006-09-16 14:45:09 +02:00
parent 956b663409
commit 7c91b1c49f
3 changed files with 97 additions and 84 deletions

View File

@ -1,108 +1,38 @@
from sugar.canvas.IconItem import IconItem
from sugar.canvas.Grid import Grid
from sugar.canvas.MenuIcon import MenuIcon
from view.BuddyPopup import BuddyPopup
class _PopupShell:
def __init__(self):
self._popup_controller = None
def set_active(self, controller):
if self._popup_controller:
self._popup_controller._popdown()
self._popup_controller = controller
class BuddyIcon(IconItem):
_popup_shell = _PopupShell()
class BuddyIcon(MenuIcon):
def __init__(self, shell, friend):
IconItem.__init__(self, icon_name='stock-buddy',
MenuIcon.__init__(self, shell.get_grid(),
icon_name='stock-buddy',
color=friend.get_color(), size=96)
self._shell = shell
self._friend = friend
self._popup = None
self._popup_distance = 0
self._hover_popup = False
self._popdown_on_leave = False
self.connect('popup', self._popup_cb)
self.connect('popdown', self._popdown_cb)
def set_popup_distance(self, distance):
self._popup_distance = distance
def get_friend(self):
return self._friend
def _popdown(self):
if self._popup:
self._popup.destroy()
self._popup = None
def _popup_cb(self, icon, x1, y1, x2, y2):
self._popdown()
BuddyIcon._popup_shell.set_active(None)
grid = self._shell.get_grid()
self._popup = BuddyPopup(self._shell, icon.get_friend())
self._popup.connect('action', self._popup_action_cb)
self._popup.connect('enter-notify-event',
self._popup_enter_notify_event_cb)
self._popup.connect('leave-notify-event',
self._popup_leave_notify_event_cb)
distance = self._popup_distance
[grid_x1, grid_y1] = grid.convert_from_screen(x1, y1)
[grid_x2, grid_y2] = grid.convert_from_screen(x2, y2)
grid_x = grid_x2 + distance
if grid_x + self._popup.get_width() > Grid.ROWS:
grid_x = grid_x1 - self._popup.get_width() + 1 - distance
grid_y = grid_y1
if grid_y < 0:
grid_y = 0
if grid_y + self._popup.get_width() > Grid.ROWS:
grid_y = Grid.ROWS - self._popup.get_width()
grid.set_constraints(self._popup, grid_x, grid_y,
self._popup.get_width(), self._popup.get_height())
self._popup.show()
BuddyIcon._popup_shell.set_active(self)
def create_menu(self):
menu = BuddyPopup(self._shell, self._friend)
menu.connect('action', self._popup_action_cb)
return menu
def _popup_action_cb(self, popup, action):
self._popdown()
model = self._shell.get_model()
if action == BuddyPopup.ACTION_REMOVE_FRIEND:
friends = model.get_friends()
friends.remove(buddy)
buddy = self._friend.get_buddy()
if buddy == None:
return
model = self._shell.get_model()
if action == BuddyPopup.ACTION_INVITE:
activity = model.get_current_activity()
activity.invite(buddy)
elif action == BuddyPopup.ACTION_MAKE_FRIEND:
friends = model.get_friends()
friends.make_friend(buddy)
elif action == BuddyPopup.ACTION_REMOVE_FRIEND:
friends = model.get_friends()
friends.remove(buddy)
def _popdown_cb(self, friend):
if not self._hover_popup:
self._popdown()
else:
self._popdown_on_leave = True
def _popup_enter_notify_event_cb(self, widget, event):
self._hover_popup = True
def _popup_leave_notify_event_cb(self, widget, event):
self._hover_popup = False
if self._popdown_on_leave:
self._popdown()

View File

@ -25,7 +25,7 @@ class RightPanel(CanvasBox):
def add(self, buddy):
icon = BuddyIcon(self._shell, BuddyInfo(buddy))
icon.set_popup_distance(1)
icon.set_menu_distance(1)
self.set_constraints(icon, 3, 3)
self.add_child(icon)

83
sugar/canvas/MenuIcon.py Normal file
View File

@ -0,0 +1,83 @@
from sugar.canvas.IconItem import IconItem
from sugar.canvas.Grid import Grid
class _MenuShell:
def __init__(self):
self._menu_controller = None
def set_active(self, controller):
if self._menu_controller:
self._menu_controller.popdown()
self._menu_controller = controller
class MenuIcon(IconItem):
_menu_shell = _MenuShell()
def __init__(self, grid, **kwargs):
IconItem.__init__(self, **kwargs)
self._grid = grid
self._menu = None
self._menu_distance = 0
self._hover_menu = False
self._popdown_on_leave = False
self.connect('popup', self._popup_cb)
self.connect('popdown', self._popdown_cb)
def set_menu_distance(self, distance):
self._menu_distance = distance
def popdown(self):
if self._menu:
self._menu.destroy()
self._menu = None
def _popup_cb(self, icon, x1, y1, x2, y2):
self.popdown()
MenuIcon._menu_shell.set_active(None)
grid = self._shell.get_grid()
self._menu = self.create_menu()
self._menu.connect('enter-notify-event',
self._menu_enter_notify_event_cb)
self._menu.connect('leave-notify-event',
self._menu_leave_notify_event_cb)
distance = self._menu_distance
[grid_x1, grid_y1] = grid.convert_from_screen(x1, y1)
[grid_x2, grid_y2] = grid.convert_from_screen(x2, y2)
grid_x = grid_x2 + distance
if grid_x + self._menu.get_width() > Grid.ROWS:
grid_x = grid_x1 - self._menu.get_width() + 1 - distance
grid_y = grid_y1
if grid_y < 0:
grid_y = 0
if grid_y + self._menu.get_width() > Grid.ROWS:
grid_y = Grid.ROWS - self._menu.get_width()
grid.set_constraints(self._menu, grid_x, grid_y,
self._menu.get_width(), self._menu.get_height())
self._menu.show()
MenuIcon._menu_shell.set_active(self)
def _popdown_cb(self, friend):
if not self._hover_menu:
self.popdown()
else:
self._popdown_on_leave = True
def _menu_enter_notify_event_cb(self, widget, event):
self._hover_menu = True
def _menu_leave_notify_event_cb(self, widget, event):
self._hover_menu = False
if self._popdown_on_leave:
self.popdown()