listhandler.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!./bin/python3
  2. import os
  3. from handler import BaseHandler, EVENT_UP, EVENT_RIGHT, EVENT_DOWN
  4. from handler.lib.pymplb import MPlayer
  5. class BaseListHandler(BaseHandler):
  6. def __init__(self, config, display):
  7. super().__init__(config, display)
  8. self.items = []
  9. def setItems(self, items):
  10. '''
  11. Set the items which should be displayed.
  12. '''
  13. self.items = items
  14. self.item_pointer = 0
  15. def update(self, text=None, highlight=-1, title=None, img=None):
  16. '''
  17. Called when the handler should update the display.
  18. '''
  19. # Show the slice of the main menu which is relevant
  20. playlist_display = text or []
  21. maxlines = self.display.drawlines
  22. for i, item in enumerate(self.items):
  23. if len(playlist_display) <= maxlines:
  24. if i >= self.item_pointer - maxlines and i <= self.item_pointer + maxlines:
  25. playlist_display.append(item.get("name", "<unknown>"))
  26. else:
  27. break
  28. super().update(playlist_display,
  29. highlight if highlight != -1 else min(self.item_pointer, maxlines),
  30. title, img)
  31. def emitEvent(self, event):
  32. '''
  33. Called when an event has happened.
  34. '''
  35. super().emitEvent(event)
  36. if event == EVENT_RIGHT:
  37. item = self.items[self.item_pointer]
  38. self.itemSelected(item)
  39. if event == EVENT_UP:
  40. self.item_pointer -=1
  41. if event == EVENT_DOWN:
  42. self.item_pointer +=1
  43. if self.item_pointer >= len(self.items):
  44. self.item_pointer = 0
  45. if self.item_pointer < 0:
  46. self.item_pointer = len(self.items) - 1
  47. def nextItem(self):
  48. self.item_pointer +=1
  49. if self.item_pointer >= len(self.items):
  50. self.item_pointer = 0
  51. item = self.items[self.item_pointer]
  52. self.itemSelected(item)
  53. self.update()
  54. def itemSelected(self, item):
  55. '''
  56. Called when an item has been selected by the user.
  57. '''
  58. pass