diff options
Diffstat (limited to 'src/ui.vala')
-rw-r--r-- | src/ui.vala | 206 |
1 files changed, 180 insertions, 26 deletions
diff --git a/src/ui.vala b/src/ui.vala index db19d77..41b33b1 100644 --- a/src/ui.vala +++ b/src/ui.vala @@ -9,7 +9,7 @@ * license. */ -public class SimpleScan +public class UserInterface { private const int DEFAULT_TEXT_DPI = 150; private const int DEFAULT_PHOTO_DPI = 300; @@ -60,6 +60,8 @@ public class SimpleScan private bool user_selected_device; private Gtk.FileChooserDialog? save_dialog; + private ProgressBarDialog progress_dialog; + private DragAndDropHandler dnd_handler = null; private bool have_error; private string error_title; @@ -88,9 +90,8 @@ public class SimpleScan public signal void start_scan (string? device, ScanOptions options); public signal void stop_scan (); public signal void email (string profile); - public signal void quit (); - public SimpleScan () + public UserInterface () { book = new Book (); book.page_removed.connect (page_removed_cb); @@ -455,12 +456,14 @@ public class SimpleScan else if (uri_lower.has_suffix (".tif") || uri_lower.has_suffix (".tiff")) format = "tiff"; + show_progress_dialog (); try { book.save (format, file); } catch (Error e) { + hide_progress_dialog (); warning ("Error saving file: %s", e.message); show_error (/* Title of error dialog when save failed */ _("Failed to save file"), @@ -771,28 +774,6 @@ public class SimpleScan updating_page_menu = false; } - // FIXME: Duplicated from simple-scan.vala - private string? get_temporary_filename (string prefix, string extension) - { - /* NOTE: I'm not sure if this is a 100% safe strategy to use g_file_open_tmp(), close and - * use the filename but it appears to work in practise */ - - var filename = "%sXXXXXX.%s".printf (prefix, extension); - string path; - try - { - var fd = FileUtils.open_tmp (filename, out path); - Posix.close (fd); - } - catch (Error e) - { - warning ("Error saving email attachment: %s", e.message); - return null; - } - - return path; - } - private void show_page_cb (BookView view, Page page) { var path = get_temporary_filename ("scanned-page", "tiff"); @@ -1118,7 +1099,7 @@ public class SimpleScan settings.set_int ("page-height", default_page_height); settings.set_int ("page-dpi", default_page_dpi); - quit (); + window.destroy (); return true; } @@ -1269,6 +1250,8 @@ public class SimpleScan builder.connect_signals (this); window = (Gtk.Window) builder.get_object ("simple_scan_window"); + var app = Application.get_default () as Gtk.Application; + app.add_window (window); main_vbox = (Gtk.VBox) builder.get_object ("main_vbox"); page_move_left_menuitem = (Gtk.MenuItem) builder.get_object ("page_move_left_menuitem"); page_move_right_menuitem = (Gtk.MenuItem) builder.get_object ("page_move_right_menuitem"); @@ -1418,6 +1401,41 @@ public class SimpleScan add_default_page (); book.set_needs_saving (false); book.needs_saving_changed.connect (needs_saving_cb); + + progress_dialog = new ProgressBarDialog (window, _("Saving document...")); + book.saving.connect (book_saving_cb); + + dnd_handler = new DragAndDropHandler (book_view); + } + + private void book_saving_cb (int page_number) + { + /* Prevent GUI from freezing */ + while (Gtk.events_pending ()) + Gtk.main_iteration (); + + var total = (int) book.get_n_pages (); + var fraction = (page_number + 1.0) / total; + var complete = fraction == 1.0; + if (complete) + Timeout.add(500, () => { + progress_dialog.hide(); + return false; + }); + var message = _("Saving page %d out of %d").printf (page_number + 1, total); + + progress_dialog.set_fraction (fraction); + progress_dialog.set_message (message); + } + + public void show_progress_dialog () + { + progress_dialog.show (); + } + + public void hide_progress_dialog () + { + progress_dialog.hide (); } public Book get_book () @@ -1457,3 +1475,139 @@ public class SimpleScan window.show (); } } + +class ProgressBarDialog : Gtk.Window +{ + Gtk.ProgressBar bar; + + public ProgressBarDialog (Gtk.Window parent, string title) + { + bar = new Gtk.ProgressBar (); + var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 5); + var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 5); + hbox.set_hexpand (true); + + bar.set_text (""); + bar.set_show_text (true); + bar.set_size_request (225, 25); + set_size_request (250, 50); + + vbox.pack_start (bar, true, false, 0); + hbox.pack_start (vbox, true, false, 0); + add (hbox); + set_title (title); + + set_transient_for (parent); + set_position (Gtk.WindowPosition.CENTER_ON_PARENT); + set_modal (true); + set_resizable (false); + + hbox.show (); + vbox.show (); + bar.show (); + } + + public void set_fraction (double percent) + { + bar.set_fraction (percent); + } + + public void set_message (string message) + { + bar.set_text (message); + } +} + +class DragAndDropHandler +{ + private enum TargetType + { + IMAGE, + URI + } + + private BookView book_view; + + public DragAndDropHandler (BookView book_view) + { + this.book_view = book_view; + var event_source = book_view.get_event_source (); + + set_targets (event_source); + event_source.drag_data_get.connect (on_drag_data_get); + } + + private void set_targets (Gtk.Widget event_source) + { + var table = new Gtk.TargetEntry [0]; + var targets = new Gtk.TargetList (table); + targets.add_uri_targets (TargetType.URI); + targets.add_image_targets (TargetType.IMAGE, true); + + Gtk.drag_source_set (event_source, Gdk.ModifierType.BUTTON1_MASK, table, Gdk.DragAction.COPY); + Gtk.drag_source_set_target_list (event_source, targets); + } + + private void on_drag_data_get (Gdk.DragContext context, Gtk.SelectionData selection, uint target_type, uint time) + { + var page = book_view.get_selected (); + return_if_fail (page != null); + + switch (target_type) + { + case TargetType.IMAGE: + var image = page.get_image (true); + selection.set_pixbuf (image); + + debug ("Saving page to pixbuf"); + break; + + case TargetType.URI: + var filetype = "png"; + var path = get_temporary_filename ("scanned-page", filetype); + return_if_fail (path != null); + + var file = File.new_for_path (path); + var uri = file.get_uri (); + + try + { + page.save (filetype, file); + selection.set_uris ({ uri }); + debug ("Saving page to %s", uri); + } + catch (Error e) + { + warning ("Unable to save file using drag-drop %s", e.message); + } + break; + + default: + warning ("Invalid DND target type %u", target_type); + break; + } + } +} + +// FIXME: Duplicated from simple-scan.vala +private string? get_temporary_filename (string prefix, string extension) +{ + /* NOTE: I'm not sure if this is a 100% safe strategy to use g_file_open_tmp(), close and + * use the filename but it appears to work in practise */ + + var filename = "%sXXXXXX.%s".printf (prefix, extension); + string path; + try + { + var fd = FileUtils.open_tmp (filename, out path); + Posix.close (fd); + } + catch (Error e) + { + warning ("Error saving email attachment: %s", e.message); + return null; + } + + return path; +} + |