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 {
00015 [FoodoliniActivity("Edit recipe", true)]
00016 [System.ComponentModel.ToolboxItem(true)]
00017 public partial class RecipeEditor : Gtk.Bin, IActivity
00018 {
00019 private IOwner owner;
00020 private Recipe recipe;
00021 private NodeStore ingredientStorage = new NodeStore(typeof(IngredientAdapter));
00022 private IList<string> listCategory = new List<string>();
00023 private List<string> listDirections = new List<string>();
00024
00028 public RecipeEditor()
00029 {
00030 this.Build();
00031
00032
00033 this.nvIngredients.NodeStore = this.ingredientStorage;
00034 this.nvIngredients.AppendColumn("Added ingredients", new CellRendererText(), (tree_column, cell, node)=>{
00035 IngredientAdapter n = (IngredientAdapter)node;
00036 CellRendererText c = (CellRendererText)cell;
00037 c.Text = n.Name;
00038 });
00039
00040 CellRendererSpin quantityCell = new CellRendererSpin();
00041 quantityCell.Editable = true;
00042 quantityCell.Adjustment = new Adjustment(0,1,100000,1,1,1);
00043 quantityCell.Digits = 1;
00044 this.nvIngredients.AppendColumn("Quantity (g)", quantityCell, (column, cell, node) =>
00045 {
00046 ((CellRendererSpin)cell).Text = ((IngredientAdapter)node).Quantity.ToString ("0");
00047 });
00048
00049 quantityCell.Edited += delegate(object o, EditedArgs args) {
00050 var node = (IngredientAdapter)ingredientStorage.GetNode(new TreePath(args.Path));
00051 double newValue;
00052 if(double.TryParse(args.NewText, out newValue)) {
00053 node.Quantity = newValue;
00054 }
00055 };
00056
00057
00058 quantityCell.EditingStarted += (o, args) =>
00059 {
00060 var node = (IngredientAdapter)ingredientStorage.GetNode (new TreePath (args.Path));
00061 ((SpinButton)args.Editable).Value = node.Quantity;
00062 };
00063
00064 bxIngredient.ShowAll();
00065 }
00066
00067 public event EventHandler<RecipeEventArgs> OnRecipeEdited;
00068
00072 private void CheckIfFilled()
00073 {
00074 if (this.entTitle.Text == "" | !ingredientsAdded | tvDirections.Buffer.Text == "")
00075 btnSave.Sensitive = false;
00076 else
00077 btnSave.Sensitive = true;
00078 }
00079
00080 private bool ingredientsAdded = false;
00081
00085 protected virtual void OnAddIngredient (object sender, System.EventArgs e)
00086 {
00087 Ingredient selectedIngredient;
00088 IngredientSelector dialog = new IngredientSelector(true);
00089 if (dialog.Run() == (int)ResponseType.Ok)
00090 {
00091 selectedIngredient = dialog.SelectedIngredient;
00092 this.ingredientStorage.AddNode(new IngredientAdapter(selectedIngredient, dialog.Quantity));
00093 }
00094 dialog.Destroy();
00095 ingredientsAdded = true;
00096 CheckIfFilled();
00097 }
00098
00099 protected virtual void OnBtnNewSaveClicked (object sender, System.EventArgs e)
00100 {
00101 string title = entTitle.Text;
00102 if(Recipe.GetByTitle(title) == null & Ingredient.GetByLongDescription(title) == null)
00103 {
00104 SaveNewRecipe();
00105 }
00106 else
00107 {
00108 if(title == recipe.Title)
00109 SaveNewRecipe();
00110 else
00111 {
00112 ConfirmDialog dialog = new ConfirmDialog("Recipe or ingredient allready exist", "A recipe or ingredient with that name allready exist.\nTry to remane the recipe.");
00113 if(dialog.Run() == (int)ResponseType.Ok){}
00114 dialog.Destroy();
00115 }
00116 }
00117 }
00118
00122 protected virtual void OnCancelClick (object sender, System.EventArgs e)
00123 {
00124 this.owner.PopActivity();
00125 }
00126
00132 protected virtual void OnDeleteIngredientClick (object sender, System.EventArgs e)
00133 {
00134 if(this.nvIngredients.NodeSelection.SelectedNode != null)
00135 {
00136 var ingredient = (IngredientAdapter)this.nvIngredients.NodeSelection.SelectedNode;
00137 this.ingredientStorage.RemoveNode(ingredient);
00138 if(!this.ingredientStorage.GetEnumerator().MoveNext())
00139 this.ingredientsAdded = false;
00140 CheckIfFilled();
00141 }
00142 }
00143
00147 protected virtual void OnEntryChanged (object sender, System.EventArgs e)
00148 {
00149 CheckIfFilled();
00150 }
00151
00157 protected virtual void OnSaveClicked (object sender, System.EventArgs e)
00158 {
00159 string title = entTitle.Text;
00160 if(Recipe.GetByTitle(title) == null & Ingredient.GetByLongDescription(title) == null)
00161 {
00162 SaveRecipe();
00163 }
00164 else
00165 {
00166 if(title == recipe.Title)
00167 SaveRecipe();
00168 else
00169 {
00170 ConfirmDialog dialog = new ConfirmDialog("Recipe or ingredient allready exist", "A recipe or ingredient with that name allready exist.\nTry to remane the recipe.");
00171 if(dialog.Run() == (int)ResponseType.Ok){}
00172 dialog.Destroy();
00173 }
00174 }
00175 }
00176
00177 private void SaveNewRecipe()
00178 {
00179 Recipe newRecipe = new Recipe(entTitle.Text);
00180
00181 if(cbxMealtype.Active == 0)
00182 listCategory.Add("Breakfast");
00183 else if(cbxMealtype.Active == 1)
00184 listCategory.Add("Lunch");
00185 else if (cbxMealtype.Active == 2)
00186 listCategory.Add("Dinner");
00187 else
00188 listCategory.Add("Other");
00189
00190 newRecipe.Difficulty = (Difficulty)cbxDifficulty.Active;
00191
00192 newRecipe.PreparationTime = new TimeSpan((int)sbDays.Value, (int)sbHours.Value, (int)sbMinuts.Value, 0);
00193
00194
00195 foreach(string category in this.entCategories.Text.Split(new char[]{','}))
00196 this.listCategory.Add(category.Trim());
00197 newRecipe.Categories = listCategory;
00198
00199
00200 foreach(string direction in this.tvDirections.Buffer.Text.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries))
00201 this.listDirections.Add(direction.Trim());
00202 newRecipe.Directions = listDirections;
00203
00204
00205 foreach(IngredientAdapter ingredient in this.ingredientStorage)
00206 newRecipe.Ingredients.Add(ingredient.Ingredient, ingredient.Quantity);
00207
00208
00209 if (byteArray != null)
00210 newRecipe.Picture = byteArray;
00211
00212 newRecipe.Save();
00213
00214 if(OnRecipeEdited != null)
00215 OnRecipeEdited(this, new RecipeEventArgs(this.recipe));
00216 this.owner.PopActivity();
00217 }
00218
00219
00224 private void SaveRecipe()
00225 {
00226 this.recipe.Title = entTitle.Text;
00227
00228
00229 if(cbxMealtype.Active == 0)
00230 listCategory.Add("Breakfast");
00231 else if(cbxMealtype.Active == 1)
00232 listCategory.Add("Lunch");
00233 else if (cbxMealtype.Active == 2)
00234 listCategory.Add("Dinner");
00235 else
00236 listCategory.Add("Other");
00237
00238 this.recipe.Difficulty = (Difficulty)cbxDifficulty.Active;
00239
00240 this.recipe.PreparationTime = new TimeSpan((int)sbDays.Value, (int)sbHours.Value, (int)sbMinuts.Value, 0);
00241
00242
00243 foreach(string category in this.entCategories.Text.Split(new char[]{','}))
00244 this.listCategory.Add(category.Trim());
00245 this.recipe.Categories = listCategory;
00246
00247
00248 this.recipe.Servings = sbMultiplier.Value;
00249
00250
00251 foreach(string direction in this.tvDirections.Buffer.Text.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries))
00252 this.listDirections.Add(direction.Trim());
00253 this.recipe.Directions = listDirections;
00254
00255
00256 this.recipe.ClearIngredients();
00257 foreach(IngredientAdapter ingredient in this.ingredientStorage)
00258 this.recipe.Ingredients.Add(ingredient.Ingredient, ingredient.Quantity);
00259
00260
00261 if (byteArray != null)
00262 this.recipe.Picture = byteArray;
00263
00264 this.recipe.Save();
00265
00266 if(OnRecipeEdited != null)
00267 OnRecipeEdited(this, new RecipeEventArgs(this.recipe));
00268 this.owner.PopActivity();
00269 }
00270
00271 private FileStream stream;
00272 private byte[] byteArray;
00273
00277 protected virtual void OnSelectPictureClick (object sender, System.EventArgs e)
00278 {
00279 stream = File.OpenRead(fcbPicture.Filename);
00280 byteArray = new byte[stream.Length];
00281 for(int i = 0; i < stream.Length; i++)
00282 byteArray[i] = (byte)stream.ReadByte();
00283 Gdk.Pixbuf px = new Gdk.Pixbuf(byteArray);
00284 px = px.ScaleSimple(100, 100, Gdk.InterpType.Bilinear);
00285 imgRecipe.Pixbuf = px;
00286 }
00287
00291 protected virtual void OnTvDirectionsKeyReleaseEvent (object o, Gtk.KeyReleaseEventArgs args)
00292 {
00293 CheckIfFilled();
00294 }
00295
00302 public void SetParamenters(Recipe recipe){
00303 this.recipe = recipe;
00304 SetRecipeDetails();
00305 }
00306
00310 private void SetRecipeDetails(){
00311 entTitle.Text = this.recipe.Title;
00312
00313
00314 for(int i = 0; i < this.recipe.Categories.Count-1; i++)
00315 {
00316 string category = this.recipe.Categories[i];
00317 if(category == "Breakfast")
00318 this.cbxMealtype.Active = 0;
00319 else if (category == "Lunch")
00320 this.cbxMealtype.Active = 1;
00321 else if (category == "Dinner")
00322 this.cbxMealtype.Active = 2;
00323 else if (category == "Other")
00324 this.cbxMealtype.Active = 3;
00325 else
00326 this.entCategories.Text += category + ", ";
00327 }
00328 this.entCategories.Text += this.recipe.Categories[this.recipe.Categories.Count-1];
00329
00330
00331 cbxDifficulty.Active = (int)this.recipe.Difficulty;
00332 TimeSpan preparationTime = this.recipe.PreparationTime;
00333 sbDays.Value = preparationTime.Days;
00334 sbHours.Value = preparationTime.Hours;
00335 sbMinuts.Value = preparationTime.Minutes;
00336 sbMultiplier.Value = this.recipe.Servings;
00337
00338
00339 foreach (var direction in this.recipe.Directions)
00340 this.tvDirections.Buffer.Text += direction + "\n";
00341
00342
00343 foreach(KeyValuePair<Ingredient, double> ing in this.recipe.Ingredients)
00344 {
00345 this.ingredientStorage.AddNode(new IngredientAdapter(ing.Key, ing.Value));
00346 this.ingredientsAdded = true;
00347 }
00348
00349
00350 if(this.recipe.Picture != null)
00351 {
00352 Gdk.Pixbuf px = new Gdk.Pixbuf(this.recipe.Picture);
00353 px = px.ScaleSimple(100, 100, Gdk.InterpType.Bilinear);
00354 this.imgRecipe.Pixbuf = px;
00355 }
00356
00357 CheckIfFilled();
00358 }
00359
00360 public Recipe Recipe{
00361 get{return this.recipe;}
00362 }
00363
00364 #region IActivity
00365 public void Register(IOwner owner){
00366 this.owner = owner;
00367 }
00368
00369 public void Unregister(){
00370 this.owner = null;
00371 }
00372
00373
00374
00375 public Widget Widget{
00376 get{
00377 return this;
00378 }
00379 }
00380 #endregion
00381 }
00382
00383
00384 }
00385