following_screen.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from PyQt5 import QtWidgets, uic
  2. from PyQt5.QtCore import QThread, pyqtSignal
  3. from lyberry_qt import helpers
  4. import lyberry_api.channel
  5. class ListScreen(QtWidgets.QDialog):
  6. change_url = pyqtSignal(str)
  7. def __init__(self, window):
  8. super(ListScreen, self).__init__()
  9. self._window = window
  10. def setup(self):
  11. self.title_label.setText(self.title)
  12. self.load_more_button.clicked.connect(self.more)
  13. self.amt = 2
  14. self.width = 2
  15. self.items = []
  16. self.more()
  17. def new_pub(self, pub):
  18. item = PubWidget(pub)
  19. if type(pub) == lyberry_api.channel.LBRY_Channel:
  20. item.title.clicked.connect(lambda: self.change_url.emit(pub.url))
  21. item.channel.clicked.connect(lambda: self.change_url.emit(pub.url))
  22. else:
  23. item.title.clicked.connect(lambda: self.change_url.emit(pub.url))
  24. item.channel.clicked.connect(lambda: self.change_url.emit(pub.channel.url))
  25. self._window.img_url_to_label(pub.thumbnail, item.thumbnail)
  26. self.items.append(item)
  27. self.pub_thumb_grid_layout.addWidget(item, self.amt // self.width, self.amt % self.width, 1, 1)
  28. self.amt += 1
  29. return item.pub_grid
  30. def get_images_and_fix_button(self, pubs):
  31. for thread in self._window.img_threads:
  32. thread.start()
  33. self.pub_thumb_grid_layout.addWidget(self.load_more_button, self.amt // self.width +1, 0, 1, 2)
  34. self.load_more_button.setEnabled(True)
  35. def more(self):
  36. self.load_more_button.setEnabled(False)
  37. self.worker = helpers.FeedUpdater()
  38. self.worker.set_feed(self.feed)
  39. self.worker.moveToThread(self._window.load_thread)
  40. self._window.load_thread.started.connect(self.worker.run)
  41. self.worker.progress.connect(self.new_pub)
  42. self.worker.finished.connect(self.get_images_and_fix_button)
  43. self.worker.finished.connect(self.worker.deleteLater)
  44. self.worker.finished.connect(self._window.load_thread.quit)
  45. self.scroll_area.ensureWidgetVisible(self.load_more_button)
  46. self._window.load_thread.start()
  47. class FollowingScreen(ListScreen):
  48. def __init__(self, window, feed):
  49. self.feed = feed
  50. self.title = 'Following'
  51. self.url = 'about:following'
  52. super(FollowingScreen, self).__init__(window)
  53. uic.loadUi(helpers.relative_path('designer/following.ui'), self)
  54. self.setup()
  55. class ChannelScreen(ListScreen):
  56. def __init__(self, window, channel):
  57. self.title = channel.name
  58. self.channel = channel
  59. self.feed = channel.pubs_feed
  60. self.url = channel.url
  61. super(ChannelScreen, self).__init__(window)
  62. uic.loadUi(helpers.relative_path('designer/channel.ui'), self)
  63. self.setup()
  64. self.description_label.setText(helpers.fix_markdown(self.channel.description))
  65. self.description_label.linkActivated.connect(self.change_url.emit)
  66. if channel.is_followed:
  67. self.set_to_unfollow()
  68. else:
  69. self.set_to_follow()
  70. self.finished.connect(self.channel.refresh_feed)
  71. def follow(self):
  72. self.channel.follow()
  73. self.set_to_unfollow()
  74. def set_to_follow(self):
  75. self.follow_button.clicked.connect(self.follow)
  76. self.follow_button.setText('Follow')
  77. def set_to_unfollow(self):
  78. self.follow_button.clicked.connect(self.unfollow)
  79. self.follow_button.setText('Following')
  80. def unfollow(self):
  81. self.channel.unfollow()
  82. self.set_to_follow()
  83. class PubWidget(QtWidgets.QDialog):
  84. def __init__(self, pub):
  85. super(PubWidget, self).__init__()
  86. uic.loadUi(helpers.relative_path('designer/pub_thumb.ui'), self)
  87. self.loaders = []
  88. self.pub = pub
  89. self.title.setText(pub.title)
  90. if type(pub) == lyberry_api.channel.LBRY_Channel:
  91. self.channel.setText(pub.name)
  92. else:
  93. self.channel.setText(pub.channel.name)