-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
121 lines (105 loc) · 3.55 KB
/
MainWindow.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
using System;
using System.Data;
using System.Drawing;
using System.Collections.Generic;
using Gtk;
using MySql.Data;
using MySql.Data.MySqlClient;
using Moi;
using System.Windows.Forms;
public partial class MainWindow : Gtk.Window {
string ConnectionString, query, query2;
MySqlConnection Dbcon, Dbcon2;
MySqlCommand Dbcmd, Dbcmd2;
IDataReader reader,reader2;
Graph graph;
List<Node> nodes;
Dictionary<Node, int> edges;
double lat, lon;
Node aux;
public MainWindow() : base(Gtk.WindowType.Toplevel) {
ConnectionString =
"Server=localhost;" +
"Database=Grafo;" +
"User ID=poi;" +
"Password=poi;" +
"Pooling=false";
Dbcon = new MySqlConnection(ConnectionString);
Dbcon2 = new MySqlConnection(ConnectionString);
Update();
Build();
}
protected Node SearchNodeById(List<Node> list, int id) {
foreach (Node node in list)
if (node.Id == id)
return node;
return null;
}
protected void Update() {
Dbcon.Open();
Dbcmd = Dbcon.CreateCommand();
query = "SELECT * FROM Nodo;";
Dbcmd.CommandText = query;
reader = Dbcmd.ExecuteReader();
graph = new Graph();
nodes = new List<Node>();
while (reader.Read()) {
nodes.Add(new Node(Int32.Parse(reader["idNodo"].ToString()), reader["nombre"].ToString(), double.Parse(reader["latitud"].ToString()), double.Parse(reader["longitud"].ToString())));
}
Dbcon.Close();
Dbcon.Open();
reader = Dbcmd.ExecuteReader();
while (reader.Read()) {
aux = new Node(Int32.Parse(reader["idNodo"].ToString()), reader["nombre"].ToString(), double.Parse(reader["latitud"].ToString()), double.Parse(reader["longitud"].ToString()));
Dbcon2.Open();
Dbcmd2 = Dbcon2.CreateCommand();
query2 = "SELECT * FROM Arista WHERE nodoIni = " + reader["idNodo"] + ";";
Dbcmd2.CommandText = query2;
reader2 = Dbcmd2.ExecuteReader();
edges = new Dictionary<Node, int>();
while (reader2.Read()) {
aux = SearchNodeById(nodes, Int32.Parse(reader2["nodoFin"].ToString()));
if (aux != null) {
Console.WriteLine(aux.Id + ", " + aux.Nombre);
edges[aux] = Int32.Parse(reader2["costo"].ToString());
}
}
graph.Vertices[SearchNodeById(nodes, Int32.Parse(reader["idNodo"].ToString()))] = edges;
Dbcon2.Close();
}
Dbcon.Close();
Console.WriteLine("----\n" + graph + "\n----");
foreach (Node nodo in nodes)
Console.WriteLine(nodo.Id + ", " + nodo.Nombre + ", " + nodo.Latitud + ", " + nodo.Longitud);
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a) {
Gtk.Application.Quit();
a.RetVal = true;
}
protected void MapClick(object o, ButtonPressEventArgs args) {
Update();
lat = args.Event.X;
lon = args.Event.Y;
Gtk.Menu m = new Gtk.Menu();
Gtk.MenuItem createNode = new Gtk.MenuItem("Create a Node");
Gtk.MenuItem createEdge = new Gtk.MenuItem("Create an Edge");
Gtk.MenuItem calculateDjkstra = new Gtk.MenuItem("Calculate cost");
createNode.ButtonPressEvent += new ButtonPressEventHandler(CreateNodePressed);
createEdge.ButtonPressEvent += new ButtonPressEventHandler(CreateEdgePressed);
calculateDjkstra.ButtonPressEvent += new ButtonPressEventHandler(CalculateDjkstraPressed);
m.Add(createNode);
m.Add(createEdge);
m.Add(calculateDjkstra);
m.ShowAll();
m.Popup();
}
protected void CreateNodePressed(object sender, EventArgs args) {
NodeWindow NWin = new NodeWindow(nodes, lat, lon);
}
protected void CreateEdgePressed(object sender, EventArgs args) {
EdgeWindow EWin = new EdgeWindow(nodes);
}
protected void CalculateDjkstraPressed(object sender, EventArgs args) {
DjkstraWindow DWin = new DjkstraWindow(nodes, graph);
}
}