settings.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. ########################################################################
  2. # Wiizard - A Wii games manager
  3. # Copyright (C) 2023 CYBERDEViL
  4. #
  5. # This file is part of Wiizard.
  6. #
  7. # Wiizard is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Wiizard is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from PyQt5.QtWidgets import (
  22. QDialog,
  23. QLabel,
  24. QGridLayout,
  25. QTabWidget,
  26. QWidget,
  27. QComboBox,
  28. QCheckBox,
  29. QLineEdit,
  30. QPushButton,
  31. QToolButton,
  32. QFileDialog,
  33. QSizePolicy
  34. )
  35. from PyQt5.QtGui import QValidator
  36. from PyQt5.QtCore import pyqtSignal, QVariant
  37. from wiizard.models.settings import GameImageSettings
  38. from wiizard import globals as Global
  39. from wiizard.widgets.line import HLine
  40. from wiizard.translations import _
  41. """
  42. SETTINGS
  43. =====================================
  44. [General] >[Game Images]< [Connection]
  45. =====================================
  46. ...
  47. =====================================
  48. [CANCEL] [SAVE]
  49. """
  50. class GeneralSettings(QWidget):
  51. """
  52. SETTINGS
  53. =====================================
  54. [General] [Game Images] >[Connection]<
  55. =====================================
  56. TODO
  57. =====================================
  58. [CANCEL] [SAVE]
  59. """
  60. def __init__(self, parent=None):
  61. QWidget.__init__(self, parent=parent)
  62. layout = QGridLayout(self)
  63. layout.addWidget(QLabel("TODO", self), 0, 0, 3, 3)
  64. class GameImageSettingsView(QWidget):
  65. """
  66. SETTINGS
  67. =====================================
  68. [General] >[Game Images]< [Connection]
  69. =====================================
  70. LANGUAGE
  71. --------
  72. Preferred [ select lang ]
  73. Fallback [ none ]
  74. Enable any fallback [X]
  75. CACHE
  76. -----
  77. Location [/home/user/..]
  78. =====================================
  79. [CANCEL] [SAVE]
  80. """
  81. changed = pyqtSignal(bool)
  82. def __init__(self, parent=None):
  83. QWidget.__init__(self, parent=parent)
  84. layout = QGridLayout(self)
  85. # Language
  86. label = QLabel(_("<b>Language</b>"), self)
  87. layout.addWidget(label , 0, 0, 1, 3)
  88. # Preferred language combobox
  89. label = QLabel(_("Preferred"), self)
  90. self.preferredCombo = QComboBox(self)
  91. layout.addWidget(label , 1, 0, 1, 1)
  92. layout.addWidget(self.preferredCombo , 1, 1, 1, 2)
  93. # Fallback language combobox
  94. label = QLabel(_("Fallback"), self)
  95. self.fallbackCombo = QComboBox(self)
  96. layout.addWidget(label , 2, 0, 1, 1)
  97. layout.addWidget(self.fallbackCombo , 2, 1, 1, 2)
  98. # Any fallback checkbox
  99. label = QLabel(_("Enable any fallback"), self)
  100. self.anyFallbackCheck = QCheckBox(self)
  101. layout.addWidget(label , 3, 0, 1, 1)
  102. layout.addWidget(self.anyFallbackCheck, 3, 1, 1, 2)
  103. # Horizontal line
  104. line = HLine(self)
  105. layout.addWidget(line , 4, 0, 1, 3)
  106. # Cache
  107. label = QLabel(_("<b>Cache</b>"), self)
  108. layout.addWidget(label , 5, 0, 1, 3)
  109. label = QLabel(_("Location"), self)
  110. self.cachePathEdit = QLineEdit(self)
  111. self.cachePathEdit.setReadOnly(True)
  112. button = QToolButton(self)
  113. button.setText("..")
  114. layout.addWidget(label , 6, 0, 1, 1)
  115. layout.addWidget(self.cachePathEdit , 6, 1, 1, 1)
  116. layout.addWidget(button , 6, 2, 1, 1)
  117. self.restoreChanges()
  118. # Connections
  119. self.preferredCombo.currentIndexChanged.connect(self.__onPrefLangChanged)
  120. self.fallbackCombo.currentIndexChanged.connect(self.__onFallbackLangChanged)
  121. self.anyFallbackCheck.stateChanged.connect(self.__onAnyFallbackChanged)
  122. button.clicked.connect(self.__onChooseCacheDir)
  123. def restoreChanges(self):
  124. # Restore preferred language
  125. self.preferredCombo.clear()
  126. #self.preferredCombo.addItem("System", QVariant(None))
  127. index = 0
  128. for i in range(0, len(GameImageSettings.LANGUAGES)):
  129. lang = GameImageSettings.LANGUAGES[i]
  130. self.preferredCombo.addItem(lang, QVariant(lang))
  131. if lang == GameImageSettings.PreferredLang:
  132. index = i# + 1
  133. self.preferredCombo.setCurrentIndex(index)
  134. # Restore fallback language
  135. self.fallbackCombo.clear()
  136. self.fallbackCombo.addItem(_("None"), QVariant(None))
  137. index = 0
  138. for i in range(0, len(GameImageSettings.LANGUAGES)):
  139. lang = GameImageSettings.LANGUAGES[i]
  140. self.fallbackCombo.addItem(lang, QVariant(lang))
  141. if lang == GameImageSettings.FallbackLang:
  142. index = i + 1
  143. self.fallbackCombo.setCurrentIndex(index)
  144. # Restore any fallback state
  145. self.anyFallbackCheck.setChecked(GameImageSettings.AnyFallback)
  146. # Restore cache path
  147. self.cachePathEdit.setText(Global.ImageCache.path)
  148. def __onPrefLangChanged(self, index):
  149. self.__processChanges()
  150. def __onFallbackLangChanged(self, index):
  151. self.__processChanges()
  152. def __onAnyFallbackChanged(self, state):
  153. self.__processChanges()
  154. def __onChooseCacheDir(self):
  155. dialog = QFileDialog(self, _("Set cache dir"), Global.ImageCache.path)
  156. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  157. dialog.setFileMode(QFileDialog.Directory)
  158. if dialog.exec():
  159. self.cachePathEdit.setText(dialog.directory().path())
  160. self.__processChanges()
  161. def __processChanges(self):
  162. self.changed.emit(self.hasChanges())
  163. def hasChanges(self):
  164. if self.preferredCombo.currentData() != GameImageSettings.PreferredLang:
  165. return True
  166. if self.fallbackCombo.currentData() != GameImageSettings.FallbackLang:
  167. return True
  168. if self.anyFallbackCheck.isChecked() != GameImageSettings.AnyFallback:
  169. return True
  170. if Global.ImageCache.path != self.cachePathEdit.text():
  171. return True
  172. return False
  173. def saveChanges(self):
  174. GameImageSettings.PreferredLang = self.preferredCombo.currentData()
  175. GameImageSettings.FallbackLang = self.fallbackCombo.currentData()
  176. GameImageSettings.AnyFallback = self.anyFallbackCheck.isChecked()
  177. Global.ImageCache.setPath(self.cachePathEdit.text())
  178. class ConnectionSettings(QWidget):
  179. """
  180. SETTINGS
  181. =====================================
  182. [General] [Game Images] >[Connection]<
  183. =====================================
  184. PROXY
  185. -----
  186. [socks5] [user:pass@host:port ]
  187. [X] Proxy DNS
  188. USERAGENT
  189. ---------
  190. [Wiizard ]
  191. =====================================
  192. [CANCEL] [SAVE]
  193. """
  194. def __init__(self, parent=None):
  195. QWidget.__init__(self, parent=parent)
  196. layout = QGridLayout(self)
  197. # Proxy
  198. label = QLabel(_("<b>Proxy</b>"), self)
  199. layout.addWidget(label , 0, 0, 1, 3)
  200. self.proxyTypeCombo = QComboBox(self)
  201. self.proxyEdit = QLineEdit(self)
  202. layout.addWidget(self.proxyTypeCombo, 1, 0, 1, 1)
  203. layout.addWidget(self.proxyEdit , 1, 1, 1, 2)
  204. self.proxyDnsCheck = QCheckBox(_("Proxy DNS"), self)
  205. layout.addWidget(self.proxyDnsCheck , 2, 0, 1, 3)
  206. layout.addWidget(HLine(self) , 3, 0, 1, 3)
  207. # Useragent
  208. label = QLabel(_("<b>Useragent</b>"), self)
  209. layout.addWidget(label , 4, 0, 1, 3)
  210. self.useragentEdit = QLineEdit(self)
  211. layout.addWidget(self.useragentEdit , 5, 0, 1, 3)
  212. layout.rowStretch(5)
  213. self.proxyTypeCombo.addItem("http")
  214. self.proxyTypeCombo.addItem("socks4")
  215. self.proxyTypeCombo.addItem("socks5")
  216. self.useragentEdit.setText("Wiizard")
  217. class SettingsDialog(QDialog):
  218. def __init__(self, parent=None):
  219. QDialog.__init__(self, parent=parent)
  220. self.setWindowTitle(_("Settings"))
  221. layout = QGridLayout(self)
  222. self.tabWidget = QTabWidget(self)
  223. self.tabWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
  224. self.gameImagesTab = GameImageSettingsView(parent=self)
  225. #self.tabWidget.addTab(GeneralSettings(parent=self) , "General") # TODO
  226. self.tabWidget.addTab(self.gameImagesTab , _("Game Images"))
  227. #self.tabWidget.addTab(ConnectionSettings(parent=self), "Connection") #TODO
  228. self.cancelButton = QPushButton(_("Cancel"), self)
  229. self.saveButton = QPushButton(_("Save"), self)
  230. self.saveButton.setEnabled(False)
  231. layout.addWidget(self.tabWidget , 0, 0, 1, 4)
  232. layout.addWidget(self.cancelButton, 1, 2, 1, 1)
  233. layout.addWidget(self.saveButton , 1, 3, 1, 1)
  234. # Connections
  235. self.cancelButton.clicked.connect(self.__onCancelClicked)
  236. self.saveButton.clicked.connect(self.__onSaveClicked)
  237. self.gameImagesTab.changed.connect(self.__onSettingsChanged)
  238. def __onCancelClicked(self):
  239. self.close()
  240. def __onSaveClicked(self):
  241. if self.gameImagesTab.hasChanges():
  242. self.gameImagesTab.saveChanges()
  243. self.close()
  244. def __onSettingsChanged(self, hasChanges):
  245. if self.gameImagesTab.hasChanges():
  246. self.saveButton.setEnabled(True)
  247. else:
  248. self.saveButton.setEnabled(False)