forked from cvfosammmm/Setzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetzer.in
executable file
·201 lines (162 loc) · 8.8 KB
/
setzer.in
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
#!/usr/bin/env python3
# coding: utf-8
# Copyright (C) 2018 Robert Griesel
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
import sys
import gettext
import argparse
import os.path
from setzer.workspace.workspace import Workspace
import setzer.workspace.workspace_viewgtk as view
from setzer.app.service_locator import ServiceLocator
from setzer.dialogs.dialog_locator import DialogLocator
class MainApplicationController(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id='org.cvfosammmm.Setzer', flags=Gio.ApplicationFlags.HANDLES_OPEN)
def do_startup(self):
Gtk.Application.do_startup(self)
self.is_active = False
# setup gettext
gettext.install('setzer', names=('ngettext',), localedir='@localedir_path@')
# get settings
self.settings = ServiceLocator.get_settings()
# init static variables
ServiceLocator.init_setzer_version('@setzer_version@')
ServiceLocator.init_resources_path('@resources_path@')
ServiceLocator.init_app_icons_path('@app_icons_path@')
resources_path = ServiceLocator.get_resources_path()
app_icons_path = ServiceLocator.get_app_icons_path()
Gtk.IconTheme.append_search_path(Gtk.IconTheme.get_default(), os.path.join(resources_path, 'icons'))
for folder in ['arrows', 'greek_letters', 'misc_math', 'misc_text', 'operators', 'relations']:
path = os.path.join(resources_path, 'symbols', folder)
Gtk.IconTheme.append_search_path(Gtk.IconTheme.get_default(), path)
Gtk.IconTheme.append_search_path(Gtk.IconTheme.get_default(), app_icons_path)
# init main window, model, dialogs
self.main_window = view.MainWindow(self)
ServiceLocator.init_main_window(self.main_window)
def do_open(self, files, number_of_files, hint=""):
if not self.is_active:
self.activate()
self.is_active = True
# open first session file if any
for file in files:
if file.get_path().endswith('.stzs'):
filename = file.get_path()
active_document = self.workspace.get_active_document()
documents = self.workspace.get_all_documents()
unsaved_documents = self.workspace.get_unsaved_documents()
dialog = DialogLocator.get_dialog('close_confirmation')
not_save_to_close_documents = dialog.run(unsaved_documents)['not_save_to_close_documents']
if len(not_save_to_close_documents) == 0:
if documents != None:
for document in documents:
self.workspace.remove_document(document)
self.workspace.load_documents_from_session_file(filename)
# open latex and bibtex files
for file in files:
if file.get_path().endswith('.tex') or file.get_path().endswith('.bib'):
document_candidate = self.workspace.get_document_by_filename(file.get_path())
if document_candidate != None:
self.workspace.set_active_document(document_candidate)
else:
self.workspace.create_document_from_filename(file.get_path(), activate=True)
def do_activate(self):
if not self.is_active:
self.activate()
self.is_active = True
def activate(self):
self.workspace = Workspace()
ServiceLocator.init_workspace(self.workspace)
ServiceLocator.init_autocomplete_provider(self.workspace)
DialogLocator.init_dialogs(self.main_window, self.workspace)
# init view
if self.settings.get_value('window_state', 'is_maximized'):
self.main_window.maximize()
else:
self.main_window.unmaximize()
self.main_window.set_default_size(self.settings.get_value('window_state', 'width'),
self.settings.get_value('window_state', 'height'))
self.main_window.current_width = self.settings.get_value('window_state', 'width')
self.main_window.current_height = self.settings.get_value('window_state', 'height')
self.first_window_state_event = True
self.main_window.show_all()
self.color_manager = ServiceLocator.get_color_manager()
self.fg_color = self.color_manager.get_theme_color('theme_fg_color')
self.bg_color = self.color_manager.get_theme_color('theme_bg_color')
self.main_window.connect('size-allocate', self.on_window_size_allocate)
self.main_window.connect('notify::is-maximized', self.on_window_maximize_event)
self.main_window.connect('delete-event', self.on_window_close)
self.main_window.connect('draw', self.on_window_draw)
# init controller
self.workspace.init_workspace_controller()
self.workspace.actions.quit_action.connect('activate', self.on_quit_action)
def on_window_size_allocate(self, main_window, window_size):
''' signal handler, update window size variables '''
if not main_window.ismaximized:
main_window.current_width, main_window.current_height = main_window.get_size()
def on_window_maximize_event(self, main_window, state_event):
''' signal handler, update window state variables '''
main_window.ismaximized = main_window.is_maximized()
return False
def on_window_draw(self, main_window, context):
''' check for theme changes, update sidebar, textviews '''
fg_color = self.color_manager.get_theme_color('theme_fg_color')
bg_color = self.color_manager.get_theme_color('theme_bg_color')
if self.fg_color.red != fg_color.red or self.bg_color.red != bg_color.red:
self.fg_color = fg_color
self.bg_color = bg_color
try: documents = self.workspace.open_documents
except AttributeError: pass
else:
is_dark_mode = ServiceLocator.get_is_dark_mode()
for document in documents:
document.set_dark_mode(is_dark_mode)
return False
def save_window_state(self):
main_window = self.main_window
self.settings.set_value('window_state', 'width', main_window.current_width)
self.settings.set_value('window_state', 'height', main_window.current_height)
self.settings.set_value('window_state', 'is_maximized', main_window.ismaximized)
self.settings.set_value('window_state', 'show_sidebar', self.workspace.show_sidebar)
self.settings.set_value('window_state', 'sidebar_paned_position', self.workspace.sidebar_position)
self.settings.set_value('window_state', 'show_help', self.workspace.show_help)
self.settings.set_value('window_state', 'show_preview', self.workspace.show_preview)
self.settings.set_value('window_state', 'preview_paned_position', self.workspace.preview_position)
self.settings.set_value('window_state', 'show_build_log', self.workspace.show_build_log)
self.settings.set_value('window_state', 'build_log_paned_position', self.workspace.build_log_position)
self.settings.pickle()
def on_window_close(self, window=None, parameter=None):
self.save_quit()
return True
def on_quit_action(self, action=None, parameter=None):
self.save_quit()
def save_quit(self):
for document in self.workspace.open_documents: document.state_manager.save_document_state()
documents = self.workspace.get_unsaved_documents()
active_document = self.workspace.get_active_document()
if documents == None or active_document == None or DialogLocator.get_dialog('close_confirmation').run(documents)['all_save_to_close']:
self.save_window_state()
self.workspace.save_to_disk()
self.quit()
argparser = argparse.ArgumentParser(usage='%(prog)s [OPTION...] [FILE...]')
argparser.add_argument('-V', '--version', action='version', version='@setzer_version@')
argparser.add_argument('file', nargs='*', help=argparse.SUPPRESS)
argparser.parse_args()
main_controller = MainApplicationController()
exit_status = main_controller.run(sys.argv)
sys.exit(exit_status)