Form1.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (scanned.sampleData.HammerOil)
  47. {
  48. label5.Text = $"Sample {scanned.barcode} contains Hammer Oil";
  49. BackColor = Color.Maroon;
  50. label5.Show();
  51. }
  52. else
  53. {
  54. label5.Hide();
  55. }
  56. if (scanned.sampleData.Manganese)
  57. {
  58. label4.Text = $"Sample {scanned.barcode} contains Manganese";
  59. BackColor = Color.Blue;
  60. label4.Show();
  61. }
  62. else
  63. {
  64. label4.Hide();
  65. }
  66. if (scanned.sampleData.Plastic)
  67. {
  68. label3.Text = $"Sample {scanned.barcode} contains Plastic";
  69. BackColor = Color.Orange;
  70. label3.Show();
  71. }
  72. else
  73. {
  74. label3.Hide();
  75. }
  76. if (scanned.sampleData.Pyrite)
  77. {
  78. label2.Text = $"Sample {scanned.barcode} contains Pyrite";
  79. BackColor = Color.Fuchsia;
  80. label2.Show();
  81. }
  82. else
  83. {
  84. label2.Hide();
  85. }
  86. if (!scanned.Contaminated)
  87. {
  88. label1.Text = scanned.message;
  89. label1.Show();
  90. }
  91. else
  92. {
  93. label1.Hide();
  94. }
  95. if (scanned.sampleData.Job != null)
  96. {
  97. label8.Text = $"Job in progress : {scanned.sampleData.Job}";
  98. }
  99. else
  100. {
  101. label8.Text = "";
  102. }
  103. if (scanned.BufferCount != -1)
  104. {
  105. toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
  106. }
  107. }
  108. }
  109. private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
  110. {
  111. this.Close();
  112. }
  113. #region SettingsHandlers
  114. private void EditToolStripMenuItem_Click(object sender, EventArgs e)
  115. {
  116. SettingsDialog dlg = new();
  117. dlg.ShowDialog();
  118. dlg.Dispose();
  119. }
  120. private void PurgeToolStripMenuItem1_Click(object sender, EventArgs e)
  121. {
  122. DialogResult result = MessageBox.Show("Are you sure you would like to purge all setings?", "Clear settings.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  123. if(result == DialogResult.Yes)
  124. {
  125. Properties.Settings.Default.Reset();
  126. MessageBox.Show("Settings purged.", "Clear settings.");
  127. }
  128. }
  129. /// <summary>
  130. /// Import settings from JSON file.
  131. /// </summary>
  132. private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
  133. {
  134. openFileDialog1.DefaultExt = "json";
  135. openFileDialog1.Filter = "Json files (*.json)|*.json";
  136. openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  137. openFileDialog1.ShowDialog();
  138. string configFile = openFileDialog1.FileName;
  139. try
  140. {
  141. if(configFile=="" && File.Exists(configFile) && dataCore != null)
  142. {
  143. ProgramFunctions.ImportConfigFile();
  144. }
  145. else if(File.Exists(configFile) && dataCore != null)
  146. {
  147. ProgramFunctions.ImportConfigFile(configFile);
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  153. }
  154. }
  155. /// <summary>
  156. /// Export settings to JSON file.
  157. /// </summary>
  158. private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
  159. {
  160. saveFileDialog1.DefaultExt = "json";
  161. saveFileDialog1.Filter = "Json files (*.json)|*.json";
  162. saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  163. saveFileDialog1.ShowDialog();
  164. string configFile = saveFileDialog1.FileName;
  165. try
  166. {
  167. if(configFile == "" && dataCore != null)
  168. {
  169. ProgramFunctions.ExportConfigFile();
  170. }
  171. else if (dataCore != null)
  172. {
  173. ProgramFunctions.ExportConfigFile(configFile);
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  179. }
  180. }
  181. #endregion
  182. private void ReinitializeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  183. {
  184. Application.Restart();
  185. }
  186. private void Timer1_Tick(object sender, EventArgs e)
  187. {
  188. DateTime now = DateTime.Now;
  189. label6.Text = now.ToShortTimeString();
  190. label7.Text = now.ToShortDateString();
  191. if (counter <= 0)
  192. {
  193. BackColor = SystemColors.Control;
  194. counter = 2;
  195. }
  196. else if(BackColor.Name != "Control")
  197. {
  198. counter--;
  199. }
  200. }
  201. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  202. {
  203. if (e.Argument!=null)
  204. {
  205. WorkingDirControl.UpdateWorkingDir(e);
  206. }
  207. }
  208. private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  209. {
  210. Scan scan;
  211. if (e.Result != null)
  212. {
  213. scan = (Scan)e.Result;
  214. toolStripProgressBar1.Maximum = scan.JobTotal;
  215. toolStripProgressBar1.Value = scan.JobCount;
  216. toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
  217. }
  218. }
  219. private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
  220. {
  221. Logging.OpenLogDirectory();
  222. }
  223. private void openCurrentToolStripMenuItem_Click(object sender, EventArgs e)
  224. {
  225. Logging.OpenInTextEditor();
  226. }
  227. private void purgeToolStripMenuItem_Click(object sender, EventArgs e)
  228. {
  229. Logging.PurgeLogs();
  230. }
  231. }
  232. }