-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgtk_multitoner.py
executable file
·535 lines (456 loc) · 18.9 KB
/
gtk_multitoner.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright © 2013 by Lasse Fister <[email protected]>
#
# This file is part of Multitoner.
#
# Multitoner 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.
#
# Multitoner 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/>.
""" Multitoner Main Application.
Create “Multitone” (Monotone, Duotone, Tritone, Quadtone, …) EPS-files
for printing.
"""
from __future__ import division, print_function, unicode_literals
import os
from gi.repository import Gtk
from gtk_actiongroup import ActionGroup
from gtk_dialogs import show_open_image_dialog, show_message, show_save_as_dialog, \
show_save_as_eps_dialog, show_about_dialog
from gtk_document import Document
from ghostscript_workers import factory as gs_workers_factory
from mtt2eps import model2eps
from compatibility import decode, encode
__all__ = ['Multitoner']
DIRECTORY = decode(os.path.dirname(os.path.realpath(__file__)))
CSS_FILE = os.path.join(DIRECTORY, 'assets', 'style.css')
# just a preparation for i18n
def _(string):
return string
UI_INFO = """
<ui>
<menubar name='MenuBar'>
<menu action='FileMenu'>
<menuitem action='FileNew' />
<separator />
<menuitem action='FileOpen' />
<menuitem action='FileOpenRecent' />
<separator />
<menuitem action='FileSaveDocument' />
<menuitem action='FileSaveAsDocument' />
<menuitem action='FileSaveAll' />
<separator />
<menuitem action='FileExportImage' />
<separator />
<menuitem action='FileClose' />
<menuitem action='FileCloseOther' />
<menuitem action='FileCloseAll' />
<separator />
<menuitem action='FileQuit' />
</menu>
<menu action='EditMenu'>
<menuitem action='EditUndo' />
<menuitem action='EditRedo' />
<separator />
<menuitem action='EditOpenPreview' />
</menu>
<menu action='HelpMenu'>
<menuitem action='HelpAbout' />
</menu>
</menubar>
<toolbar name="ToolBar">
<toolitem action='FileNew' />
<toolitem action='FileOpen' />
<toolitem action='FileSaveDocument' />
<separator />
<toolitem action='EditUndo' />
<toolitem action='EditRedo' />
<separator />
<toolitem action='EditOpenPreview' />
<toolitem action='FileExportImage' />
</toolbar>
</ui>
"""
class Multitoner(Gtk.Grid):
""" Manage multiple Documents and provide gtk menus and accelarators """
def __init__(self):
Gtk.Grid.__init__(self)
self._gradient_worker, self._preview_worker = gs_workers_factory()
self._documents = {}
self._active_document = None
self._document_actions = self._make_document_actions()
self._global_actions = self._make_global_actions()
menubar, toolbar = self._init_menu()
self.attach(menubar, 0, 0, 1, 1)
self.attach(toolbar, 0, 1, 1, 1)
self._notebook = Gtk.Notebook()
self._notebook.set_scrollable(True)
self._notebook.connect('switch-page', self.switch_page_handler)
self._notebook.connect('page-removed' , self.page_add_remove_handler)
self._notebook.connect('page-added' , self.page_add_remove_handler)
self.attach(self._notebook, 0, 2, 1, 1)
self._set_active_document_state()
self._set_global_state()
@staticmethod
def _actions_set_sensitive(action_group, *action_tuples):
for action_name, sensitive in action_tuples:
action = action_group.get_action(action_name)
action.set_sensitive(sensitive)
def _set_active_document_state(self):
if self._active_document is None:
# there is no current page
self._document_actions.set_sensitive(False)
return
else:
self._document_actions.set_sensitive(True)
undos, redos = self._active_document.history.get_counts()
actions = (
('FileSaveDocument', self._active_document.has_changes)
, ('EditUndo', undos > 0)
, ('EditRedo', redos > 0)
, ('FileCloseOther', len(self._documents) > 1)
)
self._actions_set_sensitive(self._document_actions, *actions)
def _get_document_by_widget(self, widget):
for doc in self._documents.values():
if doc.widget is widget:
return doc
return None
def _get_document_by_page(self, page):
widget = self._notebook.get_nth_page(page)
return self._get_document_by_widget(widget)
def _set_global_state(self):
actions = (
('FileSaveAll', self._has_changed_documents)
, ('FileCloseAll', len(self._documents) > 0)
)
self._actions_set_sensitive(self._global_actions, *actions)
def _set_current_page(self, page=None):
if page is None:
page = self._notebook.get_current_page()
# _active_document is either a Document or None
self._active_document = self._get_document_by_page(page)
self._set_active_document_state()
self._set_global_state()
def switch_page_handler(self, widget, page, page_num):
self._set_current_page(page_num)
def page_add_remove_handler(self, *data):
self._set_current_page()
def _get_document_by_filename(self, filename):
for doc in self._documents.values():
if doc.filename == filename:
return doc
return None
@property
def _has_changed_documents(self):
for doc in self._documents.values():
if doc.has_changes:
return True
return False
def _set_active_page(self, doc):
page = self._notebook.page_num(doc.widget)
self._notebook.set_current_page(page)
def on_document_state_update(self, doc, *args):
if doc is self._active_document:
self._set_active_document_state()
self._set_global_state()
def _init_menu(self):
uimanager = Gtk.UIManager()
uimanager.add_ui_from_string(UI_INFO)
uimanager.insert_action_group(self._global_actions)
uimanager.insert_action_group(self._document_actions)
menubar = uimanager.get_widget("/MenuBar")
toolbar = uimanager.get_widget("/ToolBar")
#toolbar = uimanager.get_widget("/ToolBar")
# this removes the immediate dependency to window
def onRealize(widget, *args):
"""
closure to connect to window when it establishes this widget
"""
window = widget.get_toplevel()
# Add the accelerator group to the toplevel window
accelgroup = uimanager.get_accel_group()
window.add_accel_group(accelgroup)
# connect just once ever
widget.disconnect(realize_handler_id)
#save realize_handler_id for the closure of onRealize
realize_handler_id = self.connect('realize' , onRealize)
return menubar, toolbar
def _make_global_actions(self):
action_group = ActionGroup('global_actions')
action_group.add_actions([
('FileMenu', None, _('File'), None,
None, None)
, ('EditMenu', None, _('Edit'), None,
None, None)
, ('HelpMenu', None, _('Help'), None,
None, None)
, ('FileNew', Gtk.STOCK_NEW, _('New'), '<Ctrl>n',
_('Start a new document.'), self.action_file_new_handler)
, ('FileQuit', Gtk.STOCK_QUIT, _('Quit'), '<ctrl>q',
None, self.action_file_quit_handler)
, ('FileOpen', Gtk.STOCK_OPEN, _('Open'), '<Ctrl>o',
_('Open a document.'), self.action_file_open_handler)
, ('FileSaveAll', Gtk.STOCK_SAVE, _('Save All'), '<Ctrl><Shift>s',
_('Save all documents.'), self.action_file_save_all_handler)
, ('FileCloseAll', Gtk.STOCK_CLOSE, _('Close All'), '<Ctrl><Shift>w',
_('Close all documents.'), self.action_file_close_all_handler)
, ('HelpAbout', Gtk.STOCK_ABOUT, None, None,
None, self.action_help_about_handler)
])
# recent files chooser
# does only .mtt files
recent_action = Gtk.RecentAction.new_for_manager('FileOpenRecent',
_('Recent Files'), None, None, None)
recent_filter = Gtk.RecentFilter()
recent_filter.add_pattern ("*" + Document.file_extension)
recent_action.add_filter(recent_filter)
recent_action.connect('item-activated', self.open_recent_handler)
action_group.add_action(recent_action)
return action_group
def _make_document_actions(self):
action_group = ActionGroup('document_actions')
action_group.add_actions([
('EditUndo', Gtk.STOCK_UNDO, _('Undo'), '<Ctrl>z',
_('undo'), self.action_edit_undo_handler)
, ('EditRedo', Gtk.STOCK_REDO, _('Redo'), '<Ctrl>y',
_('redo'), self.action_edit_redo_handler)
, ('FileSaveDocument', Gtk.STOCK_SAVE, _('Save'), '<ctrl>s',
_('Save the current document.'), self.action_file_save_document_handler)
, ('FileSaveAsDocument', Gtk.STOCK_SAVE, _('Save As'), '<ctrl><alt>s',
None, self.action_file_save_document_as_handler)
, ('FileClose', Gtk.STOCK_CLOSE, _('Close'), '<ctrl>w',
None, self.action_file_close_handler)
, ('FileCloseOther', Gtk.STOCK_CLOSE, _('Close other documents'),
'<ctrl><alt>w', None, self.action_file_close_other_handler)
, ('EditOpenPreview', Gtk.STOCK_PRINT_PREVIEW, _('Open A Preview Window'),
None, _('Open a preview window.'), self.action_open_preview_handler)
])
action_group.add_icon_actions([
('FileExportImage', _('Export An Image'), _('Export an image as EPS file'),
'document-save', self.action_file_export_image_handler, '<ctrl>E')
# , ...
])
action_group.set_sensitive(False)
return action_group
def _register_document(self, doc):
self._documents[doc.id] = doc
doc.label.connect('close', self.close_document_handler, doc.id)
doc.add(self)
page = self._notebook.append_page(doc.widget, doc.label)
self._notebook.set_tab_reorderable(doc.widget, True)
self._notebook.set_current_page(page)
doc.widget.show_all()
return doc.id
def _unregister_document(self, doc):
doc.remove(self)
del self._documents[doc.id]
page = self._notebook.page_num(doc.widget)
self._notebook.remove_page(page)
def _make_new_document(self):
doc = Document(self._gradient_worker, self._preview_worker)
self._register_document(doc)
def _announce_error(self, error, moreInfo=None):
window = self.get_toplevel()
show_message(window, 'error', error, moreInfo)
def _open_document(self, filename):
try:
doc = Document.new_from_file(self._gradient_worker,
self._preview_worker, filename)
except Exception as e:
error = _('Error opening the file "{0}"').format(filename)
detail = _('Message: {0} {1}').format( e, type(e))
self._announce_error(error, detail)
else:
self._register_document(doc)
def open_document(self, filename):
doc = self._get_document_by_filename(filename)
if doc:
page = self._notebook.page_num(doc.widget)
self._notebook.set_current_page(page)
return doc.id
else:
return self._open_document(filename)
def _show_file_open_dialog(self):
window = self.get_toplevel()
dialog = Gtk.FileChooserDialog(title=_('Open File')
, parent=window
, action=Gtk.FileChooserAction.OPEN
, buttons=( Gtk.STOCK_CANCEL
, Gtk.ResponseType.CANCEL
, Gtk.STOCK_OPEN
, Gtk.ResponseType.ACCEPT
)
)
if dialog.run() == Gtk.ResponseType.ACCEPT:
filename = dialog.get_filename()
self.open_document(filename)
dialog.destroy()
def _show_file_save_as_dialog(self, doc):
self._set_active_page(doc)
window = self.get_toplevel()
filename = show_save_as_dialog(window, doc.filename,
Document.untitled_name + Document.file_extension)
if filename is not None:
doc.save_as(filename)
def _ask_ok_cancel(self, question, more_info=None):
window = self.get_toplevel()
dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.QUESTION,
Gtk.ButtonsType.OK_CANCEL, question)
if more_info is not None:
dialog.format_secondary_text(more_info)
response = dialog.run()
dialog.destroy()
if response == Gtk.ResponseType.OK:
return True
return False
def close_document(self, doc):
ok = True
if doc.has_changes:
self._set_active_page(doc)
ok = self._ask_ok_cancel(
_('Close without Saving?'),
_('All changes to the document will be lost.')
)
if not ok:
return
self._unregister_document(doc)
doc.destroy()
def save_document(self, doc):
if doc.filename is None:
self._show_file_save_as_dialog(doc)
else:
doc.save()
def open_recent_handler(self, widget):
item = widget.get_current_item()
uri = item.get_uri()
if uri.startswith('file://'):
filename = uri[(len('file://')):]
self.open_document(filename)
# global action handlers
def action_file_new_handler(self, widget):
self._make_new_document()
def action_file_open_handler(self, widget):
self._show_file_open_dialog()
def close_document_handler(self, widget, id, *data):
doc = self._documents.get(id, None)
if doc is None:
return
self.close_document(doc)
def quit(self):
ok = True
if self._has_changed_documents:
ok = self._ask_ok_cancel(
_('Quit without Saving?'),
_('There are documents with changes. All changes '
'will be lost.')
)
if ok:
Gtk.main_quit()
return False
return True
def quit_handler(self, *data):
return self.quit()
action_file_quit_handler = quit_handler
def action_file_close_all_handler(self, widget):
active = self._active_document
for doc in self._documents.values():
self.close_document(doc)
if active is not None:
self._set_active_page(active)
def action_file_save_all_handler(self, widget):
active = self._active_document
for doc in self._documents.values():
if doc.has_changes:
self.save_document(doc)
if active is not None:
self._set_active_page(active)
# document action handlers
def action_file_close_handler(self, widget):
if self._active_document is None:
return
self.close_document(self._active_document)
def action_file_close_other_handler(self, widget):
if self._active_document is None:
return
active = self._active_document
for doc in self._documents.values():
if doc is not active:
self.close_document(doc)
self._set_active_page(active)
def action_file_save_document_handler(self, widget):
if self._active_document is None:
return
self.save_document(self._active_document)
def action_file_save_document_as_handler(self, widget):
if self._active_document is None:
return
self._show_file_save_as_dialog(self._active_document)
def action_edit_undo_handler(self, widget):
if self._active_document is None:
return
self._active_document.history.undo()
def action_edit_redo_handler(self, widget):
if self._active_document is None:
return
self._active_document.history.redo()
def action_open_preview_handler(self, widget):
if self._active_document is None:
return
self._active_document.open_preview()
def action_file_export_image_handler(self, widget):
if self._active_document is None:
return
window = self.get_toplevel()
image_filename = show_open_image_dialog(window)
if image_filename is None:
return
eps_filename = show_save_as_eps_dialog(window, image_filename)
if eps_filename is None:
return
result, message = model2eps(self._active_document.model,
image_filename, eps_filename)
if message:
window = self.get_toplevel()
show_message(window, *message)
def action_help_about_handler(self, widget):
window = self.get_toplevel()
show_about_dialog(window)
if __name__ == '__main__':
""" bootstrap the application """
import sys
import os
from gi.repository import GObject, GdkPixbuf
GObject.threads_init()
use_gui, __ = Gtk.init_check(sys.argv)
window = Gtk.Window()
window.set_title(_('Multitoner'))
multitoner_icon_filename = os.path.join(DIRECTORY, 'assets', 'images',
'multitoner_icon.svg')
multitoner_icon = GdkPixbuf.Pixbuf.new_from_file(multitoner_icon_filename)
window.set_icon(multitoner_icon)
window.set_position(Gtk.WindowPosition.CENTER)
window.set_default_size(640, 480)
window.set_has_resize_grip(True)
# the theme should do so
window.set_border_width(5)
css_provider = Gtk.CssProvider()
css_provider.load_from_path(CSS_FILE)
screen = window.get_screen()
style_context = Gtk.StyleContext()
style_context.add_provider_for_screen(screen, css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
multitoner = Multitoner()
window.connect('delete-event', multitoner.quit_handler)
window.add(multitoner)
window.show_all()
Gtk.main()