qt_window.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from PyQt5 import QtWidgets, uic
  2. from PyQt5.QtCore import Qt
  3. from PyQt5.QtCore import pyqtSignal
  4. import lyberry_api.pub as lbry_pub
  5. import lyberry_api.channel as lbry_channel
  6. from lyberry_qt import settings, helpers
  7. from lyberry_qt.following_screen import FollowingScreen, ChannelScreen
  8. from lyberry_qt.connect import ConnectingWidget
  9. from lyberry_qt.pub_screen import PubScreen
  10. from PyQt5.QtCore import QThread
  11. import re
  12. import webbrowser
  13. class MainWindow(QtWidgets.QMainWindow):
  14. def __init__(self, lbry, start_url = None):
  15. super(MainWindow, self).__init__()
  16. uic.loadUi(helpers.relative_path('designer/main.ui'), self)
  17. self._lbry = lbry
  18. self.open_screens = {}
  19. self._start_url = start_url
  20. self.settings_button.clicked.connect(self.show_settings_screen)
  21. self.back_button.clicked.connect(self.go_back)
  22. self.go_button.clicked.connect(self.go_to_entered_url)
  23. self.url_line_edit.returnPressed.connect(self.go_to_entered_url)
  24. self.img_threads = [QThread() for i in range(6)]
  25. self.img_thread_no = 0
  26. self.open_thread = QThread()
  27. self.comment_thread = QThread()
  28. self.load_thread = QThread()
  29. self.loaders = []
  30. self.first_screen()
  31. def first_screen(self):
  32. if not self._lbry.online():
  33. connecting_window = ConnectingWidget(self._lbry)
  34. self.open_screen(connecting_window)
  35. connecting_window.finished.connect(self.when_connected)
  36. else:
  37. self.when_connected()
  38. def when_connected(self):
  39. self.following_screen = FollowingScreen(self, self._lbry.sub_feed)
  40. self.following_button.clicked.connect(self.show_following_screen)
  41. self.add_screen(self.following_screen)
  42. if self._start_url:
  43. self.go_to_start_url()
  44. else:
  45. self.show_following_screen()
  46. def go_to_start_url(self):
  47. try:
  48. self.go_to_lbry_url(self._start_url)
  49. except ValueError:
  50. self.show_following_screen()
  51. finally:
  52. self._start_url = None
  53. def show_following_screen(self):
  54. self.show_screen(self.following_screen)
  55. def show_connecting_screen(self):
  56. self.show_screen(ConnectingWidget(self._lbry))
  57. def show_settings_screen(self):
  58. settings_screen = settings.SettingsScreen(self._lbry)
  59. settings_screen.account_button.clicked.connect(self.show_accounts_screen)
  60. settings_screen.account_button.setEnabled(True)
  61. self.open_screen(settings_screen)
  62. def show_accounts_screen(self):
  63. self.accounts_screen = settings.AccountsScreen(self._lbry)
  64. self.open_screen(self.accounts_screen)
  65. def go_to_entered_url(self):
  66. entered_url = self.url_line_edit.text().strip()
  67. self.go_to_url(entered_url)
  68. def go_to_url(self, url):
  69. if url in self.open_screens:
  70. self.go_to_index(self.open_screens[url])
  71. return
  72. url = re.sub(r'^(https?://)?(odysee.com|lbry.tv)/', 'lbry://', url)
  73. if url.startswith('lbry://') or url.startswith('@'):
  74. try:
  75. self.go_to_lbry_url(url)
  76. except:
  77. self.search_for(url)
  78. elif url.startswith('about:'):
  79. page = url.split(':')[1]
  80. if page in ['following', 'feed', 'subs', 'subscriptions']:
  81. self.show_following_screen()
  82. elif url.startswith('http://') or url.startswith('https://'):
  83. webbrowser.open(url)
  84. else:
  85. self.search_for(url)
  86. def go_to_lbry_url(self, url):
  87. claim = self._lbry.resolve(url)
  88. if type(claim) is lbry_pub.LBRY_Pub:
  89. self.show_pub(claim)
  90. elif type(claim) is lbry_channel.LBRY_Channel:
  91. self.show_channel(claim)
  92. else:
  93. print(type(claim))
  94. def show_channel(self, claim):
  95. if claim.url in self.open_screens:
  96. self.show_screen(claim)
  97. else:
  98. channel_screen = ChannelScreen(self, claim)
  99. self.open_screen(channel_screen)
  100. def show_pub(self, claim):
  101. pub_screen = PubScreen(self, claim)
  102. self.open_screen(pub_screen)
  103. def search_for(self, search_term: str):
  104. claim_feed = self._lbry.lbrynet_search_feed(text = search_term)
  105. search_screen = FollowingScreen(self, claim_feed)
  106. search_screen.url = search_term
  107. search_screen.title_label.setText('Search: ' + search_term)
  108. self.open_screen(search_screen)
  109. def add_screen(self, screen):
  110. screen.setAttribute(Qt.WA_DeleteOnClose)
  111. index = self.stackedWidget.addWidget(screen)
  112. screen.change_url.connect(self.go_to_url)
  113. self.open_screens[screen.url] = index
  114. screen.finished.connect(lambda: self.close_screen(screen))
  115. return index
  116. def open_screen(self, screen):
  117. index = self.add_screen(screen)
  118. self.go_to_index(index)
  119. def close_screen(self, screen):
  120. print('closed', screen)
  121. self.stackedWidget.removeWidget(screen)
  122. del self.open_screens[screen.url]
  123. def index_of(self, screen):
  124. return self.open_screens[screen.url]
  125. def show_screen(self, screen):
  126. self.go_to_index(self.index_of(screen))
  127. def go_to_index(self, index):
  128. self.stackedWidget.setCurrentIndex(index)
  129. self.update_url()
  130. print(self.open_screens)
  131. return index
  132. def go_back(self):
  133. top_widget = self.stackedWidget.currentWidget()
  134. top_widget.close()
  135. self.update_url()
  136. new_top_widget = self.stackedWidget.currentWidget()
  137. if top_widget == new_top_widget:
  138. self.first_screen()
  139. def update_url(self):
  140. self.url_line_edit.setText(self.stackedWidget.currentWidget().url)
  141. def img_url_to_label(self, url, label):
  142. label.setText('Loading image')
  143. img_loader = helpers.ImageLoader()
  144. img_loader.no = self.img_thread_no
  145. img_loader.set_url(url)
  146. self.loaders.append(img_loader)
  147. img_thread = self.img_threads[self.img_thread_no]
  148. img_loader.moveToThread(img_thread)
  149. img_thread.started.connect(img_loader.run)
  150. img_loader.finished.connect(img_thread.quit)
  151. img_loader.finished.connect(img_loader.deleteLater)
  152. img_loader.finished.connect(label.setPixmap)
  153. img_loader.finished.connect(lambda: self.set_img_thread_no(img_loader.no))
  154. self.img_thread_no = (self.img_thread_no + 1) % len(self.img_threads)
  155. def set_img_thread_no(self, no):
  156. self.img_loader = no