home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / notifications / notificationlist.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  12.1 KB  |  346 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import wx
  6. import syck
  7. from wx import Rect, RectS, Point, ALIGN_CENTER_VERTICAL, Brush, TRANSPARENT_PEN, WHITE_BRUSH, CONTROL_CHECKED
  8. from operator import attrgetter
  9. GetSysColor = wx.SystemSettings.GetColour
  10. from gui import skin
  11. from gui.skin.skinobjects import Margins
  12. from gui.vlist.skinvlist import SkinVListBox
  13. from gui.toolbox import AutoDC
  14. from gui.anylists import bgcolors
  15. from cgui import SimplePanel
  16. from util import odict
  17. from common.notifications import Popup, Sound
  18. from config import platformName
  19. from logging import getLogger
  20. log = getLogger('notificationlist')
  21. _hdralign = wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL
  22. W = 45
  23. H = None if platformName != 'mac' else 15
  24.  
  25. class NotifyPanel(SimplePanel):
  26.     
  27.     def __init__(self, parent):
  28.         SimplePanel.__init__(self, parent, wx.FULL_REPAINT_ON_RESIZE)
  29.         s = wx.BoxSizer(wx.VERTICAL)
  30.         self.SetSizer(s)
  31.         self.list = NotifyView(self)
  32.         s.AddSpacer((1, H))
  33.         s.Add(self.list, 1, wx.EXPAND | wx.ALL & ~(wx.TOP), 1)
  34.         self.Bind(wx.EVT_PAINT, self._NotifyPanel__paint)
  35.         f = self.Font
  36.         f.SetWeight(wx.FONTWEIGHT_BOLD)
  37.         if platformName == 'mac':
  38.             f.SetPointSize(11)
  39.         
  40.         self.Font = f
  41.         self.Bind((wx.EVT_MOUSEWHEEL,), (lambda e: self.SetFocus()))
  42.  
  43.     
  44.     def __paint(self, e):
  45.         dc = AutoDC(self)
  46.         dc.Font = self.Font
  47.         rect = self.list.ClientRect
  48.         if platformName != 'mac':
  49.             dc.Pen = TRANSPARENT_PEN
  50.             dc.Brush = WHITE_BRUSH
  51.             dc.DrawRectangle(0, 0, self.ClientRect.width, H)
  52.         
  53.         r1 = Rect(rect.Right - W * 2, rect.y, W, H)
  54.         r2 = Rect(rect.Right - W, rect.y, W, H)
  55.         r3 = Rect(*self.list.Rect)
  56.         r3.Inflate(1, 1)
  57.         dc.DrawLabel(_('Sound'), r1, _hdralign)
  58.         dc.DrawLabel(_('Popup'), r2, _hdralign)
  59.         dc.SetBrush(wx.TRANSPARENT_BRUSH)
  60.         dc.SetPen(wx.Pen(wx.Colour(213, 213, 213)))
  61.         dc.DrawRectangleRect(r3)
  62.  
  63.     NotifyView = property(attrgetter('list'))
  64.  
  65.  
  66. class NotifyView(SkinVListBox):
  67.     
  68.     def __init__(self, parent):
  69.         SkinVListBox.__init__(self, parent)
  70.         self.InitDefaults()
  71.         self.BindEvents()
  72.         self._hovered = -1
  73.  
  74.     
  75.     def GetHovered(self):
  76.         return self._hovered
  77.  
  78.     
  79.     def SetHovered(self, i):
  80.         n = self._hovered
  81.         if i != n:
  82.             self._hovered = i
  83.             if n != -1:
  84.                 self.RefreshLine(n)
  85.             
  86.             if i != -1:
  87.                 self.RefreshLine(i)
  88.             
  89.         
  90.  
  91.     Hovered = property(GetHovered, SetHovered)
  92.     
  93.     def InitDefaults(self):
  94.         self.IconSize = 32
  95.         self.margins = Margins((3, 3))
  96.         self.padding = Point(5, 5)
  97.         self.CheckBoxSize = 16
  98.         self.SetNotificationInfo({ })
  99.         self.SetUserNotifications({ })
  100.         self.UpdateSkin()
  101.  
  102.     
  103.     def BindEvents(self):
  104.         Bind = self.Bind
  105.         Bind(wx.EVT_LEFT_DOWN, self._NotifyView__leftdown)
  106.         Bind(wx.EVT_MOTION, self.OnMotion)
  107.  
  108.     
  109.     def OnMotion(self, event):
  110.         rect = wx.RectS(self.ClientSize)
  111.         wap = wx.FindWindowAtPointer()
  112.         mp = event.Position
  113.         if not rect.Contains(mp) or wap != self:
  114.             while self.HasCapture():
  115.                 self.ReleaseMouse()
  116.             self.Hovered = -1
  117.             return None
  118.         elif not self.HasCapture():
  119.             self.CaptureMouse()
  120.         
  121.         self.Hovered = self.HitTest(mp)
  122.  
  123.     
  124.     def OnDrawItem(self, dc, rect, n):
  125.         ninfo = self.NotificationInfo
  126.         rect = rect.AddMargins(self.margins)
  127.         key = ninfo._keys[n]
  128.         topic = key
  129.         info = ninfo[key]
  130.         title = info['description']
  131.         icon = None
  132.         icon_topic = topic
  133.         while icon is None:
  134.             icon = self.icons.get(icon_topic, None)
  135.             if '.' in icon_topic:
  136.                 icon_topic = icon_topic.split('.')[0]
  137.                 continue
  138.             break
  139.         if isinstance(icon, basestring):
  140.             icon = skin.get(icon)
  141.         
  142.         iconsize = self.IconSize
  143.         dc.Font = self.Font
  144.         dc.SetTextForeground(wx.BLACK)
  145.         if icon is not None:
  146.             dc.DrawBitmap(icon.Resized(iconsize), self.padding.x, rect.y)
  147.         
  148.         rect.Subtract(left = iconsize + self.padding.x * 2)
  149.         dc.DrawLabel(title, rect, alignment = ALIGN_CENTER_VERTICAL)
  150.         self.DrawChecks(dc, n, rect)
  151.  
  152.     
  153.     def DrawChecks(self, dc, idx, rect):
  154.         r = wx.RendererNative.Get()
  155.         for i, cbrect in enumerate(self.CheckBoxRects(rect)):
  156.             flags = None if self.Checked(idx, i) else 0
  157.             r.DrawCheckBox(self, dc, cbrect, flags)
  158.         
  159.  
  160.     
  161.     def CheckBoxRects(self, rect):
  162.         cbsize = self.CheckBoxSize
  163.         num_checkboxes = 2
  164.         rects = []
  165.         y = rect.VCenterH(cbsize)
  166.         for i in reversed(range(num_checkboxes)):
  167.             r1 = Rect(rect.Right - W * (i + 1), y, W, rect.height)
  168.             r1.x = r1.HCenterW(cbsize) + 4
  169.             r1.y = r1.y
  170.             r1.width = r1.height = cbsize
  171.             rects.append(r1)
  172.         
  173.         return rects
  174.  
  175.     
  176.     def Checked(self, idx, checkbox):
  177.         reactions = self.UserNotifications.get(None, { }).get(self.TopicForIndex(idx), { })
  178.         r = self.checkBoxReactions[checkbox]
  179.         return (any,)((lambda .0: for d in .0:
  180. r == d.get('reaction', None))(reactions))
  181.  
  182.     
  183.     def CheckBoxHitTest(self, pos):
  184.         rect = Rect(0, 0, self.ClientSize.width, self.OnMeasureItem(0))
  185.         rect = rect.AddMargins(self.margins)
  186.         for i, r in enumerate(self.CheckBoxRects(rect)):
  187.             if r.Contains(pos):
  188.                 return i
  189.                 continue
  190.         
  191.         return -1
  192.  
  193.     
  194.     def TopicForIndex(self, idx):
  195.         return self.NotificationInfo._keys[idx]
  196.  
  197.     
  198.     def OnDrawBackground(self, dc, rect, n):
  199.         s = self.IsSelected(n)
  200.         h = self.Hovered
  201.         if s:
  202.             pass
  203.         elif h == n:
  204.             pass
  205.         
  206.         dc.Brush = Brush(bgcolors[n % len(bgcolors)])
  207.         dc.Pen = wx.TRANSPARENT_PEN
  208.         dc.DrawRectangle(*rect)
  209.  
  210.     
  211.     def OnMeasureItem(self, n):
  212.         return self.margins.top + self.margins.bottom + self.IconSize
  213.  
  214.     
  215.     def UpdateSkin(self):
  216.         ni = self.NotificationInfo
  217.         appdefaults = skin.get('AppDefaults.notificationicons')
  218.         all_icons = { }
  219.         all_icons.update(appdefaults)
  220.         for k in ni:
  221.             iconpath = ni[k].get('notification_icon', None)
  222.             if iconpath is not None:
  223.                 all_icons[k] = iconpath
  224.                 continue
  225.         
  226.         self.icons = all_icons
  227.  
  228.     
  229.     def __leftdown(self, e):
  230.         p = e.Position
  231.         i = self.HitTest(p)
  232.         if i != -1:
  233.             cb_i = self.CheckBoxHitTest(self.ToItemCoords(i, p))
  234.             if cb_i != -1:
  235.                 return self.CheckBoxClicked(i, cb_i)
  236.             
  237.         
  238.  
  239.     checkBoxReactions = [
  240.         'Sound',
  241.         'Popup']
  242.     
  243.     def CheckBoxClicked(self, item, checkbox):
  244.         uinfo = self.UserNotifications
  245.         topic = self.TopicForIndex(item)
  246.         reaction = self.checkBoxReactions[checkbox]
  247.         if None not in uinfo:
  248.             uinfo[None] = { }
  249.         
  250.         reactions = uinfo[None].setdefault(topic, [])
  251.         foundOne = False
  252.         for rdict in list(reactions):
  253.             if rdict['reaction'] == reaction:
  254.                 foundOne = True
  255.                 log.info('removing %r', rdict)
  256.                 reactions.remove(rdict)
  257.                 continue
  258.         
  259.         if not foundOne:
  260.             newEntry = self.ReactionEntry(topic, reaction)
  261.             log.info('adding %r', newEntry)
  262.             reactions.append(newEntry)
  263.         
  264.         self.RefreshLine(item)
  265.  
  266.     
  267.     def ReactionEntry(self, topic, reaction):
  268.         return {
  269.             'reaction': reaction }
  270.  
  271.     
  272.     def ToItemCoords(self, item, pos):
  273.         return Point(pos.x, pos.y % self.OnMeasureItem(0))
  274.  
  275.     
  276.     def SetNotificationInfo(self, notification_info):
  277.         ninfo = type(notification_info)()
  278.         for key, value in notification_info.iteritems():
  279.             if value.get('gui', True):
  280.                 ninfo[key] = value
  281.                 continue
  282.         
  283.         self._ninfo = ninfo
  284.         self.SetItemCount(len(self._ninfo))
  285.  
  286.     
  287.     def GetNotificationInfo(self):
  288.         return self._ninfo
  289.  
  290.     NotificationInfo = property(GetNotificationInfo, SetNotificationInfo)
  291.     
  292.     def SetUserNotifications(self, usernots):
  293.         self._usernots = usernots
  294.         self.RefreshAll()
  295.  
  296.     
  297.     def GetUserNotifications(self):
  298.         return self._usernots
  299.  
  300.     UserNotifications = property(GetUserNotifications, SetUserNotifications)
  301.  
  302.  
  303. def main():
  304.     get_notification_info = get_notification_info
  305.     import common.notifications
  306.     userInfo = {
  307.         None: {
  308.             'contact.available': [
  309.                 {
  310.                     'reaction': Popup }],
  311.             'contact.away': [
  312.                 {
  313.                     'reaction': Popup }],
  314.             'email.new': [
  315.                 {
  316.                     'reaction': Popup }],
  317.             'error': [
  318.                 {
  319.                     'reaction': Popup }],
  320.             'facebook.alert': [
  321.                 {
  322.                     'reaction': Popup }],
  323.             'filetransfer.request': [
  324.                 {
  325.                     'reaction': Popup }],
  326.             'message.received.background': [
  327.                 {
  328.                     'reaction': Popup }],
  329.             'myspace.alert': [
  330.                 {
  331.                     'reaction': Popup }] } }
  332.     testapp = testapp
  333.     import tests.testapp
  334.     app = testapp('../../..')
  335.     f = wx.Frame(None, -1, 'notifications gui test')
  336.     p = NotifyPanel(f)
  337.     n = p.NotifyView
  338.     n.NotificationInfo = get_notification_info()
  339.     n.UserNotifications = userInfo
  340.     f.Show()
  341.     app.MainLoop()
  342.  
  343. if __name__ == '__main__':
  344.     main()
  345.  
  346.