00001
00002 using System;
00003 using Foodolini.Activities;
00004 using Foodolini.BusinessLogic;
00005 using Gtk;
00006 using System.Collections.Generic;
00007 using System.IO;
00008 using Foodolini.Activities.Ingredients;
00009
00010 namespace Foodolini.Activities.Cookbook
00011 {
00012
00016 [FoodoliniActivity("Create recipe", false, "recipe.png")]
00017 [System.ComponentModel.ToolboxItem(true)]
00018 public partial class RecipeCreater : Gtk.Bin, IActivity
00019 {
00020 private IOwner owner;
00021 private Recipe recipe;
00022 private NodeStore ingredientStorage = new NodeStore(typeof(IngredientAdapter));
00023 private IList<string> listCategory = new List<string>();
00024 private List<string> listDirections = new List<string>();
00025 bool ingredientsAdded = false;
00026
00030 public RecipeCreater()
00031 {
00032 this.Build();
00033
00034
00035 this.nvIngredients.NodeStore = this.ingredientStorage;
00036 this.nvIngredients.AppendColumn("Added ingredients", new CellRendererText(), (tree_column, cell, node)=>{
00037 IngredientAdapter n = (IngredientAdapter)node;
00038 CellRendererText c = (CellRendererText)cell;
00039 c.Text = n.Name;
00040 });
00041
00042 CellRendererSpin quantityCell = new CellRendererSpin();
00043 quantityCell.Editable = true;
00044 quantityCell.Adjustment = new Adjustment(0,1,100000,1,1,1);
00045 quantityCell.Digits = 1;
00046 this.nvIngredients.AppendColumn("Quantity (g)", quantityCell, (column, cell, node) =>
00047 {
00048 ((CellRendererSpin)cell).Text = ((IngredientAdapter)node).Quantity.ToString ("0");
00049 });
00050
00051 quantityCell.Edited += delegate(object o, EditedArgs args) {
00052 var node = (IngredientAdapter)ingredientStorage.GetNode(new TreePath(args.Path));
00053 double newValue;
00054 if(double.TryParse(args.NewText, out newValue)) {
00055 node.Quantity = newValue;
00056 }
00057 };
00058
00059
00060 quantityCell.EditingStarted += (o, args) =>
00061 {
00062 var node = (IngredientAdapter)ingredientStorage.GetNode (new TreePath (args.Path));
00063 ((SpinButton)args.Editable).Value = node.Quantity;
00064 };
00065
00066 bxIngredient.ShowAll();
00067
00068
00069 this.btnSave.Sensitive = false;
00070 }
00071
00072 public event EventHandler<RecipeEventArgs> OnRecipeCreated;
00073
00077 private void CheckIfFilled()
00078 {
00079 if (this.entTitle.Text == "" | !ingredientsAdded | tvDirections.Buffer.Text == "")
00080 {
00081 btnSave.Sensitive = false;
00082 }
00083 else
00084 {
00085 btnSave.Sensitive = true;
00086 }
00087
00088 }
00089
00093 protected virtual void OnAddIngredient (object sender, System.EventArgs e)
00094 {
00095 Ingredient selectedIngredient;
00096 IngredientSelector dialog = new IngredientSelector(true);
00097 if (dialog.Run() == (int)ResponseType.Ok)
00098 {
00099 selectedIngredient = dialog.SelectedIngredient;
00100 this.ingredientStorage.AddNode(new IngredientAdapter(selectedIngredient, dialog.Quantity));
00101 }
00102 dialog.Destroy();
00103 ingredientsAdded = true;
00104 CheckIfFilled();
00105 }
00106
00112 protected virtual void OnBtnRemoveIngredientClicked (object sender, System.EventArgs e)
00113 {
00114 if(this.nvIngredients.NodeSelection.SelectedNode != null)
00115 {
00116 var ingredient = (IngredientAdapter)this.nvIngredients.NodeSelection.SelectedNode;
00117 this.ingredientStorage.RemoveNode(ingredient);
00118 this.ingredientsAdded = false;
00119 if(this.ingredientStorage.GetEnumerator().MoveNext())
00120 this.ingredientsAdded = true;
00121 CheckIfFilled();
00122 }
00123 }
00124
00128 protected virtual void OnCancelClick (object sender, System.EventArgs e)
00129 {
00130 this.owner.PopActivity();
00131 }
00132
00136 protected virtual void OnEntryChanged (object sender, System.EventArgs e)
00137 {
00138 CheckIfFilled();
00139 }
00140
00144 protected virtual void OnTvDirectionsKeyReleaseEvent (object o, Gtk.KeyReleaseEventArgs args)
00145 {
00146 CheckIfFilled();
00147 }
00148
00153 protected virtual void OnSaveClicked (object sender, System.EventArgs e)
00154 {
00155 string title = entTitle.Text;
00156 if(Recipe.GetByTitle(title) == null & Ingredient.GetByLongDescription(title) == null)
00157 {
00158 this.recipe = new Recipe(title);
00159
00160
00161 if(cbxMealtype.Active == 0)
00162 listCategory.Add("Breakfast");
00163 else if(cbxMealtype.Active == 1)
00164 listCategory.Add("Lunch");
00165 else if (cbxMealtype.Active == 2)
00166 listCategory.Add("Dinner");
00167 else
00168 listCategory.Add("Other");
00169
00170 this.recipe.Difficulty = (Difficulty)cbxDifficulty.Active;
00171
00172 this.recipe.PreparationTime = new TimeSpan((int)sbDays.Value, (int)sbHours.Value, (int)sbMinuts.Value, 0);
00173
00174
00175 foreach(string category in this.entCategories.Text.Split(new char[]{','}))
00176 listCategory.Add(category.Trim());
00177 this.recipe.Categories = listCategory;
00178
00179
00180 this.recipe.Servings = sbMultiplier.Value;
00181
00182
00183 foreach(string direction in this.tvDirections.Buffer.Text.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries))
00184 listDirections.Add(direction.Trim());
00185 this.recipe.Directions = listDirections;
00186
00187
00188 foreach(IngredientAdapter ingredient in this.ingredientStorage)
00189 this.recipe.Ingredients.Add(ingredient.Ingredient, ingredient.Quantity);
00190
00191
00192 if (byteArray != null)
00193 recipe.Picture = byteArray;
00194
00195 if(OnRecipeCreated != null)
00196 OnRecipeCreated(this, new RecipeEventArgs(this.recipe));
00197
00198 this.recipe.Save();
00199 this.owner.PopActivity();
00200 }
00201 else
00202 {
00203 ConfirmDialog dialog = new ConfirmDialog("Recipe or ingredient already exist", "A recipe or ingredient with that name already exists.\nTry to remane the recipe.");
00204 if(dialog.Run() == (int)ResponseType.Ok){}
00205 dialog.Destroy();
00206 }
00207 }
00208
00209 private byte[] byteArray;
00213 protected virtual void OnSelectPictureClick (object sender, System.EventArgs e)
00214 {
00215 using(FileStream stream = File.OpenRead(fcbPicture.Filename)) {
00216 byteArray = new byte[stream.Length];
00217 for(int i = 0; i < stream.Length; i++)
00218 byteArray[i] = (byte)stream.ReadByte();
00219 Gdk.Pixbuf px = new Gdk.Pixbuf(byteArray);
00220 px = px.ScaleSimple(100, 100, Gdk.InterpType.Bilinear);
00221 imgRecipe.Pixbuf = px;
00222 }
00223 }
00224
00228 public Recipe Recipe{
00229 get {return this.recipe;}
00230 }
00231
00232 #region IActivity
00233 public void Register(IOwner owner){
00234 this.owner = owner;
00235 }
00236
00237 public void Unregister(){
00238 this.owner = null;
00239 }
00240
00241 public Widget Widget{
00242 get{
00243 return this;
00244 }
00245 }
00246 #endregion
00247 }
00248 }