[gnome-sudoku] Add a PrintDialog widget



commit 93fb86b046e3ce58e98e42e2ec0383b31007426d
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Sep 28 20:27:36 2014 -0500

    Add a PrintDialog widget

 data/Makefile.am                         |    2 +-
 data/{print-games.ui => print-dialog.ui} |   31 ++++----
 src/Makefile.am                          |    3 +
 src/gnome-sudoku.gresource.xml.in        |    2 +-
 src/gnome-sudoku.vala                    |    7 +-
 src/print-dialog.vala                    |  130 ++++++++++++++++++++++++++++++
 src/sudoku-printer.vala                  |  121 ---------------------------
 7 files changed, 154 insertions(+), 142 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index cb178a7..19ab780 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -19,7 +19,7 @@ pixmap_DATA = footprints.png
 dist_noinst_DATA = \
        gnome-sudoku.ui \
        gnome-sudoku-menu.ui \
-       print-games.ui
+       print-dialog.ui
 
 man_MANS = gnome-sudoku.6
 
diff --git a/data/print-games.ui b/data/print-dialog.ui
similarity index 92%
rename from data/print-games.ui
rename to data/print-dialog.ui
index aec4626..a6fd14f 100644
--- a/data/print-games.ui
+++ b/data/print-dialog.ui
@@ -9,22 +9,21 @@
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
-  <object class="GtkDialog" id="dialog">
+  <template class="PrintDialog" parent="GtkDialog">
     <property name="can_focus">False</property>
-    <property name="use-header-bar">1</property>
     <property name="type_hint">dialog</property>
     <property name="height_request">300</property>
     <property name="width_request">450</property>
     <property name="expand">False</property>
     <property name="modal">True</property>
     <child internal-child="headerbar">
-      <object class="GtkHeaderBar" id="the_headerbar">
+      <object class="GtkHeaderBar">
         <property name="title" translatable="yes">Print Multiple Puzzles</property>
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="show-close-button">False</property>
         <child>
-          <object class="GtkButton" id="okbutton1">
+          <object class="GtkButton" id="print_button">
             <property name="label" translatable="yes">_Print</property>
             <property name="visible">True</property>
             <property name="can_focus">True</property>
@@ -93,7 +92,7 @@
                         <property name="can_focus">False</property>
                         <property name="label" translatable="yes">_Number of puzzles</property>
                         <property name="use_underline">True</property>
-                        <property name="mnemonic_widget">sudokusToPrintSpinButton</property>
+                        <property name="mnemonic_widget">n_sudokus_button</property>
                         <property name="valign">center</property>
                         <property name="halign">end</property>
                       </object>
@@ -105,7 +104,7 @@
                   </object>
                 </child>
                 <child>
-                  <object class="GtkSpinButton" id="sudokusToPrintSpinButton">
+                  <object class="GtkSpinButton" id="n_sudokus_button">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="halign">center</property>
@@ -153,7 +152,7 @@
                     <property name="valign">start</property>
                     <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | 
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                     <child>
-                      <object class="GtkRadioButton" id="easyRadioButton">
+                      <object class="GtkRadioButton" id="easy_radio_button">
                         <property name="label" translatable="yes">_Easy</property>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
@@ -169,7 +168,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkRadioButton" id="mediumRadioButton">
+                      <object class="GtkRadioButton" id="medium_radio_button">
                         <property name="label" translatable="yes">_Medium</property>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
@@ -177,7 +176,7 @@
                         <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | 
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                         <property name="use_underline">True</property>
                         <property name="draw_indicator">True</property>
-                        <property name="group">easyRadioButton</property>
+                        <property name="group">easy_radio_button</property>
                       </object>
                       <packing>
                         <property name="expand">True</property>
@@ -186,7 +185,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkRadioButton" id="hardRadioButton">
+                      <object class="GtkRadioButton" id="hard_radio_button">
                         <property name="label" translatable="yes">_Hard</property>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
@@ -194,7 +193,7 @@
                         <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | 
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                         <property name="use_underline">True</property>
                         <property name="draw_indicator">True</property>
-                        <property name="group">easyRadioButton</property>
+                        <property name="group">easy_radio_button</property>
                       </object>
                       <packing>
                         <property name="expand">True</property>
@@ -203,7 +202,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkRadioButton" id="very_hardRadioButton">
+                      <object class="GtkRadioButton" id="very_hard_radio_button">
                         <property name="label" translatable="yes">_Very Hard</property>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
@@ -211,7 +210,7 @@
                         <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | 
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                         <property name="use_underline">True</property>
                         <property name="draw_indicator">True</property>
-                        <property name="group">easyRadioButton</property>
+                        <property name="group">easy_radio_button</property>
                       </object>
                       <packing>
                         <property name="expand">True</property>
@@ -233,8 +232,8 @@
       </object>
     </child>
     <action-widgets>
-      <action-widget response="-6">cancelbutton1</action-widget>
-      <action-widget response="-5">okbutton1</action-widget>
+      <action-widget response="cancel">cancelbutton1</action-widget>
+      <action-widget response="ok" default="true">print_button</action-widget>
     </action-widgets>
-  </object>
+  </template>
 </interface>
diff --git a/src/Makefile.am b/src/Makefile.am
index f711496..5fd71a8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,6 +5,7 @@ BUILT_SOURCES = gnome-sudoku-resources.c
 gnome_sudoku_SOURCES = \
        config.vapi \
        gnome-sudoku.vala \
+       print-dialog.vala \
        sudoku-view.vala \
        number-picker.vala \
        sudoku-printer.vala \
@@ -31,6 +32,8 @@ gnome_sudoku_VALAFLAGS = \
        --pkg gio-2.0 \
        --pkg gtk+-3.0 \
        --pkg gee-0.8 \
+       --target-glib=$(GLIB_REQUIRED) \
+       --gresources=$(builddir)/gnome-sudoku.gresource.xml \
        $(top_builddir)/lib/libsudoku.vapi
 
 gnome-sudoku-resources.c: gnome-sudoku.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) 
--generate-dependencies gnome-sudoku.gresource.xml)
diff --git a/src/gnome-sudoku.gresource.xml.in b/src/gnome-sudoku.gresource.xml.in
index b9845c0..e88d36d 100644
--- a/src/gnome-sudoku.gresource.xml.in
+++ b/src/gnome-sudoku.gresource.xml.in
@@ -2,7 +2,7 @@
 <gresources>
   <gresource prefix="/org/gnome/sudoku/ui">
     <file alias="gnome-sudoku.ui" preprocess="xml-stripblanks">@top_srcdir@/data/gnome-sudoku.ui</file>
-    <file alias="print-games.ui" preprocess="xml-stripblanks">@top_srcdir@/data/print-games.ui</file>
+    <file preprocess="xml-stripblanks">@top_srcdir@/data/print-dialog.ui</file>
   </gresource>
   <gresource prefix="/org/gnome/sudoku/gtk">
     <file alias="menus.ui" preprocess="xml-stripblanks">@top_srcdir@/data/gnome-sudoku-menu.ui</file>
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index 94a3cad..d8907b0 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -478,12 +478,13 @@ public class Sudoku : Gtk.Application
     {
         print_action.set_enabled (false);
         print_multiple_action.set_enabled (false);
-        var printer = new GamePrinter (saver, ref window);
-        printer.run_dialog ();
-        printer.game_printer_closed.connect ((p) => {
+        var print_dialog = new PrintDialog (saver, window);
+        print_dialog.finished.connect (() => {
+            print_dialog.destroy ();
             this.print_action.set_enabled (main_stack.get_visible_child_name () == "frame");
             this.print_multiple_action.set_enabled (true);
         });
+        print_dialog.run ();
     }
 
     private void help_cb ()
diff --git a/src/print-dialog.vala b/src/print-dialog.vala
new file mode 100644
index 0000000..a6e8099
--- /dev/null
+++ b/src/print-dialog.vala
@@ -0,0 +1,130 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Copyright © 2014 Parin Porecha
+ * Copyright © 2014 Michael Catanzaro
+ *
+ * This file is part of GNOME Sudoku.
+ *
+ * GNOME Sudoku 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 Sudoku 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 Sudoku. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+[GtkTemplate (ui = "/org/gnome/sudoku/ui/../data/print-dialog.ui")]
+public class PrintDialog : Gtk.Dialog
+{
+    private SudokuSaver saver;
+    private Settings settings;
+
+    [GtkChild]
+    private Gtk.SpinButton n_sudokus_button;
+    [GtkChild]
+    private Gtk.RadioButton easy_radio_button;
+    [GtkChild]
+    private Gtk.RadioButton medium_radio_button;
+    [GtkChild]
+    private Gtk.RadioButton hard_radio_button;
+    [GtkChild]
+    private Gtk.RadioButton very_hard_radio_button;
+    [GtkChild]
+    private Gtk.Spinner spinner;
+
+    private const string DIFFICULTY_KEY_NAME = "print-multiple-sudoku-difficulty";
+
+    /* After emitting our response, we continue to asynchronously generate puzzles
+       in the background. This signal indicates when we are really finished. */
+    public signal void finished ();
+
+    public PrintDialog (SudokuSaver saver, Gtk.Window window)
+    {
+        Object(use_header_bar: 1);
+
+        this.saver = saver;
+        settings = new GLib.Settings ("org.gnome.sudoku");
+
+        set_transient_for (window);
+
+        var saved_difficulty = (DifficultyCategory) settings.get_enum (DIFFICULTY_KEY_NAME);
+        if (saved_difficulty == DifficultyCategory.EASY)
+            easy_radio_button.set_active (true);
+        else if (saved_difficulty == DifficultyCategory.MEDIUM)
+            medium_radio_button.set_active (true);
+        else if (saved_difficulty == DifficultyCategory.HARD)
+            hard_radio_button.set_active (true);
+        else if (saved_difficulty == DifficultyCategory.VERY_HARD)
+            very_hard_radio_button.set_active (true);
+        else
+            assert_not_reached ();
+
+        wrap_adjustment ("print-multiple-sudokus-to-print", n_sudokus_button.get_adjustment ());
+    }
+
+    private void wrap_adjustment (string key_name, Gtk.Adjustment action)
+    {
+        action.set_value (settings.get_int (key_name));
+        action.value_changed.connect (() => settings.set_int (key_name, (int) action.get_value ()));
+    }
+
+    public override void response (int response)
+    {
+        if (response != Gtk.ResponseType.OK)
+        {
+            finished ();
+            return;
+        }
+
+        var nsudokus = (int) n_sudokus_button.get_adjustment ().get_value ();
+        DifficultyCategory level;
+
+        if (easy_radio_button.get_active ())
+            level = DifficultyCategory.EASY;
+        else if (medium_radio_button.get_active ())
+            level = DifficultyCategory.MEDIUM;
+        else if (hard_radio_button.get_active ())
+            level = DifficultyCategory.HARD;
+        else if (very_hard_radio_button.get_active ())
+            level = DifficultyCategory.VERY_HARD;
+        else
+            assert_not_reached ();
+
+        settings.set_enum (DIFFICULTY_KEY_NAME, level);
+
+        spinner.show ();
+        spinner.start ();
+
+        sensitive = false;
+
+        SudokuGenerator.generate_boards_async.begin (nsudokus, level, (obj, res) => {
+            try
+            {
+                var boards = SudokuGenerator.generate_boards_async.end (res);
+
+                spinner.stop ();
+                spinner.hide ();
+                sensitive = true;
+
+                var printer = new SudokuPrinter (boards, this);
+                if (printer.print_sudoku () == Gtk.PrintOperationResult.APPLY)
+                {
+                    foreach (SudokuBoard board in boards)
+                        saver.add_game_to_finished (new SudokuGame (board));
+                }
+            }
+            catch (ThreadError e)
+            {
+                error ("Thread error: %s\n", e.message);
+            }
+
+            finished ();
+        });
+    }
+}
diff --git a/src/sudoku-printer.vala b/src/sudoku-printer.vala
index 64b720f..d8aa4b6 100644
--- a/src/sudoku-printer.vala
+++ b/src/sudoku-printer.vala
@@ -236,124 +236,3 @@ public class SudokuPrinter : GLib.Object {
         }
     }
 }
-
-public class GamePrinter: GLib.Object
-{
-    private SudokuSaver saver;
-    private ApplicationWindow window;
-    private GLib.Settings settings;
-    private Gtk.Dialog dialog;
-    private SpinButton nsudokus_button;
-
-    private RadioButton easy_button;
-    private RadioButton medium_button;
-    private RadioButton hard_button;
-    private RadioButton very_hard_button;
-
-    private Spinner spinner;
-
-    public signal void game_printer_closed ();
-
-    private const string DIFFICULTY_KEY_NAME = "print-multiple-sudoku-difficulty";
-
-    public GamePrinter (SudokuSaver saver, ref ApplicationWindow window)
-    {
-        this.saver = saver;
-        this.window = window;
-        this.settings = new GLib.Settings ("org.gnome.sudoku");
-
-        Gtk.Builder builder = new Builder.from_resource ("/org/gnome/sudoku/ui/print-games.ui");
-        builder.connect_signals (null);
-        this.dialog = builder.get_object ("dialog") as Dialog;
-        dialog.set_transient_for (window);
-        dialog.set_default_response (Gtk.ResponseType.OK);
-        dialog.response.connect (response_cb);
-
-        easy_button = builder.get_object ("easyRadioButton") as RadioButton;
-        medium_button = builder.get_object ("mediumRadioButton") as RadioButton;
-        hard_button = builder.get_object ("hardRadioButton") as RadioButton;
-        very_hard_button = builder.get_object ("very_hardRadioButton") as RadioButton;
-
-        var saved_difficulty = (DifficultyCategory) settings.get_enum (DIFFICULTY_KEY_NAME);
-
-        if (saved_difficulty == DifficultyCategory.EASY)
-            easy_button.set_active (true);
-        else if (saved_difficulty == DifficultyCategory.MEDIUM)
-            medium_button.set_active (true);
-        else if (saved_difficulty == DifficultyCategory.HARD)
-            hard_button.set_active (true);
-        else if (saved_difficulty == DifficultyCategory.VERY_HARD)
-            very_hard_button.set_active (true);
-
-        nsudokus_button = builder.get_object ("sudokusToPrintSpinButton") as SpinButton;
-        wrap_adjustment ("print-multiple-sudokus-to-print", nsudokus_button.get_adjustment ());
-
-        spinner = builder.get_object ("spinner") as Spinner;
-    }
-
-    private void wrap_adjustment (string key_name, Adjustment action)
-    {
-        action.set_value (settings.get_int (key_name));
-        action.value_changed.connect (() => settings.set_int (key_name, (int) action.get_value ()));
-    }
-
-    private void response_cb (Dialog dialog, int response)
-    {
-        if (response != Gtk.ResponseType.ACCEPT && response != Gtk.ResponseType.OK)
-        {
-            dialog.hide ();
-            game_printer_closed ();
-            return;
-        }
-
-        var nsudokus = (int) nsudokus_button.get_adjustment ().get_value ();
-        DifficultyCategory level;
-
-        if (easy_button.get_active ())
-            level = DifficultyCategory.EASY;
-        else if (medium_button.get_active ())
-            level = DifficultyCategory.MEDIUM;
-        else if (hard_button.get_active ())
-            level = DifficultyCategory.HARD;
-        else if (very_hard_button.get_active ())
-            level = DifficultyCategory.VERY_HARD;
-        else
-            assert_not_reached ();
-
-        settings.set_enum (DIFFICULTY_KEY_NAME, level);
-
-        spinner.visible = true;
-        spinner.active = true;
-        spinner.show ();
-        spinner.start ();
-        dialog.sensitive = false;
-
-        SudokuGenerator.generate_boards_async.begin(nsudokus, level, (obj, res) => {
-            try {
-                var boards = SudokuGenerator.generate_boards_async.end(res);
-
-                spinner.stop ();
-                spinner.hide ();
-                dialog.sensitive = true;
-
-                SudokuPrinter printer = new SudokuPrinter (boards, (Gtk.Window) dialog);
-                PrintOperationResult result = printer.print_sudoku ();
-                if (result == PrintOperationResult.APPLY)
-                {
-                    dialog.hide ();
-                    game_printer_closed ();
-                    foreach (SudokuBoard board in boards)
-                        saver.add_game_to_finished (new SudokuGame (board));
-                }
-            } catch (ThreadError e) {
-                error ("Thread error: %s\n", e.message);
-            }
-        });
-    }
-
-    public void run_dialog ()
-    {
-        dialog.show ();
-    }
-
-}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]