Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Coursework
- {
- public partial class Main_Form : Form
- {
- public static DataSet myDataSet = new DataSet();
- public static SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
- public static int circuitID = 0;
- public static List<Classes.Gate> GateList = new List<Classes.Gate>();
- //public DataTable Table1 = new DataTable("CircuitData");
- int userID = 0;
- int OutputID = 0;
- bool MakingSelection = false;
- public Main_Form()
- {
- InitializeComponent();
- connection.UserID = "callumk";
- connection.Password = "king-underwood";
- connection.IntegratedSecurity = false;
- connection.InitialCatalog = "callumk";
- connection.DataSource = "213.171.206.37";
- Classes.Gate AND = new Classes.Gate();
- AND.SetComponentId(0);
- AND.Input1 = 0;
- AND.Input2 = 0;
- AND.Output = 0;
- AND.InToolBox = true;
- AND.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
- AND.SetLocation(3, 3);
- AND.Location = new Point(3, 3);
- AND.SetGateType(1);
- AND.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- splitContainer1.Panel1.Controls.Add(AND);
- Classes.Gate OR = new Classes.Gate();
- OR.SetComponentId(0);
- OR.Input1 = 0;
- OR.Input2 = 0;
- OR.Output = 0;
- OR.InToolBox = true;
- OR.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
- OR.SetLocation(82, 3);
- OR.Location = new Point(82, 3);
- OR.SetGateType(2);
- OR.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- splitContainer1.Panel1.Controls.Add(OR);
- Classes.Gate NOT = new Classes.Gate();
- NOT.SetComponentId(0);
- NOT.Input1 = 0;
- NOT.Input2 = 0;
- NOT.Output = 0;
- NOT.InToolBox = true;
- NOT.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
- NOT.SetLocation(161, 3);
- NOT.Location = new Point(161, 3);
- NOT.SetGateType(3);
- NOT.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- splitContainer1.Panel1.Controls.Add(NOT);
- Classes.Gate XOR = new Classes.Gate();
- XOR.SetComponentId(0);
- XOR.Input1 = 0;
- XOR.Input2 = 0;
- XOR.Output = 0;
- XOR.InToolBox = true;
- XOR.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
- XOR.SetLocation(239, 3);
- XOR.Location = new Point(239, 3);
- XOR.SetGateType(4);
- XOR.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- splitContainer1.Panel1.Controls.Add(XOR);
- Classes.Gate NAND = new Classes.Gate();
- NAND.SetComponentId(0);
- NAND.Input1 = 0;
- NAND.Input2 = 0;
- NAND.Output = 0;
- NAND.InToolBox = true;
- NAND.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
- NAND.SetLocation(318, 3);
- NAND.Location = new Point(318, 3);
- NAND.SetGateType(5);
- NAND.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- splitContainer1.Panel1.Controls.Add(NAND);
- Classes.Gate NOR = new Classes.Gate();
- NOR.SetComponentId(0);
- NOR.Input1 = 0;
- NOR.Input2 = 0;
- NOR.Output = 0;
- NOR.InToolBox = true;
- NOR.ControlRemovedFromToolbox += new EventHandler(gate1_ControlRemovedFromToolbox);
- NOR.SetLocation(396, 3);
- NOR.Location = new Point(396, 3);
- NOR.SetGateType(6);
- NOR.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- splitContainer1.Panel1.Controls.Add(NOR);
- }
- void ControlGateMoved(object sender, EventArgs e)
- {
- splitContainer1.Panel2.Invalidate();
- }
- void Input1Selection(object sender, EventArgs e)
- {
- Classes.Gate s = (Classes.Gate)sender;
- if ((MakingSelection == true) & (s.GetComponentID() != OutputID))
- {
- //make new link
- foreach (Classes.Gate Gate in GateList)
- {
- if (Gate.Input1 == OutputID)
- {
- Gate.Input1 = 0;
- }
- if (Gate.Input2 == OutputID)
- {
- Gate.Input2 = 0;
- }
- if (Gate.Output == s.GetComponentID())
- {
- Gate.Output = 0;
- }
- if (Gate.GetComponentID() == OutputID)
- {
- Gate.Output = s.GetComponentID();
- }
- }
- s.Input1 = OutputID;
- MakingSelection = false;
- OutputID = 0;
- }
- splitContainer1.Panel2.Invalidate();
- }
- void Input2Selection(object sender, EventArgs e)
- {
- Classes.Gate s = (Classes.Gate)sender;
- if ((MakingSelection == true) & (s.GetComponentID() != OutputID))
- {
- foreach (Classes.Gate Gate in GateList)
- {
- if (Gate.Input1 == OutputID)
- {
- Gate.Input1 = 0;
- }
- if (Gate.Input2 == OutputID)
- {
- Gate.Input2 = 0;
- }
- if (Gate.Output == s.GetComponentID())
- {
- Gate.Output = 0;
- }
- if (Gate.GetComponentID() == OutputID)
- {
- Gate.Output = s.GetComponentID();
- }
- }
- MakingSelection = false;
- s.Input2 = OutputID;
- OutputID = 0;
- }
- splitContainer1.Panel2.Invalidate();
- }
- void OutputSelection(object sender, EventArgs e)
- {
- Classes.Gate s = (Classes.Gate)sender;
- if (MakingSelection == true)
- {
- MakingSelection = false;
- }
- else
- {
- MakingSelection = true;
- OutputID = s.GetComponentID();
- }
- splitContainer1.Panel2.Invalidate();
- }
- void GateRemovedFromCircuit(object sender, EventArgs e)
- {
- Classes.Gate GateToRemove = (Classes.Gate)sender;
- int IDToDelete = GateToRemove.GetComponentID();
- foreach (Classes.Gate Gate in GateList)
- {
- if (Gate.Input1 == IDToDelete)
- {
- Gate.Input1 = 0;
- }
- if (Gate.Input2 == IDToDelete)
- {
- Gate.Input2 = 0;
- }
- if (Gate.Output == IDToDelete)
- {
- Gate.Output = 0;
- }
- }
- GateList.Remove(GateToRemove);
- splitContainer1.Panel2.Controls.Remove(GateToRemove);
- splitContainer1.Panel2.Invalidate();
- }
- void AND_DoubleClick(object sender, EventArgs e)
- {
- throw new NotImplementedException();
- }
- public int CurrentUser()
- {
- return userID;
- }
- /*
- public int GetNewComponentID()
- {
- int currentNum = 1;
- foreach (Classes.Gate gate in GateList)
- {
- if (gate.GetComponentID() >= currentNum)
- {
- currentNum = gate.GetComponentID() + 1;
- }
- }
- return currentNum;
- }
- */
- static int power(int x, int y)
- {
- if (y < 0)
- {
- return 1;
- }
- switch (y){
- case 0:
- return 1;
- default:
- int result = x;
- for (int i = 1; i <= (y - 1); i++)
- {
- result = result * 2;
- }
- return result;
- }
- }
- static List<List<bool>> table(int n)
- {
- int columnsize = power(2, n);
- List<List<bool>> truthtable = new List<List<bool>>();
- for (int inputs = 1; inputs <= n; inputs++)
- {
- int length = power(2, inputs - 1);
- List<bool> sublist = new List<bool>();
- while (sublist.Count < columnsize)
- {
- for (int zero = 0; zero < length; zero++)
- {
- sublist.Add(false);
- }
- for (int zero = 0; zero < length; zero++)
- {
- sublist.Add(true);
- }
- }
- truthtable.Add(sublist);
- }
- return truthtable;
- }
- //File>Open/Save/New
- //New
- private void NewClicked(object sender, EventArgs e)
- {
- Console.WriteLine("File>New Clicked");
- string MessageBoxText = "Are You Sure You Want To Create A New File?" + Environment.NewLine + "Any Unsaves Changes Will Be Lost";
- Console.Write("Dialog Shown, Result: ");
- if (MessageBox.Show(MessageBoxText, "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
- {
- Console.WriteLine("Yes Clicked");
- Console.Write("Clearing " + splitContainer1.Panel2.Controls.Count.ToString() + " items>");
- splitContainer1.Panel2.Controls.Clear();
- //myDataSet.Tables["CircuitData"].Clear();
- GateList.Clear();
- Console.WriteLine("DONE");
- circuitID = 0;
- }
- else
- {
- Console.WriteLine("No Clicked");
- }
- splitContainer1.Panel2.Invalidate();
- }
- //Save
- private void SaveClicked(object sender, EventArgs e)
- {
- Console.WriteLine("File>Save Clicked");
- if ((userID != 0) & (circuitID != 0))
- {
- using (SqlConnection oConn = new SqlConnection(connection.ConnectionString))
- {
- oConn.Open();
- MessageBox.Show(circuitID.ToString());
- SqlCommand oCommand = new SqlCommand("delete from circuitContents where circuitID=" + circuitID.ToString(), oConn);
- oCommand.ExecuteNonQuery();
- foreach (Classes.Gate gate in GateList)
- {
- oCommand = new SqlCommand("insert into circuitcontents values (" + circuitID.ToString() + ", " + gate.GetComponentID().ToString() + ", " + gate.GetGateType() + ", " + gate.GateLocation.X.ToString() + ", " + gate.GateLocation.Y.ToString() + ", " + gate.Input1 + ", " + gate.Input2 + ", " + gate.Output + ")",oConn);
- oCommand.ExecuteNonQuery();
- }
- }
- }
- else if (userID == 0)
- {
- MessageBox.Show("Must Be Logged In To Complete This Action");
- }
- }
- //Save As
- private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Console.WriteLine("File>SaveAs Clicked");
- if (userID != 0)
- {
- SaveAsForm SaveAs = new SaveAsForm(CurrentUser());
- SaveAs.ShowDialog();
- SaveClicked(sender, e);
- }
- else
- {
- MessageBox.Show("Must Be Logged In To Complete This Action");
- }
- }
- //Open
- private void OpenClicked(object sender, EventArgs e)
- {
- Console.WriteLine("File>Open Clicked");
- bool DataAvailable = false;
- using (SqlConnection oConn = new SqlConnection(Main_Form.connection.ConnectionString))
- {
- oConn.Open();
- SqlCommand oCommand = new SqlCommand("SELECT circuitName, circuitID From circuit WHERE userID='" + userID + "'", oConn);
- SqlDataReader dr = oCommand.ExecuteReader();
- if (dr.Read())
- {
- DataAvailable = true;
- }
- oConn.Close();
- }
- if (DataAvailable)
- {
- OpenForm Open = new OpenForm(userID);
- Open.ShowDialog();
- if (Open.fileChosen == true)
- {
- //circuitID = Open.selectedCircuitID;
- GateList.Clear();
- splitContainer1.Panel2.Controls.Clear();
- Main_Form.circuitID = Open.selectedCircuitID;
- using (SqlConnection oConn = new SqlConnection(Main_Form.connection.ConnectionString))
- {
- oConn.Open();
- SqlCommand oCommand = new SqlCommand("SELECT componentID, gateType, gateX, gateY, input1, input2, output From circuitContents where circuitID='" + Main_Form.circuitID + "'", oConn);
- SqlDataReader dr = oCommand.ExecuteReader();
- while (dr.Read())
- {
- Classes.Gate newControl = new Classes.Gate();
- newControl.SetComponentId(dr.GetInt32(0));
- newControl.SetGateType(dr.GetInt32(1));
- int x = dr.GetInt32(2);
- int y = dr.GetInt32(3);
- newControl.SetLocation(x, y);
- newControl.Location = new Point(x, y);
- newControl.Input1 = dr.GetInt32(4);
- newControl.Input2 = dr.GetInt32(5);
- newControl.Output = dr.GetInt32(6);
- newControl.InToolBox = false;
- newControl.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- newControl.Input1Selected += new EventHandler(Input1Selection);
- newControl.Input2Selected += new EventHandler(Input2Selection);
- newControl.OutputSelected += new EventHandler(OutputSelection);
- newControl.DeleteComponent += new EventHandler(GateRemovedFromCircuit);
- splitContainer1.Panel2.Controls.Add(newControl);
- GateList.Add(newControl);
- }
- dr.Close();
- dr.Dispose();
- oConn.Close();
- }
- }
- }
- splitContainer1.Panel2.Invalidate();
- }
- private void ExitClicked(object sender, EventArgs e)
- {
- Console.WriteLine("Exiting...");
- Application.Exit();
- }
- private void gate1_ControlRemovedFromToolbox(object sender, EventArgs e)
- {
- Classes.Gate s = (Classes.Gate)sender;
- Console.WriteLine("Casting Successful");
- Classes.Gate newControl = new Classes.Gate();
- Console.Write("New gate generated: ");
- Console.WriteLine(newControl.ToString());
- newControl.CreateNewComponentID(ref GateList);
- newControl.SetGateType(s.GetGateType());
- newControl.SetLocation(15, 15);
- newControl.Location = new Point(15, 15);
- newControl.InToolBox = false;
- newControl.ControlHasBeenMoved += new EventHandler(ControlGateMoved);
- newControl.Input1Selected += new EventHandler(Input1Selection);
- newControl.Input2Selected += new EventHandler(Input2Selection);
- newControl.OutputSelected += new EventHandler(OutputSelection);
- newControl.DeleteComponent += new EventHandler(GateRemovedFromCircuit);
- splitContainer1.Panel2.Controls.Add(newControl);
- s.Reset();
- GateList.Add(newControl);
- }
- private void loginToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Console.WriteLine("Login");
- if (userID != 0)
- {
- Console.WriteLine("Already logged in, logging out");
- logoutToolStripMenuItem_Click(sender, e);
- }
- using (Login l = new Login())
- {
- l.ShowDialog();
- if (l.loggedIn == true)
- {
- userID = l.lastId;
- Console.WriteLine("Logging In");
- }
- }
- }
- private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Console.WriteLine("Logout");
- userID = 0;
- splitContainer1.Panel2.Controls.Clear();
- circuitID = 0;
- splitContainer1.Panel2.Invalidate();
- }
- private List<Classes.Gate> GetGatesWithNoInput()
- {
- List<Classes.Gate> Gates = new List<Classes.Gate>();
- foreach (Classes.Gate Gate in GateList)
- {
- if (Gate.GetGateType() == 3)
- {
- //NOT gate must be handle seperately
- if (Gate.Input1 == 0)
- {
- Gates.Add(Gate);
- }
- }
- else
- {
- if ((Gate.Input1 == 0) | (Gate.Input2 == 0))
- {
- Gates.Add(Gate);
- }
- }
- }
- return Gates;
- }
- private int GetNumberOfInputs(List<Classes.Gate> Gates)
- {
- int NumberInputs = 0;
- foreach (Classes.Gate Gate in Gates)
- {
- if (Gate.GetGateType() == 3)
- {
- NumberInputs += 1;
- }
- else
- {
- if (Gate.Input1 == 0)
- {
- NumberInputs += 1;
- }
- if (Gate.Input2 == 0)
- {
- NumberInputs += 1;
- }
- }
- }
- return NumberInputs;
- }
- private Classes.Gate GetGate(int ID)
- {
- Classes.Gate Result = new Classes.Gate();
- foreach (Classes.Gate Gate in GateList)
- {
- if (Gate.GetComponentID() == ID)
- {
- Result = Gate;
- }
- }
- return Result;
- }
- private int GetIdOfOutput(int StartID)
- {
- bool OutputFound = false;
- int currentID = StartID;
- while (OutputFound == false)
- {
- Classes.Gate CurrentGate = GetGate(currentID);
- if (CurrentGate.Output != 0)
- {
- //it has an output
- currentID = CurrentGate.Output;
- }
- else
- {
- //found our gate
- OutputFound = true;
- }
- }
- return currentID;
- }
- private void CalculateTruthTable(object sender, EventArgs e)
- {
- Console.WriteLine("Calculating Truth Table");
- List<Classes.Gate> ListOfInputGates = GetGatesWithNoInput();
- int NumberOfInputs = GetNumberOfInputs(ListOfInputGates);
- Console.WriteLine("Number Of Inputs: " + NumberOfInputs.ToString());
- if (ListOfInputGates.Count > 0)
- {
- int OutputID = GetIdOfOutput(ListOfInputGates[0].GetComponentID());
- Console.WriteLine("Output ID: " + OutputID.ToString());
- List<List<bool>> TruthTable = table(NumberOfInputs);
- OutputForm OutputWindow = new OutputForm(ref TruthTable, ref GateList, ref OutputID, ref NumberOfInputs);
- OutputWindow.ShowDialog();
- OutputWindow.Dispose();
- }
- else
- {
- MessageBox.Show("No Circuit To Calculate Table For");
- }
- }
- //Draw Connections
- private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- g.SetClip(e.ClipRectangle);
- float f = 4;
- Pen p = new Pen(Color.White, f);
- //g.DrawLine(p, new Point(20, 20), new Point(45, 34));
- foreach (Classes.Gate Gate in GateList)
- {
- //draw lines
- //draw line 1
- if (Gate.Input1 != 0)
- {
- try
- {
- Classes.Gate InputGate = GetGate(Gate.Input1);
- Point Point1 = new Point((InputGate.Location.X + 72), (InputGate.Location.Y + 48));
- Point Point2 = new Point(Gate.Location.X, (Gate.Location.Y + 36));;
- if (Gate.GetGateType() == 3)
- {
- Point2 = new Point(Gate.Location.X, (Gate.Location.Y + 48));
- }
- g.DrawLine(p, Point1, Point2);
- }
- catch
- {
- Gate.Input1 = 0;
- }
- }
- if (Gate.GetGateType() != 3)
- {
- //The second line is not applicable to a NOT gate
- if (Gate.Input2 != 0)
- {
- try
- {
- Classes.Gate InputGate = GetGate(Gate.Input2);
- Point Point1 = new Point((InputGate.Location.X + 72), (InputGate.Location.Y + 48));
- Point Point2 = new Point(Gate.Location.X, (Gate.Location.Y + 60));
- g.DrawLine(p, Point1, Point2);
- }
- catch
- {
- Gate.Input1 = 0;
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment