listhandler.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. self.items = items
  11. self.item_pointer = 0
  12. def update(self):
  13. '''
  14. Called when the handler should update the display.
  15. '''
  16. # Show the slice of the main menu which is relevant
  17. playlist_display = []
  18. maxlines = self.display.drawlines
  19. for i, item in enumerate(self.items):
  20. if len(playlist_display) <= maxlines:
  21. if i >= self.item_pointer - maxlines and i <= self.item_pointer + maxlines:
  22. playlist_display.append(item.get("name", "<unknown>"))
  23. else:
  24. break
  25. self.display.update(playlist_display, min(self.item_pointer, maxlines))
  26. def emitEvent(self, event):
  27. '''
  28. Called when an event has happened.
  29. '''
  30. super().emitEvent(event)
  31. if event == EVENT_RIGHT:
  32. item = self.items[self.item_pointer]
  33. self.itemSelected(item)
  34. if event == EVENT_UP:
  35. self.item_pointer -=1
  36. if event == EVENT_DOWN:
  37. self.item_pointer +=1
  38. if self.item_pointer >= len(self.items):
  39. self.item_pointer = 0
  40. if self.item_pointer < 0:
  41. self.item_pointer = len(self.items) - 1
  42. def nextItem(self):
  43. self.item_pointer +=1
  44. if self.item_pointer >= len(self.items):
  45. self.item_pointer = 0
  46. item = self.items[self.item_pointer]
  47. self.itemSelected(item)
  48. self.update()
  49. def itemSelected(self, item):
  50. '''
  51. Called when an item has been selected by the user.
  52. '''
  53. pass