eagleoneraptor

gnome-maps_PrintOperation_Abort

Feb 13th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
  2. /* vim: set et ts=4 sw=4: */
  3. /*
  4.  * GNOME Maps is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by the
  6.  * Free Software Foundation; either version 2 of the License, or (at your
  7.  * option) any later version.
  8.  *
  9.  * GNOME Maps is distributed in the hope that it will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11.  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12.  * for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
  16.  *
  17.  * Author: Amisha Singla <amishas157@gmail.com>
  18.  */
  19.  
  20. const Gtk = imports.gi.Gtk;
  21. const Lang = imports.lang;
  22. const Mainloop = imports.mainloop;
  23.  
  24. const Application = imports.application;
  25. const LongPrintLayout = imports.longPrintLayout;
  26. const PrintLayout = imports.printLayout;
  27. const ShortPrintLayout = imports.shortPrintLayout;
  28. const Utils = imports.utils;
  29.  
  30. const _MIN_TIME_TO_ABORT = 3000;
  31.  
  32. const PrintOperation = new Lang.Class({
  33.     Name: 'PrintOperation',
  34.  
  35.     _init: function(params) {
  36.         PrintLayout.SUPPORTED_TYPES.push(LongPrintLayout.LongPrintLayout);
  37.         PrintLayout.SUPPORTED_TYPES.push(ShortPrintLayout.ShortPrintLayout);
  38.         this._mainWindow = params.mainWindow;
  39.         delete params.mainWindow;
  40.  
  41.         this._operation = new Gtk.PrintOperation({ embed_page_setup: true });
  42.         this._operation.connect('begin-print', this._beginPrint.bind(this));
  43.         this._operation.connect('paginate', this._paginate.bind(this));
  44.         this._operation.connect('draw-page', this._drawPage.bind(this));
  45.  
  46.         this._abortDialog = new Gtk.MessageDialog({
  47.             transient_for: this._mainWindow,
  48.             destroy_with_parent: true,
  49.             message_type: Gtk.MessageType.OTHER,
  50.             modal: true,
  51.             text: _("Loading map tiles for printing"),
  52.             secondary_text: _("You can abort printing if this takes too long")
  53.         });
  54.         this._abortDialog.add_button(_("Abort printing"),
  55.                                      Gtk.ResponseType.CANCEL);
  56.         this._responseId = this._abortDialog.connect('response',
  57.                                                      this.onAbortDialogResponse.bind(this));
  58.  
  59.         this._runPrintOperation();
  60.     },
  61.  
  62.     _beginPrint: function(operation, context, data) {
  63.         let route = Application.routeService.route;
  64.         let width = context.get_page_setup().get_page_width(Gtk.Unit.POINTS);
  65.         let height = context.get_page_setup().get_page_height(Gtk.Unit.POINTS);
  66.         Mainloop.timeout_add(_MIN_TIME_TO_ABORT, (function() {
  67.             this._abortDialog.show();
  68.             return false;
  69.         }).bind(this), null);
  70.         this._layout = PrintLayout.newFromRoute(route, width, height);
  71.         this._layout.render();
  72.     },
  73.  
  74.     onAbortDialogResponse: function(dialog, response) {
  75.         if (this._layout.renderFinished === false) {
  76.             if (response === Gtk.ResponseType.DELETE_EVENT ||
  77.                 response === Gtk.ResponseType.CANCEL) {
  78.                 this._abortDialog.disconnect(this._responseId);
  79.                 this._operation.cancel();
  80.                 this._abortDialog.close();
  81.             }
  82.         }
  83.     },
  84.  
  85.     _paginate: function(operation, context) {
  86.         if (this._layout.renderFinished === true) {
  87.             operation.set_n_pages(this._layout.numPages);
  88.             this._abortDialog.close();
  89.         }
  90.  
  91.         return this._layout.renderFinished;
  92.     },
  93.  
  94.     _drawPage: function(operation, context, page_num, data) {
  95.         let cr = context.get_cairo_context();
  96.         this._layout.surfaceObjects[page_num].forEach((function(so) {
  97.             cr.setSourceSurface(so.surface, so.x, so.y);
  98.             cr.paint();
  99.         }).bind(this));
  100.     },
  101.  
  102.     _runPrintOperation: function() {
  103.         let result = this._operation.run(Gtk.PrintOperationAction.PRINT_DIALOG,
  104.                                          this._mainWindow, null);
  105.  
  106.         if (result === Gtk.PrintOperationResult.ERROR) {
  107.             let error = this._operation.get_error();
  108.             Utils.debug('Failed to print: %s'.format(error));
  109.         }
  110.     }
  111. });
Add Comment
Please, Sign In to add comment