[gnome-sudoku] Simplify Print Multiple Puzzles dialog
- From: Parin Porecha <parinporecha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-sudoku] Simplify Print Multiple Puzzles dialog
- Date: Mon, 9 Jun 2014 09:51:44 +0000 (UTC)
commit 9c17bef5264072fab275333c3425b3032f481312
Author: Parin Porecha <parinporecha gmail com>
Date: Mon Jun 9 20:49:04 2014 +0530
Simplify Print Multiple Puzzles dialog
https://bugzilla.gnome.org/show_bug.cgi?id=731213
data/org.gnome.sudoku.gschema.xml | 21 ++--
data/print-games.ui | 248 ++++++++----------------------------
src/sudoku-printer.vala | 116 +++++++++---------
src/sudoku-store.vala | 20 ++--
4 files changed, 133 insertions(+), 272 deletions(-)
---
diff --git a/data/org.gnome.sudoku.gschema.xml b/data/org.gnome.sudoku.gschema.xml
index bb05284..bf1d7d3 100644
--- a/data/org.gnome.sudoku.gschema.xml
+++ b/data/org.gnome.sudoku.gschema.xml
@@ -1,4 +1,10 @@
<schemalist>
+ <enum id="org.gnome.sudoku.print-multiple-sudoku-difficulty">
+ <value nick="easy" value="0" />
+ <value nick="medium" value="1" />
+ <value nick="hard" value="2" />
+ <value nick="very_hard" value="3" />
+ </enum>
<schema id="org.gnome.sudoku" path="/org/gnome/sudoku/">
<key name="auto-save-interval" type="i">
<default>60</default>
@@ -33,18 +39,6 @@
<key name="generate-target-very-hard" type="b">
<default>true</default>
</key>
- <key name="print-easy" type="b">
- <default>true</default>
- </key>
- <key name="print-medium" type="b">
- <default>true</default>
- </key>
- <key name="print-hard" type="b">
- <default>true</default>
- </key>
- <key name="print-very-hard" type="b">
- <default>true</default>
- </key>
<key name="minimum-number-of-new-puzzles" type="i">
<default>90</default>
</key>
@@ -61,6 +55,9 @@
<summary>Height of application window in pixels</summary>
<description>Height of application window in pixels</description>
</key>
+ <key name="print-multiple-sudoku-difficulty" enum="org.gnome.sudoku.print-multiple-sudoku-difficulty">
+ <default>"easy"</default>
+ </key>
<key name="print-multiple-sudokus-to-print" type="i">
<default>4</default>
</key>
diff --git a/data/print-games.ui b/data/print-games.ui
index 5f72451..72a2ab1 100644
--- a/data/print-games.ui
+++ b/data/print-games.ui
@@ -9,18 +9,14 @@
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
- <object class="GtkAdjustment" id="adjustment2">
- <property name="lower">1</property>
- <property name="upper">9</property>
- <property name="value">1</property>
- <property name="step_increment">1</property>
- <property name="page_increment">10</property>
- </object>
<object class="GtkDialog" id="dialog">
<property name="visible">True</property>
<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>
<child internal-child="headerbar">
<object class="GtkHeaderBar" id="the_headerbar">
<property name="title" translatable="yes">Print Multiple Puzzles</property>
@@ -69,142 +65,83 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">12</property>
- <property name="spacing">6</property>
+ <property name="spacing">20</property>
+ <property name="margin">7</property>
<child>
- <object class="GtkLabel" id="label1">
+ <object class="GtkBox" id="print_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
- <property name="xalign">0</property>
- <property name="label" translatable="yes">Print Games</property>
- <attributes>
- <attribute name="weight" value="bold"/>
- <attribute name="scale" value="1.2"/>
- </attributes>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">False</property>
- <property name="position">0</property>
- </packing>
- </child>
- <child>
- <object class="GtkAlignment" id="alignment2">
- <property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
- <property name="left_padding">24</property>
+ <property name="spacing">10</property>
<child>
- <object class="GtkTable" id="table1">
+ <object class="GtkBox" id="number_label_box">
<property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="n_rows">2</property>
- <property name="n_columns">2</property>
+ <property name="width_request">150</property>
<child>
- <object class="GtkLabel" id="label3">
+ <object class="GtkLabel" id="number_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
- <property name="xalign">0</property>
- <property name="label" translatable="yes">_Number of puzzles to print: </property>
+ <property name="label" translatable="yes">_Number of puzzles</property>
<property name="use_underline">True</property>
<property name="mnemonic_widget">sudokusToPrintSpinButton</property>
+ <property name="valign">center</property>
+ <property name="halign">end</property>
</object>
<packing>
- <property name="x_options">GTK_FILL</property>
- <property name="y_options"/>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
</packing>
</child>
+ </object>
+ </child>
+ <child>
+ <object class="GtkSpinButton" id="sudokusToPrintSpinButton">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="halign">center</property>
+ <property name="primary_icon_activatable">False</property>
+ <property name="secondary_icon_activatable">False</property>
+ <property name="primary_icon_sensitive">True</property>
+ <property name="secondary_icon_sensitive">True</property>
+ <property name="adjustment">adjustment1</property>
+ <property name="climb_rate">1</property>
+ <property name="valign">center</property>
+ </object>
+ </child>
+ </object>
+ </child>
+ <child>
+ <object class="GtkBox" id="difficulty_box">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="spacing">10</property>
+ <child>
+ <object class="GtkBox" id="difficulty_label_box">
+ <property name="visible">True</property>
+ <property name="width_request">150</property>
<child>
- <object class="GtkLabel" id="label2">
+ <object class="GtkLabel" id="difficulty_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
- <property name="xalign">0</property>
- <property name="label" translatable="yes">_Puzzles per page: </property>
- <property name="use_underline">True</property>
- <property name="mnemonic_widget">sudokusPerPageSpinButton</property>
- </object>
- <packing>
- <property name="top_attach">1</property>
- <property name="bottom_attach">2</property>
- <property name="x_options">GTK_FILL</property>
- <property name="y_options"/>
- </packing>
- </child>
- <child>
- <object class="GtkSpinButton" id="sudokusToPrintSpinButton">
- <property name="visible">True</property>
- <property name="can_focus">True</property>
- <property name="primary_icon_activatable">False</property>
- <property name="secondary_icon_activatable">False</property>
- <property name="primary_icon_sensitive">True</property>
- <property name="secondary_icon_sensitive">True</property>
- <property name="adjustment">adjustment1</property>
- <property name="climb_rate">1</property>
- </object>
- <packing>
- <property name="left_attach">1</property>
- <property name="right_attach">2</property>
- <property name="y_options"/>
- </packing>
- </child>
- <child>
- <object class="GtkSpinButton" id="sudokusPerPageSpinButton">
- <property name="visible">True</property>
- <property name="can_focus">True</property>
- <property name="primary_icon_activatable">False</property>
- <property name="secondary_icon_activatable">False</property>
- <property name="primary_icon_sensitive">True</property>
- <property name="secondary_icon_sensitive">True</property>
- <property name="adjustment">adjustment2</property>
- <property name="climb_rate">1</property>
+ <property name="valign">start</property>
+ <property name="label" translatable="yes">Difficulty</property>
+ <property name="halign">end</property>
</object>
<packing>
- <property name="left_attach">1</property>
- <property name="right_attach">2</property>
- <property name="top_attach">1</property>
- <property name="bottom_attach">2</property>
- <property name="y_options"/>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
</packing>
</child>
</object>
</child>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">True</property>
- <property name="position">1</property>
- </packing>
- </child>
- <child>
- <object class="GtkLabel" id="label4">
- <property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="xalign">0</property>
- <property name="label" translatable="yes">Levels of difficulty to print</property>
- <property name="wrap">True</property>
- <attributes>
- <attribute name="style" value="italic"/>
- <attribute name="weight" value="bold"/>
- </attributes>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">False</property>
- <property name="position">2</property>
- </packing>
- </child>
- <child>
- <object class="GtkAlignment" id="alignment1">
- <property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
- <property name="left_padding">24</property>
<child>
<object class="GtkVBox" id="vbox2">
<property name="visible">True</property>
- <property name="can_focus">False</property>
+ <property name="homogeneous">True</property>
+ <property name="halign">center</property>
+ <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="GtkCheckButton" id="easyCheckButton">
+ <object class="GtkRadioButton" id="easyRadioButton">
<property name="label" translatable="yes">_Easy</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -220,7 +157,7 @@
</packing>
</child>
<child>
- <object class="GtkCheckButton" id="mediumCheckButton">
+ <object class="GtkRadioButton" id="mediumRadioButton">
<property name="label" translatable="yes">_Medium</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -236,7 +173,7 @@
</packing>
</child>
<child>
- <object class="GtkCheckButton" id="hardCheckButton">
+ <object class="GtkRadioButton" id="hardRadioButton">
<property name="label" translatable="yes">_Hard</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -252,7 +189,7 @@
</packing>
</child>
<child>
- <object class="GtkCheckButton" id="very_hardCheckButton">
+ <object class="GtkRadioButton" id="very_hardRadioButton">
<property name="label" translatable="yes">_Very Hard</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -270,81 +207,6 @@
</object>
</child>
</object>
- <packing>
- <property name="expand">True</property>
- <property name="fill">True</property>
- <property name="position">3</property>
- </packing>
- </child>
- <child>
- <object class="GtkLabel" id="label6">
- <property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="xalign">0</property>
- <property name="label" translatable="yes">Details</property>
- <property name="wrap">True</property>
- <attributes>
- <attribute name="style" value="italic"/>
- <attribute name="weight" value="bold"/>
- </attributes>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">False</property>
- <property name="position">4</property>
- </packing>
- </child>
- <child>
- <object class="GtkAlignment" id="alignment3">
- <property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
- <property name="left_padding">24</property>
- <child>
- <object class="GtkVBox" id="vbox3">
- <property name="visible">True</property>
- <property name="can_focus">False</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="GtkCheckButton" id="markAsPlayedToggle">
- <property name="label" translatable="yes">_Mark games as played once you've printed
them.</property>
- <property name="visible">True</property>
- <property name="sensitive">True</property>
- <property name="can_focus">True</property>
- <property name="receives_default">False</property>
- <property name="use_underline">True</property>
- <property name="draw_indicator">True</property>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">False</property>
- <property name="position">0</property>
- </packing>
- </child>
- <child>
- <object class="GtkCheckButton" id="includeOldGamesToggle">
- <property name="label" translatable="yes">_Include games you've already played in
list of games to print</property>
- <property name="visible">True</property>
- <property name="sensitive">True</property>
- <property name="can_focus">True</property>
- <property name="receives_default">False</property>
- <property name="use_underline">True</property>
- <property name="draw_indicator">True</property>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">False</property>
- <property name="position">1</property>
- </packing>
- </child>
- </object>
- </child>
- </object>
- <packing>
- <property name="expand">True</property>
- <property name="fill">True</property>
- <property name="position">5</property>
- </packing>
</child>
</object>
<packing>
diff --git a/src/sudoku-printer.vala b/src/sudoku-printer.vala
index 2962191..b5b27c6 100644
--- a/src/sudoku-printer.vala
+++ b/src/sudoku-printer.vala
@@ -11,7 +11,7 @@ public class SudokuPrinter : GLib.Object {
private int margin;
private int n_sudokus;
- private int sudokus_per_page;
+ private const int SUDOKUS_PER_PAGE = 2;
private PrintOperation print_op;
@@ -33,13 +33,12 @@ public class SudokuPrinter : GLib.Object {
return Gtk.PrintOperationResult.ERROR;
}
- public SudokuPrinter (SudokuBoard[] boards, ref ApplicationWindow window, int sudokus_per_page = 1)
+ public SudokuPrinter (SudokuBoard[] boards, ref ApplicationWindow window)
{
this.boards = boards;
this.window = window;
this.margin = 25;
this.n_sudokus = boards.length;
- this.sudokus_per_page = sudokus_per_page;
this.print_op = new Gtk.PrintOperation ();
print_op.begin_print.connect (begin_print_cb);
@@ -48,8 +47,11 @@ public class SudokuPrinter : GLib.Object {
private void begin_print_cb (Gtk.PrintOperation operation, Gtk.PrintContext context)
{
- int remainder = (n_sudokus % sudokus_per_page) == 0 ? 0 : 1;
- operation.set_n_pages ((n_sudokus / sudokus_per_page) + remainder);
+ int pages = n_sudokus / SUDOKUS_PER_PAGE;
+ while (pages * SUDOKUS_PER_PAGE < n_sudokus)
+ pages += 1;
+
+ operation.set_n_pages (pages);
}
private void draw_page_cb (Gtk.PrintOperation operation, Gtk.PrintContext context, int page_nr)
@@ -62,8 +64,8 @@ public class SudokuPrinter : GLib.Object {
var best_fit = best_values[0];
var best_square_size = best_values[1];
- var start = page_nr * sudokus_per_page;
- var end = (start + sudokus_per_page) > boards.length ? boards.length : (start + sudokus_per_page);
+ var start = page_nr * SUDOKUS_PER_PAGE;
+ var end = int.min ((start + SUDOKUS_PER_PAGE), boards.length);
SudokuBoard[] sudokus_on_page = boards[start : end];
double left = margin;
@@ -104,7 +106,7 @@ public class SudokuPrinter : GLib.Object {
private double[] fit_squares_in_rectangle (double width, double height, int margin)
{
- var n = sudokus_per_page;
+ var n = SUDOKUS_PER_PAGE;
var best_square_size = 0.0;
double[] best_fit = {0,0};
var square_size = 0.0;
@@ -250,9 +252,14 @@ public class GamePrinter: GLib.Object {
private ApplicationWindow window;
private GLib.Settings settings;
private Gtk.Dialog dialog;
- private HashMap<string, CheckButton> options_map;
- private SpinButton sudokusToPrintSpinButton;
- private SpinButton sudokusPerPageSpinButton;
+ private SpinButton nsudokus_button;
+
+ private RadioButton easy_button;
+ private RadioButton medium_button;
+ private RadioButton hard_button;
+ private RadioButton very_hard_button;
+
+ private const string DIFFICULTY_KEY_NAME = "print-multiple-sudoku-difficulty";
public GamePrinter (SudokuStore store, SudokuSaver saver, ref ApplicationWindow window)
{
@@ -260,7 +267,6 @@ public class GamePrinter: GLib.Object {
this.saver = saver;
this.window = window;
this.settings = new GLib.Settings ("org.gnome.sudoku");
- this.options_map = new HashMap<string, CheckButton> ();
Gtk.Builder builder = new Builder ();
try
@@ -277,34 +283,33 @@ public class GamePrinter: GLib.Object {
dialog.set_default_response (Gtk.ResponseType.OK);
dialog.response.connect (response_cb);
- string[,] settings_to_widgets = {
- {"mark-printed-as-played", "markAsPlayedToggle"},
- {"print-already-played-games", "includeOldGamesToggle"},
- {"print-easy", "easyCheckButton"},
- {"print-medium", "mediumCheckButton"},
- {"print-hard", "hardCheckButton"},
- {"print-very-hard", "very_hardCheckButton"}};
+ SList<RadioButton> radio_group = new SList<RadioButton> ();
- for (var i=0; i<6; i++)
- {
- var setting0 = settings_to_widgets[i,0];
- var setting1 = settings_to_widgets[i,1];
- var check_button = builder.get_object (setting1) as CheckButton;
- wrap_toggle (setting0, check_button);
- options_map.set (setting1, check_button);
- }
+ easy_button = builder.get_object ("easyRadioButton") as RadioButton;
+ easy_button.set_group (radio_group);
- sudokusToPrintSpinButton = builder.get_object ("sudokusToPrintSpinButton") as SpinButton;
- sudokusPerPageSpinButton = builder.get_object ("sudokusPerPageSpinButton") as SpinButton;
+ medium_button = builder.get_object ("mediumRadioButton") as RadioButton;
+ medium_button.join_group (easy_button);
- wrap_adjustment ("print-multiple-sudokus-to-print", sudokusToPrintSpinButton.get_adjustment ());
- wrap_adjustment ("sudokus-per-page", sudokusPerPageSpinButton.get_adjustment ());
- }
+ hard_button = builder.get_object ("hardRadioButton") as RadioButton;
+ hard_button.join_group (easy_button);
- private void wrap_toggle (string key_name, CheckButton action)
- {
- action.set_active (settings.get_boolean (key_name));
- action.toggled.connect (() => settings.set_boolean (key_name, action.get_active ()));
+ very_hard_button = builder.get_object ("very_hardRadioButton") as RadioButton;
+ very_hard_button.join_group (easy_button);
+
+ var saved_difficulty = (DifficultyCatagory) settings.get_enum (DIFFICULTY_KEY_NAME);
+
+ if (saved_difficulty == DifficultyCatagory.EASY)
+ easy_button.set_active (true);
+ else if (saved_difficulty == DifficultyCatagory.MEDIUM)
+ medium_button.set_active (true);
+ else if (saved_difficulty == DifficultyCatagory.HARD)
+ hard_button.set_active (true);
+ else if (saved_difficulty == DifficultyCatagory.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 ());
}
private void wrap_adjustment (string key_name, Adjustment action)
@@ -321,36 +326,31 @@ public class GamePrinter: GLib.Object {
return;
}
- var nsudokus = (int) sudokusToPrintSpinButton.get_adjustment ().get_value ();
- var sudokus_per_page = (int) sudokusPerPageSpinButton.get_adjustment ().get_value ();
- DifficultyCatagory[] levels = {};
-
- if (options_map.get ("easyCheckButton").get_active () == true)
- levels += DifficultyCatagory.EASY;
- if (options_map.get ("mediumCheckButton").get_active () == true)
- levels += DifficultyCatagory.MEDIUM;
- if (options_map.get ("hardCheckButton").get_active () == true)
- levels += DifficultyCatagory.HARD;
- if (options_map.get ("very_hardCheckButton").get_active () == true)
- levels += DifficultyCatagory.VERY_HARD;
+ var nsudokus = (int) nsudokus_button.get_adjustment ().get_value ();
+ DifficultyCatagory level;
- var boards = new ArrayList<SudokuBoard> ();
- boards = store.get_assorted_boards (nsudokus, levels, !options_map.get
("includeOldGamesToggle").get_active ());
+ if (easy_button.get_active ())
+ level = DifficultyCatagory.EASY;
+ else if (medium_button.get_active ())
+ level = DifficultyCatagory.MEDIUM;
+ else if (hard_button.get_active ())
+ level = DifficultyCatagory.HARD;
+ else if (very_hard_button.get_active ())
+ level = DifficultyCatagory.VERY_HARD;
+ else
+ assert_not_reached ();
- SudokuBoard[] sorted_boards = {};
+ settings.set_enum (DIFFICULTY_KEY_NAME, level);
- foreach (SudokuBoard i in boards)
- sorted_boards += i;
+ var boards = store.get_boards_sorted (nsudokus, level, true);
+ SudokuPrinter printer = new SudokuPrinter (boards, ref window);
- SudokuPrinter printer = new SudokuPrinter (sorted_boards, ref window, sudokus_per_page);
PrintOperationResult result = printer.print_sudoku ();
-
if (result == PrintOperationResult.APPLY)
{
dialog.hide ();
- if (options_map.get ("markAsPlayedToggle").get_active ())
- foreach (SudokuBoard i in sorted_boards)
- saver.add_game_to_finished (new SudokuGame (i));
+ foreach (SudokuBoard board in boards)
+ saver.add_game_to_finished (new SudokuGame (board));
}
}
diff --git a/src/sudoku-store.vala b/src/sudoku-store.vala
index f8731a4..a493e4e 100644
--- a/src/sudoku-store.vala
+++ b/src/sudoku-store.vala
@@ -109,21 +109,19 @@ public class SudokuStore
assert_not_reached();
}
- public ArrayList<SudokuBoard> get_assorted_boards(int n, owned DifficultyCatagory[] levels, bool
exclude_finished = false)
+ // Get boards sorted ascending based on difficulty rating
+ // i.e. - the first board returned will be the easiest, and boards will become increasingly harder
+ public SudokuBoard[] get_boards_sorted (int number_of_boards, DifficultyCatagory level, bool
exclude_finished = false)
{
var boards = new ArrayList<SudokuBoard> ();
- int i = 0;
+ SudokuBoard[] sorted_boards = {};
- if (levels.length == 0)
- levels = {DifficultyCatagory.EASY, DifficultyCatagory.MEDIUM, DifficultyCatagory.HARD,
DifficultyCatagory.VERY_HARD};
-
- while (i < n)
+ while (boards.size < number_of_boards)
{
- var board = get_random_board ((DifficultyCatagory) levels[i % levels.length]);
+ var board = get_random_board (level);
if (exclude_finished && board.is_finished ())
continue;
boards.add (board);
- i++;
}
boards.sort ((a, b) => {
@@ -133,6 +131,10 @@ public class SudokuStore
return 0;
return -1;
});
- return boards;
+
+ foreach (SudokuBoard board in boards)
+ sorted_boards += board;
+
+ return sorted_boards;
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]