File: gui.py

package info (click to toggle)
bookletimposer 0.2-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 612 kB
  • sloc: python: 1,046; xml: 12; makefile: 4
file content (374 lines) | stat: -rw-r--r-- 14,982 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

########################################################################
# 
# PdfImposer - Utility to convert PDF files between diffrents page layouts
# Copyright (C) 2008-2012 Kjö Hansi Glaz <kjo@a4nancy.net.eu.org>
# 
# 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/>.
#
########################################################################

########################################################################
#
# gui.py
#
# This file contains the GTK+ graphic utility.
#
########################################################################

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
from gi.repository import GObject
GObject.threads_init()
from gi.repository import Gio

import os.path
import threading
import traceback

import pdfimposer # We need its exceptions

import backend
import pdfimposer
import config
from config import debug

class UserInterrupt(backend.BookletImposerError):
    """Exception raised when the user interrupted the conversion

    """
    # XXX: this is probably not the right way to do...

class BookletImposerUI(object):
    """BookletImposer graphical user interface
    
    """
    
    def __init__(self, preferences=None):
        """
        
        """
        if preferences:
            self.__preferences = preferences
        else:
            self.__preferences = backend.ConverterPreferences()
        self.__create_gui()
        if preferences:
            self.__apply_preferences()
        self.__main_window.show()

    def __create_gui(self):
        Gtk.IconTheme.get_default().append_search_path(config.get_datadir())

        builder = Gtk.Builder()
        builder.set_translation_domain("bookletimposer")
        builder.add_from_file(os.path.join(config.get_datadir(), "bookletimposer.ui"))
        builder.connect_signals(self)

        self.__main_window = builder.get_object("main_window")
        self.__preferences_table = builder.get_object("preferences_table")
        self.__input_file_chooser_button = \
            builder.get_object("input_file_chooser_button")
        self.__bookeltize_radiobutton = builder.get_object("bookletize_radiobutton")
        self.__linearize_radiobutton = builder.get_object("linearize_radiobutton")
        self.__reduce_radiobutton =  builder.get_object("reduce_radiobutton")
        self.__copy_pages_radiobutton = builder.get_object("copy_pages_radiobutton")
        self.__layout_combobox = builder.get_object("layout_combobox")
        self.__paper_format_combobox = \
            builder.get_object("output_paper_format_combobox")
        self.__output_file_chooser_button = self.__create_output_file_chooser_button(builder)
        self.__progressbar_conversion = builder.get_object("conversion_progressbar")
        self.__about_button = builder.get_object("about_button")
        self.__help_button = builder.get_object("help_button")
        self.__apply_button = builder.get_object("apply_button")
        self.__stop_button = builder.get_object("stop_button")

        self.__about_dialog = builder.get_object("about_dialog")

        self.__fill_paper_formats()
        self.__fill_layouts()
        self.__add_keybindings()

    def __add_keybindings(self):
        accelgroup = Gtk.AccelGroup()
        accelgroup.connect(Gdk.KEY_Escape, 0, Gtk.AccelFlags.VISIBLE,
            lambda group, accelerable, key, mod: self.close_application())
        accelgroup.connect(Gdk.KEY_q, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE,
            lambda group, accelerable, key, mod: self.close_application())
        self.__main_window.add_accel_group(accelgroup)

    def __create_output_file_chooser_button(self, builder):
        # Emulate a FileChooserButton for saving
        output_file_chooser_button = \
            builder.get_object("output_file_chooser_button")
        output_file_chooser_button.__filename = None

        def output_file_chooser_button_set_filename(filename):
            output_file_chooser_button.__filename = filename
            output_file_chooser_file_image = \
                builder.get_object("output_file_chooser_file_image")
            output_file_chooser_label = \
                builder.get_object("output_file_chooser_label")
            output_file_chooser_label.set_text(os.path.basename(filename))
            output_file_chooser_file_image.set_visible(True)
        output_file_chooser_button.set_filename = \
            output_file_chooser_button_set_filename

        def output_file_chooser_button_get_filename():
            return self.__output_file_chooser_button.__filename
        output_file_chooser_button.get_filename = \
            output_file_chooser_button_get_filename

        return output_file_chooser_button

    @staticmethod
    def set_liststore_for_combobox(combobox):
        liststore = Gtk.ListStore(GObject.TYPE_STRING)
        combobox.set_model(liststore)
        cell = Gtk.CellRendererText()
        combobox.pack_start(cell, True)
        combobox.add_attribute(cell, 'text', 0)
        return liststore

    def __fill_paper_formats(self):
        liststore = self.set_liststore_for_combobox(
            self.__paper_format_combobox)
        formats = pdfimposer.AbstractConverter.page_formats.keys()
        formats.sort()
        for format in formats:
            liststore.append([format])
        self.__paper_format_combobox.set_active(1)

    def __fill_layouts(self):
        liststore = self.set_liststore_for_combobox(
            self.__layout_combobox)
        for layout in ["2x1", "2x2", "2x4", "4x4"]:
            liststore.append([layout])
        self.__layout_combobox.set_active(0)

    @staticmethod
    def combobox_select_row(widget, row_value):
        def func(model, path, iter, widget):
            if model.get_value(iter, 0) == row_value:
                widget.set_active_iter(iter)
        widget.get_model().foreach(func, widget)

    def __apply_preferences(self):
        preferences = self.__preferences
        if preferences.infile_name:
            self.__input_file_chooser_button.set_filename(preferences.infile_name)
            self.__apply_button.set_sensitive(True)
        if preferences.conversion_type:
            if  preferences.conversion_type == backend.ConversionType.BOOKLETIZE:
                self.__bookeltize_radiobutton.set_active(True)
            if  preferences.conversion_type == backend.ConversionType.LINEARIZE:
                self.__linearize_radiobutton.set_active(True)
            if  preferences.conversion_type == backend.ConversionType.REDUCE:
                self.__reduce_radiobutton.set_active(True)
        if preferences.copy_pages:
            self.__copy_pages_radiobutton.set_active(preferences.copy_pages)
        if preferences.layout:
            self.combobox_select_row(self.__layout_combobox, preferences.layout)
        if preferences.paper_format:
            self.combobox_select_row(self.__paper_format_combobox,
                                     preferences.paper_format)
        if preferences.outfile_name:
            self.__output_file_chooser_button.set_filename(preferences.outfile_name)

    # CALLBACKS

    def cb_close_main_window(self, widget, event, data=None):
        self.close_application()

    def cb_dialog_close(self, dialog, data=None):
        dialog.hide()

    def cb_infile_set(self, widget, data=None):
        self.__preferences.infile_name = widget.get_filename()
        self.__apply_preferences()

    def cb_bookletize_toggled(self, widget, data=None):
        if widget.get_active():
            self.__preferences.conversion_type = backend.ConversionType.BOOKLETIZE

    def cb_linearize_toggled(self, widget, data=None):
        if widget.get_active():
            self.__preferences.conversion_type = backend.ConversionType.LINEARIZE

    def cb_reduce_toggled(self, widget, data=None):
        if widget.get_active():
            self.__preferences.conversion_type = backend.ConversionType.REDUCE

    def cb_copy_pages_toggled(self, widget, data=None):
        self.__preferences.copy_pages = widget.get_active()

    def cb_layout_changed(self, widget, data=None):
        self.__preferences.layout = widget.get_model().get_value(
            widget.get_active_iter(), 0)

    def cb_paper_format_changed(self, widget, data=None):
        self.__preferences.paper_format = widget.get_model().get_value(
            widget.get_active_iter(), 0)

    def cb_outfile_clicked(self, widget, data=None):
        fcdialog = Gtk.FileChooserDialog(
            title=_("Choose file to save"),
            parent=self.__main_window,
            action=Gtk.FileChooserAction.SAVE,
            buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                     Gtk.STOCK_OK, Gtk.ResponseType.OK))
        if widget.get_filename():
            fcdialog.set_filename(widget.get_filename())
        if fcdialog.run() == Gtk.ResponseType.OK:
            if fcdialog.get_filename():
                widget.set_filename(fcdialog.get_filename())
                self.__preferences.outfile_name = fcdialog.get_filename()
        fcdialog.destroy()

    def cb_about_button(self, widget, data=None):
        self.show_about_dialog()

    def cb_close_button(self, widget, data=None):
        self.close_application()
    
    def cb_help_button(self, widget, data=None):
        uri = "help:bookletimposer"
        try:
            Gtk.show_uri(screen = None, uri = uri,
                timestamp = Gtk.get_current_event_time())
        except Gio.Error, error:
            dialog = Gtk.MessageDialog(parent=self.__main_window,
                                       flags=Gtk.DialogFlags.MODAL,
                                       type=Gtk.MessageType.ERROR,
                                       buttons=Gtk.ButtonsType.CLOSE,
                                       message_format=_("Unable to display help: %s")
                                                      % str(error))
            dialog.connect("response", lambda widget, data=None: widget.destroy())
            dialog.run()

    def cb_apply_button(self, widget, data=None):
        self.run_conversion()
        pass

    def cb_progress_stop(self, widget, data=None):
        self.__stop.set()
        self.__progressbar_conversion.set_text(_("Cancel triggered, please wait..."))

    # ACTIONS
    
    def close_application(self):
        Gtk.main_quit()

    def show_about_dialog(self):
        self.__about_dialog.show()

    def run_conversion(self):

        def exception_dialog(exception):
            dialog = Gtk.MessageDialog(parent=self.__main_window,
                                       flags=Gtk.DialogFlags.MODAL,
                                       type=Gtk.MessageType.ERROR,
                                       buttons=Gtk.ButtonsType.CLOSE,
                                       message_format=_("Conversion failed"))
            dialog.format_secondary_text(str(exception))
            dialog.run()
            dialog.destroy()
            stop_conversion_mode()

        def start_conversion_mode():
            self.__preferences_table.set_sensitive(False)
            self.__progressbar_conversion.set_visible(True)
            self.__progressbar_conversion.set_fraction(0)
            self.__progressbar_conversion.set_text("")
            self.__stop_button.set_visible(True)
            self.__stop_button.show()
            self.__apply_button.set_visible(False)

        def stop_conversion_mode():
            self.__preferences_table.set_sensitive(True)
            self.__progressbar_conversion.set_visible(False)
            self.__apply_button.set_visible(True)
            self.__stop_button.set_visible(False)

        def cb_overwrite_outfile(filename):
            dialog = Gtk.MessageDialog(parent=self.__main_window,
                                       flags=Gtk.DialogFlags.MODAL,
                                       type=Gtk.MessageType.QUESTION,
                                       buttons=Gtk.ButtonsType.YES_NO,
                                       message_format=_("A file named %s already exist.") % filename)
            dialog.format_secondary_text(_("Do you want to replace it?"))
            resp = dialog.run()
            dialog.destroy()
            if resp == Gtk.ResponseType.YES:
                return True
            else:
                return False

        def cb_update_progress(message, progress):
            # there we are inside the work of the converter,
            # so we can to stop it if the user required to cancel
            # the operation. To achieve that we raise an exception.
            # XXX: that's not elegant at all
            if self.__stop.is_set():
                self.__progressbar_conversion.set_text(_("Conversion cancelled"))
                raise UserInterrupt()
            GObject.idle_add(idle_cb_update_progress, message, progress)

        def idle_cb_update_progress(message, progress):
            self.__progressbar_conversion.set_fraction(progress)
            self.__progressbar_conversion.set_text(message)
            return False

        def idle_cb_process_exception(exception):
            exception_dialog(exception)
            return False

        def idle_cb_finish_callback():
            stop_conversion_mode()
            return False

        def worker():
            try:
                converter.run()
            except UserInterrupt:
                GObject.idle_add(idle_cb_finish_callback)
            except Exception, e:
                GObject.idle_add(idle_cb_process_exception, e)
                print traceback.format_exc()
                raise
            GObject.idle_add(idle_cb_finish_callback)


        start_conversion_mode()
        try:
            converter = self.__preferences.create_converter(cb_overwrite_outfile)
        except pdfimposer.UserInterruptError:
            stop_conversion_mode()
            return
        except Exception, e:
            exception_dialog(e)
            raise
        converter.set_progress_callback(cb_update_progress)
        self.__stop = threading.Event()
        converter_thread = threading.Thread(target=worker)
        converter_thread.start()

if __name__ == "__main__":
    ui = BookletImposerUI()
    Gtk.main()