1 /* 2 * Copyright (C) 2010 Jordi Mas i Hernàndez <jmas@softcatala.org> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 of the 7 * License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public 15 * License along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 using System; 19 using Gtk; 20 using Mono.Unix; 21 22 using gbrainy.Core.Main; 23 using gbrainy.Core.Services; 24 using gbrainy.Clients.Classical.Widgets; 25 26 namespace gbrainy.Clients.Classical.Dialogs 27 { 28 public class PdfExportDialog : BuilderDialog 29 { 30 [Builder.Object] Gtk.Box hbox_file; 31 [Builder.Object] Gtk.SpinButton games_spinbutton; 32 [Builder.Object] Gtk.CheckButton colorblindcheckbutton; 33 [Builder.Object] Gtk.RadioButton rb_easy; 34 [Builder.Object] Gtk.RadioButton rb_medium; 35 [Builder.Object] Gtk.RadioButton rb_master; 36 [Builder.Object] Gtk.CheckButton checkbox_logic; 37 [Builder.Object] Gtk.CheckButton checkbox_calculation; 38 [Builder.Object] Gtk.CheckButton checkbox_verbal; 39 [Builder.Object] Gtk.ComboBox layout_combo; 40 41 BrowseFile file; 42 GameManager manager; 43 PdfExporter pdfExporter; 44 const int COLUMN_VALUE = 1; 45 const int DEF_SIDEVALUE = 4; 46 PdfExportDialog(GameManager manager, ITranslations translations)47 public PdfExportDialog (GameManager manager, ITranslations translations) : base (translations, "PdfExportDialog.ui", "pdfexportbox") 48 { 49 pdfExporter = new PdfExporter (translations); 50 this.manager = manager; 51 games_spinbutton.Value = 10; 52 checkbox_logic.Active = checkbox_calculation.Active = checkbox_verbal.Active = true; 53 54 // Use defaults from Preferences 55 switch ((GameDifficulty) Preferences.Get <int> (Preferences.DifficultyKey)) { 56 case GameDifficulty.Easy: 57 rb_easy.Active = rb_easy.HasFocus = true; 58 break; 59 case GameDifficulty.Medium: 60 rb_medium.Active = rb_medium.HasFocus = true; 61 break; 62 case GameDifficulty.Master: 63 rb_master.Active = rb_master.HasFocus = true; 64 break; 65 } 66 // File selection 67 string def_file; 68 def_file = System.IO.Path.Combine ( 69 Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), 70 // Translators: default file name used when exporting PDF files (keep the pdf extension please) 71 Catalog.GetString ("games.pdf")); 72 73 file = new BrowseFile (hbox_file, def_file, true); 74 75 FileFilter[] filters = new FileFilter [2]; 76 filters[0] = new FileFilter (); 77 filters[0].AddPattern ("*.pdf"); 78 filters[0].Name = Catalog.GetString ("PDF files"); 79 80 filters[1] = new FileFilter (); 81 filters[1].AddPattern ("*.*"); 82 filters[1].Name = Catalog.GetString ("All files"); 83 84 file.Filters = filters; 85 86 ListStore layout_store = new ListStore (typeof (string), typeof (int)); // DisplayName, index to array 87 CellRenderer layout_cell = new CellRendererText (); 88 layout_combo.Model = layout_store; 89 layout_combo.PackStart (layout_cell, true); 90 layout_combo.SetCellDataFunc (layout_cell, ComboBoxCellFunc); 91 92 int [] per_side = pdfExporter.PagesPerSide; 93 94 for (int i = 0; i < per_side.Length; i++) 95 layout_store.AppendValues (per_side[i].ToString (), per_side[i]); 96 97 // Default value 98 TreeIter iter; 99 bool more = layout_store.GetIterFirst (out iter); 100 while (more) 101 { 102 if ((int) layout_store.GetValue (iter, COLUMN_VALUE) == DEF_SIDEVALUE) { 103 layout_combo.SetActiveIter (iter); 104 break; 105 } 106 more = layout_store.IterNext (ref iter); 107 } 108 } 109 110 GameDifficulty Difficulty { 111 get { 112 if (rb_easy.Active) 113 return GameDifficulty.Easy; 114 115 if (rb_master.Active) 116 return GameDifficulty.Master; 117 118 return GameDifficulty.Medium; 119 } 120 } 121 OnOK(object sender, EventArgs args)122 void OnOK (object sender, EventArgs args) 123 { 124 GameSession.Types types = GameSession.Types.None; 125 126 if (checkbox_logic.Active) 127 types |= GameSession.Types.LogicPuzzles; 128 129 if (checkbox_calculation.Active) 130 types |= GameSession.Types.Calculation; 131 132 if (checkbox_verbal.Active) 133 types |= GameSession.Types.VerbalAnalogies; 134 135 136 TreeIter iter; 137 138 layout_combo.GetActiveIter (out iter); 139 int sides = (int) layout_combo.Model.GetValue (iter, COLUMN_VALUE); 140 141 GeneratePdf (types, 142 (int) games_spinbutton.Value, 143 sides, 144 Difficulty, 145 colorblindcheckbutton.Active, 146 file.Filename); 147 } 148 GeneratePdf(GameSession.Types types, int num_games, int gamespage, GameDifficulty difficulty, bool colorblind, string filename)149 void GeneratePdf (GameSession.Types types, int num_games, int gamespage, GameDifficulty difficulty, bool colorblind, string filename) 150 { 151 Game [] games; 152 GameSession session; 153 string msg; 154 MessageType msg_type; 155 156 games = new Game [num_games]; 157 session = new GameSession (Translations); 158 session.GameManager = manager; 159 session.PlayList.ColorBlind = colorblind; 160 session.PlayList.Difficulty = difficulty; 161 session.PlayList.GameType = types; 162 163 for (int n = 0; n < num_games; n++) 164 { 165 games [n] = session.PlayList.GetPuzzle (); 166 games [n].Translations = Translations; 167 } 168 169 if (pdfExporter.GeneratePdf (games, gamespage, filename) == true) { 170 msg = Catalog.GetString ("The PDF file has been exported correctly."); 171 msg_type = MessageType.Info; 172 } else { 173 msg = Catalog.GetString ("There was a problem generating the PDF file. The file has not been created."); 174 msg_type = MessageType.Error; 175 } 176 177 // Notify operation result 178 MessageDialog md = new MessageDialog (this, DialogFlags.Modal, msg_type, ButtonsType.Ok, msg); 179 md.Run (); 180 md.Destroy (); 181 } 182 ComboBoxCellFunc(ICellLayout cell_layout, CellRenderer cell, ITreeModel tree_model, TreeIter iter)183 static public void ComboBoxCellFunc (ICellLayout cell_layout, CellRenderer cell, ITreeModel tree_model, TreeIter iter) 184 { 185 string name = (string)tree_model.GetValue (iter, 0); 186 (cell as CellRendererText).Text = name; 187 } 188 } 189 } 190