-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
140 lines (113 loc) · 5.02 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# For GUI
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import pyqtSignal
# My files
from _settingsForm import Ui_Settings
# Other
import sys
from json import load, dump
settingsPath = "./"
settingsFile = "settings.json"
def loadSettings():
global settingsFile, settingsPath
filepath = settingsPath + settingsFile
with open(filepath, "r", encoding="utf-8") as fil:
dictToReturn = load(fil)
return dictToReturn
def dumpSettings(settingsDict):
global settingsFile, settingsPath
filepath = settingsPath + settingsFile
with open(filepath, "w", encoding="utf-8") as fil:
dump(settingsDict, fil)
class mySettingsWindow(QtWidgets.QMainWindow):
sigSettingsSaved = pyqtSignal(bool)
def __init__(self):
super(mySettingsWindow, self).__init__()
self.ui = Ui_Settings()
self.ui.setupUi(self)
self.setWindowIcon(QtGui.QIcon('img.png'))
self.setWindowTitle("Cgpa Fantasy: Settings")
# Variables
self.isChanged = False
try:
self.settingsDict = loadSettings()
except (Exception, OSError) as ex:
self.errorMessage("Unable to read settings file", str(ex))
# Set UI initial values
self.setEnabledSpinBoxWindowHeight()
self.ui.spinBoxWindowHeight.setValue(self.settingsDict["WinHei"])
self.ui.radioRememberWinHei.setChecked(self.settingsDict["rememberWinHei"])
self.ui.radioForceWinHei.setChecked(not (self.settingsDict["rememberWinHei"]) )
# Dynamic Connections
self.ui.btnSavePrefs.clicked.connect(self.onSavePrefsClick)
self.ui.radioForceWinHei.clicked.connect(self.chooseForcedWinHei)
self.ui.radioRememberWinHei.clicked.connect(self.chooseRememberWinhei)
self.ui.spinBoxWindowHeight.valueChanged.connect(self.onSBoxCustWinHeiChanged)
# Adopted from: https://stackoverflow.com/questions/45789084/show-messagebox-when-windows-x-close-button-is-pressed
def closeEvent(self, event):
if not self.isChanged:
event.accept()
elif self.confirmationMsg("Closing application", "You made changes on settings and did not saved. If you quit now, all changes will be lost. Do you really want to quit?"):
event.accept()
else:
event.ignore()
def onSavePrefsClick(self):
try:
dumpSettings(self.settingsDict)
self.isChanged = False
self.sigSettingsSaved.emit(True)
except (Exception, OSError) as ex:
self.errorMessage("Unable to save settings to file", str(ex))
def setEnabledSpinBoxWindowHeight(self):
self.ui.spinBoxWindowHeight.setEnabled(not (self.settingsDict["rememberWinHei"]) )
def chooseForcedWinHei(self):
self.settingsDict["rememberWinHei"] = 0
self.setEnabledSpinBoxWindowHeight()
self.isChanged = True
def chooseRememberWinhei(self):
self.settingsDict["rememberWinHei"] = 1
self.setEnabledSpinBoxWindowHeight()
self.isChanged = True
def onSBoxCustWinHeiChanged(self):
self.settingsDict["WinHei"] = self.ui.spinBoxWindowHeight.value()
self.isChanged = True
###############################
# Message boxes
###############################
def infoMessage(self, title="Info", text="Text text"):
msg = QMessageBox()
msg.setWindowTitle(title)
msg.setWindowIcon(QtGui.QIcon('img.png'))
msg.setText(text)
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.setStyleSheet("*{background-color: rgb(70, 70, 70);}\nQLabel{color: white;}\nQPushButton{color: white;}")
msg.exec_()
def errorMessage(self, title="Error", text="An error occured"):
msg = QMessageBox()
msg.setWindowTitle(title)
msg.setWindowIcon(QtGui.QIcon('img.png'))
msg.setText(text)
msg.setIcon(QMessageBox.Critical)
msg.setStandardButtons(QMessageBox.Ok)
msg.setStyleSheet("*{background-color: rgb(70, 70, 70);}\nQLabel{color: white;}\nQPushButton{color: white;}")
msg.exec_()
def confirmationMsg(self, title="Question", text="Are you sure doing xyz?"):
msg = QMessageBox()
msg.setWindowTitle(title)
msg.setWindowIcon(QtGui.QIcon('img.png'))
msg.setText(text)
msg.setStyleSheet("*{background-color: rgb(70, 70, 70);}\nQLabel{color: white;}\nQPushButton{color: white;}")
answer = msg.question(self, title, text, QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
return answer == QMessageBox.Yes
if __name__ == "__main__":
def app():
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
win = mySettingsWindow()
# app.aboutToQuit.connect(win.onAboutToQuit)
win.show()
sys.exit(app.exec_())
app()