cl_taskbaricon.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #! /usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. # Classe permettant l'affichage d'une icone dans la barre des taches
  4. # avec menu
  5. # avec choix de l'icone ou choix d'une grandeur à monitorer
  6. import wx
  7. import string
  8. class Tray(wx.TaskBarIcon):
  9. """ Classe pour l'affichage de l'icone et des menus
  10. dans la barre des taches. """
  11. #Entrées du menu
  12. TBMENU_CREATE = []
  13. def __init__(self, app):
  14. wx.TaskBarIcon.__init__(self)
  15. self.app = app
  16. self.menu_tmp = []
  17. #liaisons Boutons-Méthodes (actions en dur)
  18. self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarActivate)
  19. def MenuAddItem(self, nom, action):
  20. """ Add an item to the menu and link it to corresponding method """
  21. if action != None :
  22. new_item = wx.NewId()
  23. self.menu_tmp.append({nom:new_item})
  24. self.Bind(wx.EVT_MENU, action, id=new_item)
  25. else:
  26. self.menu_tmp.append({nom:None})
  27. def CreatePopupMenu(self):
  28. """ Method called when we need to display the menu """
  29. menu = wx.Menu()
  30. for item in self.menu_tmp:
  31. for nom,action in item.items():
  32. if action!=None:
  33. menu.Append(action, nom)
  34. else:
  35. menu.AppendSeparator()
  36. return menu
  37. def ChangeIcon(self, imgpath):
  38. """ Change taskbar icon """
  39. img = wx.Image(imgpath,wx.BITMAP_TYPE_PNG)
  40. bmp = img.ConvertToBitmap()
  41. # bmp.SetMask(wx.Mask(bmp, wx.Colour().Alpha()))
  42. icon = wx.IconFromBitmap(img.ConvertToBitmap())
  43. icon = wx.EmptyIcon()
  44. icon.CopyFromBitmap(bmp)
  45. self.SetIcon(icon, "Temp: %d °C" % self.temp)
  46. def IconMonitor(self, l=2, r=4):
  47. l_off=[128,0,0] # couleurs
  48. l_on=[255,0,0]
  49. r_off=[0,128,0]
  50. r_on=[0,255,0]
  51. s_line = "\xff\xff\xff"+"\0"*45
  52. s_border = "\xff\xff\xff\0\0\0"
  53. s_point = "\0"*3
  54. sl_off = string.join(map(chr,l_off),'')*6
  55. sl_on = string.join(map(chr,l_on),'')*6
  56. sr_off = string.join(map(chr,r_off),'')*6
  57. sr_on = string.join(map(chr,r_on),'')*6
  58. s=""+s_line
  59. for i in range(5):
  60. if i<(5-l):
  61. sl = sl_off
  62. else:
  63. sl = sl_on
  64. if i<(5-r):
  65. sr = sr_off
  66. else:
  67. sr = sr_on
  68. s+=s_border+sl+s_point+sr+s_point
  69. s+=s_border+sl+s_point+sr+s_point
  70. s+=s_line
  71. image = wx.EmptyImage(16,16)
  72. image.SetData(s)
  73. bmp = image.ConvertToBitmap()
  74. bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
  75. icon = wx.EmptyIcon()
  76. icon.CopyFromBitmap(bmp)
  77. self.SetIcon(icon, "Temp: %d°C" % self.temp)
  78. def SetIconTimer(self):
  79. self.icon_timer = wx.Timer(self, ID_ICON_TIMER)
  80. wx.EVT_TIMER(self, ID_ICON_TIMER, self.BlinkIcon)
  81. self.icon_timer.Start(100)
  82. def BlinkIcon(self, event):
  83. self.CheckTemp()
  84. self.MonitorIcon()