//Load colors from Databse method public void LoadColors() { SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); try { var list = from c in ctx.TblColors select new SavedColor { R = (byte)c.Red, G = (byte)c.Green, B = (byte)c.Blue }; foreach (var item in list) { Color color = RecreateAColor(item.R, item.G, item.B); Rectangle colorRec = new Rectangle { Height = 20, Fill = new SolidColorBrush(color) }; Window.lb_ColourTemplates.Items.Add(colorRec); } } catch (Exception ex) { MessageBox.Show("Error " + ex.Message); } }
//Load saved Canvasses to DataGrid public void LoadCanvasses() { SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); var list = from o in ctx.TblOverviews select o; MainWindow.dg_DrawingOverview.ItemsSource = list; }
//Delete canvas from overview public void DeleteCanvas(string name) { SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); var deleteCanvas = (from o in ctx.TblOverviews where o.Name == name select o).FirstOrDefault(); ctx.TblOverviews.DeleteOnSubmit(deleteCanvas); ctx.SubmitChanges(); LoadCanvasses(); }
//Loads shapes to ListBox from DataBase public void LoadShapes() { SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); ColorManager = new ColorManager(Window); try { var list = from s in ctx.TblShapes from c in ctx.TblColors where s.Color_ID == c.Color_ID select new SavedShape { W = (int)s.Width, H = (int)s.Height, Shape = s.Shape, R = (byte)c.Red, G = (byte)c.Green, B = (byte)c.Blue, Shape_ID = s.Shape_ID }; foreach (var item in list) { StackPanel stack = new StackPanel { Orientation = Orientation.Horizontal }; Label colorLabel = new Label { Width = 40, Height = 30 }; Color color = ColorManager.RecreateAColor(item.R, item.G, item.B); colorLabel.Background = new SolidColorBrush(color); Label descriptionLabel = new Label { Content = item.ToString(item.Shape) }; stack.Children.Add(colorLabel); stack.Children.Add(descriptionLabel); stack.Tag = item; Window.lb_ShapeTemplates.Items.Add(stack); } } catch (Exception ex) { MessageBox.Show("Error " + ex.Message); } }
//Write a new shape to DB public void WriteShapeToDB(string name, byte r, byte g, byte b, int w, int h) { SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); TblColor c = new TblColor() { Red = r, Green = g, Blue = b }; TblColor savedColor = ctx.TblColors.Where(sc => sc.Red == c.Red && sc.Blue == c.Blue && sc.Green == c.Green).FirstOrDefault(); if (savedColor == null) { ctx.TblColors.InsertOnSubmit(c); ctx.SubmitChanges(); ColorManager.LoadColors(); } else { c.Color_ID = savedColor.Color_ID; } TblShape s = new TblShape() { Width = w, Height = h, Shape = name, Color_ID = c.Color_ID }; TblShape savedShape = ctx.TblShapes.Where(ss => ss.Color_ID == s.Color_ID && ss.Width == s.Width && ss.Height == s.Height && ss.Shape == s.Shape).FirstOrDefault(); if (savedShape == null) { ctx.TblShapes.InsertOnSubmit(s); ctx.SubmitChanges(); } else { MessageBox.Show("This shape already exists"); } }
//Method to write color to database public void WriteColorToDB(byte r, byte g, byte b) { SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); TblColor c = new TblColor() { Red = r, Green = g, Blue = b }; TblColor savedColor = ctx.TblColors.Where(sc => sc.Red == c.Red && sc.Blue == c.Blue && sc.Green == c.Green).FirstOrDefault(); if (savedColor == null) { ctx.TblColors.InsertOnSubmit(c); ctx.SubmitChanges(); } else { MessageBox.Show("This color already exists."); } }
//Click event to draw the chosen shape on canvas private void cvs_Drawing_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { try { Shape shapeDrawing = CanvasManager.ShapeManager.CreateNewShape(); Point location = e.GetPosition(cvs_Drawing); Canvas.SetTop(shapeDrawing, location.Y); Canvas.SetLeft(shapeDrawing, location.X); cvs_Drawing.Children.Add(shapeDrawing); SQLServer_DrawAppDataContext ctx = new SQLServer_DrawAppDataContext(); TblColor c = new TblColor() { Red = ((SolidColorBrush)shapeDrawing.Fill).Color.R, Green = ((SolidColorBrush)shapeDrawing.Fill).Color.G, Blue = ((SolidColorBrush)shapeDrawing.Fill).Color.B }; var savedColor = ctx.TblColors.Where(sc => sc.Red == c.Red && sc.Blue == c.Blue && sc.Green == c.Green).FirstOrDefault(); if (savedColor == null) { ctx.TblColors.InsertOnSubmit(c); ctx.SubmitChanges(); } else { c.Color_ID = savedColor.Color_ID; } TblShape s = new TblShape() { Shape = CanvasManager.ShapeManager.SetFinalShapeName(shapeDrawing, shapeDrawing.Width, shapeDrawing.Height), Width = (float)shapeDrawing.Width, Height = (float)shapeDrawing.Height, Color_ID = c.Color_ID, }; var savedShape = ctx.TblShapes.Where(ss => ss.Color_ID == s.Color_ID && ss.Width == s.Width && ss.Height == s.Height && ss.Shape == s.Shape).FirstOrDefault(); if (savedShape == null) { ctx.TblShapes.InsertOnSubmit(s); ctx.SubmitChanges(); } else { s.Shape_ID = savedShape.Shape_ID; } var canvasID = ctx.TblOverviews.Where(canvas => canvas.Name == this.Title).FirstOrDefault(); if (canvasID != null) { TblPosition position = new TblPosition() { Shape_ID = s.Shape_ID, Drawing_ID = canvasID.Drawing_ID, X = Canvas.GetLeft(shapeDrawing), Y = Canvas.GetTop(shapeDrawing) }; ctx.TblPositions.InsertOnSubmit(position); } ctx.SubmitChanges(); } catch (FormatException) { MessageBox.Show("Please provide necessary values or select a shape from the selection window"); } catch (Exception ex) { MessageBox.Show("Error occured - " + ex); } }