-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicsNode.cs
301 lines (243 loc) · 9.83 KB
/
GraphicsNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace Short_Path_Dj
{
class GraphicsNode : Panel
{
//private float scaleFactor = 1.0f;
public static Dictionary<int, List<(int, int)>> graph = new Dictionary<int, List<(int, int)>>();
public static List<GraphicsNode> nodes = new List<GraphicsNode>();
public static int nodeID = 0;
public int ID;
public bool isSelected = false;
bool isMouseDown = false;
public static GraphicsNode linkFrom = null;
public static GraphicsNode linkTo = null;
public static MainForm activeForm = null;
public GraphicsNode()
{
// this.BorderStyle = BorderStyle.None;
this.DoubleBuffered = true;
this.BackColor = Color.Transparent;
this.Size = new Size(46, 46);
this.MouseDown += GraphicsNode_MouseDown;
this.MouseUp += GraphicsNode_MouseUp;
this.MouseMove += GraphicsNode_MouseMove;
ID = nodeID++;
//Add the newly created node to the dictionary
graph.Add(ID, new List<(int, int)>());
nodes.Add(this);
//Cick Event
this.Click += GraphicsNode_Click;
}
private void GraphicsNode_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
isMouseDown = false;
Control parentControl = ((Control)sender).Parent;
if (parentControl != null)
{
parentControl.Invalidate();
parentControl.Refresh();
}
//((Control)sender).Parent.Invalidate();
//((Control)sender).Parent.Refresh();
}
public static List<(int, int)>[] GetAdjListForDijkstra()
{
List<(int, int)>[] adjList = new List<(int, int)>[nodes.Count];
for (int i = 0; i < graph.Keys.Count; i++)
{
adjList[i] = new List<(int, int)>();
adjList[i] = graph.ElementAt(i).Value;
}
return adjList;
}
public static void UpdateAdjMatrixGrid()
{
DataGridView adjMatrix = activeForm.adjListDataGrid;
adjMatrix.Rows.Clear();
adjMatrix.Columns.Clear();
var _graph = GetAdjListForDijkstra();
for (int i = 0; i < nodes.Count; i++)
{
adjMatrix.Columns.Add("N " + i, "N " + i);
}
foreach (var r in _graph)
{
object[] rData = new object[nodes.Count];
foreach (var cell in r)
{
rData[cell.Item1] = cell.Item2;
}
adjMatrix.Rows.Add(rData);
}
//Fix layout
for (int i = 0; i < nodes.Count; i++)
{
adjMatrix.Rows[i].HeaderCell.Value = "N " + i;
}
adjMatrix.RowHeadersWidth = 77;
//adjMatrix.AutoResizeColumns();
adjMatrix.AllowUserToAddRows = false;
adjMatrix.ReadOnly = true;
adjMatrix.AutoSize = true;
adjMatrix.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
adjMatrix.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
adjMatrix.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
public void ModifyEdge(int src, int dst, int weight, bool isRemove = false)
{
List<(int, int)> row = graph[src];
if (row == null)
row = new List<(int, int)>();
var entry = row.FirstOrDefault(tmp => tmp.Item1 == dst);
if (entry != default)
row.Remove(entry);
if (!isRemove)
row.Add((dst, weight));
List<(int, int)> row2 = graph[dst];
if (row2 == null)
row2 = new List<(int, int)>();
var entry2 = row2.FirstOrDefault(tmp => tmp.Item1 == src);
if (entry2 != default)
row2.Remove(entry2);
if (!isRemove)
row2.Add((src, weight));
UpdateAdjMatrixGrid();
}
private void GraphicsNode_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Control parent = ((Control)sender).Parent;
Point newPos = parent.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
newPos.X -= Size.Width / 2;
newPos.Y -= Size.Height / 2;
this.Location = newPos;
((Control)sender).Parent.Invalidate();
((Control)sender).Parent.Refresh();
}
}
private void GraphicsNode_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!Control.ModifierKeys.HasFlag(Keys.Alt))
isMouseDown = true;
if (Control.ModifierKeys.HasFlag(Keys.Control))
{
RemoveNodeAndEdges(ID);
}
}
else if (e.Button == MouseButtons.Right)
{
if (linkFrom == null)
linkFrom = this;
else
linkTo = this;
isSelected = true;
Invalidate();
Refresh();
if (linkFrom != null && linkTo != null)
{
string weight = Interaction.InputBox(" Enter Weight Value OR Type 'R' to remove :", "Weight Linking from Node " +
linkFrom.ID + " to Node " + linkTo.ID, "", 600, 375);
/*if (int.Parse(weight) < 0)
{
MessageBox.Show("Weight Should be Zero or Postive integer !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
linkFrom.isSelected = linkTo.isSelected = false;
linkFrom.Invalidate();
linkTo.Invalidate();
linkFrom = linkTo = null;
return;*/
//}
// Console.WriteLine(linkFrom.ID + " " + linkTo.ID + " is " + weight);
if (weight.ToUpper() == "R")
ModifyEdge(linkFrom.ID, linkTo.ID, 0, true);
else
{
try
{
if (int.Parse(weight) >= 0)
ModifyEdge(linkFrom.ID, linkTo.ID, int.Parse(weight));
else
{
MessageBox.Show("Weight Should be Zero or a Postive integer !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
linkFrom.isSelected = linkTo.isSelected = false;
linkFrom.Invalidate();
linkTo.Invalidate();
linkFrom = linkTo = null;
return;
}
}
catch
{
}
}
// var row = graph[linkFrom.ID];
linkFrom.isSelected = linkTo.isSelected = false;
linkFrom.Invalidate();
linkTo.Invalidate();
linkFrom = linkTo = null;
}
}
Control parentControl = ((Control)sender).Parent;
if (parentControl != null)
{
parentControl.Invalidate();
parentControl.Refresh();
}
//((Control)sender).Parent.Invalidate();
//((Control)sender).Parent.Refresh();
}
private void GraphicsNode_Click(object sender, EventArgs e)
{
//invert = !invert;
//Invalidate();
//Refresh();
}
private void RemoveNodeAndEdges(int nodeId)
{
if (graph.ContainsKey(nodeId))
{
// Remove edges associated with the node
foreach (var adjacentNodeId in graph[nodeId])
{
graph[adjacentNodeId.Item1].RemoveAll(edge => edge.Item1 == nodeId);
}
// Remove the node from the graph and the UI
graph.Remove(nodeId);
var nodeToRemove = nodes.FirstOrDefault(node => node.ID == nodeId);
if (nodeToRemove != null)
{
nodes.Remove(nodeToRemove);
Parent.Controls.Remove(nodeToRemove);
}
UpdateAdjMatrixGrid();
}
}
protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Pen SamehPen = new Pen(Color.DarkRed, 2);
if (!isSelected)
g.FillEllipse(Brushes.DarkRed, 5, 5, 40, 40);
else
g.DrawEllipse(SamehPen, 5, 5, 40, 40);
g.DrawString(ID.ToString(), new Font(SystemFonts.DefaultFont.FontFamily, 14), Brushes.Silver, new Rectangle(5, 5, 40, 40), sf);
}
}
}