summaryrefslogtreecommitdiff
path: root/src/printOperation.js
blob: 6606cef933902a1fe61204749e53056035fca40b (plain)
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
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * GNOME Maps 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 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps 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 GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Amisha Singla <amishas157@gmail.com>
 */

import Adw from 'gi://Adw';
import GLib from 'gi://GLib';
import Gtk from 'gi://Gtk';

import {Application} from './application.js';
import {LongPrintLayout} from './longPrintLayout.js';
import {ShortPrintLayout} from './shortPrintLayout.js';
import {TransitPrintLayout} from './transitPrintLayout.js';
import * as Utils from './utils.js';

const _MIN_TIME_TO_ABORT = 3000;

/* Following constant has unit as meters */
const _SHORT_LAYOUT_MAX_DISTANCE = 3000;

export class PrintOperation {

    constructor({mainWindow}) {
        this._mainWindow = mainWindow;

        this._operation = new Gtk.PrintOperation({ embed_page_setup: true });
        this._operation.connect('begin-print', this._beginPrint.bind(this));
        this._operation.connect('paginate', this._paginate.bind(this));
        this._operation.connect('draw-page', this._drawPage.bind(this));

        this._abortDialog = new Adw.MessageDialog({
            transient_for: this._mainWindow,
            destroy_with_parent: true,
            modal: true,
            heading: _("Loading map tiles for printing"),
            body: _("You can abort printing if this takes too long")
        });
        this._abortDialog.add_response('abort', _("Abort printing"));
        this._abortDialog.set_response_appearance('abort',
                                                  Adw.ResponseAppearance.DESTRUCTIVE);
        this._responseId = this._abortDialog.connect('response',
                                                     this.onAbortDialogResponse.bind(this));

        this._runPrintOperation();
    }

    _beginPrint(operation, context, data) {
        let route = Application.routingDelegator.graphHopper.route;
        let selectedTransitItinerary =
            Application.routingDelegator.transitRouter.plan.selectedItinerary;
        let width = context.get_width();
        let height = context.get_height();

        if (selectedTransitItinerary) {
            this._layout =
                new TransitPrintLayout({ itinerary: selectedTransitItinerary,
                                         pageWidth: width,
                                         pageHeight: height,
                                         mainWindow: this._mainWindow });
        } else {
            // TODO: for now just use short layout, as we don't have minimaps
            this._layout = new ShortPrintLayout({ route: route,
                                                  pageWidth: width,
                                                  pageHeight: height,
                                                  mainWindow: this._mainWindow });
            /*
            if (route.distance > _SHORT_LAYOUT_MAX_DISTANCE) {
                this._layout = new LongPrintLayout({ route: route,
                                                     pageWidth: width,
                                                     pageHeight: height,
                                                     mainWindow: this._mainWindow });
            } else {
                this._layout = new ShortPrintLayout({ route: route,
                                                      pageWidth: width,
                                                      pageHeight: height,
                                                      mainWindow: this._mainWindow });
            }
            */
        }

        GLib.timeout_add(null, _MIN_TIME_TO_ABORT, () => {
            if (!this._layout.renderFinished) {
                this._abortDialog.show();
            }
            return false;
        });

        this._layout.render();
    }

    onAbortDialogResponse(dialog, response) {
        if (response === Gtk.ResponseType.DELETE_EVENT ||
            response === Gtk.ResponseType.CANCEL) {
            this._abortDialog.disconnect(this._responseId);
            this._operation.cancel();
            this._abortDialog.close();
        }
    }

    _paginate(operation, context) {
        if (this._layout) {
            if (this._layout.renderFinished) {
                operation.set_n_pages(this._layout.numPages);
                this._abortDialog.close();
            }
            return this._layout.renderFinished;
        } else {
            return false;
        }
    }

    _drawPage(operation, context, page_num, data) {
        let cr = context.get_cairo_context();
        this._layout.surfaceObjects[page_num].forEach((so) => {
            cr.setSourceSurface(so.surface, so.x, so.y);
            cr.paint();
        });
    }

    _runPrintOperation() {
        try {
            let result = this._operation.run(Gtk.PrintOperationAction.PRINT_DIALOG,
                                             this._mainWindow);
            if (result === Gtk.PrintOperationResult.ERROR) {
                let error = this._operation.get_error();
                Utils.debug('Failed to print: %s'.format(error));
            }
        } catch(e) {
            Utils.debug('Failed to print: %s'.format(e.message));
        }
    }
}