-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (108 loc) · 5.05 KB
/
main.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
import logging
import sys
from functools import partial
from PyQt5.QtCore import QTimerEvent
from PyQt5.QtWidgets import QApplication, QMessageBox
from gui.constants import AUTOSAVE_INTERVAL, LAST_OPENED_CHARACTER_FILE, APP_LANGUAGE, ASK_ABOUT_LOADING
from gui.main_window_wrapper import MainWindowWrapper
from gui.qt_gui import SingleCharCore
from utils.console_logging import config_logger
class SingleCharApp(SingleCharCore, MainWindowWrapper):
def __init__(self):
default_size = (1360, 860)
MainWindowWrapper.__init__(self, default_size)
SingleCharCore.__init__(self)
self.connect_menu_bar()
self.setCentralWidget(self.container)
self.load_predefined_language()
self.autosave_interval = self.settings.value(AUTOSAVE_INTERVAL, 5, type=int) * 60000 # 60k ms = 1 min
self.ask_about_loading_last = self.settings.value(ASK_ABOUT_LOADING, False, type=bool)
self.autosave_timer_id = self.startTimer(self.autosave_interval)
self.restore_settings()
self.settings_window.buttons_box.accepted.connect(self.setting_new_autosave_interval)
self.change_main_window_title("Character Sheet")
self.load_last_saved()
def load_predefined_language(self):
language = self.settings.value(APP_LANGUAGE, 'PL', type=str)
if language == 'PL':
self._set_lang_pl()
self.menu_bar.change_language_pl.setChecked(True)
else:
self._set_lang_en()
self.menu_bar.change_language_en.setChecked(True)
self.retranslate()
self.menu_bar.retranslate()
def load_last_saved(self):
self.character_file = self.get_character_file()
if self.character_file:
if self.ask_about_loading_last:
proceed = QMessageBox.question(self.container,
QApplication.translate("Open last",
"Opening last file"),
QApplication.translate("Open last",
"Do you want to open last opened file?"),
QMessageBox.Yes | QMessageBox.No)
if proceed == QMessageBox.Yes:
self.open_selected_file(self.character_file)
else:
self.open_selected_file(self.character_file)
def get_character_file(self):
char_file = self.settings.value(LAST_OPENED_CHARACTER_FILE, '', type=str)
logging.debug("Char file: %s", char_file)
return char_file
def setting_new_autosave_interval(self):
logging.debug("Checking if autosave value changed")
tmp = self.settings.value(AUTOSAVE_INTERVAL, 5, type=int) * 60000
if tmp != self.autosave_interval:
self.autosave_interval = tmp
self._set_new_autosave_timer()
def _set_new_autosave_timer(self):
logging.debug("Setting new autosave timer to interval: %d", self.autosave_interval)
self.killTimer(self.autosave_timer_id)
self.autosave_timer_id = self.startTimer(self.autosave_interval)
def connect_menu_bar(self):
self.common_connect_menu_bar()
self.menu_bar.open_character.triggered.connect(self.open_file)
self.menu_bar.save_character_as.triggered.connect(self.save_file_as)
self.menu_bar.save_character.triggered.connect(self.save_file)
self.menu_bar.new_character.triggered.connect(self.create_new_character)
self.menu_bar.language_menu.triggered.connect(partial(self.change_language,
self.menu_bar.change_language_en))
def change_language(self, english_language_action):
logging.debug("Changing language")
language = "EN" if english_language_action.isChecked() else "PL"
if language == "PL":
self._set_lang_pl()
else:
self._set_lang_en()
self.settings.setValue(APP_LANGUAGE, language)
self.settings.sync()
self.retranslate()
def _set_lang_en(self):
QApplication.instance().removeTranslator(self.trans)
logging.debug("Setting to english")
def _set_lang_pl(self):
self.trans.load("data/languages/eng-pl")
QApplication.instance().installTranslator(self.trans)
logging.debug("Setting to polish")
def restore_settings(self):
self.restore_window_settings()
def timerEvent(self, a0: 'QTimerEvent') -> None:
logging.debug("Timer event")
self.backup_char_file()
def title_bar_from_file(self):
self.change_main_window_title(f"Character Sheet: {self.character_file}")
def _refresh_gui(self):
super(SingleCharApp, self)._refresh_gui()
self.title_bar_from_file()
def init_gui():
app = QApplication(sys.argv)
app.setStyle("windowsvista")
form = SingleCharApp()
form.show()
app.exec_()
def main():
config_logger(logging.DEBUG)
init_gui()
if __name__ == '__main__':
main()