-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path03_settings.py
More file actions
332 lines (271 loc) · 11.2 KB
/
03_settings.py
File metadata and controls
332 lines (271 loc) · 11.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#
# MIT License
#
# Copyright (c) 2023-2026 Erriez
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Source: https://github.com/Erriez/pyside6-getting-started
#
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QStatusBar,
QDialog,
QLabel,
QPushButton,
QLineEdit,
QComboBox,
QVBoxLayout,
QGridLayout,
QDialogButtonBox,
QMessageBox
)
from PySide6.QtGui import QAction, QIcon
from PySide6.QtCore import Qt, QCoreApplication, QPoint, QSize, QSettings
import sys
# Default application defines
ORGANIZATION_NAME = "Erriez"
APPLICATION_NAME = "PySide6 Settings Example"
class Setting:
"""
Class holding a single setting value and default value
"""
def __init__(self, default_value):
self._value = None
self._default_value = default_value
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if type(value) != type(self._default_value):
raise f"Invalid type"
self._value = value
@property
def default_value(self):
return self._default_value
class Settings:
"""
Class to convert settings between application and QSettings.
Handle object types and default values.
"""
_data = {
"path": Setting(""),
"startup-count": Setting(1),
"window-title": Setting(APPLICATION_NAME),
"window-state": Setting(Qt.WindowState.WindowNoState),
"window-size": Setting(QSize(350, 200)),
"window-position": Setting(QPoint(100, 100)),
}
def __init__(self):
self._deleted = False
# Create QSettings object
self._settings = QSettings()
# Load settings
self.load()
def __del__(self):
self.save()
def get(self, key):
return self._data[key].value
def set(self, key: str, value):
# Save new setting
self._data[key].value = value
def remove_all(self):
# Remove all settings
self._settings.clear()
self._deleted = True
def load(self):
# Load settings
for key in self._data.keys():
self.set(key, self._settings.value(key, self._data[key].default_value))
# Get settings path
self._data["path"].value = self._settings.fileName()
def save(self):
# Save settings when not deleted
if not self._deleted:
for key in self._data.keys():
self._settings.setValue(key, self._data[key].value)
# Save settings
self._settings.sync()
class DialogSettings(QDialog):
"""
Settings dialog window.
"""
WINDOW_STATES = {
"Normal": Qt.WindowState.WindowNoState,
"Minimized": Qt.WindowState.WindowMinimized,
"Maximized": Qt.WindowState.WindowMaximized,
}
def __init__(self, settings: Settings):
super().__init__()
# Store settings
self.settings = settings
# Set window title and icon
self.setWindowTitle("Settings")
self.setWindowIcon(QIcon.fromTheme(QIcon.ThemeIcon.DocumentProperties))
# Add window title
self.label_window_title = QLabel("Window title:")
self.edit_window_title = QLineEdit(self.settings.get("window-title"))
self.button_reset_window_title = QPushButton("Default")
self.button_reset_window_title.clicked.connect(self.on_reset_window_title)
# Default startup window state
self.label_startup_window = QLabel("Startup state:")
self.combobox_startup_window = QComboBox()
self.combobox_startup_window.addItems(list(self.WINDOW_STATES.keys()))
# Select default item in combobox
for i, key in enumerate(self.WINDOW_STATES.keys()):
if self.WINDOW_STATES.get(key) == self.settings.get("window-state"):
self.combobox_startup_window.setCurrentIndex(i)
break
# Startup count
self.label_startup_count = QLabel("Startup count:")
self.label_startup_count_value = QLabel(str(self.settings.get("startup-count")))
self.button_reset_startup_count = QPushButton("Reset")
self.button_reset_startup_count.clicked.connect(self.on_reset_startup_count)
# Settings path
self.label_path = QLabel("Settings path:")
self.label_path_value = QLabel(self.settings.get("path"))
# Button to remove all settings
self.button_remove = QPushButton("Remove")
self.button_remove.clicked.connect(self.on_button_remove)
# Dialog buttons Ok and Cancel
self.button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok |
QDialogButtonBox.StandardButton.Cancel |
QDialogButtonBox.StandardButton.Apply)
self.button_box.accepted.connect(self.on_accept)
self.button_box.rejected.connect(self.reject)
self.button_box.clicked.connect(self.on_button_box_click)
# Add controls to grid layout
self.grid = QGridLayout()
self.grid.addWidget(self.label_startup_window, 0, 0)
self.grid.addWidget(self.combobox_startup_window, 0, 1)
self.grid.addWidget(self.label_window_title, 1, 0)
self.grid.addWidget(self.edit_window_title, 1, 1)
self.grid.addWidget(self.button_reset_window_title, 1, 2)
self.grid.addWidget(self.label_startup_count, 2, 0)
self.grid.addWidget(self.label_startup_count_value, 2, 1)
self.grid.addWidget(self.button_reset_startup_count, 2, 2)
self.grid.addWidget(self.label_path, 3, 0)
self.grid.addWidget(self.label_path_value, 3, 1)
self.grid.addWidget(self.button_remove, 3, 2)
self.grid.setColumnStretch(1, 1)
# Add grid and button box to vertical box layout
self.vbox = QVBoxLayout()
self.vbox.addLayout(self.grid)
self.vbox.addStretch()
self.vbox.addWidget(self.button_box)
# Add vertical box layout to window
self.setLayout(self.vbox)
def on_button_remove(self):
# Remove all settings
self.settings.remove_all()
# Exit application
QMessageBox.information(self, "Settings", "Settings removed, closing application.")
QCoreApplication.quit()
def on_reset_window_title(self):
self.edit_window_title.setText(APPLICATION_NAME)
def on_reset_startup_count(self):
self.settings.set("startup-count", 1)
self.label_startup_count_value.setText(str(self.settings.get("startup-count")))
def on_accept(self):
# Handle OK button
self._update()
self.settings.save()
self.accept()
def on_button_box_click(self, button: QPushButton):
# Handle Apply button
role = self.button_box.buttonRole(button)
if role == QDialogButtonBox.ApplyRole:
self._update()
self.settings.save()
def _update(self):
self.settings.set("window-title", self.edit_window_title.text())
self.settings.set("window-state",
self.WINDOW_STATES.get(self.combobox_startup_window.currentText(),
Qt.WindowState.WindowNoState))
class MainWindow(QMainWindow):
"""
Main application window with menubar, label and statusbar.
"""
def __init__(self, settings: Settings):
super().__init__()
# Store settings
self.settings = settings
# Set window title, icon, state, size and position
self.setWindowTitle(self.settings.get("window-title"))
self.setWindowIcon(QIcon.fromTheme(QIcon.ThemeIcon.DocumentProperties))
self.setWindowState(self.settings.get("window-state"))
self.resize(self.settings.get("window-size"))
self.move(self.settings.get("window-position"))
# Create actions
self.action_quit = QAction(QIcon.fromTheme(QIcon.ThemeIcon.ApplicationExit), '&Quit', self)
self.action_quit.setShortcut('Ctrl+Q')
self.action_quit.setStatusTip('Exit application')
self.action_quit.triggered.connect(self.close)
self.action_settings = QAction(QIcon.fromTheme(QIcon.ThemeIcon.DocumentProperties), '&Settings', self)
self.action_settings.setShortcut('Ctrl+,')
self.action_settings.setStatusTip('Application settings')
self.action_settings.triggered.connect(self.on_settings)
self.action_about = QAction(QIcon.fromTheme(QIcon.ThemeIcon.HelpAbout), '&Help', self)
self.action_about.setShortcut('F1')
self.action_about.setStatusTip('About')
self.action_about.triggered.connect(self.on_about)
# Create menubar
self.menu = self.menuBar()
self.menu_file = self.menu.addMenu("&File")
self.menu_file.addAction(self.action_settings)
self.menu_file.addSeparator()
self.menu_file.addAction(self.action_quit)
self.menu_help = self.menu.addMenu("&Help")
self.menu_help.addAction(self.action_about)
# Create widgets
self.label = QLabel("Click File | Settings")
self.label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(self.label)
# Create statusbar
self.setStatusBar(QStatusBar(self))
def __del__(self):
self.settings.set("window-position", self.pos())
self.settings.set("window-size", self.size())
def on_settings(self):
dialog_settings = DialogSettings(self.settings)
if dialog_settings.exec() == QDialog.Accepted:
pass # Nothing to do
# Update window title
self.setWindowTitle(self.settings.get("window-title"))
def on_about(self):
QMessageBox.information(self, "About", APPLICATION_NAME)
def main():
# Set organization and application names used by QSettings
QCoreApplication.setOrganizationName(ORGANIZATION_NAME)
QCoreApplication.setApplicationName(APPLICATION_NAME)
# Load application settings
settings = Settings()
# Increment startup count
startup_count = settings.get("startup-count")
startup_count += 1
settings.set("startup-count", startup_count)
# Create application and pass settings
app = QApplication(sys.argv)
main_window = MainWindow(settings)
main_window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()