Form1.cs 8.7 KB

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