settings.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from PyQt5 import QtWidgets
  2. from PyQt5.QtCore import pyqtSignal
  3. from PyQt5.uic import loadUi
  4. from lyberry_qt.helpers import relative_path
  5. from lyberry_api import settings
  6. app_settings = settings.settings
  7. class SettingsScreen(QtWidgets.QDialog):
  8. change_url = pyqtSignal()
  9. def __init__(self, lbry):
  10. super(SettingsScreen, self).__init__()
  11. loadUi(relative_path('designer/settings.ui'), self)
  12. self.url = 'about:settings'
  13. self._lbry = lbry
  14. self.row = 0
  15. self.inputs = []
  16. self.labels = []
  17. for key in app_settings:
  18. self.add_input(key, app_settings[key])
  19. self.apply_button.clicked.connect(self.apply)
  20. def apply(self, something_else):
  21. settings.apply()
  22. self.reapply_settings()
  23. def reapply_settings(self):
  24. self._lbry.lbrynet_api = app_settings['lbrynet_api']
  25. self._lbry.comment_api = app_settings['comment_api']
  26. self._lbry.lighthouse_api = app_settings['lighthouse_api']
  27. def add_input(self, key, value):
  28. label = QtWidgets.QLabel()
  29. label.setText(key)
  30. self.formLayout.setWidget(self.row, QtWidgets.QFormLayout.LabelRole, label)
  31. self.labels.append(label)
  32. inputbox = {}
  33. if type(value) == str:
  34. inputbox = QtWidgets.QLineEdit()
  35. inputbox.setText(value)
  36. elif type(value) == int:
  37. inputbox = QtWidgets.QSpinBox()
  38. inputbox.setValue(value)
  39. else:
  40. inputbox = QtWidgets.QLineEdit()
  41. inputbox.setText(str(value))
  42. inputbox.setEnabled(False)
  43. print(f'Config {key} is of unsupported type: {type(value)}')
  44. self.formLayout.setWidget(self.row, QtWidgets.QFormLayout.FieldRole, inputbox)
  45. def update_setting():
  46. app_settings[label.text()] = inputbox.text()
  47. inputbox.editingFinished.connect(update_setting)
  48. self.inputs.append(inputbox)
  49. self.row += 1
  50. class AccountsScreen(QtWidgets.QDialog):
  51. change_url = pyqtSignal()
  52. def __init__(self, lbry):
  53. super(AccountsScreen, self).__init__()
  54. loadUi(relative_path('designer/account.ui'), self)
  55. self._lbry = lbry
  56. self.url = 'about:accounts'
  57. for account in self._lbry.accounts:
  58. self._add_account_to_list(account)
  59. self.add_acc_button.clicked.connect(lambda: self.add_account())
  60. def add_account(self):
  61. try:
  62. account = self._lbry.add_account(
  63. self.edit_name.text(),
  64. self.edit_priv_key.text())
  65. except ValueError:
  66. print('invalid key!')
  67. return
  68. self._add_account_to_list(account)
  69. def _add_account_to_list(self, account):
  70. label = QtWidgets.QLabel()
  71. label.setText(account_to_html(account))
  72. self.acc_list_section.addWidget(label)
  73. if not account.is_default:
  74. default_button = QtWidgets.QPushButton()
  75. default_button.clicked.connect(account.set_as_default)
  76. default_button.setText(f'Set {account.name} as the default account')
  77. self.acc_list_section.addWidget(default_button)
  78. remove_button = QtWidgets.QPushButton()
  79. remove_button.clicked.connect(lambda: account.remove())
  80. remove_button.setText(f'Remove {account.name}')
  81. self.acc_list_section.addWidget(remove_button)
  82. def account_to_html(account):
  83. return f'''
  84. <h2>{account.name}{" - Default" if account.is_default else ""}</h2>
  85. <p>
  86. id: {account.id}
  87. <br>
  88. public key: {account.public_key}
  89. </p>
  90. '''