sugar-toolkit-gtk3/shell/view/FriendIcon.py

106 lines
2.7 KiB
Python
Raw Normal View History

2006-09-14 19:37:40 +02:00
from sugar.canvas.IconItem import IconItem
from sugar.canvas.Grid import Grid
2006-09-15 13:23:21 +02:00
from view.FriendPopup import FriendPopup
2006-09-14 19:37:40 +02:00
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
2006-09-14 19:37:40 +02:00
class FriendIcon(IconItem):
_popup_shell = _PopupShell()
def __init__(self, shell, friend):
2006-09-14 19:37:40 +02:00
IconItem.__init__(self, icon_name='stock-buddy',
color=friend.get_color(), size=96)
self._shell = shell
2006-09-14 19:37:40 +02:00
self._friend = friend
self._popup = None
2006-09-14 21:15:48 +02:00
self._popup_distance = 0
2006-09-15 00:34:42 +02:00
self._hover_popup = False
self._popdown_on_leave = False
2006-09-14 19:37:40 +02:00
self.connect('popup', self._popup_cb)
self.connect('popdown', self._popdown_cb)
2006-09-14 21:15:48 +02:00
def set_popup_distance(self, distance):
self._popup_distance = distance
2006-09-14 19:37:40 +02:00
def get_friend(self):
return self._friend
2006-09-15 00:34:42 +02:00
def _popdown(self):
if self._popup:
self._popup.destroy()
self._popup = None
2006-09-14 19:37:40 +02:00
def _popup_cb(self, icon, x1, y1, x2, y2):
2006-09-15 00:34:42 +02:00
self._popdown()
FriendIcon._popup_shell.set_active(None)
grid = self._shell.get_grid()
self._popup = FriendPopup(self._shell, icon.get_friend())
2006-09-15 02:54:25 +02:00
self._popup.connect('action', self._popup_action_cb)
2006-09-15 00:34:42 +02:00
self._popup.connect('enter-notify-event',
self._popup_enter_notify_event_cb)
self._popup.connect('leave-notify-event',
self._popup_leave_notify_event_cb)
2006-09-14 19:37:40 +02:00
2006-09-14 21:15:48 +02:00
distance = self._popup_distance
2006-09-14 19:37:40 +02:00
[grid_x1, grid_y1] = grid.convert_from_screen(x1, y1)
[grid_x2, grid_y2] = grid.convert_from_screen(x2, y2)
2006-09-15 01:30:37 +02:00
grid_x = grid_x2 + distance
if grid_x + self._popup.get_width() > Grid.ROWS:
2006-09-15 01:56:59 +02:00
grid_x = grid_x1 - self._popup.get_width() + 1 - distance
2006-09-14 19:37:40 +02:00
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()
FriendIcon._popup_shell.set_active(self)
2006-09-15 02:54:25 +02:00
def _popup_action_cb(self, popup, action):
self._popdown()
buddy = self._friend.get_buddy()
if buddy == None:
return
model = self._shell.get_model()
2006-09-15 02:54:25 +02:00
if action == FriendPopup.ACTION_INVITE:
activity = model.get_current_activity()
2006-09-15 02:54:25 +02:00
activity.invite(buddy)
elif action == FriendPopup.ACTION_MAKE_FRIEND:
friends = model.get_friends()
2006-09-15 02:54:25 +02:00
friends.add_buddy(buddy)
2006-09-14 19:37:40 +02:00
def _popdown_cb(self, friend):
2006-09-15 00:34:42 +02:00
if not self._hover_popup:
self._popdown()
2006-09-15 02:54:25 +02:00
else:
self._popdown_on_leave = True
2006-09-15 00:34:42 +02:00
def _popup_enter_notify_event_cb(self, widget, event):
self._hover_popup = True
2006-09-14 19:37:40 +02:00
2006-09-15 00:34:42 +02:00
def _popup_leave_notify_event_cb(self, widget, event):
self._hover_popup = False
if self._popdown_on_leave:
self._popdown()