Form1.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. namespace crusherScanner
  2. {
  3. public partial class Form1 : Form
  4. {
  5. private static ProgramFunctions? dataCore;
  6. private static int counter = 0;
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. }
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13. Logging.Append("Crusher Scanner Appliction " + Application.ProductVersion + " is starting.");
  14. TextBox1.Enabled = false;
  15. toolStripStatusLabel5.Text = "";
  16. toolStripStatusLabel2.Text = "0";
  17. label8.Text = "";
  18. label7.Text = "";
  19. label6.Text = "";
  20. label5.Hide();
  21. label4.Hide();
  22. label3.Hide();
  23. label2.Hide();
  24. label1.Hide();
  25. WindowState = FormWindowState.Maximized;
  26. //TopMost = true;
  27. dataCore = new ProgramFunctions();
  28. TextBox1.Enabled = true;
  29. TextBox1.Focus();
  30. Logging.Append("Crusher Scanner Appliction " + Application.ProductVersion + " has started.");
  31. }
  32. private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  33. {
  34. Scan scanned = new();
  35. scanned.BufferCount = -1;
  36. counter = 2;
  37. scanned.barcode = TextBox1.Text.Trim();
  38. if (e.KeyValue == 13 && dataCore != null)
  39. {
  40. scanned = ProgramFunctions.StartChecks(scanned);
  41. if(scanned.sampleData.Job != null)
  42. {
  43. backgroundWorker1.RunWorkerAsync(scanned);
  44. }
  45. TextBox1.Text = "";
  46. ContaminateAlert(scanned.sampleData.HammerOil, scanned.barcode, "Hammer Oil", label5, Color.Maroon);
  47. ContaminateAlert(scanned.sampleData.Manganese, scanned.barcode, "Manganese", label4, Color.Blue);
  48. ContaminateAlert(scanned.sampleData.Plastic, scanned.barcode, "Plastic", label3, Color.Orange);
  49. ContaminateAlert(scanned.sampleData.Pyrite, scanned.barcode, "Pyrite", label2, Color.Fuchsia);
  50. if (!scanned.Contaminated)
  51. {
  52. label1.Text = scanned.message;
  53. label1.Show();
  54. }
  55. else
  56. {
  57. label1.Hide();
  58. }
  59. if (scanned.sampleData.Job != null)
  60. {
  61. label8.Text = $"Job in progress : {scanned.sampleData.Job}";
  62. }
  63. else
  64. {
  65. label8.Text = "";
  66. }
  67. if (scanned.BufferCount != -1)
  68. {
  69. toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
  70. }
  71. }
  72. }
  73. private void ContaminateAlert(bool check,string bCode, string name,Label rtLabel, Color bColor)
  74. {
  75. if (check)
  76. {
  77. rtLabel.Text = $"Sample {bCode} contains {name}";
  78. BackColor = bColor;
  79. rtLabel.Show();
  80. }
  81. else
  82. {
  83. rtLabel.Hide();
  84. }
  85. }
  86. private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
  87. {
  88. this.Close();
  89. }
  90. #region SettingsHandlers
  91. private void EditToolStripMenuItem_Click(object sender, EventArgs e)
  92. {
  93. SettingsDialog dlg = new();
  94. dlg.ShowDialog();
  95. dlg.Dispose();
  96. }
  97. private void PurgeToolStripMenuItem1_Click(object sender, EventArgs e)
  98. {
  99. DialogResult result = MessageBox.Show("Are you sure you would like to purge all setings?", "Clear settings.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  100. if(result == DialogResult.Yes)
  101. {
  102. Properties.Settings.Default.Reset();
  103. MessageBox.Show("Settings purged.", "Clear settings.");
  104. }
  105. }
  106. /// <summary>
  107. /// Import settings from JSON file.
  108. /// </summary>
  109. private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
  110. {
  111. openFileDialog1.DefaultExt = "json";
  112. openFileDialog1.Filter = "Json files (*.json)|*.json";
  113. openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  114. openFileDialog1.ShowDialog();
  115. string configFile = openFileDialog1.FileName;
  116. try
  117. {
  118. if(configFile=="" && File.Exists(configFile) && dataCore != null)
  119. {
  120. ProgramFunctions.ImportConfigFile();
  121. }
  122. else if(File.Exists(configFile) && dataCore != null)
  123. {
  124. ProgramFunctions.ImportConfigFile(configFile);
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  130. }
  131. }
  132. /// <summary>
  133. /// Export settings to JSON file.
  134. /// </summary>
  135. private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
  136. {
  137. saveFileDialog1.DefaultExt = "json";
  138. saveFileDialog1.Filter = "Json files (*.json)|*.json";
  139. saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  140. saveFileDialog1.ShowDialog();
  141. string configFile = saveFileDialog1.FileName;
  142. try
  143. {
  144. if(configFile == "" && dataCore != null)
  145. {
  146. ProgramFunctions.ExportConfigFile();
  147. }
  148. else if (dataCore != null)
  149. {
  150. ProgramFunctions.ExportConfigFile(configFile);
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  156. }
  157. }
  158. #endregion
  159. private void ReinitializeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  160. {
  161. Application.Restart();
  162. }
  163. private void Timer1_Tick(object sender, EventArgs e)
  164. {
  165. DateTime now = DateTime.Now;
  166. label6.Text = now.ToShortTimeString();
  167. label7.Text = now.ToShortDateString();
  168. if (counter <= 0)
  169. {
  170. BackColor = SystemColors.Control;
  171. counter = 2;
  172. }
  173. else if(BackColor.Name != "Control")
  174. {
  175. counter--;
  176. }
  177. }
  178. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  179. {
  180. if (e.Argument!=null)
  181. {
  182. WorkingDirControl.UpdateWorkingDir(e);
  183. }
  184. }
  185. private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  186. {
  187. Scan scan;
  188. if (e.Result != null)
  189. {
  190. scan = (Scan)e.Result;
  191. toolStripProgressBar1.Maximum = scan.JobTotal;
  192. toolStripProgressBar1.Value = scan.JobCount;
  193. toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
  194. }
  195. }
  196. private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
  197. {
  198. Logging.OpenLogDirectory();
  199. }
  200. private void openCurrentToolStripMenuItem_Click(object sender, EventArgs e)
  201. {
  202. Logging.OpenInTextEditor();
  203. }
  204. private void purgeToolStripMenuItem_Click(object sender, EventArgs e)
  205. {
  206. Logging.PurgeLogs();
  207. }
  208. }
  209. }