[gbrainy] Support for page per side in PDF export
- From: Jordi Mas <jmas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gbrainy] Support for page per side in PDF export
- Date: Sat, 27 Nov 2010 09:59:28 +0000 (UTC)
commit f0f12adf040ab0e4ca8a5848f5b6faade285cb59
Author: Jordi Mas <jmas softcatala org>
Date: Sat Nov 27 11:01:32 2010 +0100
Support for page per side in PDF export
src/Clients/Classical/Dialogs/PdfExportDialog.cs | 61 +++++++++++++++----
.../Classical/Dialogs/ui/PdfExportDialog.ui | 18 ++----
src/Core/Main/PdfExporter.cs | 51 +++++++++++++---
3 files changed, 97 insertions(+), 33 deletions(-)
---
diff --git a/src/Clients/Classical/Dialogs/PdfExportDialog.cs b/src/Clients/Classical/Dialogs/PdfExportDialog.cs
index a523327..57d7f70 100644
--- a/src/Clients/Classical/Dialogs/PdfExportDialog.cs
+++ b/src/Clients/Classical/Dialogs/PdfExportDialog.cs
@@ -30,8 +30,7 @@ namespace gbrainy.Clients.Classical.Dialogs
public class PdfExportDialog : BuilderDialog
{
[GtkBeans.Builder.Object] Gtk.HBox hbox_file;
- [GtkBeans.Builder.Object] Gtk.SpinButton games_spinbutton;
- [GtkBeans.Builder.Object] Gtk.SpinButton gamesperpage_spinbutton;
+ [GtkBeans.Builder.Object] Gtk.SpinButton games_spinbutton;
[GtkBeans.Builder.Object] Gtk.CheckButton colorblindcheckbutton;
[GtkBeans.Builder.Object] Gtk.RadioButton rb_easy;
[GtkBeans.Builder.Object] Gtk.RadioButton rb_medium;
@@ -39,18 +38,20 @@ namespace gbrainy.Clients.Classical.Dialogs
[GtkBeans.Builder.Object] Gtk.CheckButton checkbox_logic;
[GtkBeans.Builder.Object] Gtk.CheckButton checkbox_calculation;
[GtkBeans.Builder.Object] Gtk.CheckButton checkbox_verbal;
-
+ [GtkBeans.Builder.Object] Gtk.ComboBox layout_combo;
+
BrowseFile file;
+ const int COLUMN_VALUE = 1;
+ const int DEF_SIDEVALUE = 4;
public PdfExportDialog () : base ("PdfExportDialog.ui", "pdfexportbox")
{
games_spinbutton.Value = 10;
- gamesperpage_spinbutton.Value = 4;
checkbox_logic.Active = checkbox_calculation.Active = checkbox_verbal.Active = true;
// Use defaults from Preferences
switch ((GameDifficulty) Preferences.GetIntValue (Preferences.DifficultyKey)) {
- case GameDifficulty.Easy:
+ case GameDifficulty.Easy:
rb_easy.Active = rb_easy.HasFocus = true;
break;
case GameDifficulty.Medium:
@@ -62,7 +63,6 @@ namespace gbrainy.Clients.Classical.Dialogs
}
// File selection
string def_file;
-
def_file = System.IO.Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments),
"games.pdf");
@@ -79,6 +79,29 @@ namespace gbrainy.Clients.Classical.Dialogs
filters[1].Name = Catalog.GetString ("All files");
file.Filters = filters;
+
+ ListStore layout_store = new ListStore (typeof (string), typeof (int)); // DisplayName, index to array
+ CellRenderer layout_cell = new CellRendererText ();
+ layout_combo.Model = layout_store;
+ layout_combo.PackStart (layout_cell, true);
+ layout_combo.SetCellDataFunc (layout_cell, ComboBoxCellFunc);
+
+ int [] per_side = PdfExporter.PagesPerSide;
+
+ for (int i = 0; i < per_side.Length; i++)
+ layout_store.AppendValues (per_side[i].ToString (), per_side[i]);
+
+ // Default value
+ TreeIter iter;
+ bool more = layout_store.GetIterFirst (out iter);
+ while (more)
+ {
+ if ((int) layout_store.GetValue (iter, COLUMN_VALUE) == DEF_SIDEVALUE) {
+ layout_combo.SetActiveIter (iter);
+ break;
+ }
+ more = layout_store.IterNext (ref iter);
+ }
}
GameDifficulty Difficulty {
@@ -104,11 +127,17 @@ namespace gbrainy.Clients.Classical.Dialogs
types |= GameSession.Types.CalculationTrainers;
if (checkbox_verbal.Active)
- types |= GameSession.Types.VerbalAnalogies;
-
+ types |= GameSession.Types.VerbalAnalogies;
+
+
+ TreeIter iter;
+
+ layout_combo.GetActiveIter (out iter);
+ int sides = (int) layout_combo.Model.GetValue (iter, COLUMN_VALUE);
+
GeneratePdf (types,
(int) games_spinbutton.Value,
- (int) gamesperpage_spinbutton.Value,
+ sides,
Difficulty,
colorblindcheckbutton.Active,
file.Filename);
@@ -118,20 +147,26 @@ namespace gbrainy.Clients.Classical.Dialogs
{
Game [] games;
GameManager gm;
-
+
games = new Game [num_games];
gm = new GameManager ();
gm.ColorBlind = colorblind;
gm.Difficulty = difficulty;
GtkClient.GameManagerPreload (gm);
gm.GameType = types;
-
+
for (int n = 0; n < num_games; n++)
{
- games [n] = gm.GetPuzzle ();
+ games [n] = gm.GetPuzzle ();
}
-
+
PdfExporter.GeneratePdf (games, gamespage, filename);
}
+
+ static public void ComboBoxCellFunc (CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
+ {
+ string name = (string)tree_model.GetValue (iter, 0);
+ (cell as CellRendererText).Text = name;
+ }
}
}
diff --git a/src/Clients/Classical/Dialogs/ui/PdfExportDialog.ui b/src/Clients/Classical/Dialogs/ui/PdfExportDialog.ui
index b20dc58..fcb801d 100644
--- a/src/Clients/Classical/Dialogs/ui/PdfExportDialog.ui
+++ b/src/Clients/Classical/Dialogs/ui/PdfExportDialog.ui
@@ -238,7 +238,7 @@
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="xpad">5</property>
- <property name="label" translatable="yes">Number of games per page:</property>
+ <property name="label" translatable="yes">Pages per side:</property>
</object>
<packing>
<property name="top_attach">1</property>
@@ -248,31 +248,27 @@
</packing>
</child>
<child>
- <object class="GtkSpinButton" id="gamesperpage_spinbutton">
+ <object class="GtkSpinButton" id="games_spinbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
- <property name="adjustment">adjustment2</property>
+ <property name="adjustment">adjustment3</property>
<property name="climb_rate">1</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>
</packing>
</child>
<child>
- <object class="GtkSpinButton" id="games_spinbutton">
+ <object class="GtkComboBox" id="layout_combo">
<property name="visible">True</property>
- <property name="can_focus">True</property>
- <property name="adjustment">adjustment3</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"></property>
+ <property name="top_attach">1</property>
+ <property name="bottom_attach">2</property>
</packing>
</child>
</object>
@@ -351,7 +347,7 @@
<property name="can_focus">True</property>
<property name="can_default">True</property>
<property name="receives_default">False</property>
- <property name="use_stock">True</property>
+ <property name="use_underline">True</property>
<signal name="clicked" handler="OnOK"/>
</object>
<packing>
diff --git a/src/Core/Main/PdfExporter.cs b/src/Core/Main/PdfExporter.cs
index 1507fa8..0d324d1 100644
--- a/src/Core/Main/PdfExporter.cs
+++ b/src/Core/Main/PdfExporter.cs
@@ -29,25 +29,48 @@ namespace gbrainy.Core.Main
static public class PdfExporter
{
static readonly int width = 400, height = 400, margin = 20, question_height = 100;
- static readonly int columns = 2, rows = 2;
static readonly int page_margin = 20; // space between vertical and hortizontal pages
static readonly int page_width = width + page_margin;
static readonly int page_height = height + question_height + page_margin;
+ static int [] pages_side = {1, 2, 4};
+
+ static public int [] PagesPerSide
+ {
+ get { return pages_side; }
+ }
static public void GeneratePdf (Game [] games, int games_page, string file)
{
+ int columns, rows;
+ switch (games_page) {
+ case 1:
+ columns = 1;
+ rows = 1;
+ break;
+ case 2:
+ columns = 2;
+ rows = 1;
+ break;
+ case 4:
+ columns = 2;
+ rows = 2;
+ break;
+ default:
+ throw new InvalidOperationException ("Invalid games per page value");
+ }
+
PdfSurface pdf = new PdfSurface (file, page_width * columns, page_height * rows);
CairoContextEx cr = new CairoContextEx (pdf, "sans 12", 72);
- GenerateQuestions (cr, games);
- GenerateAnswers (cr, games);
+ GenerateQuestions (cr, games, columns, rows);
+ GenerateAnswers (cr, games, columns, rows);
pdf.Finish ();
((IDisposable)cr).Dispose();
return;
}
- static void GenerateQuestions (CairoContextEx cr, Game [] games)
+ static void GenerateQuestions (CairoContextEx cr, Game [] games, int columns, int rows)
{
int x, y, page;
Game puzzle;
@@ -80,6 +103,15 @@ namespace gbrainy.Core.Main
// Translate adds always to previous matrix's transformation
cr.Translate (0, question_height);
puzzle.DrawPreview (cr, width, height, false);
+ if (i == 0) {
+ cr.Save ();
+ cr.SetPangoFontSize (0.02);
+ cr.MoveTo (0.05, 0.95);
+ cr.ShowPangoText (String.Format (Catalog.GetString ("Created by gbrainy {0}"), Defines.VERSION));
+ cr.Stroke ();
+ cr.Restore ();
+ }
+
x += width + margin;
if (x > width + margin) {
x = 0;
@@ -98,12 +130,13 @@ namespace gbrainy.Core.Main
cr.ShowPage ();
}
- static void GenerateAnswers (CairoContextEx cr, Game [] games)
+ static void GenerateAnswers (CairoContextEx cr, Game [] games, int columns, int rows)
{
int x, y, page;
Game puzzle;
string str;
int column, row;
+ const int space_lines = 80;
x = y = page = 0;
column = row = 0;
@@ -112,7 +145,7 @@ namespace gbrainy.Core.Main
cr.SetPangoFontSize (20);
cr.DrawStringWithWrapping (x + margin, y + margin,
Catalog.GetString ("Solutions"), width - margin);
- y += 75;
+ y += space_lines;
cr.Stroke ();
cr.SetPangoFontSize (12);
@@ -125,10 +158,10 @@ namespace gbrainy.Core.Main
cr.DrawStringWithWrapping (x + margin, y + margin, str, width - margin);
cr.Stroke ();
- y += 75;
+ y += space_lines;
// Next lateral page (right)
- if (y >= page_height * (row + 1) && x + page_width < page_width * columns) {
+ if (y + space_lines >= page_height * (row + 1) && x + page_width < page_width * columns) {
column++;
x = column * page_width;
@@ -136,7 +169,7 @@ namespace gbrainy.Core.Main
page++;
} else {
// No more space (right), new row
- if (y >= page_height * (row + 1) && x + page_width >= page_width * columns) {
+ if (y + space_lines >= page_height * (row + 1) && x + page_width >= page_width * columns) {
row++;
column = 0;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]