home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / gui / notifications / notificationlist.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  12.2 KB  |  348 lines

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