-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
111 lines (80 loc) · 3.49 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
import json
from PyQt5.QtWidgets import QApplication
import appdirs
import os
class Settings:
settings_dict = {}
def __init__(self):
try:
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Load settings from json file in the application data directory
with open(os.path.join(app_dir, 'settings.json'), 'r') as f:
Settings.settings_dict = json.load(f)
except (json.JSONDecodeError, FileNotFoundError):
self.default_settings()
self.save_settings()
def default_settings(self):
Settings.settings_dict = {
'window_position': [120, 120],
'window_size': [800, 600],
'color_scheme': 'dark',
'export_index': 1,
'current_problem_index': 0
}
def increment_export_index(self):
Settings.settings_dict['export_index'] += 1
self.save_settings()
def get_export_index(self):
return Settings.settings_dict['export_index']
def get_window_position(self):
return Settings.settings_dict['window_position']
def get_window_size(self):
return Settings.settings_dict['window_size']
def get_color_scheme(self):
return Settings.settings_dict['color_scheme']
def get_current_problem_index(self):
return Settings.settings_dict['current_problem_index']
def save_window_position_and_size(self, window):
screen = QApplication.desktop().screenGeometry()
# Get the window position and size
pos = window.pos()
size = window.size()
# Get the height of the title bar
title_bar_height = window.frameGeometry().height() - window.geometry().height()
# Adjust the y-coordinate of the position
pos.setY(pos.y() + title_bar_height)
# Check if the window is outside the screen and adjust if necessary
if pos.x() < 0:
pos.setX(0)
if pos.y() < 0:
pos.setY(0)
if pos.x() + size.width() > screen.width():
pos.setX(screen.width() - size.width())
if pos.y() + size.height() > screen.height():
pos.setY(screen.height() - size.height())
# Save the position and size to the settings
Settings.settings_dict['window_position'] = [pos.x(), pos.y()]
Settings.settings_dict['window_size'] = [size.width(), size.height()]
self.save_settings()
def save_current_problem_index(self, index):
Settings.settings_dict['current_problem_index'] = index
self.save_settings()
def toggle_color_scheme(self):
if Settings.settings_dict['color_scheme'] == 'dark':
Settings.settings_dict['color_scheme'] = 'light'
else:
Settings.settings_dict['color_scheme'] = 'dark'
self.save_settings()
def save_settings(self):
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Check if the directory exists and create it if it doesn't
os.makedirs(app_dir, exist_ok=True)
# Save changes to json file in the application data directory
with open(os.path.join(app_dir, 'settings.json'), 'w') as f:
json.dump(Settings.settings_dict, f, indent=4)