-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·142 lines (105 loc) · 4.67 KB
/
main.py
File metadata and controls
executable file
·142 lines (105 loc) · 4.67 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
import gettext
import locale
import os
from pathlib import Path
import wx
from constants import WORKDIR
from icons import IconRegistry
from helpers.loader import Loader
from helpers.logger import logger
from helpers.settings import Settings, SettingsRepository
from windows.components.stc.styles import apply_stc_theme, set_theme_loader
from windows.components.stc.themes import ThemeManager
from windows.components.stc.registry import SyntaxRegistry
from windows.components.stc.profiles import BASE64, CSV, HTML, JSON, MARKDOWN, REGEX, SQL, TEXT, XML, YAML
from windows.components.stc.theme_loader import ThemeLoader
class PeterSQL(wx.App):
locale: wx.Locale = wx.Locale()
settings_repository = SettingsRepository(WORKDIR / "settings.yml")
settings: Settings = settings_repository.load()
main_frame: wx.Frame = None
icon_registry_16: IconRegistry
syntax_registry: SyntaxRegistry
theme_loader: ThemeLoader
def OnInit(self) -> bool:
Loader.loading.subscribe(self._on_loading_change)
self.icon_registry_16 = IconRegistry(WORKDIR / "icons", 16)
self._init_theme_loader()
self.theme_manager = ThemeManager(apply_function=apply_stc_theme)
self.syntax_registry = SyntaxRegistry([JSON, SQL, XML, YAML, MARKDOWN, HTML, REGEX, CSV, BASE64, TEXT])
self._init_locale()
self.open_session_manager()
return True
def _init_theme_loader(self) -> None:
theme_name = self.settings.get_value("ui", "appearance", "theme", default="petersql")
self.theme_loader = ThemeLoader(WORKDIR / "themes")
try:
self.theme_loader.load_theme(theme_name)
set_theme_loader(self.theme_loader)
except FileNotFoundError:
logger.warning(f"Theme '{theme_name}' not found, using default colors")
except Exception as ex:
logger.error(f"Error loading theme: {ex}", exc_info=True)
def _init_locale(self):
_locale = self.settings.get_value("language", default="en_US")
translation = gettext.translation(
'petersql',
localedir=WORKDIR.joinpath("locale"),
languages=[_locale],
fallback=True
)
translation.install(['gettext', 'ngettext', 'npgettext', 'pgettext'])
def gettext_wrapper(message):
return translation.gettext(message)
gettext.gettext = gettext_wrapper
self.locale.Init()
self.locale.AddCatalogLookupPathPrefix(str(WORKDIR.joinpath("locale")))
self.locale.AddCatalog("petersql")
locale.setlocale(locale.LC_ALL, _locale)
def open_session_manager(self) -> None:
from windows.dialogs.connections.view import ConnectionsManager
self.connection_manager = ConnectionsManager(None)
self.connection_manager.SetIcon(
wx.Icon(str(WORKDIR / "icons" / "petersql.ico"))
)
self.connection_manager.Show()
def open_main_frame(self) -> None:
try:
from windows.main.controller import MainFrameController
self.main_frame = MainFrameController()
size_values = self.settings.get_value("ui", "window", "size", default=[1920, 1080])
size = wx.Size(*list(map(int, size_values)))
self.main_frame.SetSize(width=size.width, height=size.height)
position_values = self.settings.get_value("ui", "window", "position", default=[0, 0])
position = wx.Point(*list(map(int, position_values)))
self.main_frame.SetPosition(position)
self.main_frame.Layout()
self.main_frame.SetIcon(
wx.Icon(str(WORKDIR / "icons" / "petersql.ico"))
)
self.main_frame.Show()
self.main_frame.Bind(wx.EVT_SIZE, self._on_size)
self.main_frame.Bind(wx.EVT_MOVE, self._on_move)
except Exception as ex:
logger.error(ex, exc_info=True)
def _on_size(self, event: wx.SizeEvent) -> None:
size = event.GetSize()
self.settings.set_value("ui", "window", "size", value=[size.Width, size.Height])
self.main_frame.Layout()
def _on_move(self, event: wx.MouseEvent) -> None:
position = event.GetPosition()
self.settings.set_value(
"ui", "window", "position", value=[position.x, position.y]
)
self.main_frame.Layout()
def _on_loading_change(self, loading: bool) -> None:
"""Handle loading state changes"""
if loading:
wx.BeginBusyCursor()
else:
wx.EndBusyCursor()
def do_exit(self, event: wx.Event) -> None:
self.ExitMainLoop()
if __name__ == "__main__":
app = PeterSQL()
app.MainLoop()