private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var graph = graphCanvas1.graph; treeView1.Nodes.Clear(); graphCanvas1.Clear(); var xdoc = XDocument.Load(openFileDialog1.FileName); var xgraph = xdoc.Element("graph"); foreach (var smt in xgraph.Element("sommets").Elements()) { Sommet sommet = new Sommet(smt.Attribute("text").Value, Color.White, new Point(int.Parse(smt.Attribute("X").Value), int.Parse(smt.Attribute("Y").Value))); graph.Add(sommet); } foreach (var arc in xgraph.Element("arcs").Elements()) { string s1 = arc.Attribute("s1").Value; string s2 = arc.Attribute("s2").Value; Sommet sm1 = graph.Find(x => x.text == s1); Sommet sm2 = graph.Find(x => x.text == s2); sm1.AddArc(sm2); } graphCanvas1.Invalidate(); } }
private void graphCanvas1_MouseClick(object sender, MouseEventArgs e) { switch (mode) { case Mode.Move: break; case Mode.Add: Sommet sm = new Sommet("X" + (graphCanvas1.graph.Count + 1), Color.White, e.Location); graphCanvas1.graph.Add(sm); break; case Mode.Remove: Sommet clicked = graphCanvas1.GetAt(e.Location); if (clicked != null) { while (clicked.succ.Count > 0) { clicked.RemoveArc(clicked.succ[0]); } while (clicked.pred.Count > 0) { clicked.RemoveArc(clicked.pred[0]); } graphCanvas1.graph.Remove(clicked); } break; case Mode.Link: clicked = graphCanvas1.GetAt(e.Location); if (sel1 == null) { sel1 = clicked; } else { sel1.AddArc(clicked); sel1 = null; } break; case Mode.Unlink: clicked = graphCanvas1.GetAt(e.Location); if (sel1 == null) { sel1 = clicked; } else { sel1.RemoveArc(clicked); sel1 = null; } break; default: break; } graphCanvas1.Invalidate(); }